Skip to content

Commit

Permalink
[Editor] Allow to abort the current drawing
Browse files Browse the repository at this point in the history
It fixes #19126.
  • Loading branch information
calixteman committed Nov 29, 2024
1 parent 308ca2a commit 4acc086
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/display/editor/annotation_editor_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,14 +820,14 @@ class AnnotationEditorLayer {
this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
}

endDrawingSession() {
endDrawingSession(isAborted = false) {
if (!this.#drawingAC) {
return;
return null;
}
this.#drawingAC.abort();
this.#drawingAC = null;
this.#uiManager.disableUserSelect(false);
this.#currentEditorType.endDrawing();
return this.#currentEditorType.endDrawing(isAborted);
}

/**
Expand Down
43 changes: 27 additions & 16 deletions src/display/editor/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,13 @@ class DrawingEditor extends AnnotationEditor {
return;
}

this.endDrawing();
this.endDrawing(/* isAborted = */ false);
}

static endDrawing() {
static endDrawing(isAborted) {
const parent = this._currentParent;
if (!parent) {
return;
return null;
}
parent.toggleDrawing(true);
parent.cleanUndoStack(AnnotationEditorParamsType.DRAW_STEP);
Expand All @@ -756,20 +756,31 @@ class DrawingEditor extends AnnotationEditor {
scale,
} = parent;

parent.createAndAddNewEditor({ offsetX: 0, offsetY: 0 }, false, {
drawId: this._currentDrawId,
drawOutlines: this._currentDraw.getOutlines(
pageWidth * scale,
pageHeight * scale,
scale,
this._INNER_MARGIN
),
drawingOptions: this._currentDrawingOptions,
mustBeCommitted: true,
});
} else {
parent.drawLayer.remove(this._currentDrawId);
const editor = parent.createAndAddNewEditor(
{ offsetX: 0, offsetY: 0 },
false,
{
drawId: this._currentDrawId,
drawOutlines: this._currentDraw.getOutlines(
pageWidth * scale,
pageHeight * scale,
scale,
this._INNER_MARGIN
),
drawingOptions: this._currentDrawingOptions,
mustBeCommitted: !isAborted,
}
);
this._cleanup();
return editor;
}

parent.drawLayer.remove(this._currentDrawId);
this._cleanup();
return null;
}

static _cleanup() {
this._currentDrawId = -1;
this._currentDraw = null;
this._currentDrawingOptions = null;
Expand Down
9 changes: 7 additions & 2 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2088,11 +2088,16 @@ class AnnotationEditorUIManager {
*/
delete() {
this.commitOrRemove();
if (!this.hasSelection) {
const drawingEditor = this.currentLayer?.endDrawingSession(
/* isAborted = */ true
);
if (!this.hasSelection && !drawingEditor) {
return;
}

const editors = [...this.#selectedEditors];
const editors = drawingEditor
? [drawingEditor]
: [...this.#selectedEditors];
const cmd = () => {
for (const editor of editors) {
editor.remove();
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 @@ -26,6 +26,7 @@ import {
loadAndWait,
scrollIntoView,
switchToEditor,
waitForNoElement,
waitForSerialized,
waitForStorageEntries,
} from "./test_utils.mjs";
Expand Down Expand Up @@ -567,4 +568,48 @@ describe("Ink Editor", () => {
);
});
});

describe("Can delete the drawing in progress and undo the deletion", () => {
let pages;

beforeAll(async () => {
pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
});

afterAll(async () => {
await closePages(pages);
});

it("must check that the color has been changed", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await switchToInk(page);

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

const x = rect.x + 20;
const y = rect.y + 20;
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);

const drawSelector = `.canvasWrapper svg.draw path[d]:not([d=""])`;
await page.waitForSelector(drawSelector);

await page.keyboard.press("Backspace");

const editorSelector = getEditorSelector(0);
await waitForNoElement(page, drawSelector);
await waitForNoElement(page, editorSelector);

await kbUndo(page);
await page.waitForSelector(editorSelector, { visible: true });
await page.waitForSelector(drawSelector);
})
);
});
});
});
9 changes: 9 additions & 0 deletions test/integration/test_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,14 @@ async function switchToEditor(name, page, disable = false) {
await awaitPromise(modeChangedHandle);
}

function waitForNoElement(page, selector) {
return page.waitForFunction(
sel => !document.querySelector(sel),
{},
selector
);
}

export {
applyFunctionToEditor,
awaitPromise,
Expand Down Expand Up @@ -826,6 +834,7 @@ export {
waitForAnnotationModeChanged,
waitForEntryInStorage,
waitForEvent,
waitForNoElement,
waitForPageRendered,
waitForSandboxTrip,
waitForSelectedEditor,
Expand Down

0 comments on commit 4acc086

Please sign in to comment.