diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c4c3ad2..f674d9eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## 0.3.6 - 2024-02-08 + +### 🐛 Fix a bug + +- Try to fix random crash at startup + +### 💥 Introduce breaking changes + +- Move Id from parameter to Setup, as it should never be changed + +### 🔊 Add or update logs + +- Add DebugLogs setup parameter to print debug logs + ## 0.3.5 - 2024-02-07 ### ✨ Introduce new features diff --git a/CodeMirror6/CodeMirror6.csproj b/CodeMirror6/CodeMirror6.csproj index 1d9a99dd..8d81be76 100644 --- a/CodeMirror6/CodeMirror6.csproj +++ b/CodeMirror6/CodeMirror6.csproj @@ -9,7 +9,7 @@ GaelJ.BlazorCodeMirror6 true GaelJ.BlazorCodeMirror6 - 0.3.5 + 0.3.6 true snupkg true diff --git a/CodeMirror6/CodeMirror6Wrapper.razor b/CodeMirror6/CodeMirror6Wrapper.razor index 4dc2a4bb..d148e9f5 100644 --- a/CodeMirror6/CodeMirror6Wrapper.razor +++ b/CodeMirror6/CodeMirror6Wrapper.razor @@ -13,7 +13,6 @@ Editable="@Editable" FocusChanged="@FocusChanged" GetMentionCompletions="@GetMentionCompletions" - Id="@Id" IndentationUnit="@IndentationUnit" IsWASM="@IsWASM" Language="@Language" diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor b/CodeMirror6/CodeMirror6WrapperInternal.razor index 270078f0..84bc69d9 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor @@ -16,7 +16,7 @@
diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInterop.cs b/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInterop.cs index 534bf97b..de1e3d2b 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInterop.cs +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInterop.cs @@ -39,7 +39,7 @@ internal async Task ModuleInvokeVoidAsync(string method, params object?[] args) try { var module = await _moduleTask.Value; if (module is null) return; - args = args.Prepend(cm6WrapperComponent.Id).ToArray(); + args = args.Prepend(cm6WrapperComponent.Setup.Id).ToArray(); await module.InvokeVoidAsync(method, args); } catch (Exception ex) @@ -61,7 +61,7 @@ internal async Task ModuleInvokeVoidAsync(string method, params object?[] args) try { var module = await _moduleTask.Value; if (module is null) return default; - args = args.Prepend(cm6WrapperComponent.Id).ToArray(); + args = args.Prepend(cm6WrapperComponent.Setup.Id).ToArray(); return await module.InvokeAsync(method, args); } catch (Exception ex) diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInvokables.cs b/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInvokables.cs index 1ab3eb92..2e888ae2 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInvokables.cs +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor.JsInvokables.cs @@ -1,5 +1,6 @@ using GaelJ.BlazorCodeMirror6.Models; using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.Logging; using Microsoft.JSInterop; namespace GaelJ.BlazorCodeMirror6; @@ -22,6 +23,7 @@ public partial class CodeMirror6WrapperInternal : ComponentBase, IAsyncDisposabl /// [JSInvokable] public async Task DocChangedFromJS(string value) { + if (Setup.DebugLogs) Logger.LogInformation("DocChangedFromJS: {value}", value); if (Doc?.Replace("\r", "") == value?.Replace("\r", "")) return; Doc = value?.Replace("\r", "") ?? ""; Config.Doc = Doc; @@ -35,6 +37,7 @@ [JSInvokable] public async Task DocChangedFromJS(string value) /// [JSInvokable] public async Task FocusChangedFromJS(bool value) { + if (Setup.DebugLogs) Logger.LogInformation("FocusChangedFromJS: {value}", value); if (State.HasFocus == value) return; State.HasFocus = value; await FocusChanged.InvokeAsync(State.HasFocus); @@ -47,6 +50,7 @@ [JSInvokable] public async Task FocusChangedFromJS(bool value) /// [JSInvokable] public async Task SelectionSetFromJS(IEnumerable? values) { + if (Setup.DebugLogs) Logger.LogInformation("SelectionChangedFromJS: @{values}", values); Selection = values?.ToList(); await SelectionChanged.InvokeAsync(Selection); } @@ -58,6 +62,7 @@ [JSInvokable] public async Task SelectionSetFromJS(IEnumerable? /// [JSInvokable] public async Task MarkdownStyleChangedFromJS(IEnumerable? values) { + if (Setup.DebugLogs) Logger.LogInformation("MarkdownStyleChangedFromJS: @{values}", values); State.MarkdownStylesAtSelections = new(values?.ToList() ?? []); await MarkdownStylesAtSelectionsChanged.InvokeAsync(State.MarkdownStylesAtSelections); } @@ -69,6 +74,7 @@ [JSInvokable] public async Task MarkdownStyleChangedFromJS(IEnumerable? /// [JSInvokable] public async Task> LintingRequestedFromJS(string document) { + if (Setup.DebugLogs) Logger.LogInformation("LintingRequestedFromJS: {document}", document); if (Setup.BindMode == DocumentBindMode.OnDelayedInput) { await DocChangedFromJS(document); } @@ -100,6 +106,7 @@ [JSInvokable] public async Task> LintingRequestedFrom /// [JSInvokable] public async Task UploadFileFromJS(byte[] fileBytes, string fileName, string contentType, DateTime lastModified) { + if (Setup.DebugLogs) Logger.LogInformation("UploadFileFromJS: {fileName}", fileName); using var fileStream = new MemoryStream(fileBytes); var customFormFile = new CustomFormFile(fileStream, fileName, contentType); var customBrowserFile = new CustomBrowserFile(fileStream, fileName, contentType, lastModified); diff --git a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs index f6a4dfbb..efcc699a 100644 --- a/CodeMirror6/CodeMirror6WrapperInternal.razor.cs +++ b/CodeMirror6/CodeMirror6WrapperInternal.razor.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; using Microsoft.JSInterop; namespace GaelJ.BlazorCodeMirror6; @@ -13,12 +14,8 @@ namespace GaelJ.BlazorCodeMirror6; public partial class CodeMirror6WrapperInternal : ComponentBase, IAsyncDisposable { [Inject] private IJSRuntime JSRuntime { get; set; } = null!; - /// - /// /// Gets or sets the unique identifier for the CodeMirror6 editor. - /// Defaults to CodeMirror6_Editor_{NewGuid}. - /// - /// - [Parameter] public string Id { get; set; } = $"CodeMirror6_Editor_{Guid.NewGuid()}"; + [Inject] private ILogger Logger { get; set; } = null!; + /// /// The size of the tab character to use for the editor /// @@ -182,7 +179,7 @@ public partial class CodeMirror6WrapperInternal : ComponentBase, IAsyncDisposabl /// public CMCommandDispatcher? CommandDispatcher => CmJsInterop?.CommandDispatcher; - private string LoadingDivId => $"{Id}_Loading"; + private string LoadingDivId => $"{Setup.Id}_Loading"; private string ResizeStyle => AllowVerticalResize && AllowHorizontalResize ? "both" : AllowVerticalResize ? "vertical" @@ -264,71 +261,78 @@ protected override async Task OnParametersSetAsync() shouldRender = true; if (CmJsInterop is null) return; shouldRender = false; - if (Config.TabSize != TabSize) { - Config.TabSize = TabSize; - await CmJsInterop.PropertySetters.SetTabSize(); - } - if (Config.IndentationUnit != IndentationUnit) { - Config.IndentationUnit = IndentationUnit; - await CmJsInterop.PropertySetters.SetIndentUnit(); - } - if (Config.Doc?.Replace("\r", "") != Doc?.Replace("\r", "")) { - Config.Doc = Doc; - await CmJsInterop.PropertySetters.SetDoc(); - } - if (Config.Placeholder != Placeholder) { - Config.Placeholder = Placeholder; - await CmJsInterop.PropertySetters.SetPlaceholderText(); - } - if (Config.ThemeName != Theme) { - Config.ThemeName = Theme; - await CmJsInterop.PropertySetters.SetTheme(); - } - if (Config.ReadOnly != ReadOnly) { - Config.ReadOnly = ReadOnly; - await CmJsInterop.PropertySetters.SetReadOnly(); - } - if (Config.Editable != Editable) { - Config.Editable = Editable; - await CmJsInterop.PropertySetters.SetEditable(); - } - if (Config.LanguageName != Language) { - Config.LanguageName = Language; - await CmJsInterop.PropertySetters.SetLanguage(); - } - if (Config.FileNameOrExtension != FileNameOrExtension) { - Config.FileNameOrExtension = FileNameOrExtension; - await CmJsInterop.PropertySetters.SetLanguage(); - } - if (Config.AutoFormatMarkdown != AutoFormatMarkdown) { - Config.AutoFormatMarkdown = AutoFormatMarkdown; - await CmJsInterop.PropertySetters.SetAutoFormatMarkdown(); - } - if (Config.ReplaceEmojiCodes != ReplaceEmojiCodes) { - Config.ReplaceEmojiCodes = ReplaceEmojiCodes; - await CmJsInterop.PropertySetters.SetReplaceEmojiCodes(); - } - if (Config.Resize != ResizeStyle) { - Config.Resize = ResizeStyle; - await CmJsInterop.PropertySetters.SetResize(); - } - if (Config.LineWrapping != LineWrapping) { - Config.LineWrapping = LineWrapping; - await CmJsInterop.PropertySetters.SetLineWrapping(); - } - if (Config.MergeViewConfiguration != MergeViewConfiguration) { - Config.MergeViewConfiguration = MergeViewConfiguration; - await CmJsInterop.PropertySetters.SetUnifiedMergeView(); + try { + if (Config.TabSize != TabSize) { + Config.TabSize = TabSize; + await CmJsInterop.PropertySetters.SetTabSize(); + } + if (Config.IndentationUnit != IndentationUnit) { + Config.IndentationUnit = IndentationUnit; + await CmJsInterop.PropertySetters.SetIndentUnit(); + } + if (Config.Doc?.Replace("\r", "") != Doc?.Replace("\r", "")) { + Config.Doc = Doc; + await CmJsInterop.PropertySetters.SetDoc(); + } + if (Config.Placeholder != Placeholder) { + Config.Placeholder = Placeholder; + await CmJsInterop.PropertySetters.SetPlaceholderText(); + } + if (Config.ThemeName != Theme) { + Config.ThemeName = Theme; + await CmJsInterop.PropertySetters.SetTheme(); + } + if (Config.ReadOnly != ReadOnly) { + Config.ReadOnly = ReadOnly; + await CmJsInterop.PropertySetters.SetReadOnly(); + } + if (Config.Editable != Editable) { + Config.Editable = Editable; + await CmJsInterop.PropertySetters.SetEditable(); + } + if (Config.LanguageName != Language) { + Config.LanguageName = Language; + await CmJsInterop.PropertySetters.SetLanguage(); + } + if (Config.FileNameOrExtension != FileNameOrExtension) { + Config.FileNameOrExtension = FileNameOrExtension; + await CmJsInterop.PropertySetters.SetLanguage(); + } + if (Config.AutoFormatMarkdown != AutoFormatMarkdown) { + Config.AutoFormatMarkdown = AutoFormatMarkdown; + await CmJsInterop.PropertySetters.SetAutoFormatMarkdown(); + } + if (Config.ReplaceEmojiCodes != ReplaceEmojiCodes) { + Config.ReplaceEmojiCodes = ReplaceEmojiCodes; + await CmJsInterop.PropertySetters.SetReplaceEmojiCodes(); + } + if (Config.Resize != ResizeStyle) { + Config.Resize = ResizeStyle; + await CmJsInterop.PropertySetters.SetResize(); + } + if (Config.LineWrapping != LineWrapping) { + Config.LineWrapping = LineWrapping; + await CmJsInterop.PropertySetters.SetLineWrapping(); + } + if (Config.MergeViewConfiguration != MergeViewConfiguration) { + Config.MergeViewConfiguration = MergeViewConfiguration; + await CmJsInterop.PropertySetters.SetUnifiedMergeView(); + } + if (Config.HighlightTrailingWhitespace != HighlightTrailingWhitespace) { + Config.HighlightTrailingWhitespace = HighlightTrailingWhitespace; + await CmJsInterop.PropertySetters.SetHighlightTrailingWhitespace(); + } + if (Config.HighlightWhitespace != HighlightWhitespace) { + Config.HighlightWhitespace = HighlightWhitespace; + await CmJsInterop.PropertySetters.SetHighlightWhitespace(); + } } - if (Config.HighlightTrailingWhitespace != HighlightTrailingWhitespace) { - Config.HighlightTrailingWhitespace = HighlightTrailingWhitespace; - await CmJsInterop.PropertySetters.SetHighlightTrailingWhitespace(); + catch (Exception ex) { + Logger.LogError(ex, "Error setting CodeMirror6 properties"); } - if (Config.HighlightWhitespace != HighlightWhitespace) { - Config.HighlightWhitespace = HighlightWhitespace; - await CmJsInterop.PropertySetters.SetHighlightWhitespace(); + finally { + shouldRender = true; } - shouldRender = true; } /// @@ -349,6 +353,7 @@ public async ValueTask DisposeAsync() { if (CmJsInterop?.IsJSReady == true) await CmJsInterop.DisposeAsync(); + CmJsInterop = null; try { LinterCancellationTokenSource.Cancel(); LinterCancellationTokenSource.Dispose(); diff --git a/CodeMirror6/Models/CodeMirrorSetup.cs b/CodeMirror6/Models/CodeMirrorSetup.cs index 94a8fadd..677ee785 100644 --- a/CodeMirror6/Models/CodeMirrorSetup.cs +++ b/CodeMirror6/Models/CodeMirrorSetup.cs @@ -15,6 +15,13 @@ public CodeMirrorSetup() { } + /// + /// /// Gets or sets the unique identifier for the CodeMirror6 editor. + /// Defaults to CodeMirror6_Editor_{NewGuid}. + /// + /// + [JsonPropertyName("id")] public string Id { get; init; } = $"CodeMirror6_Editor_{Guid.NewGuid()}"; + /// /// Whether to show line numbers to the left of the editor. /// @@ -134,4 +141,9 @@ public CodeMirrorSetup() /// Can the user scroll past the end of the document /// [JsonPropertyName("scrollPastEnd")] public bool ScrollPastEnd { get; init; } = false; + + /// + /// Whether to show the debug logs + /// + [JsonPropertyName("debugLogs")] public bool DebugLogs { get; init; } = false; } diff --git a/CodeMirror6/NodeLib/src/CmSetup.ts b/CodeMirror6/NodeLib/src/CmSetup.ts index 2ecf869c..943cfc82 100644 --- a/CodeMirror6/NodeLib/src/CmSetup.ts +++ b/CodeMirror6/NodeLib/src/CmSetup.ts @@ -28,4 +28,5 @@ export class CmSetup public bindValueMode: string public krokiUrl: string public scrollPastEnd: boolean + public debugLogs: boolean } diff --git a/CodeMirror6/NodeLib/src/index.ts b/CodeMirror6/NodeLib/src/index.ts index 34d5a7df..c536686b 100644 --- a/CodeMirror6/NodeLib/src/index.ts +++ b/CodeMirror6/NodeLib/src/index.ts @@ -75,10 +75,13 @@ export async function initCodeMirror( initialConfig: CmConfiguration, setup: CmSetup ) { - if (CMInstances[id] !== undefined) - return; + if (CMInstances[id] !== undefined) { + consoleLog(id, `CodeMirror instance ${id} already exists`) + return + } - console.log(`Initializing CodeMirror instance ${id}`) + if (setup.debugLogs === true) + console.log(`Initializing CodeMirror instance ${id}`) try { const minDelay = new Promise(res => setTimeout(res, 100)) @@ -223,13 +226,23 @@ export async function initCodeMirror( } } -export function getAllSupportedLanguageNames(id: string) +function consoleLog(id: string, message: string) +{ + if (CMInstances[id].setup.debugLogs === true) + console.log(message) +} + +export function getAllSupportedLanguageNames() { return languages.map((language) => language.name) } async function updateListenerExtension(id: string, update: ViewUpdate) { const dotnetHelper = CMInstances[id].dotNetHelper + if (dotnetHelper === undefined){ + consoleLog(id, `DotNetHelper is undefined`) + return + } const setup = CMInstances[id].setup if (update.docChanged) { if (setup.bindValueMode === 'OnInput') @@ -351,7 +364,10 @@ export function setHighlightWhitespace(id: string, value: boolean) { export function forceRedraw(id: string) { const view = CMInstances[id].view - if (!view) return + if (!view) { + consoleLog(id, `View is undefined`) + return + } view.requestMeasure() view.update([]) @@ -467,7 +483,7 @@ export function dispatchCommand(id: string, functionName: string, ...args: any[] * @param id */ export function dispose(id: string) { - console.log(`Disposing of CodeMirror instance ${id}`) + consoleLog(id, `Disposing of CodeMirror instance ${id}`) CMInstances[id].dotNetHelper.dispose() CMInstances[id].dotNetHelper = undefined CMInstances[id].view.destroy() diff --git a/Examples.BlazorServer/Examples.BlazorServer.csproj b/Examples.BlazorServer/Examples.BlazorServer.csproj index 349259de..4c2b7bd7 100644 --- a/Examples.BlazorServer/Examples.BlazorServer.csproj +++ b/Examples.BlazorServer/Examples.BlazorServer.csproj @@ -4,7 +4,7 @@ enable false enable - 0.3.5 + 0.3.6 diff --git a/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj b/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj index d5698e85..fc3f175a 100644 --- a/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj +++ b/Examples.BlazorServerInteractive/Examples.BlazorServerInteractive.csproj @@ -4,7 +4,7 @@ enable enable false - 0.3.5 + 0.3.6 diff --git a/Examples.BlazorWasm/Examples.BlazorWasm.csproj b/Examples.BlazorWasm/Examples.BlazorWasm.csproj index f8dcd7ca..a0c8b8e9 100644 --- a/Examples.BlazorWasm/Examples.BlazorWasm.csproj +++ b/Examples.BlazorWasm/Examples.BlazorWasm.csproj @@ -4,7 +4,7 @@ enable enable false - 0.3.5 + 0.3.6 diff --git a/Examples.Common/Examples.Common.csproj b/Examples.Common/Examples.Common.csproj index c0728ced..1b964875 100644 --- a/Examples.Common/Examples.Common.csproj +++ b/Examples.Common/Examples.Common.csproj @@ -5,7 +5,7 @@ enable enable false - 0.3.5 + 0.3.6 diff --git a/NEW_CHANGELOG.md b/NEW_CHANGELOG.md index 4b4af429..28b63853 100644 --- a/NEW_CHANGELOG.md +++ b/NEW_CHANGELOG.md @@ -1,11 +1,11 @@ -### ✨ Introduce new features +### 🐛 Fix a bug -- Expose CodeMirrorState, for @ref access +- Try to fix random crash at startup -### 🐛 Fix a bug +### 💥 Introduce breaking changes -- Don't use no-cors header +- Move Id from parameter to Setup, as it should never be changed -### 📝 Add or update documentation +### 🔊 Add or update logs -- Fix menu title in example project +- Add DebugLogs setup parameter to print debug logs