mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
caretPosition: Invert condition in getPosition()
for readability
This commit is contained in:
parent
773959ec57
commit
02fd0048bf
1 changed files with 22 additions and 25 deletions
|
@ -4,36 +4,33 @@
|
||||||
// This function is useful to get the caret position of the line as
|
// This function is useful to get the caret position of the line as
|
||||||
// is represented by the browser
|
// is represented by the browser
|
||||||
exports.getPosition = () => {
|
exports.getPosition = () => {
|
||||||
let rect, line;
|
|
||||||
const range = getSelectionRange();
|
const range = getSelectionRange();
|
||||||
const isSelectionInsideTheEditor = range &&
|
if (!range || $(range.endContainer).closest('body')[0].id !== 'innerdocbody') return null;
|
||||||
$(range.endContainer).closest('body')[0].id === 'innerdocbody';
|
let rect, line;
|
||||||
|
|
||||||
if (isSelectionInsideTheEditor) {
|
// when we have the caret in an empty line, e.g. a line with only a <br>,
|
||||||
// when we have the caret in an empty line, e.g. a line with only a <br>,
|
// getBoundingClientRect() returns all dimensions value as 0
|
||||||
// getBoundingClientRect() returns all dimensions value as 0
|
const selectionIsInTheBeginningOfLine = range.endOffset > 0;
|
||||||
const selectionIsInTheBeginningOfLine = range.endOffset > 0;
|
if (selectionIsInTheBeginningOfLine) {
|
||||||
if (selectionIsInTheBeginningOfLine) {
|
const clonedRange = createSelectionRange(range);
|
||||||
const clonedRange = createSelectionRange(range);
|
line = getPositionOfElementOrSelection(clonedRange);
|
||||||
line = getPositionOfElementOrSelection(clonedRange);
|
clonedRange.detach();
|
||||||
clonedRange.detach();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// when there's a <br> or any element that has no height, we can't get
|
// 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
|
// the dimension of the element where the caret is
|
||||||
if (!rect || rect.height === 0) {
|
if (!rect || rect.height === 0) {
|
||||||
const clonedRange = createSelectionRange(range);
|
const clonedRange = createSelectionRange(range);
|
||||||
|
|
||||||
// as we can't get the element height, we create a text node to get the dimensions
|
// as we can't get the element height, we create a text node to get the dimensions
|
||||||
// on the position
|
// on the position
|
||||||
const shadowCaret = $(document.createTextNode('|'));
|
const shadowCaret = $(document.createTextNode('|'));
|
||||||
clonedRange.insertNode(shadowCaret[0]);
|
clonedRange.insertNode(shadowCaret[0]);
|
||||||
clonedRange.selectNode(shadowCaret[0]);
|
clonedRange.selectNode(shadowCaret[0]);
|
||||||
|
|
||||||
line = getPositionOfElementOrSelection(clonedRange);
|
line = getPositionOfElementOrSelection(clonedRange);
|
||||||
clonedRange.detach();
|
clonedRange.detach();
|
||||||
shadowCaret.remove();
|
shadowCaret.remove();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue