()
| 99 | input.selectionDirection, |
| 100 | ] |
| 101 | function onDocumentSelectionChange() { |
| 102 | if (document.activeElement !== input) { |
| 103 | setMirrorSelectionStart(null) |
| 104 | setMirrorSelectionEnd(null) |
| 105 | return |
| 106 | } |
| 107 | |
| 108 | // Aliases |
| 109 | const _s = input.selectionStart |
| 110 | const _e = input.selectionEnd |
| 111 | const _dir = input.selectionDirection |
| 112 | const _ml = input.maxLength |
| 113 | const _val = input.value |
| 114 | const _prev = inputMetadataRef.current.prev |
| 115 | |
| 116 | // Algorithm |
| 117 | let start = -1 |
| 118 | let end = -1 |
| 119 | let direction: 'forward' | 'backward' | 'none' = undefined |
| 120 | if (_val.length !== 0 && _s !== null && _e !== null) { |
| 121 | const isSingleCaret = _s === _e |
| 122 | const isInsertMode = _s === _val.length && _val.length < _ml |
| 123 | |
| 124 | if (isSingleCaret && !isInsertMode) { |
| 125 | const c = _s |
| 126 | if (c === 0) { |
| 127 | start = 0 |
| 128 | end = 1 |
| 129 | direction = 'forward' |
| 130 | } else if (c === _ml) { |
| 131 | start = c - 1 |
| 132 | end = c |
| 133 | direction = 'backward' |
| 134 | } else if (_ml > 1 && _val.length > 1) { |
| 135 | let offset = 0 |
| 136 | if (_prev[0] !== null && _prev[1] !== null) { |
| 137 | direction = c < _prev[1] ? 'backward' : 'forward' |
| 138 | const wasPreviouslyInserting = |
| 139 | _prev[0] === _prev[1] && _prev[0] < _ml |
| 140 | if (direction === 'backward' && !wasPreviouslyInserting) { |
| 141 | offset = -1 |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | start = offset + c |
| 146 | end = offset + c + 1 |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | if (start !== -1 && end !== -1 && start !== end) { |
| 151 | inputRef.current.setSelectionRange(start, end, direction) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Finally, update the state |
| 156 | const s = start !== -1 ? start : _s |
| 157 | const e = end !== -1 ? end : _e |
| 158 | const dir = direction ?? _dir |
no outgoing calls
no test coverage detected
searching dependent graphs…