Skip to content

Commit

Permalink
Added a simple form with Monaco Editor. No way to communicate with th…
Browse files Browse the repository at this point in the history
…e editor yet.
  • Loading branch information
veler committed Dec 19, 2022
1 parent 114fc20 commit a66537a
Show file tree
Hide file tree
Showing 38 changed files with 1,466 additions and 54 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# TypeScript ignores
*.js
*.js.map
package-lock.json

# Monaco Dependency (Download from https://microsoft.github.io/monaco-editor/)
src/app/dev/DevToys.MonacoEditor/monaco-editor

# Nuke Build
.nuke/temp/

Expand Down
8 changes: 8 additions & 0 deletions init.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ Get-ChildItem $PSScriptRoot\src\ -rec |? { $_.FullName.EndsWith('.sln') } |% {
Write-Host "Restoring packages for $($SolutionPath)..."
ExecSafe { & $env:DOTNET_EXE restore -v:quiet $SolutionPath }
}

Write-Host "Done."
Write-Output "---------------------------------------"

# Restore Monaco Editor
Write-Host "Restoring Monaco Editor"
ExecSafe { & "$PSScriptRoot\tools\Restore-MonacoEditor.ps1" -RootFolder $PSScriptRoot }

Write-Host "Done."
Write-Output "---------------------------------------"
10 changes: 10 additions & 0 deletions init.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

bash --version 2>&1 | head -n 1

set -eo pipefail
SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)

Expand All @@ -14,5 +16,13 @@ do
echo "Restoring packages for $SOLUTION_FILE..."
"$DOTNET_EXE" restore -v:quiet $SOLUTION_FILE
done

echo "Done."
echo "---------------------------------------"

# Restore Monaco Editor
echo "Restoring Monaco Editor"
. "./tools/Restore-MonacoEditor.sh" $SCRIPT_DIR

