2019-05-13 15:42:00 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
2019-05-22 14:16:32 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2019-05-13 15:42:00 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-06-21 14:37:29 +00:00
|
|
|
import {CARET_NODE_CHAR, isCaretNode} from "./render";
|
2019-06-20 12:44:18 +00:00
|
|
|
|
2019-05-29 12:27:36 +00:00
|
|
|
export function walkDOMDepthFirst(rootNode, enterNodeCallback, leaveNodeCallback) {
|
|
|
|
let node = rootNode.firstChild;
|
|
|
|
while (node && node !== rootNode) {
|
|
|
|
const shouldDecend = enterNodeCallback(node);
|
|
|
|
if (shouldDecend && node.firstChild) {
|
2019-05-13 15:42:00 +00:00
|
|
|
node = node.firstChild;
|
|
|
|
} else if (node.nextSibling) {
|
|
|
|
node = node.nextSibling;
|
|
|
|
} else {
|
2019-05-29 12:27:36 +00:00
|
|
|
while (!node.nextSibling && node !== rootNode) {
|
2019-05-13 15:42:00 +00:00
|
|
|
node = node.parentElement;
|
2019-05-29 12:27:36 +00:00
|
|
|
if (node !== rootNode) {
|
2019-05-13 15:42:00 +00:00
|
|
|
leaveNodeCallback(node);
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 12:27:36 +00:00
|
|
|
if (node !== rootNode) {
|
2019-05-13 15:42:00 +00:00
|
|
|
node = node.nextSibling;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getCaretOffsetAndText(editor, sel) {
|
2019-06-20 12:44:18 +00:00
|
|
|
let {focusNode, focusOffset} = sel;
|
|
|
|
// sometimes focusNode is an element, and then focusOffset means
|
|
|
|
// the index of a child element ... - 1 🤷
|
2019-05-13 15:42:00 +00:00
|
|
|
if (focusNode.nodeType === Node.ELEMENT_NODE && focusOffset !== 0) {
|
|
|
|
focusNode = focusNode.childNodes[focusOffset - 1];
|
2019-06-20 12:44:18 +00:00
|
|
|
focusOffset = focusNode.textContent.length;
|
|
|
|
}
|
|
|
|
const {text, focusNodeOffset} = getTextAndFocusNodeOffset(editor, focusNode, focusOffset);
|
|
|
|
const caret = getCaret(focusNode, focusNodeOffset, focusOffset);
|
|
|
|
return {caret, text};
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the caret position details, ignoring and adjusting to
|
|
|
|
// the ZWS if you're typing in a caret node
|
|
|
|
function getCaret(focusNode, focusNodeOffset, focusOffset) {
|
|
|
|
let atNodeEnd = focusOffset === focusNode.textContent.length;
|
|
|
|
if (focusNode.nodeType === Node.TEXT_NODE && isCaretNode(focusNode.parentElement)) {
|
2019-06-21 14:37:29 +00:00
|
|
|
const zwsIdx = focusNode.nodeValue.indexOf(CARET_NODE_CHAR);
|
2019-06-20 12:44:18 +00:00
|
|
|
if (zwsIdx !== -1 && zwsIdx < focusOffset) {
|
|
|
|
focusOffset -= 1;
|
|
|
|
}
|
|
|
|
// 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 15:42:00 +00:00
|
|
|
}
|
2019-06-20 12:44:18 +00:00
|
|
|
return {offset: focusNodeOffset + focusOffset, atNodeEnd};
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets the text of the editor as a string,
|
|
|
|
// and the offset in characters where the focusNode starts in that string
|
|
|
|
// all ZWS from caret nodes are filtered out
|
|
|
|
function getTextAndFocusNodeOffset(editor, focusNode, focusOffset) {
|
|
|
|
let focusNodeOffset = 0;
|
|
|
|
let foundCaret = false;
|
|
|
|
let text = "";
|
2019-05-13 15:42:00 +00:00
|
|
|
|
|
|
|
function enterNodeCallback(node) {
|
|
|
|
if (!foundCaret) {
|
|
|
|
if (node === focusNode) {
|
|
|
|
foundCaret = true;
|
|
|
|
}
|
|
|
|
}
|
2019-06-20 12:44:18 +00:00
|
|
|
const nodeText = node.nodeType === Node.TEXT_NODE && getTextNodeValue(node);
|
2019-05-13 15:42:00 +00:00
|
|
|
if (nodeText) {
|
|
|
|
if (!foundCaret) {
|
2019-06-20 12:44:18 +00:00
|
|
|
focusNodeOffset += nodeText.length;
|
2019-05-13 15:42:00 +00:00
|
|
|
}
|
|
|
|
text += nodeText;
|
|
|
|
}
|
2019-05-29 12:27:36 +00:00
|
|
|
return true;
|
2019-05-13 15:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function leaveNodeCallback(node) {
|
|
|
|
// 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
|
|
|
|
if (node.tagName === "DIV" && node.nextSibling && node.nextSibling.tagName === "DIV") {
|
|
|
|
text += "\n";
|
|
|
|
if (!foundCaret) {
|
2019-06-20 12:44:18 +00:00
|
|
|
focusNodeOffset += 1;
|
2019-05-13 15:42:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walkDOMDepthFirst(editor, enterNodeCallback, leaveNodeCallback);
|
|
|
|
|
2019-06-20 12:44:18 +00:00
|
|
|
return {text, focusNodeOffset};
|
|
|
|
}
|
|
|
|
|
|
|
|
// get text value of text node, ignoring ZWS if it's a caret node
|
|
|
|
function getTextNodeValue(node) {
|
|
|
|
const nodeText = node.nodeValue;
|
|
|
|
// 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 14:37:29 +00:00
|
|
|
return nodeText.replace(CARET_NODE_CHAR, "");
|
2019-06-20 12:44:18 +00:00
|
|
|
} else {
|
|
|
|
// only contains ZWS, which is ignored, so return emtpy string
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return nodeText;
|
|
|
|
}
|
2019-05-13 15:42:00 +00:00
|
|
|
}
|