Home Reference Source

application/components/common/rich-editor2/plugins/soft-breaks.js

export default function(options = {}) {
    const myOptions = Object.assign({}, {
        'shift': false,
        'inBlocks': []
    }, options);

    const useShift = myOptions.shift;

    return {
        onKeyDown(e, editor, next) {

            const {start} = editor.value.selection;
            const {document} = editor.value;


            if (e.key != 'Enter') {
                return next();
            }

            const closest = document.getClosestBlock(start.key);
            let inBlock = false;

            if (myOptions.inBlocks.indexOf(closest.get('type')) !== -1) {
                inBlock = true;
            }

            if (useShift && e.shiftKey === false && inBlock == false) {
                return next();
            }

            // for some reason, if you don't insert a non-zero width character
            // the cursor moves back to the head of the block and the new line
            // isn't inserted
            editor.insertText("\n ")
                .moveBackward(1);
        }
    };
}