echo "Done."
echo "---------------------------------------"
19 changes: 19 additions & 0 deletions src/DevToys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevToys.CLI", "app\dev\plat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DevToys.UnitTests", "app\tests\DevToys.UnitTests\DevToys.UnitTests.csproj", "{FBD1A1FE-C8F7-4A1F-8B89-C81D3780DADC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DevToys.MonacoEditor", "app\dev\DevToys.MonacoEditor\DevToys.MonacoEditor.csproj", "{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -201,6 +203,22 @@ Global
{FBD1A1FE-C8F7-4A1F-8B89-C81D3780DADC}.Release|x64.Build.0 = Release|Any CPU
{FBD1A1FE-C8F7-4A1F-8B89-C81D3780DADC}.Release|x86.ActiveCfg = Release|Any CPU
{FBD1A1FE-C8F7-4A1F-8B89-C81D3780DADC}.Release|x86.Build.0 = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|arm64.ActiveCfg = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|arm64.Build.0 = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|x64.ActiveCfg = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|x64.Build.0 = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|x86.ActiveCfg = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Debug|x86.Build.0 = Debug|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|Any CPU.Build.0 = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|arm64.ActiveCfg = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|arm64.Build.0 = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|x64.ActiveCfg = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|x64.Build.0 = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|x86.ActiveCfg = Release|Any CPU
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -219,6 +237,7 @@ Global
{6279C845-92F8-4333-AB99-3D213163593C} = {B20E4027-0777-4A75-A848-609594E9A6B0}
{A1CB6EEE-3CD2-416C-85E0-2C612FE2DF49} = {3D2285D5-0E44-4B1A-9B74-2BBE3108D5B7}
{FBD1A1FE-C8F7-4A1F-8B89-C81D3780DADC} = {4C40BF47-315A-4B60-9ED6-8D65B5E79C2C}
{E4354FEC-B0BB-49AC-A9D2-ABBE7F74DCC5} = {B20E4027-0777-4A75-A848-609594E9A6B0}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C2C953F5-F97F-4198-B15C-4947A886050F}
Expand Down
9 changes: 8 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@
<PropertyGroup>
<CommunityToolkitVersion>8.0.0</CommunityToolkitVersion>
<DotNetVersion>6.0.0</DotNetVersion>
<UnoVersion>4.6.19</UnoVersion>
<UnoVersion>4.7.0-dev.666</UnoVersion>
<UnoWasmBootstrapVersion>8.0.0-dev.65</UnoWasmBootstrapVersion>
<WindowsNetSdkVersion>10.0.22000.28</WindowsNetSdkVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Common" Version="$(CommunityToolkitVersion)" />
<PackageVersion Include="CommunityToolkit.Diagnostics" Version="$(CommunityToolkitVersion)" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="$(CommunityToolkitVersion)" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(DotNetVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="$(DotNetVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="$(DotNetVersion)" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageVersion Include="Microsoft.Windows.Compatibility" Version="$(DotNetVersion)" />
<PackageVersion Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22000.196" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.1.3" />
<PackageVersion Include="Moq" Version="4.18.2" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
<PackageVersion Include="Nito.AsyncEx" Version="5.1.2"/>
<PackageVersion Include="Nuke.Common" Version="6.2.1" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
<PackageVersion Include="System.ComponentModel.Composition" Version="$(DotNetVersion)" />
<PackageVersion Include="Uno.Core.Extensions.Logging" Version="4.0.1" />
<PackageVersion Include="Uno.Core.Extensions.Logging.Singleton" Version="4.0.1" />
<PackageVersion Include="Uno.Extensions.Logging.WebAssembly.Console" Version="1.4.0" />
<PackageVersion Include="Uno.UI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.UI.Adapter.Microsoft.Extensions.Logging" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.Wasm.Bootstrap" Version="$(UnoWasmBootstrapVersion)" />
<PackageVersion Include="Uno.Wasm.Bootstrap.DevServer" Version="$(UnoWasmBootstrapVersion)" />
<PackageVersion Include="Uno.WinUI" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Lottie" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.RemoteControl" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.Runtime.WebAssembly" Version="$(UnoVersion)" />
<PackageVersion Include="Uno.WinUI.WebAssembly" Version="$(UnoVersion)" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
Expand Down
113 changes: 113 additions & 0 deletions src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.Wasm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
(function()
{
//Debug.log("Create dynamic style element");
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.id='dynamic';
head.appendChild(style);

//Debug.log("Starting Monaco Load");

var editor;
var model;
var contexts = {};
window.contexts = {};
var decorations = [];
window.decorations = [];
var modifingSelection = false; // Supress updates to selection when making edits.
window.modifyingSelection = false;

require.config({ paths: { 'vs': '{UNO_BOOTSTRAP_WEBAPP_BASE_PATH}{UNO_BOOTSTRAP_APP_BASE}/devtoys.monacoeditor/monaco-editor/min/vs' } });
require(
['vs/editor/editor.main'],
function ()
{
//Debug.log("Grabbing Monaco Options");

//var opt = {};
//try
//{
// opt = getOptions();
//}
//catch(err)
//{
// //Debug.log("Unable to read options - " + err);
//}

//Debug.log("Getting Parent Text value");
//opt["value"] = getParentValue("Text");

//Debug.log("Getting Host container");
//Debug.log("Creating Editor");
const editor = monaco.editor.create(element, null /* opt */);
window.editor = editor;

//Debug.log("Getting Editor model");
//model = editor.getModel();
//window.model = model;

//// Listen for Content Changes
////Debug.log("Listening for changes in the editor model - " + (!model));
//model.onDidChangeContent(
// (event) =>
// {
// //Parent.setValue("Text", stringifyForMarshalling(model.getValue()));
// //console.log("buffers: " + JSON.stringify(model._buffer._pieceTree._buffers));
// //console.log("commandMgr: " + JSON.stringify(model._commandManager));
// //console.log("viewState:" + JSON.stringify(editor.saveViewState()));
// });

//// Listen for Selection Changes
////Debug.log("Listening for changes in the editor selection");
//editor.onDidChangeCursorSelection(
// (event) =>
// {
// if (!modifingSelection)
// {
// console.log(event.source);
// //Parent.setValue("SelectedText", stringifyForMarshalling(model.getValueInRange(event.selection)));
// // Parent.setValueWithType("SelectedRange", stringifyForMarshalling(JSON.stringify(event.selection)), "Selection");
// }
// });

//// Set theme
////Debug.log("Getting parent theme value");
//let theme = getParentJsonValue("RequestedTheme");
//theme
// = {
// "0": "Default",
// "1": "Light",
// "2": "Dark"
// }
// [theme];

////Debug.log("Current theme value - " + theme);
//if (theme == "Default")
//{
// Debug.log("Loading default theme");
// theme = getThemeCurrentThemeName();
//}

////Debug.log("Changing theme");
//changeTheme(theme, getThemeIsHighContrast());

// Update Monaco Size when we receive a window resize event
//Debug.log("Listen for resize events on the window and resize the editor");
window.addEventListener(
"resize",
() =>
{
editor.layout();
});

// Disable WebView Scrollbar so Monaco Scrollbar can do heavy lifting
document.body.style.overflow = 'hidden';

// Callback to Parent that we're loaded
//Debug.log("Loaded Monaco");
//Parent.callAction("Loaded");

//Debug.log("Ending Monaco Load");
}
);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style>
html, body {
height: 100%;
margin: 0;
}
/* Just have Monaco Fill All Available Space within control */
#container {
height: 100%;
width: 100%;
}
</style>
<style id="dynamic">
</style>
</head>
<body>

<div id="container" onkeydown="keyDown(event)"></div>

<!--<script src="https://devtoys.local/ts-helpermethods/ts-helpermethods-Windows/asyncCallbackHelpers.js"></script>-->
<script src="https://devtoys.local/monaco-editor/min/vs/loader.js"></script>
<!--<script src="https://devtoys.local/ts-helpermethods/registerCodeLensProvider.js"></script>
<script src="https://devtoys.local/ts-helpermethods/registerColorProvider.js"></script>
<script src="https://devtoys.local/ts-helpermethods/registerCompletionItemProvider.js"></script>
<script src="https://devtoys.local/ts-helpermethods/otherScriptsToBeOrganized.js"></script>
<script src="https://devtoys.local/ts-helpermethods/updateSelectedContent.js"></script>-->

<script>
var editor;
var model;
var contexts = {};
var decorations = [];
var modifingSelection = false; // Supress updates to selection when making edits.

//Debug.log("Starting Monaco Load");
require.config({ paths: { 'vs': 'https://devtoys.local/monaco-editor/min/vs' } });
require(['vs/editor/editor.main'], function () {
//Debug.log("Grabbing Monaco Options");

//let opt = getOptions();
//opt["value"] = Parent.getValue("Text");

editor = monaco.editor.create(document.getElementById('container'), null /* opt */);
model = editor.getModel();

//// Listen for Content Changes
//model.onDidChangeContent((event) => {
// Parent.setValue("Text", model.getValue());
// //console.log("buffers: " + JSON.stringify(model._buffer._pieceTree._buffers));
// //console.log("commandMgr: " + JSON.stringify(model._commandManager));
// //console.log("viewState:" + JSON.stringify(editor.saveViewState()));
//});

//// Listen for Selection Changes
//editor.onDidChangeCursorSelection((event) => {
// if (!modifingSelection) {
// console.log(event.source);
// Parent.setValue("SelectedText", model.getValueInRange(event.selection));
// Parent.setValue("SelectedRange", JSON.stringify(event.selection), "Selection");
// }
//})

//// Set theme
//let theme = Parent.getJsonValue("RequestedTheme");
//theme = {
// "0": "Default",
// "1": "Light",
// "2": "Dark"
//}[theme];
//if (theme == "Default") {
// theme = Theme.currentThemeName.toString();
//}
//changeTheme(theme, Theme.isHighContrast.toString());

//// Update Monaco Size when we receive a window resize event
//window.addEventListener("resize", () => {
// editor.layout();
//});

// Disable WebView Scrollbar so Monaco Scrollbar can do heavy lifting
document.body.style.overflow = 'hidden';

//// Callback to Parent that we're loaded
//Debug.log("Loaded Monaco");
//Parent.callAction("Loaded");
});
</script>
</body>
</html>
46 changes: 46 additions & 0 deletions src/app/dev/DevToys.MonacoEditor/CodeEditor/CodeEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace DevToys.MonacoEditor;

/// <summary>
/// .Net wrapper for the Monaco CodeEditor
/// https://microsoft.github.io/monaco-editor/
/// </summary>
[TemplatePart(Name = "View", Type = typeof(ICodeEditorPresenter))]
public sealed partial class CodeEditor : Control
{
private ICodeEditorPresenter _view;

public CodeEditor()
{
DefaultStyleKey = typeof(CodeEditor);
}

protected override void OnApplyTemplate()
{
if (_view != null)
{
//_view.NavigationStarting -= WebView_NavigationStarting;
//_view.DOMContentLoaded -= WebView_DOMContentLoaded;
//_view.NavigationCompleted -= WebView_NavigationCompleted;
//_view.NewWindowRequested -= WebView_NewWindowRequested;
//Debug.WriteLine("Setting initialized - false");
//_initialized = false;
}

_view = (ICodeEditorPresenter)GetTemplateChild("View");

if (_view != null)
{
//_view.NavigationStarting += WebView_NavigationStarting;
//_view.DOMContentLoaded += WebView_DOMContentLoaded;
//_view.NavigationCompleted += WebView_NavigationCompleted;
//_view.NewWindowRequested += WebView_NewWindowRequested;

_view.LaunchAsync();

base.OnApplyTemplate();
}
}
}
Loading

0 comments on commit a66537a

Please sign in to comment.