Skip to content
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

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

Merged
merged 1 commit into from
Jul 2, 2024
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
1 change: 1 addition & 0 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function getSelector(id) {
async function getRect(page, selector) {
// In Chrome something is wrong when serializing a `DomRect`,
// so we extract the values and return them ourselves.
await page.waitForSelector(selector);
Copy link
Contributor

@timvandermeij timvandermeij Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for including this! It fixes one of the last few intermittents we had left, and makes the tests that use this utility function safe by default.

return page.$eval(selector, el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
Expand Down
29 changes: 15 additions & 14 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,9 +1122,7 @@ class PDFViewer {
this.#hiddenCopyElement?.remove();
this.#hiddenCopyElement = null;

this.#onPageRenderedCallback = null;
clearTimeout(this.#switchAnnotationEditorModeTimeoutId);
this.#switchAnnotationEditorModeTimeoutId = null;
this.#cleanupSwitchAnnotationEditorMode();
}

#ensurePageViewVisible() {
Expand Down Expand Up @@ -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() {
return this.#annotationEditorUIManager
? this.#annotationEditorMode
Expand Down Expand Up @@ -2296,14 +2305,7 @@ class PDFViewer {

const { eventBus } = this;
const updater = () => {
if (this.#onPageRenderedCallback) {
eventBus._off("pagerendered", this.#onPageRenderedCallback);
this.#onPageRenderedCallback = null;
}
if (this.#switchAnnotationEditorModeTimeoutId !== null) {
clearTimeout(this.#switchAnnotationEditorModeTimeoutId);
this.#switchAnnotationEditorModeTimeoutId = null;
}
this.#cleanupSwitchAnnotationEditorMode();
this.#annotationEditorMode = mode;
eventBus.dispatch("annotationeditormodechanged", {
source: this,
Expand All @@ -2329,15 +2331,14 @@ class PDFViewer {
if (isEditing && editId && idsToRefresh) {
// We're editing an existing annotation so we must switch to editing
// mode when the rendering is done.
const { signal } = this.#eventAbortController;
this.#cleanupSwitchAnnotationEditorMode();
this.#onPageRenderedCallback = ({ pageNumber }) => {
idsToRefresh.delete(pageNumber);
if (idsToRefresh.size === 0) {
eventBus._off("pagerendered", this.#onPageRenderedCallback);
this.#onPageRenderedCallback = null;
this.#switchAnnotationEditorModeTimeoutId = setTimeout(updater, 0);
}
};
const { signal } = this.#eventAbortController;
eventBus._on("pagerendered", this.#onPageRenderedCallback, { signal });
return;
}
Expand Down