mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +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:
commit
a372bf8f4d
3 changed files with 59 additions and 33 deletions
|
@ -133,9 +133,52 @@ class StampEditor extends AnnotationEditor {
|
||||||
) {
|
) {
|
||||||
this._editToolbar.hide();
|
this._editToolbar.hide();
|
||||||
this._uiManager.editAltText(this, /* firstTime = */ true);
|
this._uiManager.editAltText(this, /* firstTime = */ true);
|
||||||
} else {
|
return;
|
||||||
this.div.focus();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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() {
|
#getBitmap() {
|
||||||
|
@ -370,6 +413,13 @@ class StampEditor extends AnnotationEditor {
|
||||||
}
|
}
|
||||||
|
|
||||||
copyCanvas(maxDimension, createImageData = false) {
|
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 { width: bitmapWidth, height: bitmapHeight } = this.#bitmap;
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
get mlManager() {
|
||||||
return this.#mlManager;
|
return this.#mlManager;
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,29 +240,17 @@ class NewAltTextManager {
|
||||||
|
|
||||||
let hasError = false;
|
let hasError = false;
|
||||||
try {
|
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
|
// 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.
|
// that the alt text dialog can have been closed before the response is.
|
||||||
const response = await this.#uiManager.mlGuess({
|
|
||||||
name: "altText",
|
const altText = await this.#currentEditor.mlGuessAltText(
|
||||||
request: {
|
this.#imageData,
|
||||||
data,
|
/* updateAltTextData = */ false
|
||||||
width,
|
);
|
||||||
height,
|
if (altText === null) {
|
||||||
channels: data.length / (width * height),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
if (!response || response.error || !response.output) {
|
|
||||||
throw new Error("No valid response from the AI service.");
|
throw new Error("No valid response from the AI service.");
|
||||||
}
|
}
|
||||||
const altText = (this.#guessedAltText = response.output);
|
this.#guessedAltText = altText;
|
||||||
await editor.setGuessedAltText(altText);
|
|
||||||
this.#wasAILoading = this.#isAILoading;
|
this.#wasAILoading = this.#isAILoading;
|
||||||
if (this.#isAILoading) {
|
if (this.#isAILoading) {
|
||||||
this.#addAltText(altText);
|
this.#addAltText(altText);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue