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

Merge pull request #18541 from calixteman/use_ml_but_no_dialog

[Editor] Guess alt text even when showing the dialog is disabled
This commit is contained in:
calixteman 2024-08-02 18:02:53 +02:00 committed by GitHub
commit a372bf8f4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 59 additions and 33 deletions

View file

@ -240,29 +240,17 @@ class NewAltTextManager {
let hasError = false;
try {
const { width, height, data } = this.#imageData;
// Take a reference on the current editor, as it can be set to null (if
// the dialog is closed before the end of the guess).
// But in case we've an alt-text, we want to set it on the editor.
const editor = this.#currentEditor;
// When calling #mlGuessAltText we don't wait for it, so we must take care
// that the alt text dialog can have been closed before the response is.
const response = await this.#uiManager.mlGuess({
name: "altText",
request: {
data,
width,
height,
channels: data.length / (width * height),
},
});
if (!response || response.error || !response.output) {
const altText = await this.#currentEditor.mlGuessAltText(
this.#imageData,
/* updateAltTextData = */ false
);
if (altText === null) {
throw new Error("No valid response from the AI service.");
}
const altText = (this.#guessedAltText = response.output);
await editor.setGuessedAltText(altText);
this.#guessedAltText = altText;
this.#wasAILoading = this.#isAILoading;
if (this.#isAILoading) {
this.#addAltText(altText);