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] When in non-editing mode, add a new editor only once the editing mode has switched #18440

Merged
merged 1 commit into from
Jul 16, 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
33 changes: 24 additions & 9 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,18 @@ class AnnotationEditorUIManager {
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 }) {
this.#currentPageIndex = pageNumber - 1;
}
Expand Down Expand Up @@ -1002,16 +1014,11 @@ class AnnotationEditorUIManager {
return;
}
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);
if (layer) {
layer.createAndAddNewEditor({ x: 0, y: 0 }, false, {
const isNoneMode = this.#mode === AnnotationEditorType.NONE;
const callback = () => {
layer?.createAndAddNewEditor({ x: 0, y: 0 }, false, {
methodOfCreation,
boxes,
anchorNode,
Expand All @@ -1020,7 +1027,15 @@ class AnnotationEditorUIManager {
focusOffset,
text,
});
if (isNoneMode) {
this.showAllEditors("highlight", true, /* updateButton = */ true);
}
};
if (isNoneMode) {
this.switchToMode(AnnotationEditorType.HIGHLIGHT, callback);
return;
}
callback();
}

#displayHighlightToolbar() {
Expand Down
42 changes: 42 additions & 0 deletions test/integration/highlight_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,4 @@
!issue18305.pdf
!issue18360.pdf
!issue18099_reduced.pdf
!file_pdfjs_test.pdf
Binary file added test/pdfs/file_pdfjs_test.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2311,11 +2311,11 @@ class PDFViewer {
const updater = () => {
this.#cleanupSwitchAnnotationEditorMode();
this.#annotationEditorMode = mode;
this.#annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
eventBus.dispatch("annotationeditormodechanged", {
source: this,
mode,
});
this.#annotationEditorUIManager.updateMode(mode, editId, isFromKeyboard);
};

if (
Expand Down