1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

[Editor] Avoid an exception when undoing the deletion of a pre-existing annotation

It fixes #18849.

When such an annotation is deleted, we make sure that there are some data
to restore.
The version of this patch was making undoing a svg deletion buggy, so it's fixed now and
an integration test has been added for this case.
This commit is contained in:
Calixte Denizet 2024-10-04 17:07:25 +02:00
parent e5b6144bfa
commit b6e60d033a
3 changed files with 117 additions and 8 deletions

View file

@ -837,6 +837,9 @@ class StampEditor extends AnnotationEditor {
editor.altTextData = accessibilityData;
}
editor._initialData = initialData;
// No need to be add in the undo stack if the editor is created from an
// existing one.
editor.#hasBeenAddedInUndoStack = !!initialData;
return editor;
}

View file

@ -131,8 +131,10 @@ class ImageManager {
if (typeof rawData === "string") {
data.url = rawData;
image = await fetchData(rawData, "blob");
} else {
} else if (rawData instanceof File) {
image = data.file = rawData;
} else if (rawData instanceof Blob) {
image = rawData;
}
if (image.type === "image/svg+xml") {
@ -183,6 +185,11 @@ class ImageManager {
return this.#get(url, url);
}
async getFromBlob(id, blobPromise) {
const blob = await blobPromise;
return this.#get(id, blob);
}
async getFromId(id) {
this.#cache ||= new Map();
const data = this.#cache.get(id);
@ -197,6 +204,11 @@ class ImageManager {
if (data.file) {
return this.getFromFile(data.file);
}
if (data.blobPromise) {
const { blobPromise } = data;
delete data.blobPromise;
return this.getFromBlob(data.id, blobPromise);
}
return this.getFromUrl(data.url);
}
@ -239,7 +251,16 @@ class ImageManager {
if (data.refCounter !== 0) {
return;
}
data.bitmap.close?.();
const { bitmap } = data;
if (!data.url && !data.file) {
// The image has no way to be restored (ctrl+z) so we must fix that.
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
const ctx = canvas.getContext("bitmaprenderer");
ctx.transferFromImageBitmap(bitmap);
data.blobPromise = canvas.convertToBlob();
}
bitmap.close?.();
data.bitmap = null;
}