Last active
August 25, 2019 16:43
-
-
Save zaydek-old/4a39895396a124aff84edf208ba7f21c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>[#] </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>#< /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