Skip to content

Commit 1182284

Browse files
committed
Trigger the current find after a page has been moved (bug 2023150)
1 parent ff1af5a commit 1182284

2 files changed

Lines changed: 157 additions & 33 deletions

File tree

test/integration/reorganize_pages_spec.mjs

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import {
1717
awaitPromise,
18-
clearInput,
1918
closePages,
2019
createPromise,
2120
createPromiseWithArgs,
@@ -400,14 +399,6 @@ describe("Reorganize Pages View", () => {
400399
]);
401400

402401
await movePages(page, [11, 2], 3);
403-
await page.waitForFunction(
404-
() => document.querySelectorAll("span.highlight").length === 0
405-
);
406-
407-
await clearInput(page, "#findInput", true);
408-
await page.type("#findInput", "1");
409-
await page.keyboard.press("Enter");
410-
411402
await page.waitForFunction(
412403
() => document.querySelectorAll("span.highlight").length === 10
413404
);
@@ -429,13 +420,6 @@ describe("Reorganize Pages View", () => {
429420
]);
430421

431422
await movePages(page, [13], 0);
432-
await page.waitForFunction(
433-
() => document.querySelectorAll("span.highlight").length === 0
434-
);
435-
436-
await clearInput(page, "#findInput", true);
437-
await page.type("#findInput", "1");
438-
await page.keyboard.press("Enter");
439423

440424
await page.waitForFunction(
441425
() => document.querySelectorAll("span.highlight").length === 10
@@ -459,6 +443,115 @@ describe("Reorganize Pages View", () => {
459443
})
460444
);
461445
});
446+
447+
it("should check if the search is working after copy and paste (bug 2023150)", async () => {
448+
await Promise.all(
449+
pages.map(async ([browserName, page]) => {
450+
await waitForThumbnailVisible(page, 1);
451+
await page.waitForSelector("#viewsManagerStatusActionButton", {
452+
visible: true,
453+
});
454+
455+
await waitAndClick(page, "#viewFindButton");
456+
await waitAndClick(page, ":has(> #findHighlightAll)");
457+
458+
await page.waitForSelector("#findInput", { visible: true });
459+
await page.type("#findInput", "1");
460+
await page.keyboard.press("Enter");
461+
462+
await page.waitForFunction(
463+
() => document.querySelectorAll("span.highlight").length === 10
464+
);
465+
466+
// Select page 1 and copy it.
467+
await waitAndClick(
468+
page,
469+
`.thumbnail:has(${getThumbnailSelector(1)}) input`
470+
);
471+
let handlePagesEdited = await waitForPagesEdited(page, "copy");
472+
await waitAndClick(page, "#viewsManagerStatusActionButton");
473+
await waitAndClick(page, "#viewsManagerStatusActionCopy");
474+
await awaitPromise(handlePagesEdited);
475+
476+
// Paste after page 3.
477+
handlePagesEdited = await waitForPagesEdited(page);
478+
await waitAndClick(page, `${getThumbnailSelector(3)}+button`);
479+
await awaitPromise(handlePagesEdited);
480+
481+
await page.waitForFunction(
482+
() => document.querySelectorAll("span.highlight").length === 11
483+
);
484+
485+
const results = await getSearchResults(page);
486+
expect(results)
487+
.withContext(`In ${browserName}`)
488+
.toEqual([
489+
// Page number, [matches]; copy of page 1 inserted at position 4
490+
[1, ["1"]],
491+
[4, ["1"]],
492+
[11, ["1"]],
493+
[12, ["1", "1"]],
494+
[13, ["1"]],
495+
[14, ["1"]],
496+
[15, ["1"]],
497+
[16, ["1"]],
498+
[17, ["1"]],
499+
[18, ["1"]],
500+
]);
501+
})
502+
);
503+
});
504+
505+
it("should check if the search is working after deleting pages (bug 2023150)", async () => {
506+
await Promise.all(
507+
pages.map(async ([browserName, page]) => {
508+
await waitForThumbnailVisible(page, 1);
509+
await page.waitForSelector("#viewsManagerStatusActionButton", {
510+
visible: true,
511+
});
512+
513+
await waitAndClick(page, "#viewFindButton");
514+
await waitAndClick(page, ":has(> #findHighlightAll)");
515+
516+
await page.waitForSelector("#findInput", { visible: true });
517+
await page.type("#findInput", "1");
518+
await page.keyboard.press("Enter");
519+
520+
await page.waitForFunction(
521+
() => document.querySelectorAll("span.highlight").length === 10
522+
);
523+
524+
// Select page 1 and delete it.
525+
await waitAndClick(
526+
page,
527+
`.thumbnail:has(${getThumbnailSelector(1)}) input`
528+
);
529+
const handlePagesEdited = await waitForPagesEdited(page);
530+
await waitAndClick(page, "#viewsManagerStatusActionButton");
531+
await waitAndClick(page, "#viewsManagerStatusActionDelete");
532+
await awaitPromise(handlePagesEdited);
533+
534+
await page.waitForFunction(
535+
() => document.querySelectorAll("span.highlight").length === 9
536+
);
537+
538+
const results = await getSearchResults(page);
539+
expect(results)
540+
.withContext(`In ${browserName}`)
541+
.toEqual([
542+
// Page number, [matches]; page 1 removed, all positions shifted
543+
[9, ["1"]],
544+
[10, ["1", "1"]],
545+
[11, ["1"]],
546+
[12, ["1"]],
547+
[13, ["1"]],
548+
[14, ["1"]],
549+
[15, ["1"]],
550+
[16, ["1"]],
551+
]);
552+
})
553+
);
554+
});
462555
});
463556

