Skip to content

Instantly share code, notes, and snippets.

@zaydek-old
Last active August 25, 2019 16:43
Show Gist options
  • Save zaydek-old/4a39895396a124aff84edf208ba7f21c to your computer and use it in GitHub Desktop.
Save zaydek-old/4a39895396a124aff84edf208ba7f21c to your computer and use it in GitHub Desktop.
// Fix for `cmd-r` when the cursor is on or in-between
// `<span style="display: inline-block" />` and a text
// node. This behavior depends on `traverseDOM`’s
// `findNode` resolving to the sibling text node.
const [isCmdArrowR, newAnchorNode] = detectIsCmdArrowR(e, props.state)
if (isCmdArrowR) {
e.preventDefault()
console.log("test")
const selection = document.getSelection()
const range = document.createRange()
range.setStartAfter(newAnchorNode)
range.collapse(true)
selection.removeAllRanges()
selection.addRange(range)
return
}
function detectIsCmdArrowR(e, state) {
const { anchorNode } = document.getSelection()
let newAnchorNode = null
const isCmdArrowR = (
((globals.isAppleOS ? e.metaKey : e.ctrlKey) && e.key === "ArrowRight") &&
state.pos1.index === state.pos2.index && ( // Focused on the same semantic node.
(
// E.g. `<span>[#]&nbsp;</span>Header`.
anchorNode.nodeType === Node.TEXT_NODE &&
anchorNode.parentNode.nodeType === Node.ELEMENT_NODE &&
anchorNode.parentNode.nodeName === "SPAN" &&
anchorNode.parentNode.nextSibling &&
anchorNode.parentNode.nextSibling.nodeType === Node.TEXT_NODE &&
(newAnchorNode = anchorNode.parentNode.nextSibling)
) || (
// E.g. `<span>#<&nbsp;/span>[Header]`
anchorNode.nodeType === Node.TEXT_NODE &&
anchorNode.previousSibling &&
anchorNode.previousSibling.nodeType === Node.ELEMENT_NODE &&
anchorNode.previousSibling.nodeName === "SPAN" &&
anchorNode.previousSibling.childNodes.length &&
anchorNode.previousSibling.childNodes[0].nodeType && Node.TEXT_NODE &&
(newAnchorNode = anchorNode.previousSibling.childNodes[0])
)
)
)
return [isCmdArrowR, newAnchorNode]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment