Adds undo for drawn shapes, zoom scales rdp

This commit is contained in:
Steve Ruiz 2021-05-27 21:11:48 +01:00
parent c41def99d3
commit bd02ca4fd9
6 changed files with 51 additions and 37 deletions

40
state/commands/draw.ts Normal file
View file

@ -0,0 +1,40 @@
import Command from "./command"
import history from "../history"
import { Data } from "types"
import { getPage } from "utils/utils"
import { getShapeUtils } from "lib/shape-utils"
import { current } from "immer"
export default function drawCommand(
data: Data,
id: string,
before: number[][],
after: number[][]
) {
const selectedIds = Array.from(data.selectedIds.values())
const restoreShape = current(getPage(data).shapes[id])
getShapeUtils(restoreShape).setPoints!(restoreShape, after)
history.execute(
data,
new Command({
name: "set_points",
category: "canvas",
manualSelection: true,
do(data, initial) {
if (!initial) {
getPage(data).shapes[id] = restoreShape
}
data.selectedIds.clear()
},
undo(data) {
delete getPage(data).shapes[id]
data.selectedIds.clear()
for (let id of selectedIds) {
data.selectedIds.add(id)
}
},
})
)
}

View file

@ -4,7 +4,7 @@ import direct from "./direct"
import distribute from "./distribute"
import generate from "./generate"
import move from "./move"
import points from "./points"
import draw from "./draw"
import rotate from "./rotate"
import stretch from "./stretch"
import style from "./style"
@ -19,7 +19,7 @@ const commands = {
distribute,
generate,
move,
points,
draw,
rotate,
stretch,
style,

View file

@ -1,28 +0,0 @@
import Command from "./command"
import history from "../history"
import { Data } from "types"
import { getPage } from "utils/utils"
import { getShapeUtils } from "lib/shape-utils"
export default function pointsCommand(
data: Data,
id: string,
before: number[][],
after: number[][]
) {
history.execute(
data,
new Command({
name: "set_points",
category: "canvas",
do(data) {
const shape = getPage(data).shapes[id]
getShapeUtils(shape).setPoints!(shape, after)
},
undo(data) {
const shape = getPage(data).shapes[id]
getShapeUtils(shape).setPoints!(shape, before)
},
})
)
}