예제 #1
0
        private void WebBrowser_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            DotnetInterop = new WebViewPreviewDotnetInterop(Model, WebBrowser);

            WebBrowser.CoreWebView2.AddHostObjectToScript("mm", DotnetInterop);
            JsInterop = DotnetInterop.JsInterop;
            JsInterop.InitializeInterop();
        }
예제 #2
0
        private void WebBrowser_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            // Assign interop objects for each request
            DotnetInterop = new WebViewPreviewDotnetInterop(Model, WebBrowser);
            JsInterop     = DotnetInterop.JsInterop;

            WebBrowser.CoreWebView2.AddHostObjectToScript("mm", DotnetInterop);
            DotnetInterop.InitializeInteropAsync().ConfigureAwait(false).GetAwaiter();
        }
예제 #3
0
        async Task InitializeAsync()
        {
            // must create a data folder if running out of a secured folder that can't write like Program Files
            var browserFolder = Path.Combine(mmApp.Configuration.CommonFolder, "WebView_Browser");
            var env           = await CoreWebView2Environment.CreateAsync(
                userDataFolder : browserFolder
                );

            await WebBrowser.EnsureCoreWebView2Async(env);

            WebBrowser.NavigationCompleted += WebBrowser_NavigationCompleted;

            if (Model.Configuration.System.ShowDeveloperToolsOnStartup)
            {
                WebBrowser.CoreWebView2.OpenDevToolsWindow();
            }

            // Set up interop object to pass into JavaScript
            DotnetInterop = new WebViewPreviewDotnetInterop(Model, WebBrowser);
            JsInterop     = DotnetInterop.JsInterop;
            WebBrowser.CoreWebView2.AddHostObjectToScript("mm", DotnetInterop);
        }
예제 #4
0
        async Task InitializeAsync()
        {
            // initial assignment of interop objects
            //DotnetInterop = new WebViewPreviewDotnetInterop(Model, WebBrowser);
            //JsInterop = DotnetInterop.JsInterop;

            var browserFolder = Path.Combine(mmApp.Configuration.CommonFolder, "WebView_Browser");
            // must create a data folder if running out of a secured folder that can't write like Program Files
            var env = await CoreWebView2Environment.CreateAsync(
                userDataFolder : browserFolder
                );

            await WebBrowser.EnsureCoreWebView2Async(env);

            if (Model.Configuration.System.ShowDeveloperToolsOnStartup)
            {
                WebBrowser.CoreWebView2.OpenDevToolsWindow();
            }

            // initialize here 'initially' then re-initialize for each navigation
            DotnetInterop = new WebViewPreviewDotnetInterop(Model, WebBrowser);
            JsInterop     = DotnetInterop.JsInterop;
        }
