[Editor] Make sure everything is cleaned up when we switch to annotation editor mode

This commit is contained in:
Calixte Denizet 2024-07-02 15:49:54 +02:00
parent bdcc4a0feb
commit 68175323f1
2 changed files with 16 additions and 14 deletions

View file

@ -135,6 +135,7 @@ function getSelector(id) {
async function getRect(page, selector) { async function getRect(page, selector) {
// In Chrome something is wrong when serializing a `DomRect`, // In Chrome something is wrong when serializing a `DomRect`,
// so we extract the values and return them ourselves. // so we extract the values and return them ourselves.
await page.waitForSelector(selector);
return page.$eval(selector, el => { return page.$eval(selector, el => {
const { x, y, width, height } = el.getBoundingClientRect(); const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height }; return { x, y, width, height };

View file

@ -1122,9 +1122,7 @@ class PDFViewer {
this.#hiddenCopyElement?.remove(); this.#hiddenCopyElement?.remove();
this.#hiddenCopyElement = null; this.#hiddenCopyElement = null;
this.#onPageRenderedCallback = null; this.#cleanupSwitchAnnotationEditorMode();
clearTimeout(this.#switchAnnotationEditorModeTimeoutId);
this.#switchAnnotationEditorModeTimeoutId = null;
} }
#ensurePageViewVisible() { #ensurePageViewVisible() {
@ -2263,6 +2261,17 @@ class PDFViewer {
]); ]);
} }
#cleanupSwitchAnnotationEditorMode() {
if (this.#onPageRenderedCallback) {
this.eventBus._off("pagerendered", this.#onPageRenderedCallback);
this.#onPageRenderedCallback = null;
}
if (this.#switchAnnotationEditorModeTimeoutId !== null) {
clearTimeout(this.#switchAnnotationEditorModeTimeoutId);
this.#switchAnnotationEditorModeTimeoutId = null;
}
}
get annotationEditorMode() { get annotationEditorMode() {
return this.#annotationEditorUIManager return this.#annotationEditorUIManager
? this.#annotationEditorMode ? this.#annotationEditorMode
@ -2296,14 +2305,7 @@ class PDFViewer {
const { eventBus } = this; const { eventBus } = this;
const updater = () => { const updater = () => {
if (this.#onPageRenderedCallback) { this.#cleanupSwitchAnnotationEditorMode();
eventBus._off("pagerendered", this.#onPageRenderedCallback);
this.#onPageRenderedCallback = null;
}
if (this.#switchAnnotationEditorModeTimeoutId !== null) {
clearTimeout(this.#switchAnnotationEditorModeTimeoutId);
this.#switchAnnotationEditorModeTimeoutId = null;
}
this.#annotationEditorMode = mode; this.#annotationEditorMode = mode;
eventBus.dispatch("annotationeditormodechanged", { eventBus.dispatch("annotationeditormodechanged", {
source: this, source: this,
@ -2329,15 +2331,14 @@ class PDFViewer {
if (isEditing && editId && idsToRefresh) { if (isEditing && editId && idsToRefresh) {
// We're editing an existing annotation so we must switch to editing // We're editing an existing annotation so we must switch to editing
// mode when the rendering is done. // mode when the rendering is done.
const { signal } = this.#eventAbortController; this.#cleanupSwitchAnnotationEditorMode();
this.#onPageRenderedCallback = ({ pageNumber }) => { this.#onPageRenderedCallback = ({ pageNumber }) => {
idsToRefresh.delete(pageNumber); idsToRefresh.delete(pageNumber);
if (idsToRefresh.size === 0) { if (idsToRefresh.size === 0) {
eventBus._off("pagerendered", this.#onPageRenderedCallback);
this.#onPageRenderedCallback = null;
this.#switchAnnotationEditorModeTimeoutId = setTimeout(updater, 0); this.#switchAnnotationEditorModeTimeoutId = setTimeout(updater, 0);
} }
}; };
const { signal } = this.#eventAbortController;
eventBus._on("pagerendered", this.#onPageRenderedCallback, { signal }); eventBus._on("pagerendered", this.#onPageRenderedCallback, { signal });
return; return;
} }