private void InitializePreviewBrowser()
        {
            PreviewBrowser.Navigated += (sender, e) =>
            {
                // No Script Errors
                NoScriptErrors(PreviewBrowser, true);
            };
            PreviewBrowser.LoadCompleted += (sender, e) =>
            {
                if (e.Uri.ToString().Contains("about:blank"))
                {
                    return;
                }

                var     editor = GetActiveMarkdownEditor();
                dynamic dom    = PreviewBrowser.Document;
                dom.documentElement.scrollTop = editor.MarkdownDocument.LastBrowserScrollPosition;

                new Timer((editr) =>
                {
                    try
                    {
                        if (File.Exists(editor.MarkdownDocument.HtmlRenderFilename))
                        {
                            File.Delete(editor.MarkdownDocument.HtmlRenderFilename);
                        }
                    }
                    catch
                    { }
                }, null, 2000, Timeout.Infinite);
            };
            PreviewBrowser.Navigate("about:blank");
        }
        public EditArticleWindow(NewspaperArticle article)
        {
            InitializeComponent();
            DataContext = new EditArticleViewModel(article);
            //PreviewBrowser.NavigateToString("<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></head><body style='border: solid 1px gray'><div style='text-align:center;margin-top:100px;'>Тут появится статья</div></body></html>");


            ViewModel.ShowHtml = () =>
            {
                var ph = Path.Combine(Environment.CurrentDirectory, "ArticlePreview/Article.htm");
                PreviewBrowser.Navigate("file:///" + ph);
            };

            ViewModel.Init();
        }
        private void TreeTopicBrowser_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            SelectedTopic = TopicPicker.SelectedTopic;
            if (SelectedTopic == null)
            {
                return;
            }

            var doc = new MarkdownDocument();

            doc.Load(SelectedTopic.GetTopicFileName());
            doc.RenderHtmlToFile();

            PreviewBrowser.Navigate(doc.HtmlRenderFilename);
        }
        /// <summary>
        /// Shows or hides the preview browser
        /// </summary>
        /// <param name="hide"></param>
        public void ShowPreviewBrowser(bool hide = false)
        {
            if (!hide)
            {
                PreviewBrowser.Visibility = Visibility.Visible;

                ContentGrid.ColumnDefinitions[1].Width = new GridLength(12);
                ContentGrid.ColumnDefinitions[2].Width = new GridLength(Width / 2 - 40);
            }
            else
            {
                if (ContentGrid.ColumnDefinitions[2].Width.Value > 100)
                {
                    mmApp.Configuration.WindowPosition.SplitterPosition = Convert.ToInt32(ContentGrid.ColumnDefinitions[2].Width.Value);
                }

                ContentGrid.ColumnDefinitions[1].Width = new GridLength(0);
                ContentGrid.ColumnDefinitions[2].Width = new GridLength(0);
                PreviewBrowser.Navigate("about:blank");
            }
        }
        public void PreviewMarkdown(MarkdownDocumentEditor editor = null, bool keepScrollPosition = false, bool showInBrowser = false)
        {
            if (!Model.IsPreviewBrowserVisible && !showInBrowser)
            {
                return;
            }

            if (editor == null)
            {
                editor = GetActiveMarkdownEditor();
            }

            if (editor == null)
            {
                return;
            }

            var doc = editor.MarkdownDocument;
            var ext = Path.GetExtension(editor.MarkdownDocument.Filename).ToLower().Replace(".", "");


            int     lastPos = 0;
            dynamic dom     = null;

            if (string.IsNullOrEmpty(ext) || ext == "md" || ext == "html" || ext == "htm")
            {
                this.ShowPreviewBrowser();

                if (keepScrollPosition)
                {
                    dom = PreviewBrowser.Document;
                    editor.MarkdownDocument.LastBrowserScrollPosition = dom.documentElement.scrollTop;
                }
                else
                {
                    editor.MarkdownDocument.LastBrowserScrollPosition = 0;
                }


                if (ext == "html" || ext == "htm")
                {
                    File.WriteAllText(editor.MarkdownDocument.HtmlRenderFilename, editor.MarkdownDocument.CurrentText);
                }
                else
                {
                    editor.MarkdownDocument.RenderHtmlToFile();
                }

                if (showInBrowser)
                {
                    ShellUtils.GoUrl(editor.MarkdownDocument.HtmlRenderFilename);
                }
                else
                {
                    PreviewBrowser.Cursor      = Cursors.None;
                    PreviewBrowser.ForceCursor = true;
                    if (keepScrollPosition &&
                        PreviewBrowser.Source.ToString() == editor.MarkdownDocument.HtmlRenderFilename)
                    {
                        PreviewBrowser.Refresh(true);
                    }
                    else
                    {
                        PreviewBrowser.Navigate(editor.MarkdownDocument.HtmlRenderFilename);
                    }
                }
            }
            else
            {
                ShowPreviewBrowser(true);
            }
        }
        /// <summary>
        /// Closes a tab and ask for confirmation if the tab doc
        /// is dirty.
        /// </summary>
        /// <param name="tab"></param>
        /// <returns>true if tab can close, false if it should stay open</returns>
        public bool CloseTab(TabItem tab)
        {
            if (tab == null)
            {
                return(false);
            }

            var editor = tab.Tag as MarkdownDocumentEditor;

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

            bool returnValue = true;

            var doc = editor.MarkdownDocument;

            if (!string.IsNullOrEmpty(doc.HtmlRenderFilename) && File.Exists(doc.HtmlRenderFilename))
            {
                File.Delete(doc.HtmlRenderFilename);
            }

            if (doc.IsDirty)
            {
                var res = MessageBox.Show(Path.GetFileName(doc.Filename) + "\r\n\r\nhas been modified.\r\n" +
                                          "Do you want to save changes?",
                                          "Save Document",
                                          MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Cancel);
                if (res == MessageBoxResult.Cancel)
                {
                    return(false); // don't close
                }
                else if (res == MessageBoxResult.No)
                {
                    // close but don't save
                }
                else
                {
                    if (doc.Filename == "untitled")
                    {
                        Model.SaveAsCommand.Execute(ButtonSaveAsFile);
                    }
                    else if (!SaveFile())
                    {
                        returnValue = false;
                    }
                }
            }

            tab.Tag = null;
            TabControl.Items.Remove(tab);

            if (TabControl.Items.Count == 0)
            {
                PreviewBrowser.Visibility = Visibility.Hidden;
                PreviewBrowser.Navigate("about:blank");
                Model.ActiveDocument = null;
            }

            return(returnValue); // close
        }
        public void PreviewMarkdown(MarkdownDocumentEditor editor = null, bool keepScrollPosition = false, bool showInBrowser = false)
        {
            // only render if the preview is actually visible and rendering in Preview Browser
            if (!Model.IsPreviewBrowserVisible && !showInBrowser)
            {
                return;
            }

            if (editor == null)
            {
                editor = GetActiveMarkdownEditor();
            }

            if (editor == null)
            {
                return;
            }

            var doc = editor.MarkdownDocument;
            var ext = Path.GetExtension(doc.Filename).ToLower().Replace(".", "");

            string renderedHtml = null;

            if (string.IsNullOrEmpty(ext) || ext == "md" || ext == "html" || ext == "htm")
            {
                dynamic dom = null;
                if (keepScrollPosition)
                {
                    dom = PreviewBrowser.Document;
                    editor.MarkdownDocument.LastBrowserScrollPosition = dom.documentElement.scrollTop;
                }
                else
                {
                    ShowPreviewBrowser(false, false);
                    editor.MarkdownDocument.LastBrowserScrollPosition = 0;
                }

                if (ext == "html" || ext == "htm")
                {
                    editor.MarkdownDocument.WriteFile(editor.MarkdownDocument.HtmlRenderFilename, editor.MarkdownDocument.CurrentText);
                }
                else
                {
                    renderedHtml = editor.MarkdownDocument.RenderHtmlToFile();
                }

                if (showInBrowser)
                {
                    ShellUtils.GoUrl(editor.MarkdownDocument.HtmlRenderFilename);
                }
                else
                {
                    PreviewBrowser.Cursor      = Cursors.None;
                    PreviewBrowser.ForceCursor = true;
                    if (keepScrollPosition)
                    {
                        string browserUrl   = PreviewBrowser.Source.ToString().ToLower();
                        string documentFile = "file:///" + editor.MarkdownDocument.HtmlRenderFilename.Replace('\\', '/').ToLower();
                        if (browserUrl == documentFile)
                        {
                            dom = PreviewBrowser.Document;
                            //var content = dom.getElementById("MainContent");


                            renderedHtml = StringUtils.ExtractString(renderedHtml,
                                                                     "<!-- Markdown Monster Content -->",
                                                                     "<!-- End Markdown Monster Content -->");

                            if (string.IsNullOrEmpty(renderedHtml))
                            {
                                PreviewMarkdown(editor, false, false); // fully reload document
                            }
                            else
                            {
                                try
                                {
                                    // explicitly update the document with JavaScript code
                                    // much more efficient and non-jumpy and no wait cursor
                                    var window = dom.parentWindow;
                                    window.updateDocumentContent(renderedHtml);
                                }
                                catch
                                {
                                    PreviewBrowser.Refresh(true);
                                }
                            }

                            return;
                        }
                    }

                    PreviewBrowser.Navigate(editor.MarkdownDocument.HtmlRenderFilename);
                    return;
                }
            }

            ShowPreviewBrowser(true, keepScrollPosition);
        }
 public void Navigate(string url)
 {
     PreviewBrowser.Navigate(url);
 }