Exemplo n.º 1
0
        void UpdateMovedTopic(DocTopic topic)
        {
            // TODO: Get latest changes from Editor
            var editorTopic = Model.MarkdownMonsterModel.ActiveEditor?.Properties[Constants.EditorPropertyNames.KavaDocsTopic] as DocTopic;

            if (editorTopic == topic)
            {
                topic.Body = Model.MarkdownMonsterModel.ActiveEditor.GetMarkdown();
            }
            else
            {
                // TODO: Check if the topic is open in another tab
                var tab = Model.MarkdownMonsterModel.Window.GetTabFromFilename(topic.GetTopicFileName());
                if (tab != null)
                {
                    topic = (tab.Tag as MarkdownDocumentEditor)?.Properties[Constants.EditorPropertyNames.KavaDocsTopic] as DocTopic;
                    if (topic != null)
                    {
                        topic.Body = Model.MarkdownMonsterModel.ActiveEditor.GetMarkdown();
                    }
                }
                else
                {
                    topic.LoadTopicFile(); // get latest from disk
                }
            }

            // delete the old file
            topic.DeleteTopicFile();  // delete in old location

            // create new link and slug
            topic.CreateRelativeSlugAndLink();
            topic.SaveTopicFile();  // write in new location
        }
Exemplo n.º 2
0
        /// <summary>
        /// Opens a topic for editing in a standard Markdown Monster
        /// tab (not the preview Tab)
        /// </summary>
        /// <param name="topic"></param>
        /// <returns></returns>
        public TabItem OpenTopicInMMEditor(DocTopic topic = null)
        {
            if (topic == null)
            {
                topic = TreeTopicBrowser.SelectedItem as DocTopic;
            }

            TabItem tab = null;

            if (topic != null)
            {
                var file = topic.GetTopicFileName();
                if (file == null)
                {
                    return(null);
                }

                if (!File.Exists(file))
                {
                    File.WriteAllText(file, "");
                }

                tab = Model.KavaDocsModel.Window.RefreshTabFromFile(file, isPreview: false, noFocus: false);
                Model.KavaDocsModel.Window.BindTabHeaders();

                if (tab.Tag is MarkdownDocumentEditor editor)
                {
                    editor.Properties[Constants.EditorPropertyNames.KavaDocsTopic]    = topic;
                    editor.Properties[Constants.EditorPropertyNames.KavaDocsUnedited] = false;
                }
            }

            return(tab);
        }
        /// <summary>
        /// Called after a topic file (the MD file linked to a topic)
        /// has been saved on disk. This allows updating of topic
        /// info from the saved content.
        /// </summary>
        /// <param name="topic"></param>
        /// <param name="doc"></param>
        public void OnTopicFilesSaved(DocTopic topic, MarkdownDocument doc)
        {
            if (topic == null)
            {
                return;
            }

            var projectFilename = topic?.Project?.Filename;
            var filename        = doc.Filename.ToLower();


            if (topic.Body == "NoContent")
            {
                topic.Body = null;
            }

            // Save Project File
            if (projectFilename != null && filename.Equals(projectFilename, StringComparison.InvariantCultureIgnoreCase))
            {
                // reload the project
                KavaDocsModel.LoadProject(KavaDocsModel.ActiveProject.Filename);
                return;
            }

            // Check for explicit KavaDocs Documents
            if (string.IsNullOrEmpty(doc.CurrentText))
            {
                return;
            }

            // Save the underlying topic file
            if (filename.Equals(topic.GetTopicFileName(), StringComparison.InvariantCultureIgnoreCase))
            {
                KavaDocsModel.ActiveProject.UpdateTopicFromMarkdown(doc, topic);
                KavaDocsModel.ActiveProject.SaveProject();
            }
            // READ-ONLY this shouldn't really happen any more
            // Saving the KavaDocs.md file - assume we're on the active topic
            else if (filename.Equals(topic.GetKavaDocsEditorFilePath(), StringComparison.InvariantCultureIgnoreCase))
            {
                topic.Body = doc.CurrentText;
                KavaDocsModel.ActiveProject.UpdateTopicFromMarkdown(doc, topic);
                KavaDocsModel.ActiveProject.SaveProject();
            }
            // Any previously activated document file
            else if (KavaDocsModel.ActiveMarkdownEditor.Properties.TryGetValue(Constants.EditorPropertyNames.KavaDocsTopic, out object objTopic))
            {
                KavaDocsModel.ActiveProject.UpdateTopicFromMarkdown(doc, objTopic as DocTopic);
                KavaDocsModel.ActiveProject.SaveProject();
            }
        }
        public bool SaveProjectFileForTopic(DocTopic topic, DocProject project = null)
        {
            if (topic == null)
            {
                return(false);
            }

            if (!topic.TopicState.IsDirty)
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(topic.TopicState.OldLink) && topic.TopicState.OldLink != topic.Link)
            {
                if (MessageBox.Show(
                        $@"Link has changed from {topic.TopicState.OldLink} to {
                                topic.Link
                            }.\r\n\rnDo you want to fix up the link and file?",
                        "Topic Link Changed",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    var oldFile = topic.GetTopicFileName(topic.TopicState.OldLink);
                    topic.SaveTopicFile(); // save new file
                    File.Delete(oldFile);
                }
            }


            if (project == null)
            {
                project = kavaUi.AddinModel.ActiveProject;
            }

            project.SaveProjectAsync();
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Opens read
        /// </summary>
        /// <returns></returns>
        public TabItem OpenTopicInEditor(bool setFocus = false)
        {
            Debug.WriteLine("OpenTopicInEditor");

            DocTopic topic = TreeTopicBrowser.SelectedItem as DocTopic;

            if (topic == null)
            {
                return(null);
            }

            var window = Model.KavaDocsModel.Window;

            TabItem tab;

            if (topic != null && topic.Body != null && (topic.IsLink || topic.Body.StartsWith("http")))
            {
                tab = Model.MarkdownMonsterModel.Window.OpenBrowserTab(topic.Link ?? topic.Body);
                return(tab);
            }

            var file = topic.GetTopicFileName(force: true);

            MarkdownDocumentEditor editor = null;

            // is tab open already as a file? If so use that
            tab = window.GetTabFromFilename(file);
            if (tab != null)
            {
                editor = tab?.Tag as MarkdownDocumentEditor;
                if (editor == null)
                {
                    return(null);
                }
            }
            else
            {
                // Will also open the tab if not open yet
                // EXPLICITLY NOT SELECTING THE TAB SO THAT IT'S NOT RENDERED YET
                // Assign topic first then explicitly select
                //tab = Model.KavaDocsModel.Window.RefreshTabFromFile(file, noFocus: !setFocus, isPreview: true, noSelectTab:true);

                tab = Model.KavaDocsModel.Window.ActivateTab(file,
                                                             noFocus: !setFocus,
                                                             isPreview: true,
                                                             noSelectTab: true);

                //RefreshTabFromFile(file, noFocus: !setFocus, isPreview: true, noSelectTab: true);

                editor = tab?.Tag as MarkdownDocumentEditor;
                if (editor == null)
                {
                    return(null);
                }
            }

            if (tab == null)
            {
                return(null);
            }

            // make sure topic is associated with editor
            SetEditorWithTopic(editor, topic, isUnEdited: true); // kavaUi.AddinModel.ActiveTopic);

            // Explicitly read in the current text from an open tab and save to body
            var body = editor.GetMarkdown();

            if (!string.IsNullOrEmpty(body))
            {
                topic.Body = topic.StripYaml(body);
            }

            if (string.IsNullOrEmpty(topic.Link))
            {
                var relative = FileUtils.GetRelativePath(topic.GetTopicFileName(), topic.Project.ProjectDirectory);
                if (!string.IsNullOrEmpty(relative))
                {
                    topic.Link = FileUtils.NormalizePath(relative);
                    if (string.IsNullOrEmpty(topic.Link))
                    {
                        topic.Link = topic.Link.Replace("\\", "/");
                    }
                }
            }

            if (body != topic.Body)
            {
                editor.SetMarkdown(topic.Body);
            }

            window.TabControl.SelectedItem = tab;


            return(tab);
        }
