Add the Monaco Editor to WelsonJS Launcher #137
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run

This commit is contained in:
Namhyeon Go 2025-03-08 22:52:45 +09:00
parent 24ba77d1f6
commit e9c6a493eb
6 changed files with 166 additions and 1 deletions

View File

@ -41,6 +41,7 @@
this.instancesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.instancesToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.runAsAdministratorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.runAsAdministratorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.globalSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.globalSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.startTheCodeEditorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout(); this.menuStrip1.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -133,7 +134,8 @@
this.userdefinedVariablesToolStripMenuItem, this.userdefinedVariablesToolStripMenuItem,
this.instancesToolStripMenuItem, this.instancesToolStripMenuItem,
this.runAsAdministratorToolStripMenuItem, this.runAsAdministratorToolStripMenuItem,
this.globalSettingsToolStripMenuItem}); this.globalSettingsToolStripMenuItem,
this.startTheCodeEditorToolStripMenuItem});
this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem"; this.settingsToolStripMenuItem.Name = "settingsToolStripMenuItem";
this.settingsToolStripMenuItem.Size = new System.Drawing.Size(62, 20); this.settingsToolStripMenuItem.Size = new System.Drawing.Size(62, 20);
this.settingsToolStripMenuItem.Text = "Settings"; this.settingsToolStripMenuItem.Text = "Settings";
@ -166,6 +168,13 @@
this.globalSettingsToolStripMenuItem.Text = "Global settings..."; this.globalSettingsToolStripMenuItem.Text = "Global settings...";
this.globalSettingsToolStripMenuItem.Click += new System.EventHandler(this.globalSettingsToolStripMenuItem_Click); this.globalSettingsToolStripMenuItem.Click += new System.EventHandler(this.globalSettingsToolStripMenuItem_Click);
// //
// startTheCodeEditorToolStripMenuItem
//
this.startTheCodeEditorToolStripMenuItem.Name = "startTheCodeEditorToolStripMenuItem";
this.startTheCodeEditorToolStripMenuItem.Size = new System.Drawing.Size(196, 22);
this.startTheCodeEditorToolStripMenuItem.Text = "Start the code editor...";
this.startTheCodeEditorToolStripMenuItem.Click += new System.EventHandler(this.startTheCodeEditorToolStripMenuItem_Click);
//
// MainForm // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
@ -207,6 +216,7 @@
private System.Windows.Forms.ToolStripMenuItem instancesToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem instancesToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem runAsAdministratorToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem runAsAdministratorToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem globalSettingsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem globalSettingsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem startTheCodeEditorToolStripMenuItem;
} }
} }

View File

@ -210,5 +210,24 @@ namespace WelsonJS.Launcher
{ {
(new GlobalSettingsForm()).Show(); (new GlobalSettingsForm()).Show();
} }
private void startTheCodeEditorToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Program.resourceServer == null)
{
Program.resourceServer = new ResourceServer("http://localhost:3000/", "editor.html");
}
if (!Program.resourceServer.IsRunning())
{
Program.resourceServer.Start();
((ToolStripMenuItem)sender).Text = "Stop the code editor...";
}
else
{
Program.resourceServer.Stop();
((ToolStripMenuItem)sender).Text = "Start the code editor...";
}
}
} }
} }

View File

@ -8,6 +8,8 @@ namespace WelsonJS.Launcher
{ {
internal static class Program internal static class Program
{ {
public static ResourceServer resourceServer;
[STAThread] [STAThread]
static void Main() static void Main()
{ {

View File

@ -0,0 +1,104 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WelsonJS.Launcher
{
public class ResourceServer
{
private readonly HttpListener _listener;
private CancellationTokenSource _cts;
private Task _serverTask;
private bool _isRunning;
private string _prefix;
private string _resourceName;
public ResourceServer(string prefix, string resourceName)
{
_prefix = prefix;
_listener = new HttpListener();
_listener.Prefixes.Add(prefix);
_resourceName = typeof(ResourceServer).Namespace + "." + resourceName;
}
public void Start()
{
if (_isRunning) return;
_isRunning = true;
_cts = new CancellationTokenSource();
_listener.Start();
// Open the web browser
Process.Start(_prefix);
// Run a task with cancellation token
_serverTask = Task.Run(() => ListenLoop(_cts.Token));
}
public void Stop()
{
_isRunning = false;
_cts.Cancel();
_listener.Stop();
MessageBox.Show("Server stopped.");
}
public bool IsRunning()
{
return _isRunning;
}
private async Task ListenLoop(CancellationToken token)
{
while (!token.IsCancellationRequested && _isRunning)
{
try
{
ProcessRequest(await _listener.GetContextAsync());
}
catch (Exception ex)
{
if (token.IsCancellationRequested || !_isRunning) break;
MessageBox.Show($"Error: {ex.Message}");
}
}
}
private void ProcessRequest(HttpListenerContext context)
{
string responseString = GetEmbeddedResource();
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
context.Response.ContentType = "text/html";
context.Response.ContentLength64 = buffer.Length;
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.OutputStream.Close();
}
private string GetEmbeddedResource()
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(_resourceName))
{
if (stream == null)
{
return "<html><body><h1>Could not find the resource.</h1></body></html>";
}
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
}
}
}

View File

@ -96,6 +96,7 @@
<Compile Include="GlobalSettingsForm.Designer.cs"> <Compile Include="GlobalSettingsForm.Designer.cs">
<DependentUpon>GlobalSettingsForm.cs</DependentUpon> <DependentUpon>GlobalSettingsForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="ResourceServer.cs" />
<EmbeddedResource Include="EnvForm.resx"> <EmbeddedResource Include="EnvForm.resx">
<DependentUpon>EnvForm.cs</DependentUpon> <DependentUpon>EnvForm.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
@ -151,5 +152,8 @@
<ItemGroup> <ItemGroup>
<None Include="Resources\icon_start_32.png" /> <None Include="Resources\icon_start_32.png" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<EmbeddedResource Include="editor.html" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet"
data-name="vs/editor/editor.main"
href="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.52.2/min/vs/editor/editor.main.css" />
</head>
<body>
<h2>Monaco Editor Sync Loading Sample</h2>
<div id="container" style="width: 800px; height: 600px; border: 1px solid grey"></div>
<script>
var require = { paths: { vs: '../node_modules/monaco-editor/min/vs' } };
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.52.2/min/vs/loader.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.52.2/min/vs/editor/editor.main.js"></script>
<script>
var editor = monaco.editor.create(document.getElementById('container'), {
value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
language: 'javascript'
});
</script>
</body>
</html>