Merge pull request #16840 from Snuffleupagus/Stamp-#getBitmapFetched

Add a helper method to reduce duplication in `StampEditor.#getBitmap`
This commit is contained in:
Jonas Jenwald 2023-08-18 20:38:55 +02:00 committed by GitHub
commit 520397e1e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -87,7 +87,21 @@ class StampEditor extends AnnotationEditor {
}); });
} }
#getBitmapFetched(data, fromId = false) {
if (!data) {
this.remove();
return;
}
this.#bitmap = data.bitmap;
if (!fromId) {
this.#bitmapId = data.id;
this.#isSvg = data.isSvg;
}
this.#createCanvas();
}
#getBitmapDone() { #getBitmapDone() {
this.#bitmapPromise = null;
this._uiManager.enableWaiting(false); this._uiManager.enableWaiting(false);
if (this.#canvas) { if (this.#canvas) {
this.div.focus(); this.div.focus();
@ -99,14 +113,7 @@ class StampEditor extends AnnotationEditor {
this._uiManager.enableWaiting(true); this._uiManager.enableWaiting(true);
this._uiManager.imageManager this._uiManager.imageManager
.getFromId(this.#bitmapId) .getFromId(this.#bitmapId)
.then(data => { .then(data => this.#getBitmapFetched(data, /* fromId = */ true))
if (!data) {
this.remove();
return;
}
this.#bitmap = data.bitmap;
this.#createCanvas();
})
.finally(() => this.#getBitmapDone()); .finally(() => this.#getBitmapDone());
return; return;
} }
@ -117,19 +124,7 @@ class StampEditor extends AnnotationEditor {
this._uiManager.enableWaiting(true); this._uiManager.enableWaiting(true);
this.#bitmapPromise = this._uiManager.imageManager this.#bitmapPromise = this._uiManager.imageManager
.getFromUrl(url) .getFromUrl(url)
.then(data => { .then(data => this.#getBitmapFetched(data))
this.#bitmapPromise = null;
if (!data) {
this.remove();
return;
}
({
bitmap: this.#bitmap,
id: this.#bitmapId,
isSvg: this.#isSvg,
} = data);
this.#createCanvas();
})
.finally(() => this.#getBitmapDone()); .finally(() => this.#getBitmapDone());
return; return;
} }
@ -140,19 +135,7 @@ class StampEditor extends AnnotationEditor {
this._uiManager.enableWaiting(true); this._uiManager.enableWaiting(true);
this.#bitmapPromise = this._uiManager.imageManager this.#bitmapPromise = this._uiManager.imageManager
.getFromFile(file) .getFromFile(file)
.then(data => { .then(data => this.#getBitmapFetched(data))
this.#bitmapPromise = null;
if (!data) {
this.remove();
return;
}
({
bitmap: this.#bitmap,
id: this.#bitmapId,
isSvg: this.#isSvg,
} = data);
this.#createCanvas();
})
.finally(() => this.#getBitmapDone()); .finally(() => this.#getBitmapDone());
return; return;
} }
@ -167,7 +150,6 @@ class StampEditor extends AnnotationEditor {
input.accept = StampEditor.supportedTypesStr; input.accept = StampEditor.supportedTypesStr;
this.#bitmapPromise = new Promise(resolve => { this.#bitmapPromise = new Promise(resolve => {
input.addEventListener("change", async () => { input.addEventListener("change", async () => {
this.#bitmapPromise = null;
if (!input.files || input.files.length === 0) { if (!input.files || input.files.length === 0) {
this.remove(); this.remove();
} else { } else {
@ -175,16 +157,7 @@ class StampEditor extends AnnotationEditor {
const data = await this._uiManager.imageManager.getFromFile( const data = await this._uiManager.imageManager.getFromFile(
input.files[0] input.files[0]
); );
if (!data) { this.#getBitmapFetched(data);
this.remove();
return;
}
({
bitmap: this.#bitmap,
id: this.#bitmapId,
isSvg: this.#isSvg,
} = data);
this.#createCanvas();
} }
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
input.remove(); input.remove();
@ -192,7 +165,6 @@ class StampEditor extends AnnotationEditor {
resolve(); resolve();
}); });
input.addEventListener("cancel", () => { input.addEventListener("cancel", () => {
this.#bitmapPromise = null;
this.remove(); this.remove();
resolve(); resolve();
}); });