forked from hawkerm/monaco-editor-uwp
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCodeEditorPresenter.wasm.cs
More file actions
129 lines (105 loc) · 4.79 KB
/
CodeEditorPresenter.wasm.cs
File metadata and controls
129 lines (105 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Diagnostics;
using System.Runtime.InteropServices.JavaScript;
using Microsoft.UI.Xaml.Controls;
using Monaco.Helpers;
using Uno.Extensions;
using Uno.Logging;
using Uno.UI.NativeElementHosting;
using Windows.Foundation;
using Windows.UI.Core;
namespace Monaco
{
public partial class CodeEditorPresenter : ContentControl, ICodeEditorPresenter
{
private static readonly string UNO_BOOTSTRAP_APP_BASE = global::System.Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_APP_BASE)) ?? "";
private static readonly string UNO_BOOTSTRAP_WEBAPP_BASE_PATH = Environment.GetEnvironmentVariable(nameof(UNO_BOOTSTRAP_WEBAPP_BASE_PATH)) ?? "";
private readonly BrowserHtmlElement _element;
public CodeEditorPresenter()
{
Debug.WriteLine("CodeEditorPresenter()");
Content = _element = BrowserHtmlElement.CreateHtmlElement("monaco-" + this.GetHashCode(), "div");
}
public string ElementId => _element.ElementId;
/// <inheritdoc />
public event TypedEventHandler<ICodeEditorPresenter?, WebViewNewWindowRequestedEventArgs?>? NewWindowRequested; // ignored for now (external navigation)
/// <inheritdoc />
public event TypedEventHandler<ICodeEditorPresenter?, WebViewNavigationStartingEventArgs?>? NavigationStarting;
/// <inheritdoc />
public event TypedEventHandler<ICodeEditorPresenter?, WebViewNavigationCompletedEventArgs?>? NavigationCompleted; // ignored for now (only focus the editor)
public CodeEditor? ParentCodeEditor { get; set; }
public bool IsSettingValue
{
get => ParentCodeEditor?.IsSettingValue ?? false;
set
{
ParentCodeEditor?.IsSettingValue = value;
}
}
public bool TriggerKeyDown(WebKeyEventArgs args)
=> ParentCodeEditor?.TriggerKeyDown(args) ?? false;
/// <inheritdoc />
public global::System.Uri Source
{
get => new(NativeMethods.GetSrc(_element.ElementId));
set
{
//var path = Environment.GetEnvironmentVariable("UNO_BOOTSTRAP_APP_BASE");
//var target = $"/{path}/MonacoCodeEditor.html";
//var target = (value.IsAbsoluteUri && value.IsFile)
// ? value.PathAndQuery
// : value.ToString();
string target;
if (value.IsAbsoluteUri)
{
if (value.Scheme == "file")
{
// Local files are assumed as coming from the remoter server
target = UNO_BOOTSTRAP_APP_BASE == null ? value.PathAndQuery : UNO_BOOTSTRAP_WEBAPP_BASE_PATH + UNO_BOOTSTRAP_APP_BASE + value.PathAndQuery;
}
else
{
target = value.AbsoluteUri;
}
}
else
{
target = UNO_BOOTSTRAP_APP_BASE == null
? value.OriginalString
: UNO_BOOTSTRAP_WEBAPP_BASE_PATH + UNO_BOOTSTRAP_APP_BASE + "/" + value.OriginalString;
}
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
{
this.Log().Debug($"Loading {target} (Nav is null {NavigationStarting == null})");
}
NativeMethods.SetSrc(_element.ElementId, target);
//NavigationStarting?.Invoke(this, new WebViewNavigationStartingEventArgs());
_ = Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => NavigationStarting?.Invoke(this, null));
}
}
public async Task Launch()
{
try
{
if (ParentCodeEditor is null)
{
throw new InvalidOperationException($"The ParentCodeEditor property must be set");
}
Debug.WriteLine($"InitializeMonaco({this.GetHashCode():X8})");
await NativeMethods.InitializeMonaco(this, _element.ElementId, $"{UNO_BOOTSTRAP_WEBAPP_BASE_PATH}{UNO_BOOTSTRAP_APP_BASE}");
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
static partial class NativeMethods
{
[JSImport("globalThis.getSrc")]
public static partial string GetSrc(string elementId);
[JSImport("globalThis.setSrc")]
public static partial void SetSrc(string elementId, string src);
[JSImport("globalThis.createMonacoEditor")]
public static partial Task InitializeMonaco([JSMarshalAs<JSType.Any>] object managedOwner, string elementId, string baseUri);
}
}
}