MCPcopy Create free account
hub / github.com/MathMan05/Fermi / getTextNodeAtPosition

Function getTextNodeAtPosition

src/webpage/markdown.ts:1429–1519  ·  view source on GitHub ↗
(
	root: Node,
	index: number,
	txtLengthFunc = MarkDown.gatherBoxText.bind(MarkDown),
)

Source from the content-addressed store, hash-verified

1427 }
1428 } else {
1429 console.error(context);
1430 }
1431 return;
1432 }
1433 for (const node of children as Node[]) {
1434 if (selection.containsNode(node, false)) {
1435 if (node instanceof HTMLElement) {
1436 build += txtLengthFunc(node);
1437 } else {
1438 build += node.textContent;
1439 }
1440 } else if (selection.containsNode(node, true)) {
1441 if (node instanceof HTMLElement) {
1442 crawlForText(node);
1443 } else {
1444 build += node.textContent;
1445 }
1446 } else {
1447 //console.error(node, "This shouldn't happen");
1448 }
1449 }
1450 }
1451 crawlForText(context);
1452 if (baseString === "\n") {
1453 //build += baseString;
1454 }
1455 text = build;
1456 len += build.length;
1457 if (computedLength !== undefined) {
1458 len = computedLength;
1459 }
1460 len = Math.min(len, txtLengthFunc(context).length);
1461 len += offset;
1462
1463 return function restore(_backspace = false) {
1464 if (!selection) return;
1465 const pos = getTextNodeAtPosition(context, len, txtLengthFunc);
1466 if (
1467 pos.node instanceof Text &&
1468 pos.node.textContent === "\n" &&
1469 pos.node.nextSibling &&
1470 isFirefox
1471 ) {
1472 if (pos.node.nextSibling instanceof Text && pos.node.nextSibling.textContent === "\n") {
1473 pos.position = 1;
1474 } else {
1475 pos.node = pos.node.nextSibling;
1476 pos.position = 0;
1477 }
1478 }
1479 selection.removeAllRanges();
1480 const range = new Range();
1481 range.setStart(pos.node, pos.position);
1482 selection.addRange(range);
1483 };
1484 } catch {
1485 return undefined;
1486 }

Callers 2

saveCaretPositionFunction · 0.85
MDSearchOptionsMethod · 0.85

Calls 1

bindMethod · 0.45

Tested by

no test coverage detected