Skip to content

Commit

Permalink
Merge pull request #14103 from Snuffleupagus/PDFFindController-event
Browse files Browse the repository at this point in the history
[api-minor] Change `PDFFindController` to use the "find"-event directly (issue 12731)
  • Loading branch information
timvandermeij committed Oct 16, 2021
2 parents a37bc60 + fa8c0ef commit 9890f35
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 72 deletions.
6 changes: 5 additions & 1 deletion examples/components/simpleviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ eventBus.on("pagesinit", function () {

// We can try searching for things.
if (SEARCH_FOR) {
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
if (!pdfFindController._onFind) {
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
} else {
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
}
}
});

Expand Down
6 changes: 5 additions & 1 deletion examples/components/singlepageviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ eventBus.on("pagesinit", function () {

// We can try searching for things.
if (SEARCH_FOR) {
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
if (!pdfFindController._onFind) {
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
} else {
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
}
}
});

Expand Down
49 changes: 21 additions & 28 deletions test/unit/pdf_find_controller_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,27 @@ async function initPdfFindController(filename) {
function testSearch({
eventBus,
pdfFindController,
parameters,
state,
matchesPerPage,
selectedMatch,
pageMatches = null,
pageMatchesLength = null,
}) {
return new Promise(function (resolve) {
pdfFindController.executeCommand("find", parameters);
const eventState = Object.assign(
Object.create(null),
{
source: this,
type: "",
query: null,
caseSensitive: false,
entireWord: false,
phraseSearch: true,
findPrevious: false,
},
state
);
eventBus.dispatch("find", eventState);

// The `updatefindmatchescount` event is only emitted if the page contains
// at least one match for the query, so the last non-zero item in the
Expand Down Expand Up @@ -142,12 +155,8 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "Dynamic",
caseSensitive: false,
entireWord: false,
phraseSearch: true,
findPrevious: false,
},
matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
selectedMatch: {
Expand All @@ -166,11 +175,8 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "conference",
caseSensitive: false,
entireWord: false,
phraseSearch: true,
findPrevious: true,
},
matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
Expand All @@ -187,12 +193,9 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "Dynamic",
caseSensitive: true,
entireWord: false,
phraseSearch: true,
findPrevious: false,
},
matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
selectedMatch: {
Expand All @@ -210,12 +213,9 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "Government",
caseSensitive: false,
entireWord: true,
phraseSearch: true,
findPrevious: false,
},
matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
selectedMatch: {
Expand All @@ -233,12 +233,9 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "alternate solution",
caseSensitive: false,
entireWord: false,
phraseSearch: false,
findPrevious: false,
},
matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
selectedMatch: {
Expand All @@ -256,12 +253,8 @@ describe("pdf_find_controller", function () {
await testSearch({
eventBus,
pdfFindController,
parameters: {
state: {
query: "fraction",
caseSensitive: false,
entireWord: false,
phraseSearch: true,
findPrevious: false,
},
matchesPerPage: [3],
selectedMatch: {
Expand Down
38 changes: 11 additions & 27 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1929,7 +1929,6 @@ const PDFViewerApplication = {
eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
eventBus._on("spreadmodechanged", webViewerSpreadModeChanged);
eventBus._on("documentproperties", webViewerDocumentProperties);
eventBus._on("find", webViewerFind);
eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
Expand Down Expand Up @@ -2025,7 +2024,6 @@ const PDFViewerApplication = {
eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
eventBus._off("documentproperties", webViewerDocumentProperties);
eventBus._off("find", webViewerFind);
eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
Expand Down Expand Up @@ -2605,19 +2603,10 @@ function webViewerDocumentProperties() {
PDFViewerApplication.pdfDocumentProperties.open();
}

function webViewerFind(evt) {
PDFViewerApplication.findController.executeCommand("find" + evt.type, {
query: evt.query,
phraseSearch: evt.phraseSearch,
caseSensitive: evt.caseSensitive,
entireWord: evt.entireWord,
highlightAll: evt.highlightAll,
findPrevious: evt.findPrevious,
});
}

function webViewerFindFromUrlHash(evt) {
PDFViewerApplication.findController.executeCommand("find", {
PDFViewerApplication.eventBus.dispatch("find", {
source: evt.source,
type: "",
query: evt.query,
phraseSearch: evt.phraseSearch,
caseSensitive: false,
Expand Down Expand Up @@ -2794,6 +2783,8 @@ function webViewerKeyDown(evt) {
if (PDFViewerApplication.overlayManager.active) {
return;
}
const { eventBus, pdfViewer } = PDFViewerApplication;
const isViewerInPresentationMode = pdfViewer.isInPresentationMode;

let handled = false,
ensureViewerFocused = false;
Expand All @@ -2803,9 +2794,6 @@ function webViewerKeyDown(evt) {
(evt.shiftKey ? 4 : 0) |
(evt.metaKey ? 8 : 0);

const pdfViewer = PDFViewerApplication.pdfViewer;
const isViewerInPresentationMode = pdfViewer?.isInPresentationMode;

// First, handle the key bindings that are independent whether an input
// control is selected or not.
if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) {
Expand All @@ -2819,16 +2807,14 @@ function webViewerKeyDown(evt) {
break;
case 71: // g
if (!PDFViewerApplication.supportsIntegratedFind) {
const findState = PDFViewerApplication.findController.state;
if (findState) {
PDFViewerApplication.findController.executeCommand("findagain", {
query: findState.query,
phraseSearch: findState.phraseSearch,
caseSensitive: findState.caseSensitive,
entireWord: findState.entireWord,
highlightAll: findState.highlightAll,
const { state } = PDFViewerApplication.findController;
if (state) {
const eventState = Object.assign(Object.create(null), state, {
source: window,
type: "again",
findPrevious: cmd === 5 || cmd === 12,
});
eventBus.dispatch("find", eventState);
}
handled = true;
}
Expand Down Expand Up @@ -2883,8 +2869,6 @@ function webViewerKeyDown(evt) {
}

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC || CHROME")) {
const { eventBus } = PDFViewerApplication;

// CTRL or META without shift
if (cmd === 1 || cmd === 8) {
switch (evt.keyCode) {
Expand Down
4 changes: 3 additions & 1 deletion web/firefoxcom.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class MozL10n {
"findentirewordchange",
"findbarclose",
];
const findLen = "find".length;

const handleEvent = function ({ type, detail }) {
if (!PDFViewerApplication.initialized) {
return;
Expand All @@ -229,7 +231,7 @@ class MozL10n {
}
PDFViewerApplication.eventBus.dispatch("find", {
source: window,
type: type.substring("find".length),
type: type.substring(findLen),
query: detail.query,
phraseSearch: true,
caseSensitive: !!detail.caseSensitive,
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_find_bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PDFFindBar {
this.updateUIState();
}

dispatchEvent(type, findPrev) {
dispatchEvent(type, findPrev = false) {
this.eventBus.dispatch("find", {
source: this,
type,
Expand Down
41 changes: 30 additions & 11 deletions web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,22 @@ class PDFFindController {
this._eventBus = eventBus;

this._reset();
eventBus._on("find", this._onFind.bind(this));
eventBus._on("findbarclose", this._onFindBarClose.bind(this));

if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
this.executeCommand = (cmd, state) => {
console.error(
"Deprecated method `PDFFindController.executeCommand` called, " +
'please dispatch a "find"-event using the EventBus instead.'
);

const eventState = Object.assign(Object.create(null), state, {
type: cmd.substring("find".length),
});
this._onFind(eventState);
};
}
}

get highlightMatches() {
Expand Down Expand Up @@ -144,17 +159,21 @@ class PDFFindController {
this._firstPageCapability.resolve();
}

executeCommand(cmd, state) {
/**
* @private
*/
_onFind(state) {
if (!state) {
return;
}
const pdfDocument = this._pdfDocument;
const { type } = state;

if (this._state === null || this._shouldDirtyMatch(cmd, state)) {
if (this._state === null || this._shouldDirtyMatch(state)) {
this._dirtyMatch = true;
}
this._state = state;
if (cmd !== "findhighlightallchange") {
if (type !== "highlightallchange") {
this._updateUIState(FindState.PENDING);
}

Expand All @@ -176,7 +195,7 @@ class PDFFindController {
clearTimeout(this._findTimeout);
this._findTimeout = null;
}
if (cmd === "find") {
if (!type) {
// Trigger the find action with a small delay to avoid starting the
// search when the user is still typing (saving resources).
this._findTimeout = setTimeout(() => {
Expand All @@ -187,15 +206,15 @@ class PDFFindController {
// Immediately trigger searching for non-'find' operations, when the
// current state needs to be reset and matches re-calculated.
this._nextMatch();
} else if (cmd === "findagain") {
} else if (type === "again") {
this._nextMatch();

// When the findbar was previously closed, and `highlightAll` is set,
// ensure that the matches on all active pages are highlighted again.
if (findbarClosed && this._state.highlightAll) {
this._updateAllPages();
}
} else if (cmd === "findhighlightallchange") {
} else if (type === "highlightallchange") {
// If there was a pending search operation, synchronously trigger a new
// search *first* to ensure that the correct matches are highlighted.
if (pendingTimeout) {
Expand Down Expand Up @@ -275,14 +294,14 @@ class PDFFindController {
return this._normalizedQuery;
}

_shouldDirtyMatch(cmd, state) {
_shouldDirtyMatch(state) {
// When the search query changes, regardless of the actual search command
// used, always re-calculate matches to avoid errors (fixes bug 1030622).
if (state.query !== this._state.query) {
return true;
}
switch (cmd) {
case "findagain":
switch (state.type) {
case "again":
const pageNumber = this._selected.pageIdx + 1;
const linkService = this._linkService;
// Only treat a 'findagain' event as a new search operation when it's
Expand All @@ -302,7 +321,7 @@ class PDFFindController {
return true;
}
return false;
case "findhighlightallchange":
case "highlightallchange":
return false;
}
return true;
Expand Down Expand Up @@ -797,7 +816,7 @@ class PDFFindController {
});
}

_updateUIState(state, previous) {
_updateUIState(state, previous = false) {
this._eventBus.dispatch("updatefindcontrolstate", {
source: this,
state,
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ class PDFPageView {
renderTask.promise.then(
function () {
showCanvas();
renderCapability.resolve(undefined);
renderCapability.resolve();
},
function (error) {
showCanvas();
Expand Down
2 changes: 1 addition & 1 deletion web/pdf_thumbnail_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class PDFThumbnailView {
draw() {
if (this.renderingState !== RenderingStates.INITIAL) {
console.error("Must be in new state before drawing");
return Promise.resolve(undefined);
return Promise.resolve();
}
const { pdfPage } = this;

Expand Down

0 comments on commit 9890f35

Please sign in to comment.