diff --git a/packages/slate-react/src/plugins/after.js b/packages/slate-react/src/plugins/after.js index e4f97c5b3..29a8c7ef1 100644 --- a/packages/slate-react/src/plugins/after.js +++ b/packages/slate-react/src/plugins/after.js @@ -514,36 +514,36 @@ function AfterPlugin(options = {}) { // an inline is selected, we need to handle these hotkeys manually because // browsers won't know what to do. if (HOTKEYS.COLLAPSE_CHAR_BACKWARD(event)) { - const { isInVoid, previousText, document } = state + const { document, isInVoid, previousText, startText } = state const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key) - if (isInVoid || isPreviousInVoid) { + if (isInVoid || isPreviousInVoid || startText.text == '') { event.preventDefault() return change.collapseCharBackward() } } if (HOTKEYS.COLLAPSE_CHAR_FORWARD(event)) { - const { isInVoid, nextText, document } = state + const { document, isInVoid, nextText, startText } = state const isNextInVoid = nextText && document.hasVoidParent(nextText.key) - if (isInVoid || isNextInVoid) { + if (isInVoid || isNextInVoid || startText.text == '') { event.preventDefault() return change.collapseCharForward() } } if (HOTKEYS.EXTEND_CHAR_BACKWARD(event)) { - const { isInVoid, previousText, document } = state + const { document, isInVoid, previousText, startText } = state const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key) - if (isInVoid || isPreviousInVoid) { + if (isInVoid || isPreviousInVoid || startText.text == '') { event.preventDefault() return change.extendCharBackward() } } if (HOTKEYS.EXTEND_CHAR_FORWARD(event)) { - const { isInVoid, nextText, document } = state + const { document, isInVoid, nextText, startText } = state const isNextInVoid = nextText && document.hasVoidParent(nextText.key) - if (isInVoid || isNextInVoid) { + if (isInVoid || isNextInVoid || startText.text == '') { event.preventDefault() return change.extendCharForward() } diff --git a/packages/slate-react/src/utils/find-point.js b/packages/slate-react/src/utils/find-point.js index dee8c66f5..e2b239715 100644 --- a/packages/slate-react/src/utils/find-point.js +++ b/packages/slate-react/src/utils/find-point.js @@ -56,6 +56,12 @@ function findPoint(nativeNode, nativeOffset, state) { offset = node.textContent.length } + // COMPAT: If the parent node is a Slate zero-width space, this is because the + // text node has no characters, so the offset can only be zero. + if (offset != 0 && parentNode.getAttribute('data-slate-zero-width')) { + offset = 0 + } + // Get the string value of the offset key attribute. const offsetKey = rangeNode.getAttribute(OFFSET_KEY_ATTRIBUTE) if (!offsetKey) return null