|
| 1 | +import {isContentEditable, isElementType} from '..' |
| 2 | + |
| 3 | +declare global { |
| 4 | + interface Text { |
| 5 | + nodeValue: string |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +export function getNextCursorPosition( |
| 10 | + node: Node, |
| 11 | + offset: number, |
| 12 | + direction: -1 | 1, |
| 13 | + inputType?: string, |
| 14 | +): |
| 15 | + | { |
| 16 | + node: Node |
| 17 | + offset: number |
| 18 | + } |
| 19 | + | undefined { |
| 20 | + // The behavior at text node zero offset is inconsistent. |
| 21 | + // When walking backwards: |
| 22 | + // Firefox always moves to zero offset and jumps over last offset. |
| 23 | + // Chrome jumps over zero offset per default but over last offset when Shift is pressed. |
| 24 | + // The cursor always moves to zero offset if the focus area (contenteditable or body) ends there. |
| 25 | + // When walking foward both ignore zero offset. |
| 26 | + // When walking over input elements the cursor moves before or after that element. |
| 27 | + // When walking over line breaks the cursor moves inside any following text node. |
| 28 | + |
| 29 | + if ( |
| 30 | + isTextNode(node) && |
| 31 | + offset + direction >= 0 && |
| 32 | + offset + direction <= node.nodeValue.length |
| 33 | + ) { |
| 34 | + return {node, offset: offset + direction} |
| 35 | + } |
| 36 | + const nextNode = getNextCharacterContentNode(node, offset, direction) |
| 37 | + if (nextNode) { |
| 38 | + if (isTextNode(nextNode)) { |
| 39 | + return { |
| 40 | + node: nextNode, |
| 41 | + offset: |
| 42 | + direction > 0 |
| 43 | + ? Math.min(1, nextNode.nodeValue.length) |
| 44 | + : Math.max(nextNode.nodeValue.length - 1, 0), |
| 45 | + } |
| 46 | + } else if (isElementType(nextNode, 'br')) { |
| 47 | + const nextPlusOne = getNextCharacterContentNode( |
| 48 | + nextNode, |
| 49 | + undefined, |
| 50 | + direction, |
| 51 | + ) |
| 52 | + if (!nextPlusOne) { |
| 53 | + // The behavior when there is no possible cursor position beyond the line break is inconsistent. |
| 54 | + // In Chrome outside of contenteditable moving before a leading line break is possible. |
| 55 | + // A leading line break can still be removed per deleteContentBackward. |
| 56 | + // A trailing line break on the other hand is not removed by deleteContentForward. |
| 57 | + if (direction < 0 && inputType === 'deleteContentBackward') { |
| 58 | + return { |
| 59 | + node: nextNode.parentNode as Node, |
| 60 | + offset: getOffset(nextNode), |
| 61 | + } |
| 62 | + } |
| 63 | + return undefined |
| 64 | + } else if (isTextNode(nextPlusOne)) { |
| 65 | + return { |
| 66 | + node: nextPlusOne, |
| 67 | + offset: direction > 0 ? 0 : nextPlusOne.nodeValue.length, |
| 68 | + } |
| 69 | + } else if (direction < 0 && isElementType(nextPlusOne, 'br')) { |
| 70 | + return { |
| 71 | + node: nextNode.parentNode as Node, |
| 72 | + offset: getOffset(nextNode), |
| 73 | + } |
| 74 | + } else { |
| 75 | + return { |
| 76 | + node: nextPlusOne.parentNode as Node, |
| 77 | + offset: getOffset(nextPlusOne) + (direction > 0 ? 0 : 1), |
| 78 | + } |
| 79 | + } |
| 80 | + } else { |
| 81 | + return { |
| 82 | + node: nextNode.parentNode as Node, |
| 83 | + offset: getOffset(nextNode) + (direction > 0 ? 1 : 0), |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function getNextCharacterContentNode( |
| 90 | + node: Node, |
| 91 | + offset: number | undefined, |
| 92 | + direction: -1 | 1, |
| 93 | +) { |
| 94 | + const nextOffset = Number(offset) + (direction < 0 ? -1 : 0) |
| 95 | + if ( |
| 96 | + offset !== undefined && |
| 97 | + isElement(node) && |
| 98 | + nextOffset >= 0 && |
| 99 | + nextOffset < node.children.length |
| 100 | + ) { |
| 101 | + node = node.children[nextOffset] |
| 102 | + } |
| 103 | + return walkNodes( |
| 104 | + node, |
| 105 | + direction === 1 ? 'next' : 'previous', |
| 106 | + isTreatedAsCharacterContent, |
| 107 | + ) |
| 108 | +} |
| 109 | + |
| 110 | +function isTreatedAsCharacterContent(node: Node): node is Text | HTMLElement { |
| 111 | + if (isTextNode(node)) { |
| 112 | + return true |
| 113 | + } |
| 114 | + if (isElement(node)) { |
| 115 | + if (isElementType(node, ['input', 'textarea'])) { |
| 116 | + return (node as HTMLInputElement).type !== 'hidden' |
| 117 | + } else if (isElementType(node, 'br')) { |
| 118 | + return true |
| 119 | + } |
| 120 | + } |
| 121 | + return false |
| 122 | +} |
| 123 | + |
| 124 | +function getOffset(node: Node) { |
| 125 | + let i = 0 |
| 126 | + while (node.previousSibling) { |
| 127 | + i++ |
| 128 | + node = node.previousSibling |
| 129 | + } |
| 130 | + return i |
| 131 | +} |
| 132 | + |
| 133 | +function isElement(node: Node): node is Element { |
| 134 | + return node.nodeType === 1 |
| 135 | +} |
| 136 | + |
| 137 | +function isTextNode(node: Node): node is Text { |
| 138 | + return node.nodeType === 3 |
| 139 | +} |
| 140 | + |
| 141 | +function walkNodes<T extends Node>( |
| 142 | + node: Node, |
| 143 | + direction: 'previous' | 'next', |
| 144 | + callback: (node: Node) => node is T, |
| 145 | +) { |
| 146 | + for (;;) { |
| 147 | + const sibling = node[`${direction}Sibling`] |
| 148 | + if (sibling) { |
| 149 | + node = getDescendant(sibling, direction === 'next' ? 'first' : 'last') |
| 150 | + if (callback(node)) { |
| 151 | + return node |
| 152 | + } |
| 153 | + } else if ( |
| 154 | + node.parentNode && |
| 155 | + (!isElement(node.parentNode) || |
| 156 | + (!isContentEditable(node.parentNode) && |
| 157 | + node.parentNode !== node.ownerDocument?.body)) |
| 158 | + ) { |
| 159 | + node = node.parentNode |
| 160 | + } else { |
| 161 | + break |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +function getDescendant(node: Node, direction: 'first' | 'last') { |
| 167 | + while (node.hasChildNodes()) { |
| 168 | + node = node[`${direction}Child`] as ChildNode |
| 169 | + } |
| 170 | + return node |
| 171 | +} |
0 commit comments