Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 459x 459x 459x 459x 126x 16x 16x 16x 16x 16x 16x 16x 126x 311x 311x 311x 126x 126x 221x 221x 152x 152x 210x 5x 5x 210x 64x 64x 126x 126x 206x 206x 206x 206x 219x 219x 67x 10x 219x 5x 5x 219x 219x 219x 217x 217x 219x 206x 206x 126x 126x 353x 353x 353x 353x 353x 353x 353x 353x 353x 353x 353x 353x 635x 635x 635x 353x 147x 353x 206x 206x 206x 2x 2x 2x 2x 2x 2x 2x 2x 2x 206x 353x 353x 353x 282x 635x 39x 39x 39x 635x 2x 2x 2x 635x 241x 241x 373x 282x 282x 282x 96x 282x 282x 282x 282x 80x 282x 635x 353x 353x 353x 353x 126x 126x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 159x 521x 521x 521x 216x 216x 216x 216x 216x 216x 216x 29x 29x 29x 187x 187x 187x 187x 187x 305x 521x 6x 6x 1x 1x 1x 1x 1x 1x 2x 2x 2x 299x 521x 299x 299x 507x 362x 362x 362x 146x 362x 362x 362x 362x 358x 362x 521x 159x 159x 159x 129x 152x 127x 127x 127x 127x 3x 3x 3x 3x 3x 3x 124x 124x 124x 127x 2x 2x 126x 126x 126x 126x 126x 126x 126x 126x 142x 142x 126x 126x 126x 351x 351x 343x 343x 343x 343x 343x 343x 343x 126x 126x 126x 11x 11x 10x 10x 10x 126x 126x 126x 37x 37x 36x 36x 36x 36x 36x 36x 36x 36x 126x 126x 126x 61x 61x 61x 61x 61x 61x 61x 60x 60x 60x 60x 60x 60x 60x 60x | // ---------------------------------------------------------------------------
// Cursor management utilities for contenteditable.
//
// The DOM produced by our renderer is a flat list of <span> tokens (newlines
// are "\n" characters inside <span class="tok-newline">) plus an optional
// trailing <br data-sentinel> that exists only so Firefox renders an empty
// final line. Browser-inserted structures (e.g. from yank/paste) are also
// possible: a bare <br> represents a real newline, and a <div> or <p> at the
// editable's root level represents a line block. Both are translated to "\n"
// when computing plain-text offsets so the rebuild after handleInput sees the
// correct value.
// ---------------------------------------------------------------------------
/** Gets the Selection object, handling shadow DOM. */
export function getSelectionFromRoot(element: HTMLElement): Selection | null {
const root = element.getRootNode() as ShadowRoot;
if ((root as any).getSelection) {
return (root as any).getSelection();
}
return window.getSelection();
}
function isSentinelBr(node: Node): boolean {
return (
node.nodeName === 'BR' &&
typeof (node as Element).hasAttribute === 'function' &&
(node as Element).hasAttribute('data-sentinel')
);
}
function isBlockElement(node: Node): boolean {
return node.nodeName === 'DIV' || node.nodeName === 'P';
}
/** Plain-text contribution of a node and its descendants. */
function textOfNode(node: Node): string {
if (node.nodeType === Node.TEXT_NODE) {
return node.textContent || '';
}
if (node.nodeName === 'BR') {
return isSentinelBr(node) ? '' : '\n';
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return '';
}
return textOfChildren(node);
}
/** Plain-text contribution of an element's children, with block-prefix \n. */
function textOfChildren(parent: Node): string {
let text = '';
let hasContent = false;
for (const child of Array.from(parent.childNodes)) {
if (
child.nodeType === Node.ELEMENT_NODE &&
isBlockElement(child) &&
hasContent
) {
text += '\n';
}
const piece = textOfNode(child);
text += piece;
if (piece.length > 0) {
hasContent = true;
}
}
return text;
}
/** Converts a DOM selection position (container + offset) to a plain-text offset. */
function domPositionToTextOffset(
root: Node,
targetContainer: Node,
targetOffset: number
): number {
// Build the text from `root` up to (target, targetOffset) and return its
// length. This mirrors textOfChildren so block-prefix newlines and BR
// newlines are accounted for the same way when reading and writing.
let text = '';
let stopped = false;
const walk = (node: Node): void => {
if (stopped) return;
if (node === targetContainer) {
if (node.nodeType === Node.TEXT_NODE) {
text += (node.textContent || '').substring(0, targetOffset);
} else if (node.nodeType === Node.ELEMENT_NODE) {
const children = Array.from(node.childNodes);
let hasContent = false;
for (let i = 0; i < Math.min(targetOffset, children.length); i++) {
const child = children[i];
if (
child.nodeType === Node.ELEMENT_NODE &&
isBlockElement(child) &&
hasContent
) {
text += '\n';
}
const piece = textOfNode(child);
text += piece;
if (piece.length > 0) hasContent = true;
}
}
stopped = true;
return;
}
if (node.nodeType === Node.TEXT_NODE) {
text += node.textContent || '';
return;
}
if (node.nodeName === 'BR') {
if (!isSentinelBr(node)) text += '\n';
return;
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
let hasContent = false;
for (const child of Array.from(node.childNodes)) {
if (stopped) break;
if (
child.nodeType === Node.ELEMENT_NODE &&
isBlockElement(child) &&
hasContent
) {
text += '\n';
}
const before = text.length;
walk(child);
if (stopped) break;
if (text.length > before) hasContent = true;
}
};
walk(root);
return text.length;
}
/** Converts a plain-text offset to a DOM position (node + offset). */
function textOffsetToDomPosition(
root: Node,
targetOffset: number
): { node: Node; offset: number } | null {
let remaining = targetOffset;
// Track the last text node passed through so we can fall back to its end
// when the offset sits past all text content.
let lastTextNode: Node | null = null;
let lastTextOffset = 0;
let result: { node: Node; offset: number } | null = null;
const walk = (node: Node, parent: Node | null): void => {
if (result !== null) return;
if (node.nodeType === Node.TEXT_NODE) {
const len = node.textContent.length;
// Use strict "<" so positions at the boundary between two text nodes
// resolve to the START of the next node rather than the END of the
// previous one. Firefox doesn't paint the caret reliably when it lands
// at the end of an inline span whose text is just "\n", but happily
// renders it at the start of the following node.
if (remaining < len) {
result = { node, offset: remaining };
return;
}
lastTextNode = node;
lastTextOffset = len;
remaining -= len;
return;
}
if (node.nodeName === 'BR') {
if (isSentinelBr(node)) return;
if (remaining === 0 && parent) {
const idx = Array.from(parent.childNodes).indexOf(node as ChildNode);
if (idx >= 0) {
result = { node: parent, offset: idx };
return;
}
}
remaining -= 1;
return;
}
if (node.nodeType !== Node.ELEMENT_NODE) {
return;
}
let hasContent = false;
for (const child of Array.from(node.childNodes)) {
if (result !== null) return;
if (
child.nodeType === Node.ELEMENT_NODE &&
isBlockElement(child) &&
hasContent
) {
if (remaining === 0) {
const idx = Array.from(node.childNodes).indexOf(child as ChildNode);
if (idx >= 0) {
result = { node, offset: idx };
return;
}
}
remaining -= 1;
}
const before = remaining;
walk(child, node);
if (result !== null) return;
if (remaining < before) hasContent = true;
}
};
walk(root, null);
if (result) return result;
if (remaining === 0) {
// Past all content. If the editor ends in a <br>, position relative to it
// so the caret renders on the empty trailing line in Firefox.
const lastChild = root.lastChild;
if (lastChild && lastChild.nodeName === 'BR') {
// Sentinel: render before it. Non-sentinel (transient post-yank state):
// render after it so the caret sits on the empty line the BR produces.
const offset = isSentinelBr(lastChild)
? root.childNodes.length - 1
: root.childNodes.length;
return { node: root, offset };
}
if (lastTextNode) {
return { node: lastTextNode, offset: lastTextOffset };
}
}
return null;
}
/**
* Extracts plain text from the contenteditable DOM. Translates non-sentinel
* <br> elements to "\n" and inserts a "\n" before block-level (DIV/P) children
* that follow other content, so yank/paste-inserted DOM structures round-trip
* through handleInput correctly.
*/
export function getTextFromEditableDiv(element: HTMLElement): string {
return textOfChildren(element);
}
/** Gets the caret (selection start) as a plain-text offset. */
export function getCaretOffset(element: HTMLElement): number {
const selection = getSelectionFromRoot(element);
if (!selection || selection.rangeCount === 0) return 0;
const range = selection.getRangeAt(0);
return domPositionToTextOffset(
element,
range.startContainer,
range.startOffset
);
}
/** Gets the selection end as a plain-text offset. */
export function getCaretEndOffset(element: HTMLElement): number {
const selection = getSelectionFromRoot(element);
if (!selection || selection.rangeCount === 0) return 0;
const range = selection.getRangeAt(0);
return domPositionToTextOffset(element, range.endContainer, range.endOffset);
}
/** Sets the caret to a plain-text offset. */
export function setCaretOffset(element: HTMLElement, offset: number): void {
const pos = textOffsetToDomPosition(element, offset);
if (!pos) return;
const selection = getSelectionFromRoot(element);
if (!selection) return;
const range = document.createRange();
range.setStart(pos.node, pos.offset);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
/** Sets a selection range by plain-text offsets. */
export function setCaretRange(
element: HTMLElement,
start: number,
end: number
): void {
const startPos = textOffsetToDomPosition(element, start);
const endPos = textOffsetToDomPosition(element, end);
if (!startPos || !endPos) return;
const selection = getSelectionFromRoot(element);
if (!selection) return;
const range = document.createRange();
range.setStart(startPos.node, startPos.offset);
range.setEnd(endPos.node, endPos.offset);
selection.removeAllRanges();
selection.addRange(range);
}
|