1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 20:51:20 +02:00

fix findRange for text nodes next to inlines (#1298)

This commit is contained in:
Ian Storm Taylor
2017-10-26 09:26:58 -07:00
committed by GitHub
parent c170e7940e
commit 1235a0a8c5
2 changed files with 14 additions and 8 deletions

View File

@@ -514,36 +514,36 @@ function AfterPlugin(options = {}) {
// an inline is selected, we need to handle these hotkeys manually because // an inline is selected, we need to handle these hotkeys manually because
// browsers won't know what to do. // browsers won't know what to do.
if (HOTKEYS.COLLAPSE_CHAR_BACKWARD(event)) { if (HOTKEYS.COLLAPSE_CHAR_BACKWARD(event)) {
const { isInVoid, previousText, document } = state const { document, isInVoid, previousText, startText } = state
const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key) const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key)
if (isInVoid || isPreviousInVoid) { if (isInVoid || isPreviousInVoid || startText.text == '') {
event.preventDefault() event.preventDefault()
return change.collapseCharBackward() return change.collapseCharBackward()
} }
} }
if (HOTKEYS.COLLAPSE_CHAR_FORWARD(event)) { if (HOTKEYS.COLLAPSE_CHAR_FORWARD(event)) {
const { isInVoid, nextText, document } = state const { document, isInVoid, nextText, startText } = state
const isNextInVoid = nextText && document.hasVoidParent(nextText.key) const isNextInVoid = nextText && document.hasVoidParent(nextText.key)
if (isInVoid || isNextInVoid) { if (isInVoid || isNextInVoid || startText.text == '') {
event.preventDefault() event.preventDefault()
return change.collapseCharForward() return change.collapseCharForward()
} }
} }
if (HOTKEYS.EXTEND_CHAR_BACKWARD(event)) { if (HOTKEYS.EXTEND_CHAR_BACKWARD(event)) {
const { isInVoid, previousText, document } = state const { document, isInVoid, previousText, startText } = state
const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key) const isPreviousInVoid = previousText && document.hasVoidParent(previousText.key)
if (isInVoid || isPreviousInVoid) { if (isInVoid || isPreviousInVoid || startText.text == '') {
event.preventDefault() event.preventDefault()
return change.extendCharBackward() return change.extendCharBackward()
} }
} }
if (HOTKEYS.EXTEND_CHAR_FORWARD(event)) { if (HOTKEYS.EXTEND_CHAR_FORWARD(event)) {
const { isInVoid, nextText, document } = state const { document, isInVoid, nextText, startText } = state
const isNextInVoid = nextText && document.hasVoidParent(nextText.key) const isNextInVoid = nextText && document.hasVoidParent(nextText.key)
if (isInVoid || isNextInVoid) { if (isInVoid || isNextInVoid || startText.text == '') {
event.preventDefault() event.preventDefault()
return change.extendCharForward() return change.extendCharForward()
} }

View File

@@ -56,6 +56,12 @@ function findPoint(nativeNode, nativeOffset, state) {
offset = node.textContent.length 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. // Get the string value of the offset key attribute.
const offsetKey = rangeNode.getAttribute(OFFSET_KEY_ATTRIBUTE) const offsetKey = rangeNode.getAttribute(OFFSET_KEY_ATTRIBUTE)
if (!offsetKey) return null if (!offsetKey) return null