From d5834bb5b010bcb38db068efd58fe64314e6533c Mon Sep 17 00:00:00 2001 From: Kaspars Dancis Date: Thu, 25 Oct 2018 10:47:29 -0600 Subject: [PATCH] Prevent crash when handling composition of 2+ keypresses when selection is not collapsed (#2218) https://github.com/ianstormtaylor/slate/issues/1879 When composition starts and the current selection is not collapsed, the second composition key-down would drop the text wrapping which resulted on crash in content.updateSelection after composition ends (because it cannot find nodes in DOM). This is a workaround that erases selection as soon as composition starts and preventing to be dropped. --- packages/slate-react/src/plugins/before.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/packages/slate-react/src/plugins/before.js b/packages/slate-react/src/plugins/before.js index 8dfd7fbc5..2a68b04d2 100644 --- a/packages/slate-react/src/plugins/before.js +++ b/packages/slate-react/src/plugins/before.js @@ -167,6 +167,20 @@ function BeforePlugin() { editor.setState({ isComposing: true }) } + const { value } = change + const { selection } = value + + if (!selection.isCollapsed) { + // https://github.com/ianstormtaylor/slate/issues/1879 + // When composition starts and the current selection is not collapsed, the + // second composition key-down would drop the text wrapping which + // resulted on crash in content.updateSelection after composition ends + // (because it cannot find nodes in DOM). This is a workaround that + // erases selection as soon as composition starts and preventing + // to be dropped. + change.delete() + } + debug('onCompositionStart', { event }) next() }