steveathon / bootstrap-wysiwyg

Tiny bootstrap-compatible WYSIWYG rich text editor
MIT License
661 stars 1.71k forks source link

Problem with 2 editors on one html page #110

Open atatanasov opened 7 years ago

atatanasov commented 7 years ago

Replication steps:

  1. Format a text in editor 1 in bold
  2. Move the mouse to editor 2

Actual Result: Bold button in the second editor is selected Expected Results: only the bold button in the first editor is selected

I added the following code in order to fix this:

getSelectedNode = function ()
{
    if (document.selection) {
        return document.selection.createRange().parentElement();
    }
    else
    {
        var selection = window.getSelection();
        if (selection.rangeCount > 0) {
            return selection.getRangeAt(0).startContainer.parentNode;
        }
    }
},
updateToolbar = function () {
    if (options.activeToolbarClass) {
        $(options.toolbarSelector).find(toolbarBtnSelector).each(function () {
            var commandArr = $(this).data(options.commandRole).split(' '),
                command = commandArr[0];

            // If the command has an argument and its value matches this button. == used for string/number comparison
            if (commandArr.length > 1 && document.queryCommandEnabled(command) && document.queryCommandValue(command) === commandArr[1]) {
                $(this).addClass(options.activeToolbarClass);
            // Else if the command has no arguments and it is active
            } else if (commandArr.length === 1 && document.queryCommandEnabled(command) && document.queryCommandState(command) && $.contains($(this).closest('.bs-editor')[0], getSelectedNode())) {
                $(this).addClass(options.activeToolbarClass);
            // Else the command is not active
            } else {
                $(this).removeClass(options.activeToolbarClass);
            }
        });
    }
},
codewithtyler commented 7 years ago

So you added this code into the bootstrapy-wysiwyg.js file?

atatanasov commented 7 years ago

Yes. This is correct. We are using version 1.0.4 and I can't push this to master, because the code in the master is different, but I'm sure that this bug still exists in master.

codewithtyler commented 7 years ago

Have you tested this with the latest 2.0.1 release?

spreadred commented 7 years ago

Using the multiple-editors.html in the examples folder of 2.0dev this bug still exists. In fact, almost any button clicked in either editor, results in that same button being clicked in the other editor. This is especially bad when using the text alignment options as it actually applies that alignment to both editor windows simultaneously. This applies to the ordered/unordered lists as well.

nicopowa commented 5 years ago

Hi thought i'd publish my fix here :

i inserted this code before "mouseup keyup mouseout" listener it will reset selection if the mouse leaves an editor and enters en other editor.

.on("mouseenter", function() {
    if(window.getSelection && window.lastWysiwygEditor !== editor[0].id) {
        if(window.getSelection().empty) {  // Chrome
            window.getSelection().empty();
        }
        else if(window.getSelection().removeAllRanges) {  // Firefox
            window.getSelection().removeAllRanges();
        }
    }
    else if (document.selection) {  // IE?
        document.selection.empty();
    }
})
.on("mouseleave", function() {
    window.lastWysiwygEditor = editor[0].id;
})