2019-05-13 18:42:00 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2019-2024 New Vector Ltd.
|
2019-05-22 17:16:32 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-13 18:42:00 +03:00
|
|
|
|
2024-09-09 16:57:16 +03:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-05-13 18:42:00 +03:00
|
|
|
*/
|
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
import { CARET_NODE_CHAR, isCaretNode } from "./render";
|
2019-09-03 16:58:50 +03:00
|
|
|
import DocumentOffset from "./offset";
|
2022-05-09 15:39:32 +03:00
|
|
|
import EditorModel from "./model";
|
|
|
|
import Range from "./range";
|
2019-06-20 15:44:18 +03:00
|
|
|
|
2020-07-15 11:45:45 +03:00
|
|
|
type Predicate = (node: Node) => boolean;
|
|
|
|
type Callback = (node: Node) => void;
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
export function walkDOMDepthFirst(rootNode: Node, enterNodeCallback: Predicate, leaveNodeCallback: Callback): void {
|
2019-05-29 15:27:36 +03:00
|
|
|
let node = rootNode.firstChild;
|
|
|
|
while (node && node !== rootNode) {
|
2019-10-13 14:10:11 +03:00
|
|
|
const shouldDescend = enterNodeCallback(node);
|
|
|
|
if (shouldDescend && node.firstChild) {
|
2019-05-13 18:42:00 +03:00
|
|
|
node = node.firstChild;
|
|
|
|
} else if (node.nextSibling) {
|
|
|
|
node = node.nextSibling;
|
|
|
|
} else {
|
2023-02-16 20:21:44 +03:00
|
|
|
while (node && !node.nextSibling && node !== rootNode) {
|
2019-05-13 18:42:00 +03:00
|
|
|
node = node.parentElement;
|
2023-03-23 16:35:55 +03:00
|
|
|
if (node && node !== rootNode) {
|
2019-05-13 18:42:00 +03:00
|
|
|
leaveNodeCallback(node);
|
|
|
|
}
|
|
|
|
}
|
2023-03-23 16:35:55 +03:00
|
|
|
if (node && node !== rootNode) {
|
2019-05-13 18:42:00 +03:00
|
|
|
node = node.nextSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
export function getCaretOffsetAndText(
|
|
|
|
editor: HTMLDivElement,
|
|
|
|
sel: Selection,
|
|
|
|
): {
|
|
|
|
caret: DocumentOffset;
|
|
|
|
text: string;
|
|
|
|
} {
|
2021-06-29 15:11:58 +03:00
|
|
|
const { offset, text } = getSelectionOffsetAndText(editor, sel.focusNode, sel.focusOffset);
|
|
|
|
return { caret: offset, text };
|
2019-09-03 16:58:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
function tryReduceSelectionToTextNode(
|
2023-03-23 16:35:55 +03:00
|
|
|
selectionNode: Node | null,
|
2023-01-12 16:25:14 +03:00
|
|
|
selectionOffset: number,
|
|
|
|
): {
|
2023-03-23 16:35:55 +03:00
|
|
|
node: Node | null;
|
2023-01-12 16:25:14 +03:00
|
|
|
characterOffset: number;
|
|
|
|
} {
|
2019-09-04 16:58:59 +03:00
|
|
|
// if selectionNode is an element, the selected location comes after the selectionOffset-th child node,
|
|
|
|
// which can point past any childNode, in which case, the end of selectionNode is selected.
|
|
|
|
// we try to simplify this to point at a text node with the offset being
|
|
|
|
// a character offset within the text node
|
|
|
|
// Also see https://developer.mozilla.org/en-US/docs/Web/API/Selection
|
|
|
|
while (selectionNode && selectionNode.nodeType === Node.ELEMENT_NODE) {
|
|
|
|
const childNodeCount = selectionNode.childNodes.length;
|
|
|
|
if (childNodeCount) {
|
|
|
|
if (selectionOffset >= childNodeCount) {
|
|
|
|
selectionNode = selectionNode.lastChild;
|
2023-03-23 16:35:55 +03:00
|
|
|
if (selectionNode?.nodeType === Node.TEXT_NODE) {
|
|
|
|
selectionOffset = selectionNode.textContent?.length || 0;
|
2019-09-04 16:58:59 +03:00
|
|
|
} else {
|
|
|
|
// this will select the last child node in the next iteration
|
|
|
|
selectionOffset = Number.MAX_SAFE_INTEGER;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
selectionNode = selectionNode.childNodes[selectionOffset];
|
|
|
|
// this will select the first child node in the next iteration
|
|
|
|
selectionOffset = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// here node won't be a text node,
|
|
|
|
// but characterOffset should be 0,
|
|
|
|
// this happens under some circumstances
|
|
|
|
// when the editor is empty.
|
|
|
|
// In this case characterOffset=0 is the right thing to do
|
|
|
|
break;
|
|
|
|
}
|
2019-06-20 15:44:18 +03:00
|
|
|
}
|
2019-09-04 16:58:59 +03:00
|
|
|
return {
|
|
|
|
node: selectionNode,
|
|
|
|
characterOffset: selectionOffset,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-12 16:25:14 +03:00
|
|
|
function getSelectionOffsetAndText(
|
|
|
|
editor: HTMLDivElement,
|
2023-03-23 16:35:55 +03:00
|
|
|
selectionNode: Node | null,
|
2023-01-12 16:25:14 +03:00
|
|
|
selectionOffset: number,
|
|
|
|
): {
|
|
|
|
offset: DocumentOffset;
|
|
|
|
text: string;
|
|
|
|
} {
|
2021-06-29 15:11:58 +03:00
|
|
|
const { node, characterOffset } = tryReduceSelectionToTextNode(selectionNode, selectionOffset);
|
|
|
|
const { text, offsetToNode } = getTextAndOffsetToNode(editor, node);
|
2019-09-04 16:58:59 +03:00
|
|
|
const offset = getCaret(node, offsetToNode, characterOffset);
|
2021-06-29 15:11:58 +03:00
|
|
|
return { offset, text };
|
2019-06-20 15:44:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// gets the caret position details, ignoring and adjusting to
|
|
|
|
// the ZWS if you're typing in a caret node
|
2023-03-23 16:35:55 +03:00
|
|
|
function getCaret(node: Node | null, offsetToNode: number, offsetWithinNode: number): DocumentOffset {
|
2019-10-10 17:39:41 +03:00
|
|
|
// if no node is selected, return an offset at the start
|
|
|
|
if (!node) {
|
|
|
|
return new DocumentOffset(0, false);
|
|
|
|
}
|
2023-03-23 16:35:55 +03:00
|
|
|
let atNodeEnd = offsetWithinNode === node.textContent?.length;
|
2019-09-03 16:58:50 +03:00
|
|
|
if (node.nodeType === Node.TEXT_NODE && isCaretNode(node.parentElement)) {
|
2023-03-23 16:35:55 +03:00
|
|
|
const nodeValue = node.nodeValue || "";
|
|
|
|
const zwsIdx = nodeValue.indexOf(CARET_NODE_CHAR);
|
2019-09-03 16:58:50 +03:00
|
|
|
if (zwsIdx !== -1 && zwsIdx < offsetWithinNode) {
|
|
|
|
offsetWithinNode -= 1;
|
2019-06-20 15:44:18 +03:00
|
|
|
}
|
|
|
|
// if typing in a caret node, you're either typing before or after the ZWS.
|
|
|
|
// In both cases, you should be considered at node end because the ZWS is
|
|
|
|
// not included in the text here, and once the model is updated and rerendered,
|
|
|
|
// that caret node will be removed.
|
|
|
|
atNodeEnd = true;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
2019-09-03 16:58:50 +03:00
|
|
|
return new DocumentOffset(offsetToNode + offsetWithinNode, atNodeEnd);
|
2019-06-20 15:44:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// gets the text of the editor as a string,
|
2019-09-03 16:58:50 +03:00
|
|
|
// and the offset in characters where the selectionNode starts in that string
|
2019-06-20 15:44:18 +03:00
|
|
|
// all ZWS from caret nodes are filtered out
|
2023-03-23 16:35:55 +03:00
|
|
|
function getTextAndOffsetToNode(
|
|
|
|
editor: HTMLDivElement,
|
|
|
|
selectionNode: Node | null,
|
|
|
|
): { offsetToNode: number; text: string } {
|
2019-09-03 16:58:50 +03:00
|
|
|
let offsetToNode = 0;
|
2019-09-04 16:58:13 +03:00
|
|
|
let foundNode = false;
|
2019-06-20 15:44:18 +03:00
|
|
|
let text = "";
|
2019-05-13 18:42:00 +03:00
|
|
|
|
2023-05-16 16:25:43 +03:00
|
|
|
function enterNodeCallback(node: Node): boolean {
|
2019-09-04 16:58:13 +03:00
|
|
|
if (!foundNode) {
|
2019-09-03 16:58:50 +03:00
|
|
|
if (node === selectionNode) {
|
2019-09-04 16:58:13 +03:00
|
|
|
foundNode = true;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 13:50:23 +03:00
|
|
|
// usually newlines are entered as new DIV elements,
|
|
|
|
// but for example while pasting in some browsers, they are still
|
|
|
|
// converted to BRs, so also take these into account when they
|
|
|
|
// are not the last element in the DIV.
|
2023-05-16 16:25:43 +03:00
|
|
|
if (node instanceof HTMLElement && node.tagName === "BR" && node.nextSibling) {
|
2019-09-04 16:58:13 +03:00
|
|
|
if (!foundNode) {
|
|
|
|
offsetToNode += 1;
|
|
|
|
}
|
2019-08-29 13:50:23 +03:00
|
|
|
text += "\n";
|
|
|
|
}
|
2019-06-20 15:44:18 +03:00
|
|
|
const nodeText = node.nodeType === Node.TEXT_NODE && getTextNodeValue(node);
|
2019-05-13 18:42:00 +03:00
|
|
|
if (nodeText) {
|
2019-09-04 16:58:13 +03:00
|
|
|
if (!foundNode) {
|
2019-09-03 16:58:50 +03:00
|
|
|
offsetToNode += nodeText.length;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
|
|
|
text += nodeText;
|
|
|
|
}
|
2019-05-29 15:27:36 +03:00
|
|
|
return true;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
|
|
|
|
2023-05-16 16:25:43 +03:00
|
|
|
function leaveNodeCallback(node: Node): void {
|
2019-05-13 18:42:00 +03:00
|
|
|
// if this is not the last DIV (which are only used as line containers atm)
|
|
|
|
// we don't just check if there is a nextSibling because sometimes the caret ends up
|
|
|
|
// after the last DIV and it creates a newline if you type then,
|
|
|
|
// whereas you just want it to be appended to the current line
|
2023-05-16 16:25:43 +03:00
|
|
|
if (
|
|
|
|
node instanceof HTMLElement &&
|
|
|
|
node.tagName === "DIV" &&
|
|
|
|
(<HTMLElement>node.nextSibling)?.tagName === "DIV"
|
|
|
|
) {
|
2019-05-13 18:42:00 +03:00
|
|
|
text += "\n";
|
2019-09-04 16:58:13 +03:00
|
|
|
if (!foundNode) {
|
2019-09-03 16:58:50 +03:00
|
|
|
offsetToNode += 1;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walkDOMDepthFirst(editor, enterNodeCallback, leaveNodeCallback);
|
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
return { text, offsetToNode };
|
2019-06-20 15:44:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// get text value of text node, ignoring ZWS if it's a caret node
|
2022-05-09 15:39:32 +03:00
|
|
|
function getTextNodeValue(node: Node): string {
|
2019-06-20 15:44:18 +03:00
|
|
|
const nodeText = node.nodeValue;
|
2023-02-16 12:38:44 +03:00
|
|
|
if (!nodeText) return "";
|
|
|
|
|
2019-06-20 15:44:18 +03:00
|
|
|
// filter out ZWS for caret nodes
|
|
|
|
if (isCaretNode(node.parentElement)) {
|
|
|
|
// typed in the caret node, so there is now something more in it than the ZWS
|
|
|
|
// so filter out the ZWS, and take the typed text into account
|
|
|
|
if (nodeText.length !== 1) {
|
2019-06-21 17:37:29 +03:00
|
|
|
return nodeText.replace(CARET_NODE_CHAR, "");
|
2019-06-20 15:44:18 +03:00
|
|
|
} else {
|
2022-05-10 01:52:05 +03:00
|
|
|
// only contains ZWS, which is ignored, so return empty string
|
2019-06-20 15:44:18 +03:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 12:38:44 +03:00
|
|
|
|
|
|
|
return nodeText;
|
2019-05-13 18:42:00 +03:00
|
|
|
}
|
2019-09-04 13:37:27 +03:00
|
|
|
|
2022-05-09 15:39:32 +03:00
|
|
|
export function getRangeForSelection(editor: HTMLDivElement, model: EditorModel, selection: Selection): Range {
|
2019-09-04 13:37:27 +03:00
|
|
|
const focusOffset = getSelectionOffsetAndText(editor, selection.focusNode, selection.focusOffset).offset;
|
|
|
|
const anchorOffset = getSelectionOffsetAndText(editor, selection.anchorNode, selection.anchorOffset).offset;
|
|
|
|
const focusPosition = focusOffset.asPosition(model);
|
|
|
|
const anchorPosition = anchorOffset.asPosition(model);
|
|
|
|
return model.startRange(focusPosition, anchorPosition);
|
|
|
|
}
|