464557
describe("Links and outlines", () => {

web/pdf_find_controller.js

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,9 @@ class PDFFindController {
422422

423423
#visitedPagesCount = 0;
424424

425-
#copiedExtractTextPromises = null;
425+
#copiedPageData = null;
426426

427-
#savedExtractTextPromises = null;
427+
#savedPageData = null;
428428

429429
/**
430430
* @param {PDFFindControllerOptions} options
@@ -613,7 +613,7 @@ class PDFFindController {
613613
this._dirtyMatch = false;
614614
clearTimeout(this._findTimeout);
615615
this._findTimeout = null;
616-
this.#copiedExtractTextPromises = null;
616+
this.#copiedPageData = null;
617617

618618
this._firstPageCapability = Promise.withResolvers();
619619
}
@@ -1138,51 +1138,82 @@ class PDFFindController {
11381138
}
11391139

11401140
if (type === "copy") {
1141-
this.#copiedExtractTextPromises = new Map();
1141+
const promises = new Map();
1142+
const contents = new Map();
1143+
const diffs = new Map();
1144+
const diacritics = new Map();
11421145
for (const pageNum of pageNumbers) {
1143-
this.#copiedExtractTextPromises.set(
1144-
pageNum,
1145-
this._extractTextPromises[pageNum - 1]
1146-
);
1146+
promises.set(pageNum, this._extractTextPromises[pageNum - 1]);
1147+
contents.set(pageNum, this._pageContents[pageNum - 1]);
1148+
diffs.set(pageNum, this._pageDiffs[pageNum - 1]);
1149+
diacritics.set(pageNum, this._hasDiacritics[pageNum - 1]);
11471150
}
1151+
this.#copiedPageData = { promises, contents, diffs, diacritics };
11481152
return;
11491153
}
11501154

11511155
if (type === "cancelCopy") {
1152-
this.#copiedExtractTextPromises = null;
1156+
this.#copiedPageData = null;
11531157
return;
11541158
}
11551159

11561160
if (type === "delete") {
1157-
this.#savedExtractTextPromises = this._extractTextPromises;
1161+
this.#savedPageData = {
1162+
promises: this._extractTextPromises,
1163+
contents: this._pageContents,
1164+
diffs: this._pageDiffs,
1165+
diacritics: this._hasDiacritics,
1166+
};
11581167
}
11591168

11601169
if (type === "cancelDelete") {
1161-
this._extractTextPromises = this.#savedExtractTextPromises;
1170+
this._extractTextPromises = this.#savedPageData.promises;
1171+
this._pageContents = this.#savedPageData.contents;
1172+
this._pageDiffs = this.#savedPageData.diffs;
1173+
this._hasDiacritics = this.#savedPageData.diacritics;
11621174
return;
11631175
}
11641176

11651177
if (type === "cleanSavedData") {
1166-
this.#savedExtractTextPromises = null;
1178+
this.#savedPageData = null;
11671179
return;
11681180
}
11691181

11701182
this.#onFindBarClose();
11711183
this._dirtyMatch = true;
1172-
const prevTextPromises = this._extractTextPromises;
1184+
const prevPromises = this._extractTextPromises;
1185+
const prevContents = this._pageContents;
1186+
const prevDiffs = this._pageDiffs;
1187+
const prevDiacritics = this._hasDiacritics;
11731188
const extractTextPromises = (this._extractTextPromises = []);
1174-
for (let i = 1, ii = pagesMapper.length; i <= ii; i++) {
1189+
const pageContents = (this._pageContents = []);
1190+
const pageDiffs = (this._pageDiffs = []);
1191+
const hasDiacritics = (this._hasDiacritics = []);
1192+
for (let i = 1, ii = pagesMapper.pagesNumber; i <= ii; i++) {
11751193
const prevPageNumber = pagesMapper.getPrevPageNumber(i);
11761194
if (prevPageNumber < 0) {
1195+
const src = -prevPageNumber;
11771196
extractTextPromises.push(
1178-
this.#copiedExtractTextPromises?.get(-prevPageNumber) ||
1179-
Promise.resolve()
1197+
this.#copiedPageData?.promises.get(src) || Promise.resolve()
11801198
);
1199+
pageContents.push(this.#copiedPageData?.contents.get(src) ?? "");
1200+
pageDiffs.push(this.#copiedPageData?.diffs.get(src) ?? null);
1201+
hasDiacritics.push(this.#copiedPageData?.diacritics.get(src) ?? false);
11811202
continue;
11821203
}
11831204
extractTextPromises.push(
1184-
prevTextPromises[prevPageNumber - 1] || Promise.resolve()
1205+
prevPromises[prevPageNumber - 1] || Promise.resolve()
11851206
);
1207+
pageContents.push(prevContents[prevPageNumber - 1] ?? "");
1208+
pageDiffs.push(prevDiffs[prevPageNumber - 1] ?? null);
1209+
hasDiacritics.push(prevDiacritics[prevPageNumber - 1] ?? false);
1210+
}
1211+
if (this.#state) {
1212+
this._eventBus.dispatch("find", {
1213+
source: this,
1214+
type: "",
1215+
...this.#state,
1216+
});
11861217
}
11871218
}
11881219

0 commit comments

Comments
 (0)