Skip to content

Commit

Permalink
[Editor] Avoid to throw when deleting some invisible editors
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Jul 6, 2023
1 parent 8281bb8 commit e4b4d22
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,11 @@ class AnnotationEditor {
// undo/redo so we must commit it before.
this.commit();
}
this.parent.remove(this);
if (this.parent) {
this.parent.remove(this);
} else {
this._uiManager.removeEditor(this);
}
}

/**
Expand Down Expand Up @@ -638,6 +642,9 @@ class AnnotationEditor {
*/
set isEditing(value) {
this.#isEditing = value;
if (!this.parent) {
return;
}
if (value) {
this.parent.setSelected(this);
this.parent.setActiveEditor(this);
Expand Down
6 changes: 4 additions & 2 deletions src/display/editor/freetext.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,10 @@ class FreeTextEditor extends AnnotationEditor {
/** @inheritdoc */
remove() {
this.isEditing = false;
this.parent.setEditingState(true);
this.parent.div.classList.add("freeTextEditing");
if (this.parent) {
this.parent.setEditingState(true);
this.parent.div.classList.add("freeTextEditing");
}
super.remove();
}

Expand Down
86 changes: 85 additions & 1 deletion test/integration/freetext_editor_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ describe("FreeText Editor", () => {
await page.click("#editorFreeText");
let currentId = 0;
const expected = [];
const oneToFourteen = [...new Array(14).keys()].map(x => x + 1);
const oneToFourteen = Array.from(new Array(14).keys(), x => x + 1);

for (const pageNumber of oneToFourteen) {
const pageSelector = `.page[data-page-number = "${pageNumber}"]`;
Expand Down Expand Up @@ -1262,4 +1262,88 @@ describe("FreeText Editor", () => {
);
});
});

describe("FreeText (remove)", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("tracemonkey.pdf", ".annotationEditorLayer");
});

afterAll(async () => {
await closePages(pages);
});

it("must delete invisible annotations", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.click("#editorFreeText");
let currentId = 0;
const oneToFourteen = Array.from(new Array(14).keys(), x => x + 1);

for (const pageNumber of oneToFourteen) {
const pageSelector = `.page[data-page-number = "${pageNumber}"]`;

await page.evaluate(selector => {
const element = window.document.querySelector(selector);
element.scrollIntoView();
}, pageSelector);

const annotationLayerSelector = `${pageSelector} > .annotationEditorLayer`;
await page.waitForSelector(annotationLayerSelector, {
visible: true,
timeout: 0,
});
await page.waitForTimeout(50);
if (![1, 14].includes(pageNumber)) {
continue;
}

const rect = await page.$eval(annotationLayerSelector, el => {
// With Chrome something is wrong when serializing a DomRect,
// hence we extract the values and just return them.
const { x, y } = el.getBoundingClientRect();
return { x, y };
});

const data = `Hello PDF.js World !! on page ${pageNumber}`;
await page.mouse.click(rect.x + 100, rect.y + 100);
await page.type(`${getEditorSelector(currentId)} .internal`, data);

// Commit.
await page.keyboard.press("Escape");
await page.waitForTimeout(10);

currentId += 1;
}

// Select all.
await page.keyboard.down("Control");
await page.keyboard.press("a");
await page.keyboard.up("Control");
await page.waitForTimeout(10);

const serialize = () =>
page.evaluate(() => {
const { map } =
window.PDFViewerApplication.pdfDocument.annotationStorage
.serializable;
return map ? Array.from(map.values(), x => x.pageIndex) : [];
});

expect(await serialize())
.withContext(`In ${browserName}`)
.toEqual([0, 13]);

// Delete
await page.keyboard.press("Backspace");
await page.waitForTimeout(10);

expect(await serialize())
.withContext(`In ${browserName}`)
.toEqual([]);
})
);
});
});
});

0 comments on commit e4b4d22

Please sign in to comment.