Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

All children elements of medium-editor can be permanently deleted, causing the editor to stop working #408

Closed
jsindos opened this issue Jan 7, 2017 · 2 comments

Comments

@jsindos
Copy link

jsindos commented Jan 7, 2017

TL;DR
medium-editor-insert-plugin v2.4.0 and medium-editor v5.22.1 has a bug which isn't present when using medium-editor v5.10.0.

Playing around with the example in medium-editor-insert-plugin v2.4.0 and medium-editor v5.22.1 causes no new <p> tag to be spawned when the last <p> of the editor is backspaced, giving the following DOM structure:

screen shot 2017-01-07 at 1 08 38 pm

Which looks like:

screen shot 2017-01-07 at 1 06 09 pm

(No blinking cursor as there are no <p> children.)

This bug does not occur on the official hosted example, running medium-editor v5.10.0 + medium-editor-insert-plugin v2.2.3.

SOLUTION
v5.22.0 of medium-editor introduced a change to how they handle removal of first empty paragraph on backspace: yabwe/medium-editor#1187

} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) &&
        MediumEditor.util.isMediumEditorElement(node.parentElement) &&
        !node.previousElementSibling &&
        node.nextElementSibling &&
        isEmpty.test(node.innerHTML)) {

    // when cursor is in the first element, it's empty and user presses backspace,
    // do delete action instead to get rid of the first element and move caret to 2nd
    event.preventDefault();
    MediumEditor.selection.moveCursor(this.options.ownerDocument, node.nextSibling);
    node.parentElement.removeChild(node);
}

As there is a nextElementSibling (medium-insert-buttons) this check passes and the <p> is deleted.

@dreamapplehappy
Copy link

you can use the code below to prevent this problem

let that = this;
this.editor.subscribe('editableKeyup', function (e, editable) {
                // watch the content and solve this problem
                let allContents = that.editor.serialize();
                let content = allContents["element-0"].value;
                if(!content) {
                    that.editor.setContent('<p><br></p>');
                }
});

this works for me 😂

@zengxiaoluan
Copy link

zengxiaoluan commented Dec 21, 2017

@dreamapplehappy 属性“element-0”访问不到,改成了
getRealContents: function (contents) { for (var i in contents) { return contents[i].value } }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants