can load via url

This commit is contained in:
Clint Rutkas 2021-11-12 21:14:58 -08:00
parent 6bc83491cc
commit 086fcd8b1e
6 changed files with 20 additions and 255 deletions

View file

@ -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"));
}
}
}

View file

@ -1,21 +0,0 @@
<Window x:Class="monacoPreview.PreviewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="MainWindow"
Height="700"
Width="300"
SizeChanged="FormResize"
x:Name="window"
xmlns:System="clr-namespace:System;assembly=System.Runtime">
<Window.Resources>
<!-- Data url for loading page-->
<System:Uri x:Key="LoadingUrl">data:text/html;base64,PHA+TG9hZGluZy4uLjwvcD48YnI+PGltZyBzcmM9Imh0dHBzOi8vZG9jcy5taWNyb3NvZnQuY29tL2RlLWRlL3dpbmRvd3Mvd2ludWkvYXBpL21pY3Jvc29mdC51aS54YW1sLmNvbnRyb2xzL2ltYWdlcy9jb250cm9scy9wcm9ncmVzc3JpbmdfaW5kZXRlcm1pbmF0ZS5naWYiPg==</System:Uri>
</Window.Resources>
<Viewbox x:Name="viewbox">
<wv2:WebView2 Width="300" Height="700" Name="webView" Source="{DynamicResource LoadingUrl}" />
</Viewbox>
</Window>

View file

@ -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
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
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();
}
}
}

View file

@ -2,11 +2,6 @@
<html>
<head>
<script>
function getFile(path) {
//To-DO: get file directly
}
// Set variables
// Get URL parameters:

View file

@ -29,7 +29,7 @@
<!-- Outputs the needed html files -->
<ItemGroup>
<None Update="index.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
@ -38,9 +38,9 @@
</None>
</ItemGroup>
<ItemGroup>
<Page Update="PreviewWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Compile Update="FileHandler.cs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Compile>
</ItemGroup>
</Project>

2
deps/cziplib vendored

@ -1 +1 @@
Subproject commit f9e0959eb212262aff67e8425aceb62d7a518f62
Subproject commit 692cbcf8ca3cd90c43d7159da098e0763ecdab38