Skip to content

Commit 9890f35

Browse files
Merge pull request #14103 from Snuffleupagus/PDFFindController-event
[api-minor] Change `PDFFindController` to use the "find"-event directly (issue 12731)
2 parents a37bc60 + fa8c0ef commit 9890f35

File tree

9 files changed

+78
-72
lines changed

9 files changed

+78
-72
lines changed

examples/components/simpleviewer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ eventBus.on("pagesinit", function () {
7777

7878
// We can try searching for things.
7979
if (SEARCH_FOR) {
80-
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
80+
if (!pdfFindController._onFind) {
81+
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
82+
} else {
83+
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
84+
}
8185
}
8286
});
8387

examples/components/singlepageviewer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ eventBus.on("pagesinit", function () {
7777

7878
// We can try searching for things.
7979
if (SEARCH_FOR) {
80-
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
80+
if (!pdfFindController._onFind) {
81+
pdfFindController.executeCommand("find", { query: SEARCH_FOR });
82+
} else {
83+
eventBus.dispatch("find", { type: "", query: SEARCH_FOR });
84+
}
8185
}
8286
});
8387

test/unit/pdf_find_controller_spec.js

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,27 @@ async function initPdfFindController(filename) {
6969
function testSearch({
7070
eventBus,
7171
pdfFindController,
72-
parameters,
72+
state,
7373
matchesPerPage,
7474
selectedMatch,
7575
pageMatches = null,
7676
pageMatchesLength = null,
7777
}) {
7878
return new Promise(function (resolve) {
79-
pdfFindController.executeCommand("find", parameters);
79+
const eventState = Object.assign(
80+
Object.create(null),
81+
{
82+
source: this,
83+
type: "",
84+
query: null,
85+
caseSensitive: false,
86+
entireWord: false,
87+
phraseSearch: true,
88+
findPrevious: false,
89+
},
90+
state
91+
);
92+
eventBus.dispatch("find", eventState);
8093

8194
// The `updatefindmatchescount` event is only emitted if the page contains
8295
// at least one match for the query, so the last non-zero item in the
@@ -142,12 +155,8 @@ describe("pdf_find_controller", function () {
142155
await testSearch({
143156
eventBus,
144157
pdfFindController,
145-
parameters: {
158+
state: {
146159
query: "Dynamic",
147-
caseSensitive: false,
148-
entireWord: false,
149-
phraseSearch: true,
150-
findPrevious: false,
151160
},
152161
matchesPerPage: [11, 5, 0, 3, 0, 0, 0, 1, 1, 1, 0, 3, 4, 4],
153162
selectedMatch: {
@@ -166,11 +175,8 @@ describe("pdf_find_controller", function () {
166175
await testSearch({
167176
eventBus,
168177
pdfFindController,
169-
parameters: {
178+
state: {
170179
query: "conference",
171-
caseSensitive: false,
172-
entireWord: false,
173-
phraseSearch: true,
174180
findPrevious: true,
175181
},
176182
matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5],
@@ -187,12 +193,9 @@ describe("pdf_find_controller", function () {
187193
await testSearch({
188194
eventBus,
189195
pdfFindController,
190-
parameters: {
196+
state: {
191197
query: "Dynamic",
192198
caseSensitive: true,
193-
entireWord: false,
194-
phraseSearch: true,
195-
findPrevious: false,
196199
},
197200
matchesPerPage: [3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3],
198201
selectedMatch: {
@@ -210,12 +213,9 @@ describe("pdf_find_controller", function () {
210213
await testSearch({
211214
eventBus,
212215
pdfFindController,
213-
parameters: {
216+
state: {
214217
query: "Government",
215-
caseSensitive: false,
216218
entireWord: true,
217-
phraseSearch: true,
218-
findPrevious: false,
219219
},
220220
matchesPerPage: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
221221
selectedMatch: {
@@ -233,12 +233,9 @@ describe("pdf_find_controller", function () {
233233
await testSearch({
234234
eventBus,
235235
pdfFindController,
236-
parameters: {
236+
state: {
237237
query: "alternate solution",
238-
caseSensitive: false,
239-
entireWord: false,
240238
phraseSearch: false,
241-
findPrevious: false,
242239
},
243240
matchesPerPage: [0, 0, 0, 0, 0, 1, 0, 0, 4, 0, 0, 0, 0, 0],
244241
selectedMatch: {
@@ -256,12 +253,8 @@ describe("pdf_find_controller", function () {
256253
await testSearch({
257254
eventBus,
258255
pdfFindController,
259-
parameters: {
256+
state: {
260257
query: "fraction",
261-
caseSensitive: false,
262-
entireWord: false,
263-
phraseSearch: true,
264-
findPrevious: false,
265258
},
266259
matchesPerPage: [3],
267260
selectedMatch: {

web/app.js

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,6 @@ const PDFViewerApplication = {
19291929
eventBus._on("switchspreadmode", webViewerSwitchSpreadMode);
19301930
eventBus._on("spreadmodechanged", webViewerSpreadModeChanged);
19311931
eventBus._on("documentproperties", webViewerDocumentProperties);
1932-
eventBus._on("find", webViewerFind);
19331932
eventBus._on("findfromurlhash", webViewerFindFromUrlHash);
19341933
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount);
19351934
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState);
@@ -2025,7 +2024,6 @@ const PDFViewerApplication = {
20252024
eventBus._off("switchspreadmode", webViewerSwitchSpreadMode);
20262025
eventBus._off("spreadmodechanged", webViewerSpreadModeChanged);
20272026
eventBus._off("documentproperties", webViewerDocumentProperties);
2028-
eventBus._off("find", webViewerFind);
20292027
eventBus._off("findfromurlhash", webViewerFindFromUrlHash);
20302028
eventBus._off("updatefindmatchescount", webViewerUpdateFindMatchesCount);
20312029
eventBus._off("updatefindcontrolstate", webViewerUpdateFindControlState);
@@ -2605,19 +2603,10 @@ function webViewerDocumentProperties() {
26052603
PDFViewerApplication.pdfDocumentProperties.open();
26062604
}
26072605

2608-
function webViewerFind(evt) {
2609-
PDFViewerApplication.findController.executeCommand("find" + evt.type, {
2610-
query: evt.query,
2611-
phraseSearch: evt.phraseSearch,
2612-
caseSensitive: evt.caseSensitive,
2613-
entireWord: evt.entireWord,
2614-
highlightAll: evt.highlightAll,
2615-
findPrevious: evt.findPrevious,
2616-
});
2617-
}
2618-
26192606
function webViewerFindFromUrlHash(evt) {
2620-
PDFViewerApplication.findController.executeCommand("find", {
2607+
PDFViewerApplication.eventBus.dispatch("find", {
2608+
source: evt.source,
2609+
type: "",
26212610
query: evt.query,
26222611
phraseSearch: evt.phraseSearch,
26232612
caseSensitive: false,
@@ -2794,6 +2783,8 @@ function webViewerKeyDown(evt) {
27942783
if (PDFViewerApplication.overlayManager.active) {
27952784
return;
27962785
}
2786+
const { eventBus, pdfViewer } = PDFViewerApplication;
2787+
const isViewerInPresentationMode = pdfViewer.isInPresentationMode;
27972788

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

2806-
const pdfViewer = PDFViewerApplication.pdfViewer;
2807-
const isViewerInPresentationMode = pdfViewer?.isInPresentationMode;
2808-
28092797
// First, handle the key bindings that are independent whether an input
28102798
// control is selected or not.
28112799
if (cmd === 1 || cmd === 8 || cmd === 5 || cmd === 12) {
@@ -2819,16 +2807,14 @@ function webViewerKeyDown(evt) {
28192807
break;
28202808
case 71: // g
28212809
if (!PDFViewerApplication.supportsIntegratedFind) {
2822-
const findState = PDFViewerApplication.findController.state;
2823-
if (findState) {
2824-
PDFViewerApplication.findController.executeCommand("findagain", {
2825-
query: findState.query,
2826-
phraseSearch: findState.phraseSearch,
2827-
caseSensitive: findState.caseSensitive,
2828-
entireWord: findState.entireWord,
2829-
highlightAll: findState.highlightAll,
2810+
const { state } = PDFViewerApplication.findController;
2811+
if (state) {
2812+
const eventState = Object.assign(Object.create(null), state, {
2813+
source: window,
2814+
type: "again",
28302815
findPrevious: cmd === 5 || cmd === 12,
28312816
});
2817+
eventBus.dispatch("find", eventState);
28322818
}
28332819
handled = true;
28342820
}
@@ -2883,8 +2869,6 @@ function webViewerKeyDown(evt) {
28832869
}
28842870

28852871
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC || CHROME")) {
2886-
const { eventBus } = PDFViewerApplication;
2887-
28882872
// CTRL or META without shift
28892873
if (cmd === 1 || cmd === 8) {
28902874
switch (evt.keyCode) {

web/firefoxcom.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class MozL10n {
219219
"findentirewordchange",
220220
"findbarclose",
221221
];
222+
const findLen = "find".length;
223+
222224
const handleEvent = function ({ type, detail }) {
223225
if (!PDFViewerApplication.initialized) {
224226
return;
@@ -229,7 +231,7 @@ class MozL10n {
229231
}
230232
PDFViewerApplication.eventBus.dispatch("find", {
231233
source: window,
232-
type: type.substring("find".length),
234+
type: type.substring(findLen),
233235
query: detail.query,
234236
phraseSearch: true,
235237
caseSensitive: !!detail.caseSensitive,

web/pdf_find_bar.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class PDFFindBar {
8989
this.updateUIState();
9090
}
9191

92-
dispatchEvent(type, findPrev) {
92+
dispatchEvent(type, findPrev = false) {
9393
this.eventBus.dispatch("find", {
9494
source: this,
9595
type,

web/pdf_find_controller.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,22 @@ class PDFFindController {
104104
this._eventBus = eventBus;
105105

106106
this._reset();
107+
eventBus._on("find", this._onFind.bind(this));
107108
eventBus._on("findbarclose", this._onFindBarClose.bind(this));
109+
110+
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
111+
this.executeCommand = (cmd, state) => {
112+
console.error(
113+
"Deprecated method `PDFFindController.executeCommand` called, " +
114+
'please dispatch a "find"-event using the EventBus instead.'
115+
);
116+
117+
const eventState = Object.assign(Object.create(null), state, {
118+
type: cmd.substring("find".length),
119+
});
120+
this._onFind(eventState);
121+
};
122+
}
108123
}
109124

110125
get highlightMatches() {
@@ -144,17 +159,21 @@ class PDFFindController {
144159
this._firstPageCapability.resolve();
145160
}
146161

147-
executeCommand(cmd, state) {
162+
/**
163+
* @private
164+
*/
165+
_onFind(state) {
148166
if (!state) {
149167
return;
150168
}
151169
const pdfDocument = this._pdfDocument;
170+
const { type } = state;
152171

153-
if (this._state === null || this._shouldDirtyMatch(cmd, state)) {
172+
if (this._state === null || this._shouldDirtyMatch(state)) {
154173
this._dirtyMatch = true;
155174
}
156175
this._state = state;
157-
if (cmd !== "findhighlightallchange") {
176+
if (type !== "highlightallchange") {
158177
this._updateUIState(FindState.PENDING);
159178
}
160179

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

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

278-
_shouldDirtyMatch(cmd, state) {
297+
_shouldDirtyMatch(state) {
279298
// When the search query changes, regardless of the actual search command
280299
// used, always re-calculate matches to avoid errors (fixes bug 1030622).
281300
if (state.query !== this._state.query) {
282301
return true;
283302
}
284-
switch (cmd) {
285-
case "findagain":
303+
switch (state.type) {
304+
case "again":
286305
const pageNumber = this._selected.pageIdx + 1;
287306
const linkService = this._linkService;
288307
// Only treat a 'findagain' event as a new search operation when it's
@@ -302,7 +321,7 @@ class PDFFindController {
302321
return true;
303322
}
304323
return false;
305-
case "findhighlightallchange":
324+
case "highlightallchange":
306325
return false;
307326
}
308327
return true;
@@ -797,7 +816,7 @@ class PDFFindController {
797816
});
798817
}
799818

800-
_updateUIState(state, previous) {
819+
_updateUIState(state, previous = false) {
801820
this._eventBus.dispatch("updatefindcontrolstate", {
802821
source: this,
803822
state,

web/pdf_page_view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ class PDFPageView {
831831
renderTask.promise.then(
832832
function () {
833833
showCanvas();
834-
renderCapability.resolve(undefined);
834+
renderCapability.resolve();
835835
},
836836
function (error) {
837837
showCanvas();

web/pdf_thumbnail_view.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class PDFThumbnailView {
278278
draw() {
279279
if (this.renderingState !== RenderingStates.INITIAL) {
280280
console.error("Must be in new state before drawing");
281-
return Promise.resolve(undefined);
281+
return Promise.resolve();
282282
}
283283
const { pdfPage } = this;
284284

0 commit comments

Comments
 (0)