Alex-D / Trumbowyg

A lightweight and amazing WYSIWYG JavaScript editor under 10kB
https://alex-d.github.io/Trumbowyg
MIT License
3.96k stars 606 forks source link

Firefox spell check fires tbwchange but content does not update until manual input #1444

Open Duberry opened 4 months ago

Duberry commented 4 months ago

When I perform a spellcheck in Firefox, tbwchange fires. However, if I get editor content using $('#my_trumbowyg_id').trumbowyg('html'), the value returned is the content of the editor before I ran the spell check.

If I enter any additional content in the editor manually, then $('#my_trumbowyg_id').trumbowyg('html') begins to return the correct value.

Description

How to reproduce?

$('#my_trumbowyg_id').trumbowyg().on('tbwchange', function() {console.log('change');});

Type misspelt string

Use spell check to correct string spelling

console.log($('#my_trumbowyg_id').trumbowyg('html'))

Type a space after the correctly spelt string displaying in the editor

$('#my_trumbowyg_id').trumbowyg('html')

Duberry commented 3 months ago

Hi Alex-D. I see you have tagged this as a bug. Do you have any feedback on when it might be resolved or a pointer for the code causing it so that I could look at a fix?

Duberry commented 3 months ago

Quick Fix is to add this jQuery extension:

(function($) {
    // Extend jQuery to include a function to get Trumbowyg content
    $.fn.getTrumbowygContent = function() {
        // Find the closest '.trumbowyg-box' parent to ensure we're working within a Trumbowyg editor
        var trumbowygBox = this.closest('.trumbowyg-box');
        if (trumbowygBox.length) {
            // If a '.trumbowyg-box' is found, find the '.trumbowyg-editor' within it to get the content
            var editor = trumbowygBox.find('.trumbowyg-editor');
            return editor.html(); // Return the HTML content of the 'trumbowyg-editor'
        } else {
            // If no '.trumbowyg-box' parent is found, log an error and return an empty string
            console.error("No Trumbowyg editor found in the ancestor elements.");
            return '';
        }
    };
})(jQuery);

Then when you want to retrieve the Trumbowyg content, call $('#your_textarea_id').getTrumbowygContent()