[Editor] When in non-editing mode, add a new editor only once the editing mode has switched

Switching to an editing mode can be asynchronous (e.g. if an editable annotation exists on a
visible page), so we must add a new editor only when the page rendering is done.
This commit is contained in:
Calixte Denizet 2024-07-15 21:04:47 +02:00
parent f9e3b6bcc4
commit 6dd75c0e62
5 changed files with 68 additions and 10 deletions

View file

@ -916,6 +916,18 @@ class AnnotationEditorUIManager {
this.#altTextManager?.editAltText(this, editor); this.#altTextManager?.editAltText(this, editor);
} }
switchToMode(mode, callback) {
// Switching to a mode can be asynchronous.
this._eventBus.on("annotationeditormodechanged", callback, {
once: true,
signal: this._signal,
});
this._eventBus.dispatch("showannotationeditorui", {
source: this,
mode,
});
}
onPageChanging({ pageNumber }) { onPageChanging({ pageNumber }) {
this.#currentPageIndex = pageNumber - 1; this.#currentPageIndex = pageNumber - 1;
} }
@ -1002,16 +1014,11 @@ class AnnotationEditorUIManager {
return; return;
} }
selection.empty(); selection.empty();
if (this.#mode === AnnotationEditorType.NONE) {
this._eventBus.dispatch("showannotationeditorui", {
source: this,
mode: AnnotationEditorType.HIGHLIGHT,
});
this.showAllEditors("highlight", true, /* updateButton = */ true);
}
const layer = this.#getLayerForTextLayer(textLayer); const layer = this.#getLayerForTextLayer(textLayer);
if (layer) { const isNoneMode = this.#mode === AnnotationEditorType.NONE;
layer.createAndAddNewEditor({ x: 0, y: 0 }, false, { const callback = () => {
layer?.createAndAddNewEditor({ x: 0, y: 0 }, false, {
methodOfCreation, methodOfCreation,
boxes, boxes,
anchorNode, anchorNode,
@ -1020,7 +1027,15 @@ class AnnotationEditorUIManager {
focusOffset, focusOffset,
text, text,
}); });
if (isNoneMode) {
this.showAllEditors("highlight", true, /* updateButton = */ true);
} }
};
if (isNoneMode) {
this.switchToMode(AnnotationEditorType.HIGHLIGHT, callback);
return;
}
callback();
} }
#displayHighlightToolbar() { #displayHighlightToolbar() {

View file

@ -1890,4 +1890,46 @@ describe("Highlight Editor", () => {
); );
}); });
}); });
describe("Highlight with the floating button in a pdf containing a FreeText", () => {
let pages;
beforeAll(async () => {
pages = await loadAndWait(
"file_pdfjs_test.pdf",
".annotationEditorLayer",
null,
null,
{ highlightEditorColors: "red=#AB0000" }
);
});
afterAll(async () => {
await closePages(pages);
});
it("must check that the highlight is created", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
const rect = await getSpanRectFromText(page, 1, "In production");
const x = rect.x + rect.width / 2;
const y = rect.y + rect.height / 2;
await page.mouse.click(x, y, { count: 3, delay: 100 });
await page.waitForSelector(".textLayer .highlightButton");
await page.click(".textLayer .highlightButton");
await page.waitForSelector(getEditorSelector(0));
const usedColor = await page.evaluate(() => {
const highlight = document.querySelector(
`.page[data-page-number = "1"] .canvasWrapper > svg.highlight`
);
return highlight.getAttribute("fill");
});
expect(usedColor).withContext(`In ${browserName}`).toEqual("#AB0000");
})
);
});
});
}); });

View file

@ -657,3 +657,4 @@
!issue18305.pdf !issue18305.pdf
!issue18360.pdf !issue18360.pdf
!issue18099_reduced.pdf !issue18099_reduced.pdf
!file_pdfjs_test.pdf

BIN
test/pdfs/file_pdfjs_test.pdf Executable file

Binary file not shown.

View file

@ -2311,11 +2311,11 @@ class PDFViewer {
const updater = () => { const updater = () => {
this.#cleanupSwitchAnnotationEditorMode(); this.#cleanupSwitchAnnotationEditorMode();
this.#annotationEditorMode = mode; this.#annotationEditorMode = mode;
this.#annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
eventBus.dispatch("annotationeditormodechanged", { eventBus.dispatch("annotationeditormodechanged", {
source: this, source: this,
mode, mode,
}); });
this.#annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
}; };
if ( if (