Megabit / Blazorise

Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
https://blazorise.com/
Other
3.25k stars 526 forks source link

RichTextEditor does not currently support Quill methods for inserting text/images #4490

Open jimitndiaye opened 1 year ago

jimitndiaye commented 1 year ago

Is your feature request related to a problem? Please describe. I'm trying to add a custom button to a RichTextEditor toolbar that inserts some text into the current cursor position. Quill supports this but it is not currently exposed by the RichTextEditor, unlike Blazored's version for instance which does (unfortunately theirs doesn't have the ContentChanged hook that Blazorise does which is why I'm here)

Describe the solution you'd like In the same way that the GetHtmlAsync methods are exposed, allow access to the InsertTextAsync method.

stsrki commented 1 year ago

Sorry for net getting to you sooner. Can you give us a link to the Quill documentation where the needed button API is mentioned?

jimitndiaye commented 1 year ago

I wasn't able to find a link but here's what works for me (adapted from Blazored's implementation to be more inline with your implementation):

export function insertText(editorRef, text) {
    const editor = editorRef.quill;
    if (!editor)
        return;

    let editorIndex = 0;
    let selectionLength = 0;

    if (editor.getSelection() !== null) {
        const selection = editor.getSelection();
        editorIndex = selection.index;
        selectionLength = selection.length;
    }

    return editor.deleteText(editorIndex, selectionLength)
        .concat(editor.insertText(editorIndex, text));
}

export function insertImage(editorRef, imageURL) {
    const editor = editorRef.quill;
    if (!editor)
        return;

    var Delta = Quill.import('delta');
    editorIndex = 0;

    if (editor.getSelection() !== null) {
        editorIndex = editor.getSelection().index;
    }

    return editor.updateContents(
        new Delta()
            .retain(editorIndex)
            .insert({ image: imageURL },
                { alt: imageURL }));
}