Exemplo n.º 6
0
        public bool HandleSelection(DocTopic topic = null, bool forceFocus = false)
        {
            bool selectTopic = false;

            if (topic == null)
            {
                topic = TreeTopicBrowser.SelectedItem as DocTopic;
            }
            else
            {
                selectTopic = true;
            }

            if (topic == null)
            {
                return(false);
            }

            var lastTopic = kavaUi.AddinModel.ActiveTopic;

            if (lastTopic != null)
            {
                lastTopic.TopicState.IsSelected = false;
            }

            kavaUi.AddinModel.LastTopic = lastTopic;

            bool result = SaveProjectFileForTopic(kavaUi.AddinModel.LastTopic);

            if (result)
            {
                kavaUi.AddinModel.Window.ShowStatus("Topic saved.", 3000);
            }

            kavaUi.AddinModel.ActiveTopic = topic;

            // TODO: Move to function
            if (kavaUi.AddinModel.RecentTopics.Contains(topic))
            {
                kavaUi.AddinModel.RecentTopics.Remove(topic);
            }
            kavaUi.AddinModel.RecentTopics.Insert(0, topic);

            if (kavaUi.AddinModel.RecentTopics.Count > 15)
            {
                kavaUi.AddinModel.RecentTopics =
                    new ObservableCollection <DocTopic>(kavaUi.AddinModel.RecentTopics.Take(14));
            }

            //OpenTopicInEditor();
            Dispatcher.InvokeAsync(() => OpenTopicInEditor(), DispatcherPriority.ApplicationIdle);

            var file = topic.GetTopicFileName();
            var doc  = new MarkdownDocument();

            doc.Load(file);

            // set topic state to selected and unchanged
            if (selectTopic)
            {
                topic.TopicState.IsSelected = true;
            }
            topic.TopicState.IsDirty = false;

            return(true);
        }