コード例 #1
0
        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);
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
 public void Refresh(bool noCache = false)
 {
     PreviewBrowser.Refresh(noCache);
     PreviewMarkdownAsync();
 }