1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Merge pull request #18573 from calixteman/cancel_ai_requests

[Editor] Dispatch changes in prefs enableAltTextModelDownload and enableGuessAltText to the viewer (bug 1912024)
This commit is contained in:
calixteman 2024-08-07 23:11:04 +02:00 committed by GitHub
commit fef2853263
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 164 additions and 53 deletions

View file

@ -141,9 +141,11 @@ class StampEditor extends AnnotationEditor {
this._uiManager.useNewAltTextFlow &&
this.#bitmap
) {
// The alt-text dialog isn't opened but we still want to guess the alt
// text.
this.mlGuessAltText();
try {
// The alt-text dialog isn't opened but we still want to guess the alt
// text.
this.mlGuessAltText();
} catch {}
}
this.div.focus();
@ -155,8 +157,11 @@ class StampEditor extends AnnotationEditor {
}
const { mlManager } = this._uiManager;
if (!mlManager || !(await mlManager.isEnabledFor("altText"))) {
return null;
if (!mlManager) {
throw new Error("No ML.");
}
if (!(await mlManager.isEnabledFor("altText"))) {
throw new Error("ML isn't enabled for alt text.");
}
const { data, width, height } =
imageData ||
@ -170,9 +175,18 @@ class StampEditor extends AnnotationEditor {
channels: data.length / (width * height),
},
});
if (!response || response.error || !response.output) {
if (!response) {
throw new Error("No response from the AI service.");
}
if (response.error) {
throw new Error("Error from the AI service.");
}
if (response.cancel) {
return null;
}
if (!response.output) {
throw new Error("No valid response from the AI service.");
}
const altText = response.output;
await this.setGuessedAltText(altText);
if (updateAltTextData && !this.hasAltTextData()) {