[Developer Preview] Bug fixes (#16108)

* Renamed languages.json to monaco_languages.json

* Update excludes.txt

* Fixed encoding issue

* Reverted accidently removed file

* push

* push

* push

* Fixing path to monaco_languages.json

* Removing unused file

* Fixing NullReferenceException bug

* Minor fixes

Co-authored-by: Stefan Markovic <stefan@janeasystems.com>
This commit is contained in:
Aaron Junker
2022-02-21 16:10:55 +01:00
committed by GitHub
parent 6b2cde7eb0
commit 4cc74602c1
10 changed files with 43 additions and 28 deletions

View File

@@ -20,15 +20,15 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
{
try
{
JsonDocument a = JsonDocument.Parse(File.ReadAllText(Settings.AssemblyDirectory + "\\languages.json"));
JsonElement list = a.RootElement.GetProperty("list");
for (int i = 0; i < list.GetArrayLength(); i++)
JsonDocument languageListDocument = JsonDocument.Parse(File.ReadAllText(Settings.AssemblyDirectory + "\\monaco_languages.json"));
JsonElement languageList = languageListDocument.RootElement.GetProperty("list");
foreach (JsonElement e in languageList.EnumerateArray())
{
for (int j = 0; j < list[i].GetProperty("extensions").GetArrayLength(); j++)
for (int j = 0; j < e.GetProperty("extensions").GetArrayLength(); j++)
{
if (list[i].GetProperty("extensions")[j].ToString() == fileExtension)
if (e.GetProperty("extensions")[j].ToString() == fileExtension)
{
return list[i].GetProperty("id").ToString();
return e.GetProperty("id").ToString();
}
}
}

View File

@@ -82,7 +82,7 @@
</ItemGroup>
<ItemGroup>
<None Update="languages.json">
<None Update="monaco_languages.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.CompilerServices;
@@ -91,11 +92,11 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
_webView2Environment = webView2EnvironmentAwaiter.GetResult();
var vsCodeLangSet = FileHandler.GetLanguage(Path.GetExtension(filePath));
var fileContent = File.ReadAllText(filePath);
var fileContent = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)).ReadToEnd();
var base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
// prepping index html to load in
var html = File.ReadAllText(Settings.AssemblyDirectory + "\\index.html").Replace("\t", string.Empty, StringComparison.InvariantCulture);
var html = new StreamReader(new FileStream(Settings.AssemblyDirectory + "\\index.html", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)).ReadToEnd();
html = html.Replace("[[PT_LANG]]", vsCodeLangSet, StringComparison.InvariantCulture);
html = html.Replace("[[PT_WRAP]]", _settings.Wrap ? "1" : "0", StringComparison.InvariantCulture);
@@ -104,16 +105,24 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
html = html.Replace("[[PT_URL]]", VirtualHostName, StringComparison.InvariantCulture);
// Initialize WebView
await _webView.EnsureCoreWebView2Async(_webView2Environment);
_webView.CoreWebView2.SetVirtualHostNameToFolderMapping(VirtualHostName, Settings.AssemblyDirectory, CoreWebView2HostResourceAccessKind.Allow);
_webView.NavigateToString(html);
_webView.NavigationCompleted += WebView2Init;
_webView.Height = this.Height;
_webView.Width = this.Width;
Controls.Add(_webView);
try
{
await _webView.EnsureCoreWebView2Async(_webView2Environment).ConfigureAwait(true);
_webView.CoreWebView2.SetVirtualHostNameToFolderMapping(VirtualHostName, Settings.AssemblyDirectory, CoreWebView2HostResourceAccessKind.Allow);
_webView.NavigateToString(html);
_webView.NavigationCompleted += WebView2Init;
_webView.Height = this.Height;
_webView.Width = this.Width;
Controls.Add(_webView);
}
catch (NullReferenceException)
{
}
}
catch (WebView2RuntimeNotFoundException)
{
Controls.Remove(_loading);
// WebView2 not installed message
Label errorMessage = new Label();
errorMessage.Text = Resources.WebView2_Not_Installed_Message;
@@ -138,6 +147,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
{
InvokeOnControlThread(() =>
{
Controls.Remove(_loading);
Label text = new Label();
text.Text = Resources.Exception_Occurred;
text.Text += e.Message;
@@ -155,6 +165,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Monaco
{
InvokeOnControlThread(() =>
{
Controls.Remove(_loading);
Label errorMessage = new Label();
errorMessage.Text = Resources.Max_File_Size_Error;
errorMessage.Width = 500;

View File

@@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8">
<title>Monaco Preview Handler languages.json generator</title>
<title>Monaco Preview Handler monaco_languages.json generator</title>
<script src="monacoSRC/min/vs/loader.js"></script>
<script>
function download(filename, text) {
@@ -19,7 +19,7 @@
}
require.config({ paths: { vs: 'monacoSRC/min/vs' } });
require(['vs/editor/editor.main'], function () {
download("languages.json", "{\"list\":"+ JSON.stringify(monaco.languages.getLanguages())+"}");
download("monaco_languages.json", "{\"list\":"+ JSON.stringify(monaco.languages.getLanguages())+"}");
});
</script>

View File

@@ -14,7 +14,11 @@
var wrap = ([[PT_WRAP]] == 1) ? true : false;
var base64code = "[[PT_CODE]]";
var code = [atob(base64code)].join('\n');
// Code taken from https://stackoverflow.com/a/30106551/14774889
var code = decodeURIComponent(atob(base64code).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
</script>
<!-- Set browser to Edge-->
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
@@ -41,8 +45,8 @@
}
</style>
</head>
<!-- This oncontextmenu disables the browser context menu -->
<body oncontextmenu="return false;">
<body>
<!-- Container for the editor -->
<div id="container"></div>
<!-- Script -->