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] Fix the position in the page of a drawing after it has been moved with the keyboard #19538

Merged
merged 1 commit into from
Feb 22, 2025
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
4 changes: 2 additions & 2 deletions src/display/editor/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ class DrawingEditor extends AnnotationEditor {
}

/** @inheritdoc */
_onTranslating(x, y) {
_onTranslating(_x, _y) {
this.parent?.drawLayer.updateProperties(this._drawId, {
bbox: this.#rotateBox(x, y),
bbox: this.#rotateBox(),
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ class AnnotationEditor {
this.div.scrollIntoView({ block: "nearest" });
}

translationDone() {
this._onTranslated(this.x, this.y);
}

drag(tx, ty) {
this.#initialRect ||= [this.x, this.y, this.width, this.height];
const {
Expand Down
3 changes: 3 additions & 0 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2264,13 +2264,15 @@ class AnnotationEditorUIManager {
for (const editor of editors) {
if (this.#allEditors.has(editor.id)) {
editor.translateInPage(totalX, totalY);
editor.translationDone();
}
}
},
undo: () => {
for (const editor of editors) {
if (this.#allEditors.has(editor.id)) {
editor.translateInPage(-totalX, -totalY);
editor.translationDone();
}
}
},
Expand All @@ -2280,6 +2282,7 @@ class AnnotationEditorUIManager {

for (const editor of editors) {
editor.translateInPage(x, y);
editor.translationDone();
}
}

Expand Down
29 changes: 1 addition & 28 deletions test/integration/freetext_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
kbSelectAll,
kbUndo,
loadAndWait,
moveEditor,
paste,
pasteFromClipboard,
scrollIntoView,
Expand All @@ -48,7 +49,6 @@ import {
unselectEditor,
waitForAnnotationEditorLayer,
waitForAnnotationModeChanged,
waitForEditorMovedInDOM,
waitForSelectedEditor,
waitForSerialized,
waitForStorageEntries,
Expand Down Expand Up @@ -79,33 +79,6 @@ const commit = async page => {

const switchToFreeText = switchToEditor.bind(null, "FreeText");

const getXY = async (page, selector) => {
const rect = await getRect(page, selector);
return `${rect.x}::${rect.y}`;
};

const waitForPositionChange = (page, selector, xy) =>
page.waitForFunction(
(sel, currentXY) => {
const bbox = document.querySelector(sel).getBoundingClientRect();
return `${bbox.x}::${bbox.y}` !== currentXY;
},
{},
selector,
xy
);

const moveEditor = async (page, selector, n, pressKey) => {
let xy = await getXY(page, selector);
for (let i = 0; i < n; i++) {
const handle = await waitForEditorMovedInDOM(page);
await pressKey();
await awaitPromise(handle);
await waitForPositionChange(page, selector, xy);
xy = await getXY(page, selector);
}
};

const cancelFocusIn = async (page, selector) => {
page.evaluate(sel => {
const el = document.querySelector(sel);
Expand Down
45 changes: 45 additions & 0 deletions test/integration/ink_editor_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
kbSelectAll,
kbUndo,
loadAndWait,
moveEditor,
scrollIntoView,
selectEditor,
switchToEditor,
Expand Down Expand Up @@ -142,6 +143,50 @@ describe("Ink Editor", () => {
})
);
});

it("must draw and move with the keyboard", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await switchToInk(page);

const rect = await getRect(page, ".annotationEditorLayer");

const x = rect.x + 100;
const y = rect.y + 100;
const clickHandle = await waitForPointerUp(page);
await page.mouse.move(x, y);
await page.mouse.down();
await page.mouse.move(x + 50, y + 50);
await page.mouse.up();
await awaitPromise(clickHandle);

await commit(page);

const editorSelector = getEditorSelector(0);
await page.waitForSelector(editorSelector);
const rectBefore = (await getSerialized(page, s => s.rect))[0];

const N = 20;
await moveEditor(page, editorSelector, N, () =>
page.keyboard.press("ArrowDown")
);
const rectAfter = (await getSerialized(page, s => s.rect))[0];

expect(Math.abs(rectBefore[0] - rectAfter[0]))
.withContext(`In ${browserName}`)
.toBeLessThan(1e-2);
expect(Math.abs(rectBefore[1] - N - rectAfter[1]))
.withContext(`In ${browserName}`)
.toBeLessThan(1e-2);
expect(Math.abs(rectBefore[2] - rectAfter[2]))
.withContext(`In ${browserName}`)
.toBeLessThan(1e-2);
expect(Math.abs(rectBefore[3] - N - rectAfter[3]))
.withContext(`In ${browserName}`)
.toBeLessThan(1e-2);
})
);
});
});

describe("with a rotated pdf", () => {
Expand Down
29 changes: 29 additions & 0 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,34 @@ async function cleanupEditing(pages, switcher) {
}
}

async function getXY(page, selector) {
const rect = await getRect(page, selector);
return `${rect.x}::${rect.y}`;
}

function waitForPositionChange(page, selector, xy) {
return page.waitForFunction(
(sel, currentXY) => {
const bbox = document.querySelector(sel).getBoundingClientRect();
return `${bbox.x}::${bbox.y}` !== currentXY;
},
{},
selector,
xy
);
}

async function moveEditor(page, selector, n, pressKey) {
let xy = await getXY(page, selector);
for (let i = 0; i < n; i++) {
const handle = await waitForEditorMovedInDOM(page);
await pressKey();
await awaitPromise(handle);
await waitForPositionChange(page, selector, xy);
xy = await getXY(page, selector);
}
}

export {
applyFunctionToEditor,
awaitPromise,
Expand Down Expand Up @@ -893,6 +921,7 @@ export {
kbUndo,
loadAndWait,
mockClipboard,
moveEditor,
paste,
pasteFromClipboard,
scrollIntoView,
Expand Down