Medium-editor: Get rid of previous "selection" when hit "enter"

Created on 12 Feb 2020  路  1Comment  路  Source: yabwe/medium-editor

Currently the editor works just like the demo
I don't like when we select one of the toolbar, it keeps the same effect even after we hit the "enter/return key"

What i want is:
//typing headline using bold effect
This is headline
//after enter
it automaticallya remove the "bold effect"

what happen now, it keeps the bold effect even on the new line

anyone know how to do this?

Most helpful comment

After peeked some of the code code, here is my current solution

In this case, i'm checking if the cursor currenly in "CODE" or "PRE" tag, then i want "ENTER" function to move outside and create a new paragraph. To continue using the selection tag, just use "SHIFT+ENTER".

You can change the CODE or PRE in if/else part, hope it helps

/*
Custom meditum-editor function
On event "ENTER", by pre or CODE
make a new line
*/
editor.subscribe("editableKeydownEnter", function (event, element) {
        //allow shift + enter to continue behaviour 
         if (event.shiftKey)
            return

        let currentNode = MediumEditor.selection.getSelectionStart(editor.options.ownerDocument)
        if(currentNode.nodeName == "PRE" || currentNode.nodeName == "CODE") {
            //then go outside pre, make a normal paragraph tag
            newP = editor.options.ownerDocument.createElement('p');
            newP.innerHTML = '<br>';
            currentNode.parentElement.insertBefore(newP, currentNode.nextSibling);

            // move the cursor into the new paragraph
            MediumEditor.selection.moveCursor(editor.options.ownerDocument, newP);
        }
    }.bind(this))

>All comments

After peeked some of the code code, here is my current solution

In this case, i'm checking if the cursor currenly in "CODE" or "PRE" tag, then i want "ENTER" function to move outside and create a new paragraph. To continue using the selection tag, just use "SHIFT+ENTER".

You can change the CODE or PRE in if/else part, hope it helps

/*
Custom meditum-editor function
On event "ENTER", by pre or CODE
make a new line
*/
editor.subscribe("editableKeydownEnter", function (event, element) {
        //allow shift + enter to continue behaviour 
         if (event.shiftKey)
            return

        let currentNode = MediumEditor.selection.getSelectionStart(editor.options.ownerDocument)
        if(currentNode.nodeName == "PRE" || currentNode.nodeName == "CODE") {
            //then go outside pre, make a normal paragraph tag
            newP = editor.options.ownerDocument.createElement('p');
            newP.innerHTML = '<br>';
            currentNode.parentElement.insertBefore(newP, currentNode.nextSibling);

            // move the cursor into the new paragraph
            MediumEditor.selection.moveCursor(editor.options.ownerDocument, newP);
        }
    }.bind(this))
Was this page helpful?
0 / 5 - 0 ratings