From 7be3e0e4ec20ef4156299d06e2ac879afecef30c Mon Sep 17 00:00:00 2001 From: Nick Bailey Date: Mon, 14 Oct 2024 16:31:01 +0100 Subject: [PATCH 1/3] Added CustomHandlers --- README.md | 16 ++++++++++++++++ .../MudHtmlEditor.razor.cs | 5 +++-- .../MudHtmlEditor.razor.js | 11 ++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f790d96..513f71d 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,22 @@ For more advanced customization, you can define your own toolbar options inside ``` +#### Custom Handlers +As Quill expects a handler created for every class you can add in custom handlers through the CustomHandler parameter where CustomHandler is a Dictionary: + +```csharp +// classname is added to the dictionary without the leading ql- as that is inferred during Quill instanciation +private Dictionary _customHandler_ = new() +{ + {"classname", "console.log('hello world');"} +} +``` + +```razor + + +``` + See the [QuillJS documentation](https://quilljs.com/docs/modules/toolbar/) for more information on customizing the toolbar. ## Migrating from v1.0 to v2.0 diff --git a/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs b/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs index 58aadcc..d003db9 100644 --- a/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs +++ b/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.cs @@ -64,6 +64,8 @@ public sealed partial class MudHtmlEditor : IAsyncDisposable [Parameter(CaptureUnmatchedValues = true)] public IDictionary? UserAttributes { get; set; } + [Parameter] + public IDictionary? CustomHandlers { get; set; } /// /// Clears the content of the editor. @@ -114,8 +116,7 @@ protected override async Task OnAfterRenderAsync(bool firstRender) _dotNetRef = DotNetObjectReference.Create(this); await using var module = await JS.InvokeAsync("import", "./_content/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js"); - _quill = await module.InvokeAsync("createQuillInterop", _dotNetRef, _editor, _toolbar, Placeholder); - + _quill = await module.InvokeAsync("createQuillInterop", _dotNetRef, _editor, _toolbar, Placeholder, CustomHandlers); await SetHtml(Html); StateHasChanged(); diff --git a/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js b/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js index 974d5b6..a102c8e 100644 --- a/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js +++ b/src/Tizzani.MudBlazor.HtmlEditor/MudHtmlEditor.razor.js @@ -16,7 +16,7 @@ try { Quill.register('modules/blotFormatter', QuillBlotFormatter.default); } catch { } -export function createQuillInterop(dotNetRef, editorRef, toolbarRef, placeholder) { +export function createQuillInterop(dotNetRef, editorRef, toolbarRef, placeholder, customHandlers) { var quill = new Quill(editorRef, { modules: { toolbar: { @@ -27,6 +27,15 @@ export function createQuillInterop(dotNetRef, editorRef, toolbarRef, placeholder placeholder: placeholder, theme: 'snow' }); + + // Dynamically add handlers from customHandlers to the toolbar + const toolbar = quill.getModule('toolbar'); + if (customHandlers) { + Object.keys(customHandlers).forEach(key => { + toolbar.addHandler(key, new Function('value', customHandlers[key])); + }); + } + return new MudQuillInterop(dotNetRef, quill, editorRef, toolbarRef); } From 65b6ed73ae959f33fb099206c4fe6179bbbe1b48 Mon Sep 17 00:00:00 2001 From: Nick Bailey Date: Mon, 14 Oct 2024 16:33:57 +0100 Subject: [PATCH 2/3] Improved readme --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 513f71d..facf880 100644 --- a/README.md +++ b/README.md @@ -101,9 +101,14 @@ private Dictionary _customHandler_ = new() ```razor + +