Adds undo redo for shapes

This commit is contained in:
Steve Ruiz 2021-05-15 15:10:11 +01:00
parent 5420b0365f
commit a4643edd62
3 changed files with 39 additions and 8 deletions

View file

@ -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
},
})
)
}

View file

@ -1,7 +1,8 @@
import translate from "./translate" import translate from "./translate"
import transform from "./transform" import transform from "./transform"
import generateShapes from "./generate-shapes" import generateShapes from "./generate-shapes"
import createShape from "./create-shape"
const commands = { translate, transform, generateShapes } const commands = { translate, transform, generateShapes, createShape }
export default commands export default commands

View file

@ -58,7 +58,8 @@ const state = createState({
on: { on: {
UNDO: { do: "undo" }, UNDO: { do: "undo" },
REDO: { do: "redo" }, REDO: { do: "redo" },
DELETED: { do: "deleteSelection" }, CANCELLED: { do: "clearSelectedIds" },
DELETED: { do: "deleteSelectedIds" },
GENERATED_SHAPES_FROM_CODE: "setGeneratedShapes", GENERATED_SHAPES_FROM_CODE: "setGeneratedShapes",
INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize", INCREASED_CODE_FONT_SIZE: "increaseCodeFontSize",
DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize", DECREASED_CODE_FONT_SIZE: "decreaseCodeFontSize",
@ -171,7 +172,7 @@ const state = createState({
PANNED_CAMERA: "updateTranslateSession", PANNED_CAMERA: "updateTranslateSession",
STOPPED_POINTING: { do: "completeSession", to: "selecting" }, STOPPED_POINTING: { do: "completeSession", to: "selecting" },
CANCELLED: { CANCELLED: {
do: ["cancelSession", "deleteSelection"], do: ["cancelSession", "deleteSelectedIds"],
to: "selecting", to: "selecting",
}, },
}, },
@ -221,9 +222,7 @@ const state = createState({
point: screenToWorld(payload.point, data), point: screenToWorld(payload.point, data),
}) })
data.selectedIds.clear() commands.createShape(data, shape)
data.selectedIds.add(shape.id)
data.document.pages[data.currentPageId].shapes[shape.id] = shape
}, },
// History // History
@ -299,7 +298,7 @@ const state = createState({
}, },
// Selection // Selection
deleteSelection(data) { deleteSelectedIds(data) {
const { document, currentPageId } = data const { document, currentPageId } = data
const shapes = document.pages[currentPageId].shapes const shapes = document.pages[currentPageId].shapes
@ -312,7 +311,6 @@ const state = createState({
data.hoveredId = undefined data.hoveredId = undefined
data.pointedId = undefined data.pointedId = undefined
}, },
setHoveredId(data, payload: PointerInfo) { setHoveredId(data, payload: PointerInfo) {
data.hoveredId = payload.target data.hoveredId = payload.target
}, },