etherpad-lite/src/static/js/caretPosition.js

204 lines
7.5 KiB
JavaScript
Raw Normal View History

2020-12-22 15:01:20 +00:00
'use strict';
// One rep.line(div) can be broken in more than one line in the browser.
// This function is useful to get the caret position of the line as
// is represented by the browser
2020-12-22 15:01:20 +00:00
exports.getPosition = () => {
2020-11-23 13:24:19 -05:00
const range = getSelectionRange();
if (!range || $(range.endContainer).closest('body')[0].id !== 'innerdocbody') return null;
// When there's a <br> or any element that has no height, we can't get the dimension of the
// element where the caret is. As we can't get the element height, we create a text node to get
// the dimensions on the position.
const clonedRange = createSelectionRange(range);
const shadowCaret = $(document.createTextNode('|'));
clonedRange.insertNode(shadowCaret[0]);
clonedRange.selectNode(shadowCaret[0]);
const line = getPositionOfElementOrSelection(clonedRange);
shadowCaret.remove();
return line;
2020-11-23 13:24:19 -05:00
};
2020-12-22 15:01:20 +00:00
const createSelectionRange = (range) => {
const clonedRange = range.cloneRange();
// we set the selection start and end to avoid error when user selects a text bigger than
// the viewport height and uses the arrow keys to expand the selection. In this particular
// case is necessary to know where the selections ends because both edges of the selection
// is out of the viewport but we only use the end of it to calculate if it needs to scroll
clonedRange.setStart(range.endContainer, range.endOffset);
clonedRange.setEnd(range.endContainer, range.endOffset);
return clonedRange;
2020-11-23 13:24:19 -05:00
};
2020-12-22 15:01:20 +00:00
const getPositionOfRepLineAtOffset = (node, offset) => {
// it is not a text node, so we cannot make a selection
if (node.tagName === 'BR' || node.tagName === 'EMPTY') {
return getPositionOfElementOrSelection(node);
}
while (node.length === 0 && node.nextSibling) {
node = node.nextSibling;
}
2020-11-23 13:24:19 -05:00
const newRange = new Range();
newRange.setStart(node, offset);
newRange.setEnd(node, offset);
2020-11-23 13:24:19 -05:00
const linePosition = getPositionOfElementOrSelection(newRange);
newRange.detach(); // performance sake
return linePosition;
2020-11-23 13:24:19 -05:00
};
2020-12-22 15:01:20 +00:00
const getPositionOfElementOrSelection = (element) => {
2020-11-23 13:24:19 -05:00
const rect = element.getBoundingClientRect();
const linePosition = {
bottom: rect.bottom,
height: rect.height,
2020-11-23 13:24:19 -05:00
top: rect.top,
};
return linePosition;
2020-12-22 15:01:20 +00:00
};
// here we have two possibilities:
2020-12-22 15:01:20 +00:00
// [1] the line before the caret line has the same type, so both of them has the same margin,
// padding height, etc. So, we can use the caret line to make calculation necessary to know
// where is the top of the previous line
// [2] the line before is part of another rep line. It's possible this line has different margins
// height. So we have to get the exactly position of the line
2020-12-22 15:01:20 +00:00
exports.getPositionTopOfPreviousBrowserLine = (caretLinePosition, rep) => {
2020-11-23 13:24:19 -05:00
let previousLineTop = caretLinePosition.top - caretLinePosition.height; // [1]
const isCaretLineFirstBrowserLine = caretLineIsFirstBrowserLine(caretLinePosition.top, rep);
// the caret is in the beginning of a rep line, so the previous browser line
// is the last line browser line of the a rep line
2020-11-23 13:24:19 -05:00
if (isCaretLineFirstBrowserLine) { // [2]
const lineBeforeCaretLine = rep.selStart[0] - 1;
const firstLineVisibleBeforeCaretLine = getPreviousVisibleLine(lineBeforeCaretLine, rep);
2020-12-22 15:01:20 +00:00
const linePosition =
getDimensionOfLastBrowserLineOfRepLine(firstLineVisibleBeforeCaretLine, rep);
previousLineTop = linePosition.top;
}
return previousLineTop;
2020-11-23 13:24:19 -05:00
};
2020-12-22 15:01:20 +00:00
const caretLineIsFirstBrowserLine = (caretLineTop, rep) => {
2020-11-23 13:24:19 -05:00
const caretRepLine = rep.selStart[0];
const lineNode = rep.lines.atIndex(caretRepLine).lineNode;
const firstRootNode = getFirstRootChildNode(lineNode);
// to get the position of the node we get the position of the first char
2020-11-23 13:24:19 -05:00
const positionOfFirstRootNode = getPositionOfRepLineAtOffset(firstRootNode, 1);
return positionOfFirstRootNode.top === caretLineTop;
2020-12-22 15:01:20 +00:00
};
// find the first root node, usually it is a text node
2020-12-22 15:01:20 +00:00
const getFirstRootChildNode = (node) => {
2020-11-23 13:24:19 -05:00
if (!node.firstChild) {
return node;
2020-11-23 13:24:19 -05:00
} else {
return getFirstRootChildNode(node.firstChild);
}
2020-12-22 15:01:20 +00:00
};
2020-12-22 15:01:20 +00:00
const getDimensionOfLastBrowserLineOfRepLine = (line, rep) => {
2020-11-23 13:24:19 -05:00
const lineNode = rep.lines.atIndex(line).lineNode;
const lastRootChildNode = getLastRootChildNode(lineNode);
// we get the position of the line in the last char of it
2020-12-22 15:01:20 +00:00
const lastRootChildNodePosition =
getPositionOfRepLineAtOffset(lastRootChildNode.node, lastRootChildNode.length);
return lastRootChildNodePosition;
2020-12-22 15:01:20 +00:00
};
2020-12-22 15:01:20 +00:00
const getLastRootChildNode = (node) => {
2020-11-23 13:24:19 -05:00
if (!node.lastChild) {
return {
2020-11-23 13:24:19 -05:00
node,
length: node.length,
};
2020-11-23 13:24:19 -05:00
} else {
return getLastRootChildNode(node.lastChild);
}
2020-12-22 15:01:20 +00:00
};
// here we have two possibilities:
// [1] The next line is part of the same rep line of the caret line, so we have the same dimensions.
// So, we can use the caret line to calculate the bottom of the line.
2020-12-22 15:01:20 +00:00
// [2] the next line is part of another rep line.
// It's possible this line has different dimensions, so we have to get the exactly dimension of it
exports.getBottomOfNextBrowserLine = (caretLinePosition, rep) => {
2020-11-23 13:24:19 -05:00
let nextLineBottom = caretLinePosition.bottom + caretLinePosition.height; // [1]
2020-12-22 15:01:20 +00:00
const isCaretLineLastBrowserLine =
caretLineIsLastBrowserLineOfRepLine(caretLinePosition.top, rep);
// the caret is at the end of a rep line, so we can get the next browser line dimension
// using the position of the first char of the next rep line
2020-11-23 13:24:19 -05:00
if (isCaretLineLastBrowserLine) { // [2]
const nextLineAfterCaretLine = rep.selStart[0] + 1;
const firstNextLineVisibleAfterCaretLine = getNextVisibleLine(nextLineAfterCaretLine, rep);
2020-12-22 15:01:20 +00:00
const linePosition =
getDimensionOfFirstBrowserLineOfRepLine(firstNextLineVisibleAfterCaretLine, rep);
nextLineBottom = linePosition.bottom;
}
return nextLineBottom;
2020-11-23 13:24:19 -05:00
};
2020-12-22 15:01:20 +00:00
const caretLineIsLastBrowserLineOfRepLine = (caretLineTop, rep) => {
2020-11-23 13:24:19 -05:00
const caretRepLine = rep.selStart[0];
const lineNode = rep.lines.atIndex(caretRepLine).lineNode;
const lastRootChildNode = getLastRootChildNode(lineNode);
// we take a rep line and get the position of the last char of it
2020-12-22 15:01:20 +00:00
const lastRootChildNodePosition =
getPositionOfRepLineAtOffset(lastRootChildNode.node, lastRootChildNode.length);
return lastRootChildNodePosition.top === caretLineTop;
2020-12-22 15:01:20 +00:00
};
2020-12-22 15:01:20 +00:00
const getPreviousVisibleLine = (line, rep) => {
2020-11-23 13:24:19 -05:00
const firstLineOfPad = 0;
if (line <= firstLineOfPad) {
return firstLineOfPad;
2020-11-23 13:24:19 -05:00
} else if (isLineVisible(line, rep)) {
return line;
2020-11-23 13:24:19 -05:00
} else {
return getPreviousVisibleLine(line - 1, rep);
}
2020-12-22 15:01:20 +00:00
};
exports.getPreviousVisibleLine = getPreviousVisibleLine;
2020-12-22 15:01:20 +00:00
const getNextVisibleLine = (line, rep) => {
2020-11-23 13:24:19 -05:00
const lastLineOfThePad = rep.lines.length() - 1;
if (line >= lastLineOfThePad) {
return lastLineOfThePad;
2020-11-23 13:24:19 -05:00
} else if (isLineVisible(line, rep)) {
return line;
2020-11-23 13:24:19 -05:00
} else {
return getNextVisibleLine(line + 1, rep);
}
2020-12-22 15:01:20 +00:00
};
exports.getNextVisibleLine = getNextVisibleLine;
2020-12-22 15:01:20 +00:00
const isLineVisible = (line, rep) => rep.lines.atIndex(line).lineNode.offsetHeight > 0;
2020-12-22 15:01:20 +00:00
const getDimensionOfFirstBrowserLineOfRepLine = (line, rep) => {
2020-11-23 13:24:19 -05:00
const lineNode = rep.lines.atIndex(line).lineNode;
const firstRootChildNode = getFirstRootChildNode(lineNode);
// we can get the position of the line, getting the position of the first char of the rep line
2020-11-23 13:24:19 -05:00
const firstRootChildNodePosition = getPositionOfRepLineAtOffset(firstRootChildNode, 1);
return firstRootChildNodePosition;
2020-12-22 15:01:20 +00:00
};
2020-12-22 15:01:20 +00:00
const getSelectionRange = () => {
if (!window.getSelection) {
2020-11-23 13:24:19 -05:00
return;
}
2020-12-22 15:01:20 +00:00
const selection = window.getSelection();
if (selection && selection.type !== 'None' && selection.rangeCount > 0) {
2020-11-23 13:24:19 -05:00
return selection.getRangeAt(0);
} else {
2020-11-23 13:24:19 -05:00
return null;
}
2020-12-22 15:01:20 +00:00
};