Skip to content

[Editor] Tweak the save flow in the alt-text dialog #17005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,18 +822,17 @@ class AnnotationEditor {
this.fixAndSetPosition();
}

addAltTextButton() {
async addAltTextButton() {
if (this.#altTextButton) {
return;
}
const altText = (this.#altTextButton = document.createElement("button"));
altText.className = "altText";
AnnotationEditor._l10nPromise
.get("editor_alt_text_button_label")
.then(msg => {
altText.textContent = msg;
altText.setAttribute("aria-label", msg);
});
const msg = await AnnotationEditor._l10nPromise.get(
"editor_alt_text_button_label"
);
altText.textContent = msg;
altText.setAttribute("aria-label", msg);
altText.tabIndex = "0";
altText.addEventListener(
"click",
Expand Down Expand Up @@ -864,7 +863,12 @@ class AnnotationEditor {

async #setAltTextButtonState() {
const button = this.#altTextButton;
if (!button || (!this.#altTextDecorative && !this.#altText)) {
if (!button) {
return;
}
if (!this.#altText && !this.#altTextDecorative) {
button.classList.remove("done");
this.#altTextTooltip?.remove();
return;
}
AnnotationEditor._l10nPromise
Expand All @@ -879,7 +883,6 @@ class AnnotationEditor {
tooltip.className = "tooltip";
tooltip.setAttribute("role", "tooltip");
const id = (tooltip.id = `alt-text-tooltip-${this.id}`);
button.append(tooltip);
button.setAttribute("aria-describedby", id);

const DELAY_TO_SHOW_TOOLTIP = 100;
Expand Down Expand Up @@ -911,6 +914,10 @@ class AnnotationEditor {
"editor_alt_text_decorative_tooltip"
)
: this.#altText;

if (!tooltip.parentNode) {
button.append(tooltip);
}
}

getClientDimensions() {
Expand Down Expand Up @@ -1238,6 +1245,12 @@ class AnnotationEditor {
} else {
this._uiManager.removeEditor(this);
}

// The editor is removed so we can remove the alt text button and if it's
// restored then it's up to the subclass to add it back.
this.#altTextButton?.remove();
this.#altTextButton = null;
this.#altTextTooltip = null;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions web/alt_text_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AltTextManager {

#container;

#previousDecorative = null;

constructor(
{
dialog,
Expand Down Expand Up @@ -149,6 +151,7 @@ class AltTextManager {
this.#optionDescription.checked = true;
}
this.#previousAltText = this.#textarea.value = altText?.trim() || "";
this.#previousDecorative = decorative;
this.#updateUIState();

this.#currentEditor = editor;
Expand Down Expand Up @@ -265,11 +268,14 @@ class AltTextManager {
}

#updateUIState() {
const hasAltText = !!this.#textarea.value.trim();
const altText = this.#textarea.value.trim();
const decorative = this.#optionDecorative.checked;

this.#textarea.disabled = decorative;
this.#saveButton.disabled = !decorative && !hasAltText;
this.#saveButton.disabled = !(
this.#previousDecorative !== decorative ||
this.#previousAltText !== altText
);
}

#save() {
Expand Down