mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 12:18:50 +02:00
[Peek]Add wrap and formatting options for Monaco previewer (#29378)
* add options for monaco previewer * fix formatting
This commit is contained in:
committed by
GitHub
parent
9693fd7035
commit
5a06bcb473
@@ -3,7 +3,10 @@
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Peek.FilePreviewer.Previewers.Archives;
|
||||
using Peek.UI.Telemetry.Events;
|
||||
|
||||
@@ -11,6 +14,13 @@ namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
public class PreviewerFactory
|
||||
{
|
||||
private readonly IPreviewSettings _previewSettings;
|
||||
|
||||
public PreviewerFactory()
|
||||
{
|
||||
_previewSettings = Application.Current.GetService<IPreviewSettings>();
|
||||
}
|
||||
|
||||
public IPreviewer Create(IFileSystemItem file)
|
||||
{
|
||||
if (ImagePreviewer.IsFileTypeSupported(file.Extension))
|
||||
@@ -23,7 +33,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
}
|
||||
else if (WebBrowserPreviewer.IsFileTypeSupported(file.Extension))
|
||||
{
|
||||
return new WebBrowserPreviewer(file);
|
||||
return new WebBrowserPreviewer(file, _previewSettings);
|
||||
}
|
||||
else if (ArchivePreviewer.IsFileTypeSupported(file.Extension))
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Common.UI;
|
||||
using ManagedCommon;
|
||||
@@ -23,11 +24,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
JsonDocument languageListDocument = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguages();
|
||||
JsonElement languageList = languageListDocument.RootElement.GetProperty("list");
|
||||
foreach (JsonElement e in languageList.EnumerateArray())
|
||||
{
|
||||
if (e.TryGetProperty("extensions", out var extensions))
|
||||
{
|
||||
for (int j = 0; j < extensions.GetArrayLength(); j++)
|
||||
if (e.TryGetProperty("extensions", out var extensions))
|
||||
{
|
||||
for (int j = 0; j < extensions.GetArrayLength(); j++)
|
||||
{
|
||||
set.Add(extensions[j].ToString());
|
||||
}
|
||||
}
|
||||
@@ -44,15 +45,32 @@ namespace Peek.FilePreviewer.Previewers
|
||||
/// <summary>
|
||||
/// Prepares temp html for the previewing
|
||||
/// </summary>
|
||||
public static string PreviewTempFile(string fileText, string extension, string tempFolder)
|
||||
public static string PreviewTempFile(string fileText, string extension, string tempFolder, bool tryFormat, bool wrapText)
|
||||
{
|
||||
// TODO: check if file is too big, add MaxFileSize to settings
|
||||
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder);
|
||||
return InitializeIndexFileAndSelectedFile(fileText, extension, tempFolder, tryFormat, wrapText);
|
||||
}
|
||||
|
||||
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder)
|
||||
private static string InitializeIndexFileAndSelectedFile(string fileContent, string extension, string tempFolder, bool tryFormat, bool wrapText)
|
||||
{
|
||||
string vsCodeLangSet = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.GetLanguage(extension);
|
||||
|
||||
if (tryFormat)
|
||||
{
|
||||
var formatter = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.Formatters.SingleOrDefault(f => f.LangSet == vsCodeLangSet);
|
||||
if (formatter != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
fileContent = formatter.Format(fileContent);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError($"Failed to apply formatting", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
|
||||
string theme = ThemeManager.GetWindowsBaseColor().ToLowerInvariant();
|
||||
|
||||
@@ -60,7 +78,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
string html = Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.ReadIndexHtml();
|
||||
|
||||
html = html.Replace("[[PT_LANG]]", vsCodeLangSet, StringComparison.InvariantCulture);
|
||||
html = html.Replace("[[PT_WRAP]]", "1", StringComparison.InvariantCulture); // TODO: add to settings
|
||||
html = html.Replace("[[PT_WRAP]]", wrapText ? "1" : "0", StringComparison.InvariantCulture);
|
||||
html = html.Replace("[[PT_THEME]]", theme, StringComparison.InvariantCulture);
|
||||
html = html.Replace("[[PT_CODE]]", base64FileCode, StringComparison.InvariantCulture);
|
||||
html = html.Replace("[[PT_URL]]", Microsoft.PowerToys.FilePreviewCommon.MonacoHelper.VirtualHostName, StringComparison.InvariantCulture);
|
||||
|
||||
@@ -13,13 +13,14 @@ using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers
|
||||
{
|
||||
public partial class WebBrowserPreviewer : ObservableObject, IBrowserPreviewer, IDisposable
|
||||
{
|
||||
private static readonly HashSet<string> _supportedFileTypes = new HashSet<string>
|
||||
private readonly IPreviewSettings _previewSettings;
|
||||
|
||||
private static readonly HashSet<string> _supportedFileTypes = new()
|
||||
{
|
||||
// Web
|
||||
".html",
|
||||
@@ -43,8 +44,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private bool disposed;
|
||||
|
||||
public WebBrowserPreviewer(IFileSystemItem file)
|
||||
public WebBrowserPreviewer(IFileSystemItem file, IPreviewSettings previewSettings)
|
||||
{
|
||||
_previewSettings = previewSettings;
|
||||
File = file;
|
||||
Dispatcher = DispatcherQueue.GetForCurrentThread();
|
||||
}
|
||||
@@ -109,7 +111,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
if (IsDevFilePreview && !isHtml && !isMarkdown)
|
||||
{
|
||||
var raw = await ReadHelper.Read(File.Path.ToString());
|
||||
Preview = new Uri(MonacoHelper.PreviewTempFile(raw, File.Extension, TempFolderPath.Path));
|
||||
Preview = new Uri(MonacoHelper.PreviewTempFile(raw, File.Extension, TempFolderPath.Path, _previewSettings.SourceCodeTryFormat, _previewSettings.SourceCodeWrapText));
|
||||
}
|
||||
else if (isMarkdown)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user