예제 #5
0
        public void PreviewMarkdown(MarkdownDocumentEditor editor = null,
                                    bool keepScrollPosition       = false, bool showInBrowser  = false,
                                    string renderedHtml           = null, int editorLineNumber = -1)
        {
            if (DotnetInterop == null)
            {
                return;
            }

            try
            {
                // only render if the preview is actually visible and rendering in Preview Browser
                if (!Model.IsPreviewBrowserVisible && !showInBrowser)
                {
                    return;
                }

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

                if (editor == null)
                {
                    return;
                }

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

                string mappedTo = "markdown";
                if (!string.IsNullOrEmpty(renderedHtml))
                {
                    mappedTo = "html";
                    ext      = null;
                }
                else
                {
                    // only show preview for Markdown and HTML documents
                    Model.Configuration.EditorExtensionMappings.TryGetValue(ext, out mappedTo);
                    mappedTo = mappedTo ?? string.Empty;
                }

                if (string.IsNullOrEmpty(ext) || mappedTo == "markdown" || mappedTo == "html")
                {
                    // TODO: Get DOM and
                    //dynamic dom = null;
                    //if (!showInBrowser)
                    //{
                    //    if (keepScrollPosition)
                    //    {
                    //        dom = WebBrowser..Document;
                    //        editor.MarkdownDocument.LastEditorLineNumber = dom.documentElement.scrollTop;
                    //    }
                    //    else
                    //    {
                    //        Window.ShowPreviewBrowser(false, false);
                    //        editor.MarkdownDocument.LastEditorLineNumber = 0;
                    //    }
                    //}

                    if (mappedTo == "html")
                    {
                        if (string.IsNullOrEmpty(renderedHtml))
                        {
                            renderedHtml = editor.MarkdownDocument.CurrentText;
                        }

                        if (!editor.MarkdownDocument.WriteFile(editor.MarkdownDocument.HtmlRenderFilename, renderedHtml))
                        {
                            // need a way to clear browser window
                            return;
                        }
                    }
                    else
                    {
                        bool usePragma = !showInBrowser && mmApp.Configuration.PreviewSyncMode != PreviewSyncMode.None;
                        if (string.IsNullOrEmpty(renderedHtml))
                        {
                            renderedHtml = editor.MarkdownDocument.RenderHtmlToFile(usePragmaLines: usePragma);
                        }

                        if (renderedHtml == null)
                        {
                            Window.SetStatusIcon(FontAwesomeIcon.Warning, Colors.Red, false);
                            Window.ShowStatus($"Access denied: {Path.GetFileName(editor.MarkdownDocument.Filename)}",
                                              5000);
                            // need a way to clear browser window

                            return;
                        }

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

                    if (showInBrowser)
                    {
                        var url = editor.MarkdownDocument.HtmlRenderFilename;
                        mmFileUtils.ShowExternalBrowser(url);
                        return;
                    }

                    WebBrowser.Cursor      = System.Windows.Input.Cursors.None;
                    WebBrowser.ForceCursor = true;

                    // if content contains <script> tags we must do a full page refresh
                    bool forceRefresh = renderedHtml != null && renderedHtml.Contains("<script ");


                    if (keepScrollPosition && !mmApp.Configuration.AlwaysUsePreviewRefresh && !forceRefresh)
                    {
                        string browserUrl   = WebBrowser.Source?.ToString().ToLower();
                        string documentFile = "file:///" +
                                              editor.MarkdownDocument.HtmlRenderFilename.Replace('\\', '/')
                                              .ToLower();
                        if (browserUrl == documentFile)
                        {
                            if (string.IsNullOrEmpty(renderedHtml))
                            {
                                PreviewMarkdown(editor, false, false); // fully reload document
                            }
                            else
                            {
                                try
                                {
                                    JsInterop = DotnetInterop.JsInterop;

                                    int lineno = editor.GetLineNumber();
                                    _ = JsInterop.UpdateDocumentContent(renderedHtml, lineno);

                                    try
                                    {
                                        // scroll preview to selected line
                                        if (mmApp.Configuration.PreviewSyncMode ==
                                            PreviewSyncMode.EditorAndPreview ||
                                            mmApp.Configuration.PreviewSyncMode == PreviewSyncMode.EditorToPreview)
                                        {
                                            if (lineno > -1)
                                            {
                                                _ = JsInterop.ScrollToPragmaLine(lineno);
                                            }
                                        }
                                    }
                                    catch
                                    {
                                        /* ignore scroll error */
                                    }
                                }
                                catch
                                {
                                    // Refresh doesn't fire Navigate event again so
                                    // the page is not getting initiallized properly
                                    //PreviewBrowser.Refresh(true);
                                    WebBrowser.Tag = "EDITORSCROLL";


                                    WebBrowser.Source = new Uri(editor.MarkdownDocument.HtmlRenderFilename);
                                    mmApp.Log("Document Update Crash", null, false, LogLevels.Information);
                                }
                            }

                            return;
                        }
                    }

                    WebBrowser.Tag = "EDITORSCROLL";
                    var uri = new Uri(editor.MarkdownDocument.HtmlRenderFilename);

                    if (WebBrowser.Source != null && WebBrowser.Source.Equals(uri))
                    {
                        WebBrowser.CoreWebView2.Reload();
                    }
                    else
                    {
                        WebBrowser.Source = new Uri(editor.MarkdownDocument.HtmlRenderFilename);
                    }

                    return;
                }

                // not a markdown or HTML document to preview
                Window.ShowPreviewBrowser(true, keepScrollPosition);
            }
            catch (Exception ex)
            {
                //mmApp.Log("PreviewMarkdown failed (Exception captured - continuing)", ex);
                Debug.WriteLine("PreviewMarkdown failed (Exception captured - continuing)", ex);
            }
        }