Skip to content

#7925: added cursor position management during a search #19477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/app_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const defaultOptions = {
},
supportsCaretBrowsingMode: {
/** @type {boolean} */
value: false,
value: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be reverted.

kind: OptionKind.BROWSER,
},
supportsDocumentFonts: {
Expand Down
35 changes: 33 additions & 2 deletions web/pdf_find_bar.js
Copy link
Collaborator

@Snuffleupagus Snuffleupagus Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this won't work in the built-in Firefox PDF Viewer since it doesn't use the web/pdf_find_bar.js file.
Probably the code would need to live in the https://github.com/mozilla/pdf.js/blob/master/web/caret_browsing.js file instead, and it's probably possible to re-use existing methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked in Firefox with a normal web page and whatever the caret browsing mode is, when dismissing the find bar the found text is selected.
In Chrome (with pdfium) the caret isn't moved to the last found string but in Acrobat the caret is positioned at the end of the string.
So I guess having the same behavior as in Acrobat isn't that bad, whatever the caret browsing mode is.

Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,47 @@ class PDFFindBar {
this.findField.focus();
}

close() {
async close() {
if (!this.opened) {
return;
}

const ele = document.querySelector(".highlight");
if (ele) {
ele.parentElement.setAttribute("id", "find-element");
}
const idxFindElement = ele
? ele.parentNode.innerHTML.indexOf(ele.outerHTML)
: null;

this.#resizeObserver.disconnect();

this.opened = false;
toggleExpandedBtn(this.toggleButton, false, this.bar);

this.eventBus.dispatch("findbarclose", { source: this });
await this.eventBus.dispatch("findbarclose", { source: this });

// apply the cursor position for the caret mode
if (ele) {
const range = document.createRange();
const selection = document.getSelection();

const findElement = document.getElementById("find-element");
const walker = document.createTreeWalker(
findElement,
NodeFilter.SHOW_TEXT
);
const findElementChild = walker.firstChild();

if (findElementChild) {
range.setStart(findElementChild, idxFindElement);
range.setEnd(findElementChild, idxFindElement);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
findElement.removeAttribute("id");
}
}
}

toggle() {
Expand Down