Skip to content

Commit

Permalink
Merge pull request #18541 from calixteman/use_ml_but_no_dialog
Browse files Browse the repository at this point in the history
[Editor] Guess alt text even when showing the dialog is disabled
  • Loading branch information
calixteman authored Aug 2, 2024
2 parents 7ac87dd + d2c519e commit a372bf8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
54 changes: 52 additions & 2 deletions src/display/editor/stamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,52 @@ class StampEditor extends AnnotationEditor {
) {
this._editToolbar.hide();
this._uiManager.editAltText(this, /* firstTime = */ true);
} else {
this.div.focus();
return;
}

if (
!this._uiManager.useNewAltTextWhenAddingImage &&
this._uiManager.useNewAltTextFlow &&
this.#bitmap
) {
// The alt-text dialog isn't opened but we still want to guess the alt
// text.
this.mlGuessAltText();
}

this.div.focus();
}

async mlGuessAltText(imageData = null, updateAltTextData = true) {
if (this.hasAltTextData()) {
return null;
}

const { mlManager } = this._uiManager;
if (!mlManager || !(await mlManager.isEnabledFor("altText"))) {
return null;
}
const { data, width, height } =
imageData ||
this.copyCanvas(null, /* createImageData = */ true).imageData;
const response = await mlManager.guess({
name: "altText",
request: {
data,
width,
height,
channels: data.length / (width * height),
},
});
if (!response || response.error || !response.output) {
return null;
}
const altText = response.output;
await this.setGuessedAltText(altText);
if (updateAltTextData && !this.hasAltTextData()) {
this.altTextData = { alt: altText, decorative: false };
}
return altText;
}

#getBitmap() {
Expand Down Expand Up @@ -370,6 +413,13 @@ class StampEditor extends AnnotationEditor {
}

copyCanvas(maxDimension, createImageData = false) {
if (!maxDimension) {
// TODO: get this value from Firefox
// (https://bugzilla.mozilla.org/show_bug.cgi?id=1908184)
// It's the maximum dimension that the AI can handle.
maxDimension = 224;
}

const { width: bitmapWidth, height: bitmapHeight } = this.#bitmap;
const canvas = document.createElement("canvas");

Expand Down
12 changes: 0 additions & 12 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -856,18 +856,6 @@ class AnnotationEditorUIManager {
}
}

hasMLManager() {
return !!this.#mlManager;
}

async mlGuess(data) {
return this.#mlManager?.guess(data) || null;
}

async isMLEnabledFor(name) {
return !!(await this.#mlManager?.isEnabledFor(name));
}

get mlManager() {
return this.#mlManager;
}
Expand Down
26 changes: 7 additions & 19 deletions web/new_alt_text_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a372bf8

Please sign in to comment.