Merge pull request #16561 from calixteman/editor_copy_existing

[Editor] Avoid an exception when copying an existing editor
This commit is contained in:
calixteman 2023-06-16 14:47:55 +02:00 committed by GitHub
commit 46b8f9e2f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 8 deletions

View File

@ -493,8 +493,9 @@ class AnnotationEditor {
* new annotation to add to the pdf document. * new annotation to add to the pdf document.
* *
* To implement in subclasses. * To implement in subclasses.
* @param {boolean} isForCopying
*/ */
serialize() { serialize(_isForCopying = false) {
unreachable("An editor must be serializable"); unreachable("An editor must be serializable");
} }

View File

@ -567,7 +567,7 @@ class FreeTextEditor extends AnnotationEditor {
} }
/** @inheritdoc */ /** @inheritdoc */
serialize() { serialize(isForCopying = false) {
if (this.isEmpty()) { if (this.isEmpty()) {
return null; return null;
} }
@ -596,13 +596,20 @@ class FreeTextEditor extends AnnotationEditor {
pageIndex: this.pageIndex, pageIndex: this.pageIndex,
rect, rect,
rotation: this.rotation, rotation: this.rotation,
id: this.annotationElementId,
}; };
if (isForCopying) {
// Don't add the id when copying because the pasted editor mustn't be
// linked to an existing annotation.
return serialized;
}
if (this.annotationElementId && !this.#hasElementChanged(serialized)) { if (this.annotationElementId && !this.#hasElementChanged(serialized)) {
return null; return null;
} }
serialized.id = this.annotationElementId;
return serialized; return serialized;
} }

View File

@ -555,11 +555,8 @@ class AnnotationEditorUIManager {
const editors = []; const editors = [];
for (const editor of this.#selectedEditors) { for (const editor of this.#selectedEditors) {
if (!editor.isEmpty()) { const serialized = editor.serialize(/* isForCopying = */ true);
const serialized = editor.serialize(); if (serialized) {
// Remove the id from the serialized data because it mustn't be linked
// to an existing annotation.
delete serialized.id;
editors.push(serialized); editors.push(serialized);
} }
} }

View File

@ -1082,4 +1082,38 @@ describe("FreeText Editor", () => {
); );
}); });
}); });
describe("FreeText (copy/paste existing)", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait("freetexts.pdf", ".annotationEditorLayer");
});
afterAll(async () => {
await closePages(pages);
});
it("must copy and paste an existing annotation", async () => {
// Run sequentially to avoid clipboard issues.
for (const [browserName, page] of pages) {
await page.click("#editorFreeText");
const editorIds = await getEditors(page, "freeText");
expect(editorIds.length).withContext(`In ${browserName}`).toEqual(6);
const editorRect = await page.$eval(getEditorSelector(1), el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});
await page.mouse.click(
editorRect.x + editorRect.width / 2,
editorRect.y + editorRect.height / 2
);
await copyPaste(page);
await waitForStorageEntries(page, 7);
}
});
});
}); });