1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

[Editor] Fix the position in the page of a drawing after it has been moved with the keyboard

When a drawing was moved with arrow keys and then printed or saved, the drawing wasn't moved finally.
So the fix is just about calling onTranslated once the translation is done.
This commit is contained in:
Calixte Denizet 2025-02-22 15:19:10 +01:00
parent a648e1e769
commit 301f1bbf2b
6 changed files with 84 additions and 30 deletions

View file

@ -280,9 +280,9 @@ class DrawingEditor extends AnnotationEditor {
} }
/** @inheritdoc */ /** @inheritdoc */
_onTranslating(x, y) { _onTranslating(_x, _y) {
this.parent?.drawLayer.updateProperties(this._drawId, { this.parent?.drawLayer.updateProperties(this._drawId, {
bbox: this.#rotateBox(x, y), bbox: this.#rotateBox(),
}); });
} }

View file

@ -476,6 +476,10 @@ class AnnotationEditor {
this.div.scrollIntoView({ block: "nearest" }); this.div.scrollIntoView({ block: "nearest" });
} }
translationDone() {
this._onTranslated(this.x, this.y);
}
drag(tx, ty) { drag(tx, ty) {
this.#initialRect ||= [this.x, this.y, this.width, this.height]; this.#initialRect ||= [this.x, this.y, this.width, this.height];
const { const {

View file

@ -2264,6 +2264,7 @@ class AnnotationEditorUIManager {
for (const editor of editors) { for (const editor of editors) {
if (this.#allEditors.has(editor.id)) { if (this.#allEditors.has(editor.id)) {
editor.translateInPage(totalX, totalY); editor.translateInPage(totalX, totalY);
editor.translationDone();
} }
} }
}, },
@ -2271,6 +2272,7 @@ class AnnotationEditorUIManager {
for (const editor of editors) { for (const editor of editors) {
if (this.#allEditors.has(editor.id)) { if (this.#allEditors.has(editor.id)) {
editor.translateInPage(-totalX, -totalY); editor.translateInPage(-totalX, -totalY);
editor.translationDone();
} }
} }
}, },
@ -2280,6 +2282,7 @@ class AnnotationEditorUIManager {
for (const editor of editors) { for (const editor of editors) {
editor.translateInPage(x, y); editor.translateInPage(x, y);
editor.translationDone();
} }
} }

View file

@ -40,6 +40,7 @@ import {
kbSelectAll, kbSelectAll,
kbUndo, kbUndo,
loadAndWait, loadAndWait,
moveEditor,
paste, paste,
pasteFromClipboard, pasteFromClipboard,
scrollIntoView, scrollIntoView,
@ -48,7 +49,6 @@ import {
unselectEditor, unselectEditor,
waitForAnnotationEditorLayer, waitForAnnotationEditorLayer,
waitForAnnotationModeChanged, waitForAnnotationModeChanged,
waitForEditorMovedInDOM,
waitForSelectedEditor, waitForSelectedEditor,
waitForSerialized, waitForSerialized,
waitForStorageEntries, waitForStorageEntries,
@ -79,33 +79,6 @@ const commit = async page => {
const switchToFreeText = switchToEditor.bind(null, "FreeText"); 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) => { const cancelFocusIn = async (page, selector) => {
page.evaluate(sel => { page.evaluate(sel => {
const el = document.querySelector(sel); const el = document.querySelector(sel);

View file

@ -27,6 +27,7 @@ import {
kbSelectAll, kbSelectAll,
kbUndo, kbUndo,
loadAndWait, loadAndWait,
moveEditor,
scrollIntoView, scrollIntoView,
selectEditor, selectEditor,
switchToEditor, switchToEditor,
@ -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", () => { describe("with a rotated pdf", () => {

View file

@ -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 { export {
applyFunctionToEditor, applyFunctionToEditor,
awaitPromise, awaitPromise,
@ -893,6 +921,7 @@ export {
kbUndo, kbUndo,
loadAndWait, loadAndWait,
mockClipboard, mockClipboard,
moveEditor,
paste, paste,
pasteFromClipboard, pasteFromClipboard,
scrollIntoView, scrollIntoView,