[Editor] Allow to abort the current drawing

It fixes #19126.
This commit is contained in:
Calixte Denizet 2024-11-29 16:22:44 +01:00
parent 308ca2a16f
commit 4acc086292
5 changed files with 91 additions and 21 deletions

View File

@ -820,14 +820,14 @@ class AnnotationEditorLayer {
this.#currentEditorType.startDrawing(this, this.#uiManager, false, event); this.#currentEditorType.startDrawing(this, this.#uiManager, false, event);
} }
endDrawingSession() { endDrawingSession(isAborted = false) {
if (!this.#drawingAC) { if (!this.#drawingAC) {
return; return null;
} }
this.#drawingAC.abort(); this.#drawingAC.abort();
this.#drawingAC = null; this.#drawingAC = null;
this.#uiManager.disableUserSelect(false); this.#uiManager.disableUserSelect(false);
this.#currentEditorType.endDrawing(); return this.#currentEditorType.endDrawing(isAborted);
} }
/** /**

View File

@ -739,13 +739,13 @@ class DrawingEditor extends AnnotationEditor {
return; return;
} }
this.endDrawing(); this.endDrawing(/* isAborted = */ false);
} }
static endDrawing() { static endDrawing(isAborted) {
const parent = this._currentParent; const parent = this._currentParent;
if (!parent) { if (!parent) {
return; return null;
} }
parent.toggleDrawing(true); parent.toggleDrawing(true);
parent.cleanUndoStack(AnnotationEditorParamsType.DRAW_STEP); parent.cleanUndoStack(AnnotationEditorParamsType.DRAW_STEP);
@ -756,7 +756,10 @@ class DrawingEditor extends AnnotationEditor {
scale, scale,
} = parent; } = parent;
parent.createAndAddNewEditor({ offsetX: 0, offsetY: 0 }, false, { const editor = parent.createAndAddNewEditor(
{ offsetX: 0, offsetY: 0 },
false,
{
drawId: this._currentDrawId, drawId: this._currentDrawId,
drawOutlines: this._currentDraw.getOutlines( drawOutlines: this._currentDraw.getOutlines(
pageWidth * scale, pageWidth * scale,
@ -765,11 +768,19 @@ class DrawingEditor extends AnnotationEditor {
this._INNER_MARGIN this._INNER_MARGIN
), ),
drawingOptions: this._currentDrawingOptions, drawingOptions: this._currentDrawingOptions,
mustBeCommitted: true, mustBeCommitted: !isAborted,
});
} else {
parent.drawLayer.remove(this._currentDrawId);
} }
);
this._cleanup();
return editor;
}
parent.drawLayer.remove(this._currentDrawId);
this._cleanup();
return null;
}
static _cleanup() {
this._currentDrawId = -1; this._currentDrawId = -1;
this._currentDraw = null; this._currentDraw = null;
this._currentDrawingOptions = null; this._currentDrawingOptions = null;

View File

@ -2088,11 +2088,16 @@ class AnnotationEditorUIManager {
*/ */
delete() { delete() {
this.commitOrRemove(); this.commitOrRemove();
if (!this.hasSelection) { const drawingEditor = this.currentLayer?.endDrawingSession(
/* isAborted = */ true
);
if (!this.hasSelection && !drawingEditor) {
return; return;
} }
const editors = [...this.#selectedEditors]; const editors = drawingEditor
? [drawingEditor]
: [...this.#selectedEditors];
const cmd = () => { const cmd = () => {
for (const editor of editors) { for (const editor of editors) {
editor.remove(); editor.remove();

View File

@ -26,6 +26,7 @@ import {
loadAndWait, loadAndWait,
scrollIntoView, scrollIntoView,
switchToEditor, switchToEditor,
waitForNoElement,
waitForSerialized, waitForSerialized,
waitForStorageEntries, waitForStorageEntries,
} from "./test_utils.mjs"; } from "./test_utils.mjs";
@ -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);
})
);
});
});
}); });

View File

@ -773,6 +773,14 @@ async function switchToEditor(name, page, disable = false) {
await awaitPromise(modeChangedHandle); await awaitPromise(modeChangedHandle);
} }
function waitForNoElement(page, selector) {
return page.waitForFunction(
sel => !document.querySelector(sel),
{},
selector
);
}
export { export {
applyFunctionToEditor, applyFunctionToEditor,
awaitPromise, awaitPromise,
@ -826,6 +834,7 @@ export {
waitForAnnotationModeChanged, waitForAnnotationModeChanged,
waitForEntryInStorage, waitForEntryInStorage,
waitForEvent, waitForEvent,
waitForNoElement,
waitForPageRendered, waitForPageRendered,
waitForSandboxTrip, waitForSandboxTrip,
waitForSelectedEditor, waitForSelectedEditor,