fix merge conflicts
This commit is contained in:
commit
201189454a
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using Microsoft.Web.WebView2.Core;
|
||||
using WK.Libraries.WTL;
|
||||
|
||||
|
@ -30,9 +31,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)
|
||||
|
@ -59,7 +64,7 @@ namespace monacoPreview
|
|||
// Disable contextmenu
|
||||
//settings.AreDefaultContextMenusEnabled = false;
|
||||
// Disable developer menu
|
||||
//settings.AreDevToolsEnabled = false;
|
||||
settings.AreDevToolsEnabled = false;
|
||||
// Disable script dialogs (like alert())
|
||||
settings.AreDefaultScriptDialogsEnabled = false;
|
||||
// Enables JavaScript
|
||||
|
@ -71,6 +76,8 @@ namespace monacoPreview
|
|||
// Disable status bar
|
||||
settings.IsStatusBarEnabled = false;
|
||||
}
|
||||
|
||||
File.Delete(fullCustomFilePath);
|
||||
}
|
||||
|
||||
private void NavigationStarted(Object sender, CoreWebView2NavigationStartingEventArgs e)
|
||||
|
@ -99,43 +106,44 @@ namespace monacoPreview
|
|||
webView.Width = this.ActualWidth;
|
||||
}
|
||||
|
||||
public async void InitializeAsync(string code, string lang)
|
||||
string customFileName = "powerToysPreview" + Guid.NewGuid().ToString("N") + ".html";
|
||||
string fullCustomFilePath;
|
||||
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
|
||||
|
||||
// Sets the url
|
||||
webView.Source = settings.compatibility?GetURLwithCode(code, lang):GetURLwithFilePath(code, lang);
|
||||
var vsCodeLangSet = fileHandler.GetLanguage(Path.GetExtension(fileName).TrimStart('.'));
|
||||
var fileContent = File.ReadAllText(fileName);
|
||||
var base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
|
||||
|
||||
// prepping index html to load in
|
||||
var html = File.ReadAllText("index.html").Replace("\t", "");
|
||||
|
||||
html = html.Replace("[[PT_LANG]]", vsCodeLangSet);
|
||||
html = html.Replace("[[PT_WRAP]]", settings.wrap ? "1" : "0");
|
||||
html = html.Replace("[[PT_THEME]]", settings.GetTheme(ThemeListener.AppMode));
|
||||
html = html.Replace("[[PT_CODE]]", base64FileCode);
|
||||
File.WriteAllText(customFileName, html);
|
||||
fullCustomFilePath = Path.Combine(AppContext.BaseDirectory, customFileName);
|
||||
// Initialize WebView
|
||||
//webView.Source = GetURLwithCode(base64FileCode, vsCodeLangSet);
|
||||
webView.Source = new Uri(fullCustomFilePath);
|
||||
await webView.EnsureCoreWebView2Async(await CoreWebView2Environment.CreateAsync());
|
||||
|
||||
var webViewOptions = new CoreWebView2EnvironmentOptions
|
||||
{
|
||||
// Enable CORS for local file access.
|
||||
AdditionalBrowserArguments = "--disable-web-security --allow-file-access-from-file"
|
||||
};
|
||||
|
||||
var env = await CoreWebView2Environment.CreateAsync(null, null, webViewOptions);
|
||||
await webView.EnsureCoreWebView2Async(env);
|
||||
//webView.NavigateToString(html);
|
||||
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 = HttpUtility.UrlEncode(code); // 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
|
@ -1,18 +1,7 @@
|
|||
<!doctype html>
|
||||
<!doctype HTML>
|
||||
<html>
|
||||
<head>
|
||||
<script>
|
||||
|
||||
function getFile(path) {
|
||||
const xhttp = new XMLHttpRequest();
|
||||
xhttp.onload = function() {
|
||||
code = this.responseText;
|
||||
InitializeEditor();
|
||||
}
|
||||
xhttp.open("GET", "file:///"+path, true);
|
||||
xhttp.send();
|
||||
}
|
||||
|
||||
// Set variables
|
||||
|
||||
// Get URL parameters:
|
||||
|
@ -20,22 +9,26 @@
|
|||
// `theme` can be "light" or "dark"
|
||||
// `lang` is the language of the file
|
||||
// `wrap` if the editor is wraping or not
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
var theme = ("[[PT_THEME]]" == "dark") ? "vs-dark" : "vs";
|
||||
var lang = "[[PT_LANG]]";
|
||||
var wrap = ([[PT_WRAP]] == 1) ? true : false;
|
||||
|
||||
var base64code = "[[PT_CODE]]";
|
||||
var code = [atob(base64code)].join('\n');
|
||||
|
||||
|
||||
// const urlParams = new URLSearchParams(window.location.search);
|
||||
// // Code for the editor
|
||||
// code = [atob(urlParams.get("code"))].join('\n');
|
||||
// // Theme of the editor
|
||||
// theme = urlParams.get("theme") == "dark" ? "vs-dark" : "vs";
|
||||
// // Code language
|
||||
// lang = urlParams.get("lang");
|
||||
// // Word wraping
|
||||
//wrap = urlParams.get("wrap") == 1 ? true : false;
|
||||
|
||||
// Code for the editor
|
||||
code = (urlParams.get("code")!==null) ? [atob(urlParams.get("code"))].join('\n') : getFile(urlParams.get("file"));
|
||||
// Theme of the editor
|
||||
theme = urlParams.get("theme")=="dark"?"vs-dark":"vs";
|
||||
// Code language
|
||||
lang = urlParams.get("lang");
|
||||
// Word wraping
|
||||
wrap = urlParams.get("wrap")==1?true:false;
|
||||
//URL for accessibility help
|
||||
accessibilityHelpUrl = "";
|
||||
|
||||
|
||||
while(code = getFile(urlParams.get("file"))){}
|
||||
InitializeEditor();
|
||||
</script>
|
||||
<!-- Set browser to Edge-->
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||
|
@ -61,54 +54,41 @@
|
|||
<!-- Script -->
|
||||
<script src="monacoSRC/min/vs/loader.js"></script>
|
||||
<script>
|
||||
var editor;
|
||||
function InitializeEditor() {
|
||||
require.config({paths: {vs: 'monacoSRC/min/vs'}});
|
||||
require(['vs/editor/editor.main'], function () {
|
||||
// Creates the editor
|
||||
// For all parameters: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandaloneeditorconstructionoptions.html
|
||||
editor = monaco.editor.create(document.getElementById('container'), {
|
||||
value: code, // Sets content of the editor
|
||||
language: lang, // Sets language fof the code
|
||||
readOnly: true, // Sets to readonly
|
||||
accessibilityHelpUrl: accessibilityHelpUrl, // Sets the url for accessibility help
|
||||
contextmenu: false, // Disable context menu
|
||||
theme: theme, // Sets editor theme
|
||||
minimap: {enabled: false}, // Disables minimap
|
||||
lineNumbersMinChars: "3", //Width of the line numbers
|
||||
scrollbar: {
|
||||
// Deactivate shadows
|
||||
shadows: false,
|
||||
require.config({ paths: { vs: 'monacoSRC/min/vs' } });
|
||||
require(['vs/editor/editor.main'], function () {
|
||||
// Creates the editor
|
||||
// For all parameters: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.istandaloneeditorconstructionoptions.html
|
||||
var editor = monaco.editor.create(document.getElementById('container'), {
|
||||
value: code, // Sets content of the editor
|
||||
language: lang, // Sets language fof the code
|
||||
readOnly: true, // Sets to readonly
|
||||
accessibilityHelpUrl: accessibilityHelpUrl, // Sets the url for accessibility help
|
||||
contextmenu: false, // Disable context menu
|
||||
theme: theme, // Sets editor theme
|
||||
minimap: {enabled: false}, // Disables minimap
|
||||
lineNumbersMinChars: "3", //Width of the line numbers
|
||||
scrollbar: {
|
||||
// Deactivate shadows
|
||||
shadows: false,
|
||||
|
||||
// Render scrollbar automatically
|
||||
vertical: 'auto',
|
||||
horizontal: 'auto',
|
||||
// Render scrollbar automatically
|
||||
vertical: 'auto',
|
||||
horizontal: 'auto',
|
||||
|
||||
// Size of the scrollbar
|
||||
verticalScrollbarSize: 17,
|
||||
horizontalScrollbarSize: 50
|
||||
},
|
||||
wordWrap: (wrap ? "on" : "off") // Word wraps
|
||||
});
|
||||
window.onresize = function () {
|
||||
editor.layout();
|
||||
};
|
||||
|
||||
// Disable command palette
|
||||
editor.addAction({
|
||||
id: 'disable-F1',
|
||||
label: '',
|
||||
keybindings: [monaco.KeyCode.F1,],
|
||||
precondition: null,
|
||||
keybindingContext: null,
|
||||
contextMenuGroupId: 'navigation',
|
||||
contextMenuOrder: 1.5,
|
||||
run: function (ed) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
// Size of the scrollbar
|
||||
verticalScrollbarSize: 17,
|
||||
horizontalScrollbarSize: 50
|
||||
},
|
||||
wordWrap: (wrap?"on":"off") // Word wraps
|
||||
});
|
||||
}
|
||||
window.onresize = function (){
|
||||
editor.layout();
|
||||
};
|
||||
|
||||
// Disable command palette
|
||||
editor.addAction({id: 'disable-F1',label: '',keybindings: [monaco.KeyCode.F1,],precondition: null,keybindingContext: null,contextMenuGroupId: 'navigation',contextMenuOrder: 1.5,run: function(ed) {return null;}});
|
||||
});
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
|
@ -1,12 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ProduceReferenceAssembly>True</ProduceReferenceAssembly>
|
||||
<Company>Microsoft Corporation</Company>
|
||||
<AssemblyVersion>0.0.0</AssemblyVersion>
|
||||
<OutputType>WinExe</OutputType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -31,7 +29,7 @@
|
|||
<!-- Outputs the needed html files -->
|
||||
<ItemGroup>
|
||||
<None Update="index.html">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -40,11 +38,9 @@
|
|||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Page Remove="PreviewWindow.xaml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Remove="PreviewWindow.xaml.cs" />
|
||||
<Compile Remove="MonacoPreviewHandlerInterfaces.cs" />
|
||||
<Compile Update="FileHandler.cs">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue