From a4643edd62f687586498e951d623a372a3ef8489 Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Sat, 15 May 2021 15:10:11 +0100 Subject: [PATCH] Adds undo redo for shapes --- state/commands/create-shape.ts | 32 ++++++++++++++++++++++++++++++++ state/commands/index.ts | 3 ++- state/state.ts | 12 +++++------- 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 state/commands/create-shape.ts diff --git a/state/commands/create-shape.ts b/state/commands/create-shape.ts new file mode 100644 index 000000000..039a83ab0 --- /dev/null +++ b/state/commands/create-shape.ts @@ -0,0 +1,32 @@ +import Command from "./command" +import history from "../history" +import { Data, Shape } from "types" + +export default function createShape(data: Data, shape: Shape) { + const { currentPageId } = data + + history.execute( + data, + new Command({ + name: "translate_shapes", + category: "canvas", + do(data) { + const { shapes } = data.document.pages[currentPageId] + + shapes[shape.id] = shape + data.selectedIds.clear() + data.pointedId = undefined + data.hoveredId = undefined + }, + undo(data) { + const { shapes } = data.document.pages[currentPageId] + + delete shapes[shape.id] + + data.selectedIds.clear() + data.pointedId = undefined + data.hoveredId = undefined + }, + }) + ) +} diff --git a/state/commands/index.ts b/state/commands/index.ts index dcc0015f2..45c030ed8 100644 --- a/state/commands/index.ts +++ b/state/commands/index.ts @@ -1,7 +1,8 @@ import translate from "./translate" import transform from "./transform" import generateShapes from "./generate-shapes" +import createShape from "./create-shape" -const commands = { translate, transform, generateShapes } +const commands = { translate, transform, generateShapes, createShape } export default commands diff --git a/state/state.ts b/state/state.ts index d56a2b020..461c261a5 100644 --- a/state/state.ts +++ b/state/state.ts @@ -58,7 +58,8 @@ const state = createState({ on: { UNDO: { do: "undo" }, REDO: { do: "redo" }, - DELETED: { do: "deleteSelection" }, + CANCELLED: { do: "clearSelectedIds" }, + DELETED: { do: "deleteSelectedIds" }, GENERATED_SHAPES_FROM_CODE: "setGeneratedShapes", INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize", DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize", @@ -171,7 +172,7 @@ const state = createState({ PANNED_CAMERA: "updateTranslateSession", STOPPED_POINTING: { do: "completeSession", to: "selecting" }, CANCELLED: { - do: ["cancelSession", "deleteSelection"], + do: ["cancelSession", "deleteSelectedIds"], to: "selecting", }, }, @@ -221,9 +222,7 @@ const state = createState({ point: screenToWorld(payload.point, data), }) - data.selectedIds.clear() - data.selectedIds.add(shape.id) - data.document.pages[data.currentPageId].shapes[shape.id] = shape + commands.createShape(data, shape) }, // History @@ -299,7 +298,7 @@ const state = createState({ }, // Selection - deleteSelection(data) { + deleteSelectedIds(data) { const { document, currentPageId } = data const shapes = document.pages[currentPageId].shapes @@ -312,7 +311,6 @@ const state = createState({ data.hoveredId = undefined data.pointedId = undefined }, - setHoveredId(data, payload: PointerInfo) { data.hoveredId = payload.target },