From 979dadf2d6af1e4bd15b04f16497d5e7f4587a1c Mon Sep 17 00:00:00 2001 From: Dan Burzo Date: Tue, 15 Aug 2017 20:42:33 +0300 Subject: [PATCH] Fixes #938 chrome eating first line of text on space at beginning of line (#991) --- src/plugins/core.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/plugins/core.js b/src/plugins/core.js index a9b99caa9..4c5d66071 100644 --- a/src/plugins/core.js +++ b/src/plugins/core.js @@ -478,6 +478,7 @@ function Plugin(options = {}) { switch (data.key) { case 'enter': return onKeyDownEnter(e, data, state) + case 'space': return onKeyDownSpace(e, data, state) case 'backspace': return onKeyDownBackspace(e, data, state) case 'delete': return onKeyDownDelete(e, data, state) case 'left': return onKeyDownLeft(e, data, state) @@ -522,6 +523,27 @@ function Plugin(options = {}) { .apply() } + /** + * On `Space` key down, prevent the default browser behavior + * in Chrome, since in some situation it will result in loss of text. + * Reference: https://github.com/ianstormtaylor/slate/issues/938 + * + * @param {Event} e + * @param {Object} data + * @param {State} state + * @return {State|Null} + */ + + function onKeyDownSpace(e, data, state) { + if (IS_CHROME) { + e.preventDefault() + return state + .transform() + .insertText(' ') + .apply() + } + } + /** * On `backspace` key down, delete backwards. *