Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/web/utils/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ import {isChildOfMultilineMarkdownElement} from './blockUtils';
import {findHTMLElementInTree, getTreeNodeByIndex} from './treeUtils';
import type {TreeNode} from './treeUtils';

function getMaxRangeOffset(node: ChildNode): number {
return node.nodeType === Node.TEXT_NODE ? (node.textContent ?? '').length : node.childNodes.length;
}

/**
* Range offsets are applied to the real DOM node, not the parsed markdown tree.
* When the tree is temporarily stale, clamp to the DOM node's bounds so cursor
* restoration lands at the nearest valid position instead of throwing.
*/
function getClampedRangeOffset(node: ChildNode, offset: number): number {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have jsdoc here with some context.

return Math.max(0, Math.min(offset, getMaxRangeOffset(node)));
}

function setCursorPosition(target: MarkdownTextInputElement, startIndex: number, endIndex: number | null = null, shouldScrollIntoView = false) {
// We don't want to move the cursor if the target is not focused
if (!target.tree || target !== document.activeElement) {
Expand All @@ -29,14 +42,16 @@ function setCursorPosition(target: MarkdownTextInputElement, startIndex: number,
range.setStartBefore(startTreeNode.element);
} else {
const startElement = startTreeNode.element;
range.setStart((startElement.childNodes[0] || startElement) as ChildNode, start - startTreeNode.start);
const startNode = (startElement.childNodes[0] || startElement) as ChildNode;
range.setStart(startNode, getClampedRangeOffset(startNode, start - startTreeNode.start));
}

if (endTreeNode.type === 'br') {
range.setEndBefore(endTreeNode.element);
} else {
const endElement = endTreeNode.element;
range.setEnd((endElement.childNodes[0] || endElement) as ChildNode, (end || start) - endTreeNode.start);
const endNode = (endElement.childNodes[0] || endElement) as ChildNode;
range.setEnd(endNode, getClampedRangeOffset(endNode, (end || start) - endTreeNode.start));
}

if (!end) {
Expand Down
Loading