mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
[Editor] In caret browsing mode, get the caret position in the text layer (bug 1881692)
The function caretPositionFromPoint return the position within the last visible element and sometimes there are some elements on top of the ones in the text layer. So the idea is to hide the visible elements which aren't in the text layer in order to get the right caret position.
This commit is contained in:
parent
101e8efad7
commit
bb19cf9b64
3 changed files with 92 additions and 5 deletions
|
@ -329,6 +329,12 @@ const PDFViewerApplication = {
|
|||
params.get("highlighteditorcolors")
|
||||
);
|
||||
}
|
||||
if (params.has("supportscaretbrowsingmode")) {
|
||||
AppOptions.set(
|
||||
"supportsCaretBrowsingMode",
|
||||
params.get("supportscaretbrowsingmode") === "true"
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -130,11 +130,29 @@ class CaretBrowsingMode {
|
|||
}
|
||||
|
||||
const midY = rect.y + rect.height / 2;
|
||||
const caretPosition = CaretBrowsingMode.#caretPositionFromPoint(
|
||||
caretX,
|
||||
midY
|
||||
);
|
||||
if (caretPosition.offsetNode?.parentElement !== element) {
|
||||
let caretPosition = CaretBrowsingMode.#caretPositionFromPoint(caretX, midY);
|
||||
let parentElement = caretPosition.offsetNode?.parentElement;
|
||||
if (parentElement && parentElement !== element) {
|
||||
// There is an element on top of the one in the text layer, so we
|
||||
// need to hide all the elements (except the one in the text layer)
|
||||
// at this position in order to get the correct caret position.
|
||||
const elementsAtPoint = document.elementsFromPoint(caretX, midY);
|
||||
const savedVisibilities = [];
|
||||
for (const el of elementsAtPoint) {
|
||||
if (el === element) {
|
||||
break;
|
||||
}
|
||||
const { style } = el;
|
||||
savedVisibilities.push([el, style.visibility]);
|
||||
style.visibility = "hidden";
|
||||
}
|
||||
caretPosition = CaretBrowsingMode.#caretPositionFromPoint(caretX, midY);
|
||||
parentElement = caretPosition.offsetNode?.parentElement;
|
||||
for (const [el, visibility] of savedVisibilities) {
|
||||
el.style.visibility = visibility;
|
||||
}
|
||||
}
|
||||
if (parentElement !== element) {
|
||||
// The element targeted by caretPositionFromPoint isn't in the text
|
||||
// layer.
|
||||
if (select) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue