Skip to content

Commit 6d2c6cf

Browse files
committed
[Editor] Allow to change the editor mode when selecting the corresponding editor (bug 1975538)
For example, selecting an ink editor just after having created a freetext will switch to ink mode.
1 parent ad31385 commit 6d2c6cf

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

src/display/annotation_layer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ class AnnotationElement {
730730
source: this,
731731
mode,
732732
editId,
733+
mustEnterInEditMode: true,
733734
});
734735
});
735736
}

src/display/editor/editor.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ class AnnotationEditor {
205205
return Object.getPrototypeOf(this).constructor._type;
206206
}
207207

208+
get mode() {
209+
return Object.getPrototypeOf(this).constructor._editorType;
210+
}
211+
208212
static get isDrawer() {
209213
return false;
210214
}

src/display/editor/tools.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,8 +1684,15 @@ class AnnotationEditorUIManager {
16841684
* @param {string|null} editId
16851685
* @param {boolean} [isFromKeyboard] - true if the mode change is due to a
16861686
* keyboard action.
1687+
* @param {boolean} [mustEnterInEditMode] - true if the editor must enter in
1688+
* edit mode.
16871689
*/
1688-
async updateMode(mode, editId = null, isFromKeyboard = false) {
1690+
async updateMode(
1691+
mode,
1692+
editId = null,
1693+
isFromKeyboard = false,
1694+
mustEnterInEditMode = false
1695+
) {
16891696
if (this.#mode === mode) {
16901697
return;
16911698
}
@@ -1732,7 +1739,9 @@ class AnnotationEditorUIManager {
17321739
for (const editor of this.#allEditors.values()) {
17331740
if (editor.annotationElementId === editId || editor.id === editId) {
17341741
this.setSelected(editor);
1735-
editor.enterInEditMode();
1742+
if (mustEnterInEditMode) {
1743+
editor.enterInEditMode();
1744+
}
17361745
} else {
17371746
editor.unselect();
17381747
}
@@ -2036,6 +2045,11 @@ class AnnotationEditorUIManager {
20362045
* @param {AnnotationEditor} editor
20372046
*/
20382047
setSelected(editor) {
2048+
this.updateToolbar({
2049+
mode: editor.mode,
2050+
editId: editor.id,
2051+
});
2052+
20392053
this.#currentDrawingSession?.commitOrRemove();
20402054
for (const ed of this.#selectedEditors) {
20412055
if (ed !== editor) {

test/integration/freetext_editor_spec.mjs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
unselectEditor,
5050
waitForAnnotationEditorLayer,
5151
waitForAnnotationModeChanged,
52+
waitForPointerUp,
5253
waitForSelectedEditor,
5354
waitForSerialized,
5455
waitForStorageEntries,
@@ -3392,5 +3393,58 @@ describe("FreeText Editor", () => {
33923393
})
33933394
);
33943395
});
3396+
3397+
it("must check that we switch to FreeText in clicking on a FreeText annotation", async () => {
3398+
await Promise.all(
3399+
pages.map(async ([browserName, page]) => {
3400+
await switchToFreeText(page);
3401+
3402+
const rect = await getRect(page, ".annotationEditorLayer");
3403+
const editorSelector = getEditorSelector(0);
3404+
3405+
const data = "Hello PDF.js World !!";
3406+
await page.mouse.click(
3407+
rect.x + rect.width / 2,
3408+
rect.y + rect.height / 2
3409+
);
3410+
await page.waitForSelector(editorSelector, { visible: true });
3411+
await page.type(`${editorSelector} .internal`, data);
3412+
await commit(page);
3413+
await waitForSerialized(page, 1);
3414+
3415+
await switchToFreeText(page, /* disable */ true);
3416+
await switchToEditor("Ink", page);
3417+
3418+
const x = rect.x + 100;
3419+
const y = rect.y + 100;
3420+
const clickHandle = await waitForPointerUp(page);
3421+
await page.mouse.move(x, y);
3422+
await page.mouse.down();
3423+
await page.mouse.move(x + 50, y + 50);
3424+
await page.mouse.up();
3425+
await awaitPromise(clickHandle);
3426+
await page.keyboard.press("Escape");
3427+
await page.waitForSelector(
3428+
".inkEditor.selectedEditor.draggable.disabled"
3429+
);
3430+
await waitForSerialized(page, 2);
3431+
3432+
const modeChangedHandle = await createPromise(page, resolve => {
3433+
window.PDFViewerApplication.eventBus.on(
3434+
"annotationeditormodechanged",
3435+
resolve,
3436+
{ once: true }
3437+
);
3438+
});
3439+
const editorRect = await getRect(page, editorSelector);
3440+
await page.mouse.click(
3441+
editorRect.x + editorRect.width / 2,
3442+
editorRect.y + editorRect.height / 2
3443+
);
3444+
await page.waitForSelector(".annotationEditorLayer.freetextEditing");
3445+
await awaitPromise(modeChangedHandle);
3446+
})
3447+
);
3448+
});
33953449
});
33963450
});

web/pdf_viewer.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,12 +2401,19 @@ class PDFViewer {
24012401
* @property {string|null} [editId] - ID of the existing annotation to edit.
24022402
* @property {boolean} [isFromKeyboard] - True if the mode change is due to a
24032403
* keyboard action.
2404+
* @property {boolean} [mustEnterInEditMode] - True if the editor must enter
2405+
* edit mode.
24042406
*/
24052407

24062408
/**
24072409
* @param {AnnotationEditorModeOptions} options
24082410
*/
2409-
set annotationEditorMode({ mode, editId = null, isFromKeyboard = false }) {
2411+
set annotationEditorMode({
2412+
mode,
2413+
editId = null,
2414+
isFromKeyboard = false,
2415+
mustEnterInEditMode = false,
2416+
}) {
24102417
if (!this.#annotationEditorUIManager) {
24112418
throw new Error(`The AnnotationEditor is not enabled.`);
24122419
}
@@ -2428,7 +2435,8 @@ class PDFViewer {
24282435
await this.#annotationEditorUIManager.updateMode(
24292436
mode,
24302437
editId,
2431-
isFromKeyboard
2438+
isFromKeyboard,
2439+
mustEnterInEditMode
24322440
);
24332441
if (
24342442
mode !== this.#annotationEditorMode ||

0 commit comments

Comments
 (0)