From 8213573fb5e41ed50e30daa63a471a55edbd0696 Mon Sep 17 00:00:00 2001 From: Ian Storm Taylor Date: Tue, 28 Jun 2016 08:42:34 -0700 Subject: [PATCH] fix on before input --- lib/plugins/core.js | 68 +++++++++++++-------------------------------- 1 file changed, 19 insertions(+), 49 deletions(-) diff --git a/lib/plugins/core.js b/lib/plugins/core.js index f3c26340b..e57fbf070 100644 --- a/lib/plugins/core.js +++ b/lib/plugins/core.js @@ -13,6 +13,9 @@ export default { /** * The core `onBeforeInput` handler. * + * If the current selection is collapsed, we can insert the text natively and + * avoid a re-render, improving performance. + * * @param {Event} e * @param {State} state * @param {Editor} editor @@ -20,29 +23,14 @@ export default { */ onBeforeInput(e, state, editor) { - const { data } = e + const isNative = state.isCollapsed - // When does this happen... - if (!data) { - debugger - return - } + if (!isNative) e.preventDefault() - // If the selection is still expanded, delete anything inside it first. - if (state.isExpanded) { - e.preventDefault() - return state - .transform() - .delete() - .insertText(data) - .apply() - } - - // Otherwise, insert text natively, without re-rendering. return state .transform() - .insertText(data) - .apply({ isNative: true }) + .insertText(e.data) + .apply({ isNative }) }, /** @@ -56,55 +44,35 @@ export default { onKeyDown(e, state, editor) { const key = keycode(e.which) + const transform = state.transform() switch (key) { case 'enter': { - return state - .transform() - .splitBlock() - .apply() + return transform.splitBlock().apply() } case 'backspace': { return isWord(e) - ? state - .transform() - .backspaceWord() - .apply() - : state - .transform() - .deleteBackward() - .apply() + ? transform.backspaceWord().apply() + : transform.deleteBackward().apply() } case 'delete': { return isWord(e) - ? state - .transform() - .deleteWord() - .apply() - : state - .transform() - .deleteForward() - .apply() + ? transform.deleteWord().apply() + : transform.deleteForward().apply() } case 'y': { if (!isWindowsCommand(e)) return - return state - .transform() - .redo() + return transform.redo() } case 'z': { if (!isCommand(e)) return return IS_MAC && e.shiftKey - ? state - .transform() - .redo() - : state - .transform() - .undo() + ? transform.redo() + : transform.undo() } } }, @@ -142,7 +110,9 @@ export default { */ renderNode(node) { - return (props) =>
{props.children}
+ return node.kind == 'block' + ? (props) =>
{props.children}
+ : (props) => {props.children} }, /**