2019-05-06 19:21:28 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 New Vector Ltd
|
|
|
|
|
|
|
|
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-05-07 17:27:09 +03:00
|
|
|
export function getCaretOffset(editor) {
|
2019-05-06 19:21:28 +03:00
|
|
|
const sel = document.getSelection();
|
|
|
|
const atNodeEnd = sel.focusOffset === sel.focusNode.textContent.length;
|
2019-05-07 17:27:09 +03:00
|
|
|
let offset = sel.focusOffset;
|
2019-05-06 19:21:28 +03:00
|
|
|
let node = sel.focusNode;
|
|
|
|
|
|
|
|
// when deleting the last character of a node,
|
|
|
|
// the caret gets reported as being after the focusOffset-th node,
|
|
|
|
// with the focusNode being the editor
|
|
|
|
if (node === editor) {
|
2019-05-07 17:27:09 +03:00
|
|
|
let offset = 0;
|
2019-05-06 19:21:28 +03:00
|
|
|
for (let i = 0; i < sel.focusOffset; ++i) {
|
2019-05-08 12:12:47 +03:00
|
|
|
const node = editor.childNodes[i];
|
|
|
|
if (isVisibleNode(node)) {
|
|
|
|
offset += node.textContent.length;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-07 17:27:09 +03:00
|
|
|
return {offset, atNodeEnd: false};
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// first make sure we're at the level of a direct child of editor
|
|
|
|
if (node.parentElement !== editor) {
|
|
|
|
// include all preceding siblings of the non-direct editor children
|
|
|
|
while (node.previousSibling) {
|
|
|
|
node = node.previousSibling;
|
2019-05-08 12:12:47 +03:00
|
|
|
if (isVisibleNode(node)) {
|
|
|
|
offset += node.textContent.length;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
// then move up
|
|
|
|
// I guess technically there could be preceding text nodes in the parents here as well,
|
|
|
|
// but we're assuming there are no mixed text and element nodes
|
|
|
|
while (node.parentElement !== editor) {
|
|
|
|
node = node.parentElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now include the text length of all preceding direct editor children
|
|
|
|
while (node.previousSibling) {
|
|
|
|
node = node.previousSibling;
|
2019-05-08 12:12:47 +03:00
|
|
|
if (isVisibleNode(node)) {
|
|
|
|
offset += node.textContent.length;
|
|
|
|
}
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
2019-05-07 17:27:09 +03:00
|
|
|
// {
|
|
|
|
// const {focusOffset, focusNode} = sel;
|
|
|
|
// console.log("selection", {focusOffset, focusNode, position, atNodeEnd});
|
|
|
|
// }
|
|
|
|
return {offset, atNodeEnd};
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
|
2019-05-08 12:12:47 +03:00
|
|
|
function isVisibleNode(node) {
|
|
|
|
return node.nodeType === Node.ELEMENT_NODE || node.nodeType === Node.TEXT_NODE;
|
|
|
|
}
|
|
|
|
|
|
|
|
function untilVisibleNode(node) {
|
|
|
|
// need to ignore comment nodes that react uses
|
|
|
|
while (node && !isVisibleNode(node)) {
|
|
|
|
node = node.nextSibling;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2019-05-06 19:21:28 +03:00
|
|
|
export function setCaretPosition(editor, caretPosition) {
|
2019-05-08 12:12:47 +03:00
|
|
|
let node = untilVisibleNode(editor.firstChild);
|
|
|
|
if (!node) {
|
|
|
|
node = editor;
|
|
|
|
} else {
|
|
|
|
let {index} = caretPosition;
|
|
|
|
while (node && index) {
|
|
|
|
node = untilVisibleNode(node.nextSibling);
|
|
|
|
--index;
|
|
|
|
}
|
|
|
|
if (!node) {
|
|
|
|
node = editor;
|
|
|
|
} else if (node.nodeType === Node.ELEMENT_NODE) {
|
2019-05-06 19:21:28 +03:00
|
|
|
// make sure we have a text node
|
2019-05-08 12:12:47 +03:00
|
|
|
node = node.childNodes[0];
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 12:12:47 +03:00
|
|
|
const sel = document.getSelection();
|
|
|
|
sel.removeAllRanges();
|
|
|
|
const range = document.createRange();
|
|
|
|
range.setStart(node, caretPosition.offset);
|
|
|
|
range.collapse(true);
|
|
|
|
sel.addRange(range);
|
2019-05-06 19:21:28 +03:00
|
|
|
}
|