From 086fcd8b1e4f0645a3e5bfb8cab7d42f5b895890 Mon Sep 17 00:00:00 2001 From: Clint Rutkas Date: Fri, 12 Nov 2021 21:14:58 -0800 Subject: [PATCH] can load via url --- .../Monaco/monacoPreview/MainWindow.xaml.cs | 36 ++-- .../Monaco/monacoPreview/PreviewWindow.xaml | 21 -- .../monacoPreview/PreviewWindow.xaml.cs | 203 ------------------ .../Monaco/monacoPreview/index.html | 5 - .../Monaco/monacoPreview/monacoPreview.csproj | 8 +- deps/cziplib | 2 +- 6 files changed, 20 insertions(+), 255 deletions(-) delete mode 100644 ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml delete mode 100644 ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml.cs diff --git a/ProofOfConcept/Monaco/monacoPreview/MainWindow.xaml.cs b/ProofOfConcept/Monaco/monacoPreview/MainWindow.xaml.cs index 90924c2a1..5b6677612 100644 --- a/ProofOfConcept/Monaco/monacoPreview/MainWindow.xaml.cs +++ b/ProofOfConcept/Monaco/monacoPreview/MainWindow.xaml.cs @@ -30,9 +30,13 @@ namespace monacoPreview string[] file = GetFile(args); - InitializeAsync(settings.compatibility?file[0]:file[2], fileHandler.GetLanguage(file[1])); + var fileName = ""; + if(args != null && args.Length > 1) + { + fileName = args[1]; + } - + InitializeAsync(fileName); } public string[] GetFile(string[] args) @@ -99,43 +103,33 @@ namespace monacoPreview webView.Width = this.ActualWidth; } - public async void InitializeAsync(string code, string lang) + public async void InitializeAsync(string fileName) { // This function initializes the webview settings // Partely copied from https://weblog.west-wind.com/posts/2021/Jan/14/Taking-the-new-Chromium-WebView2-Control-for-a-Spin-in-NET-Part-1 + var vsCodeLangSet = fileHandler.GetLanguage(Path.GetExtension(fileName).TrimStart('.')); + var fileContent = File.ReadAllText(fileName); + var base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent)); // Sets the url - webView.Source = settings.compatibility?GetURLwithCode(code, lang):GetURLwithFilePath(code, lang); + // Initialize WebView + webView.Source = GetURLwithCode(base64FileCode, vsCodeLangSet); - var webViewOptions = new CoreWebView2EnvironmentOptions - { - // Enable CORS for local file access. - AdditionalBrowserArguments = "--disable-web-security" - }; - - var env = await CoreWebView2Environment.CreateAsync(null, null, webViewOptions); - await webView.EnsureCoreWebView2Async(env); + await webView.EnsureCoreWebView2Async(await CoreWebView2Environment.CreateAsync()); webView.NavigationCompleted += WebView2Init; webView.NavigationStarting += NavigationStarted; } - - public Uri GetURLwithFilePath(string file, string lang) - { - // This function returns a url you can use to access index.html - - return new Uri(settings.baseURL + "?file=" + file + "&lang=" + lang + "&theme=" + settings.GetTheme(ThemeListener.AppMode) + "&wrap=" + (this.settings.wrap?"1":"0")); - } public Uri GetURLwithCode(string code, string lang) { // This function returns a url you can use to access index.html // Converts code to base64 - code = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(code)).Replace("+", "%2B"); + code = code.Replace("+", "%2B"); // this is needed for URL encode; - return new Uri(settings.baseURL + "?code=" + code + "&lang=" + lang + "&theme=" + settings.GetTheme(ThemeListener.AppMode) + "&wrap=" + (this.settings.wrap ? "1" : "0")); + return new Uri(settings.baseURL + "?code=" + code + "&lang=" + lang + "&theme=" + settings.GetTheme(ThemeListener.AppMode) + "&wrap=" + (settings.wrap ? "1" : "0")); } } } diff --git a/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml b/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml deleted file mode 100644 index 12ee347c9..000000000 --- a/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml +++ /dev/null @@ -1,21 +0,0 @@ - - - - data:text/html;base64,PHA+TG9hZGluZy4uLjwvcD48YnI+PGltZyBzcmM9Imh0dHBzOi8vZG9jcy5taWNyb3NvZnQuY29tL2RlLWRlL3dpbmRvd3Mvd2ludWkvYXBpL21pY3Jvc29mdC51aS54YW1sLmNvbnRyb2xzL2ltYWdlcy9jb250cm9scy9wcm9ncmVzc3JpbmdfaW5kZXRlcm1pbmF0ZS5naWYiPg== - - - - - diff --git a/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml.cs b/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml.cs deleted file mode 100644 index 2f0aeb1d8..000000000 --- a/ProofOfConcept/Monaco/monacoPreview/PreviewWindow.xaml.cs +++ /dev/null @@ -1,203 +0,0 @@ -using System; -using System.IO; -using System.Windows; -using Microsoft.Web.WebView2.Core; -using Windows.UI.Xaml; -using WK.Libraries.WTL; -using System.Windows.Shell; -using Microsoft.Win32; -using System.ComponentModel; -using System.Drawing; -using System.Runtime.InteropServices; - -namespace monacoPreview -{ - /// - /// Interaction logic for MainWindow.xaml - /// - public partial class PreviewWindow : IPreviewHandler, IOleWindow, IObjectWithSite - { - // This variable prevents users from navigating - private bool WasNavigated = false; - - // Settings from Settings.cs - private readonly Settings settings = new Settings(); - - // Filehandler class from FileHandler.cs - private readonly FileHandler fileHandler = new FileHandler(); - - public PreviewWindow() - { - System.Diagnostics.Debug.WriteLine("Start"); - - // Get command line args - string[] args = Environment.GetCommandLineArgs(); - - InitializeComponent(); - - string[] file = GetFile(args); - - InitializeAsync(settings.compatibility?file[0]:file[2], fileHandler.GetLanguage(file[1])); - - - } - - public string[] GetFile(string[] args) - { - // This function gets a file - string[] returnValue = new string[3]; - // Get source code - returnValue[0] = File.ReadAllText(args[1]); - // Gets file extension (without .) - returnValue[1] = Path.GetExtension(args[1]).Replace(".",""); - returnValue[2] = args[1]; - return returnValue; - } - - private void WebView2Init(Object sender, CoreWebView2NavigationCompletedEventArgs e) - { - // This function sets the diiferent settings for the webview - - // Checks if already navigated - if (!WasNavigated) - { - CoreWebView2Settings settings = webView.CoreWebView2.Settings; - - // Disable contextmenu - //settings.AreDefaultContextMenusEnabled = false; - // Disable developer menu - //settings.AreDevToolsEnabled = false; - // Disable script dialogs (like alert()) - settings.AreDefaultScriptDialogsEnabled = false; - // Enables JavaScript - settings.IsScriptEnabled = true; - // Disable zoom woth ctrl and scroll - settings.IsZoomControlEnabled = false; - // Disable developer menu - settings.IsBuiltInErrorPageEnabled = false; - // Disable status bar - settings.IsStatusBarEnabled = false; - } - } - - private void NavigationStarted(Object sender, CoreWebView2NavigationStartingEventArgs e) - { - // Prevents navigation if already one done to index.html - if (WasNavigated) - { - e.Cancel = false; - } - - // If it has navigated to index.html it stops further navigations - if(e.Uri.StartsWith(settings.baseURL)) - { - WasNavigated = true; - } - } - - private void FormResize(object sender, EventArgs e) - { - // This function gets called whan the form gets resized - // It's fitting the webview in the size of the window - // TO-DO: Javascript resize - viewbox.Height = this.ActualHeight; - viewbox.Width = this.ActualWidth; - webView.Height = this.ActualHeight; - webView.Width = this.ActualWidth; - } - - public async void InitializeAsync(string code, string lang) - { - // This function initializes the webview settings - // Partely copied from https://weblog.west-wind.com/posts/2021/Jan/14/Taking-the-new-Chromium-WebView2-Control-for-a-Spin-in-NET-Part-1 - - // Sets the url - webView.Source = settings.compatibility?GetURLwithCode(code, lang):GetURLwithFilePath(code, lang); - - // Initialize WebView - - var webViewOptions = new CoreWebView2EnvironmentOptions - { - // Enable CORS for local file access. - AdditionalBrowserArguments = "--disable-web-security" - }; - - var env = await CoreWebView2Environment.CreateAsync(null, null, webViewOptions); - await webView.EnsureCoreWebView2Async(env); - webView.NavigationCompleted += WebView2Init; - webView.NavigationStarting += NavigationStarted; - } - - public Uri GetURLwithFilePath(string file, string lang) - { - // This function returns a url you can use to access index.html - - return new Uri(settings.baseURL + "?file=" + file + "&lang=" + lang + "&theme=" + settings.GetTheme(ThemeListener.AppMode) + "&wrap=" + (this.settings.wrap?"1":"0")); - } - - public Uri GetURLwithCode(string code, string lang) - { - // This function returns a url you can use to access index.html - - // Converts code to base64 - code = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(code)).Replace("+", "%2B"); - - return new Uri(settings.baseURL + "?code=" + code + "&lang=" + lang + "&theme=" + settings.GetTheme(ThemeListener.AppMode) + "&wrap=" + (this.settings.wrap ? "1" : "0")); - } - - public void SetWindow(IntPtr hwnd, ref RECT rect) - { - - } - - public void SetRect(ref RECT rect) - { - - } - - public void DoPreview() - { - - } - - public void Unload() - { - - } - - public void SetFocus() - { - - } - - public void QueryFocus(out IntPtr phwnd) - { - phwnd = new IntPtr(); - } - - public uint TranslateAccelerator(ref MSG pmsg) - { - return 1; - } - - public void GetWindow(out IntPtr phwnd) - { - phwnd = this; - } - - public void ContextSensitiveHelp([MarshalAs(UnmanagedType.Bool)] bool fEnterMode) - { - throw new NotImplementedException(); - } - - public void SetSite([In, MarshalAs(UnmanagedType.IUnknown)] object pUnkSite) - { - throw new NotImplementedException(); - } - - public void GetSite(ref Guid riid, [MarshalAs(UnmanagedType.IUnknown)] out object ppvSite) - { - throw new NotImplementedException(); - } - } -} diff --git a/ProofOfConcept/Monaco/monacoPreview/index.html b/ProofOfConcept/Monaco/monacoPreview/index.html index e24454c5c..eba0c67ce 100644 --- a/ProofOfConcept/Monaco/monacoPreview/index.html +++ b/ProofOfConcept/Monaco/monacoPreview/index.html @@ -2,11 +2,6 @@