code completion works
This commit is contained in:
@@ -2,14 +2,18 @@
|
|||||||
@using BlazorMonaco
|
@using BlazorMonaco
|
||||||
@using BlazorMonaco.Editor
|
@using BlazorMonaco.Editor
|
||||||
@using BlazorMonaco.Languages
|
@using BlazorMonaco.Languages
|
||||||
|
@using CommunityToolkit.Maui.Storage
|
||||||
@using Inspectron.PrintServer.TemplateEditor.Services
|
@using Inspectron.PrintServer.TemplateEditor.Services
|
||||||
@inject IJSRuntime JSRuntime
|
@inject IJSRuntime JSRuntime
|
||||||
|
|
||||||
<div class="editor-container">
|
<div class="editor-container">
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<button class="toolbar-btn" title="New">New</button>
|
<button class="toolbar-btn" title="New" @onclick="NewFileAsync">New</button>
|
||||||
<button class="toolbar-btn" title="Open">Open</button>
|
<button class="toolbar-btn" title="Open" @onclick="OpenFileAsync">Open</button>
|
||||||
<button class="toolbar-btn" title="Save">Save</button>
|
<button class="toolbar-btn" title="Save" @onclick="SaveFileAsync">Save</button>
|
||||||
|
<span class="file-indicator" title="@(_currentFilePath ?? "Untitled")">
|
||||||
|
@FileDisplayName@(HasUnsavedChanges ? " *" : "")
|
||||||
|
</span>
|
||||||
<div class="toolbar-separator"></div>
|
<div class="toolbar-separator"></div>
|
||||||
<button class="toolbar-btn" title="Load JSON data file for expression completions" @onclick="LoadJsonAsync">Load JSON</button>
|
<button class="toolbar-btn" title="Load JSON data file for expression completions" @onclick="LoadJsonAsync">Load JSON</button>
|
||||||
@if (!string.IsNullOrEmpty(_loadedJsonFile))
|
@if (!string.IsNullOrEmpty(_loadedJsonFile))
|
||||||
@@ -41,6 +45,15 @@
|
|||||||
private string? _loadedJsonFile;
|
private string? _loadedJsonFile;
|
||||||
private readonly TemplateCompletionProvider _completionProvider = new();
|
private readonly TemplateCompletionProvider _completionProvider = new();
|
||||||
|
|
||||||
|
private string? _currentFilePath;
|
||||||
|
private string _savedContent = "";
|
||||||
|
|
||||||
|
private bool HasUnsavedChanges => _editorContent != _savedContent;
|
||||||
|
|
||||||
|
private string FileDisplayName => _currentFilePath is not null
|
||||||
|
? Path.GetFileName(_currentFilePath)
|
||||||
|
: "Untitled";
|
||||||
|
|
||||||
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
|
private StandaloneEditorConstructionOptions EditorConstructionOptions(StandaloneCodeEditor editor)
|
||||||
{
|
{
|
||||||
return new StandaloneEditorConstructionOptions
|
return new StandaloneEditorConstructionOptions
|
||||||
@@ -76,6 +89,77 @@
|
|||||||
_editorContent = await _editor.GetValue();
|
_editorContent = await _editor.GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task NewFileAsync()
|
||||||
|
{
|
||||||
|
_currentFilePath = null;
|
||||||
|
_savedContent = "";
|
||||||
|
_editorContent = "";
|
||||||
|
await _editor.SetValue("");
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task OpenFileAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await FilePicker.Default.PickAsync(new PickOptions
|
||||||
|
{
|
||||||
|
PickerTitle = "Select an XML template file",
|
||||||
|
FileTypes = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
|
||||||
|
{
|
||||||
|
{ DevicePlatform.WinUI, new[] { ".xml" } },
|
||||||
|
{ DevicePlatform.macOS, new[] { "xml" } },
|
||||||
|
{ DevicePlatform.iOS, new[] { "public.xml" } },
|
||||||
|
{ DevicePlatform.Android, new[] { "text/xml" } },
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result is null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var content = await File.ReadAllTextAsync(result.FullPath);
|
||||||
|
_currentFilePath = result.FullPath;
|
||||||
|
_savedContent = content;
|
||||||
|
_editorContent = content;
|
||||||
|
await _editor.SetValue(content);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// User cancelled or file read failed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SaveFileAsync()
|
||||||
|
{
|
||||||
|
if (_currentFilePath is not null)
|
||||||
|
{
|
||||||
|
await File.WriteAllTextAsync(_currentFilePath, _editorContent);
|
||||||
|
_savedContent = _editorContent;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
await SaveAsAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SaveAsAsync()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(_editorContent));
|
||||||
|
var result = await FileSaver.Default.SaveAsync("template.xml", stream, CancellationToken.None);
|
||||||
|
|
||||||
|
if (result.IsSuccessful)
|
||||||
|
{
|
||||||
|
_currentFilePath = result.FilePath;
|
||||||
|
_savedContent = _editorContent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// User cancelled or save failed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task LoadJsonAsync()
|
private async Task LoadJsonAsync()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -55,7 +55,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BlazorMonaco" Version="3.4.0" />
|
<PackageReference Include="BlazorMonaco" Version="3.4.0" />
|
||||||
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
|
<PackageReference Include="CommunityToolkit.Maui" Version="13.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Maui.Controls" Version="10.0.10" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="$(MauiVersion)" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="10.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using CommunityToolkit.Maui;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace Inspectron.PrintServer.TemplateEditor
|
namespace Inspectron.PrintServer.TemplateEditor
|
||||||
{
|
{
|
||||||
@@ -9,6 +10,7 @@ namespace Inspectron.PrintServer.TemplateEditor
|
|||||||
var builder = MauiApp.CreateBuilder();
|
var builder = MauiApp.CreateBuilder();
|
||||||
builder
|
builder
|
||||||
.UseMauiApp<App>()
|
.UseMauiApp<App>()
|
||||||
|
.UseMauiCommunityToolkit()
|
||||||
.ConfigureFonts(fonts =>
|
.ConfigureFonts(fonts =>
|
||||||
{
|
{
|
||||||
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
|
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
|
||||||
|
|||||||
@@ -44,6 +44,18 @@ html, body {
|
|||||||
margin: 2px 6px;
|
margin: 2px 6px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.file-indicator {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
padding: 2px 10px;
|
||||||
|
color: #999;
|
||||||
|
font-size: 12px;
|
||||||
|
max-width: 200px;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
.json-indicator {
|
.json-indicator {
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
Reference in New Issue
Block a user