Merge pull request #18881 from Snuffleupagus/AltTextManager-rm-events-AbortSignal

Remove event listeners with `AbortSignal` in the `AltTextManager` class
This commit is contained in:
Tim van der Meij 2024-10-11 19:37:00 +02:00 committed by GitHub
commit c3af34271e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,14 +13,10 @@
* limitations under the License. * limitations under the License.
*/ */
import { DOMSVGFactory, shadow } from "pdfjs-lib"; import { DOMSVGFactory } from "pdfjs-lib";
class AltTextManager { class AltTextManager {
#boundUpdateUIState = this.#updateUIState.bind(this); #clickAC = null;
#boundSetPosition = this.#setPosition.bind(this);
#boundOnClick = this.#onClick.bind(this);
#currentEditor = null; #currentEditor = null;
@ -46,6 +42,8 @@ class AltTextManager {
#previousAltText = null; #previousAltText = null;
#resizeAC = null;
#svgElement = null; #svgElement = null;
#rectElement = null; #rectElement = null;
@ -77,6 +75,8 @@ class AltTextManager {
this.#eventBus = eventBus; this.#eventBus = eventBus;
this.#container = container; this.#container = container;
const onUpdateUIState = this.#updateUIState.bind(this);
dialog.addEventListener("close", this.#close.bind(this)); dialog.addEventListener("close", this.#close.bind(this));
dialog.addEventListener("contextmenu", event => { dialog.addEventListener("contextmenu", event => {
if (event.target !== this.#textarea) { if (event.target !== this.#textarea) {
@ -85,22 +85,12 @@ class AltTextManager {
}); });
cancelButton.addEventListener("click", this.#finish.bind(this)); cancelButton.addEventListener("click", this.#finish.bind(this));
saveButton.addEventListener("click", this.#save.bind(this)); saveButton.addEventListener("click", this.#save.bind(this));
optionDescription.addEventListener("change", this.#boundUpdateUIState); optionDescription.addEventListener("change", onUpdateUIState);
optionDecorative.addEventListener("change", this.#boundUpdateUIState); optionDecorative.addEventListener("change", onUpdateUIState);
this.#overlayManager.register(dialog); this.#overlayManager.register(dialog);
} }
get _elements() {
return shadow(this, "_elements", [
this.#optionDescription,
this.#optionDecorative,
this.#textarea,
this.#saveButton,
this.#cancelButton,
]);
}
#createSVGElement() { #createSVGElement() {
if (this.#svgElement) { if (this.#svgElement) {
return; return;
@ -138,12 +128,21 @@ class AltTextManager {
if (this.#currentEditor || !editor) { if (this.#currentEditor || !editor) {
return; return;
} }
this.#createSVGElement(); this.#createSVGElement();
this.#hasUsedPointer = false; this.#hasUsedPointer = false;
for (const element of this._elements) {
element.addEventListener("click", this.#boundOnClick); this.#clickAC = new AbortController();
const clickOpts = { signal: this.#clickAC.signal },
onClick = this.#onClick.bind(this);
for (const element of [
this.#optionDescription,
this.#optionDecorative,
this.#textarea,
this.#saveButton,
this.#cancelButton,
]) {
element.addEventListener("click", onClick, clickOpts);
} }
const { altText, decorative } = editor.altTextData; const { altText, decorative } = editor.altTextData;
@ -160,7 +159,11 @@ class AltTextManager {
this.#currentEditor = editor; this.#currentEditor = editor;
this.#uiManager = uiManager; this.#uiManager = uiManager;
this.#uiManager.removeEditListeners(); this.#uiManager.removeEditListeners();
this.#eventBus._on("resize", this.#boundSetPosition);
this.#resizeAC = new AbortController();
this.#eventBus._on("resize", this.#setPosition.bind(this), {
signal: this.#resizeAC.signal,
});
try { try {
await this.#overlayManager.open(this.#dialog); await this.#overlayManager.open(this.#dialog);
@ -258,7 +261,8 @@ class AltTextManager {
this.#removeOnClickListeners(); this.#removeOnClickListeners();
this.#uiManager?.addEditListeners(); this.#uiManager?.addEditListeners();
this.#eventBus._off("resize", this.#boundSetPosition); this.#resizeAC?.abort();
this.#resizeAC = null;
this.#currentEditor.altTextFinish(); this.#currentEditor.altTextFinish();
this.#currentEditor = null; this.#currentEditor = null;
this.#uiManager = null; this.#uiManager = null;
@ -295,9 +299,8 @@ class AltTextManager {
} }
#removeOnClickListeners() { #removeOnClickListeners() {
for (const element of this._elements) { this.#clickAC?.abort();
element.removeEventListener("click", this.#boundOnClick); this.#clickAC = null;
}
} }
destroy() { destroy() {