tldraw/state/commands/translate.ts

62 lines
1.6 KiB
TypeScript
Raw Normal View History

2021-05-13 06:44:52 +00:00
import Command from "./command"
2021-05-13 08:34:56 +00:00
import history from "../history"
2021-05-13 06:44:52 +00:00
import { TranslateSnapshot } from "state/sessions/translate-session"
import { Data } from "types"
import { getPage } from "utils/utils"
import { getShapeUtils } from "lib/shape-utils"
2021-05-13 06:44:52 +00:00
export default function translateCommand(
data: Data,
before: TranslateSnapshot,
2021-05-19 21:24:41 +00:00
after: TranslateSnapshot,
isCloning: boolean
2021-05-13 06:44:52 +00:00
) {
history.execute(
data,
new Command({
2021-05-19 21:24:41 +00:00
name: isCloning ? "clone_shapes" : "translate_shapes",
2021-05-13 06:44:52 +00:00
category: "canvas",
2021-05-19 21:24:41 +00:00
manualSelection: true,
do(data, initial) {
if (initial) return
const { initialShapes, currentPageId } = after
const { shapes } = getPage(data, currentPageId)
2021-05-20 08:19:13 +00:00
const { clones } = before // !
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
2021-05-13 06:44:52 +00:00
2021-05-20 09:25:14 +00:00
if (isCloning) {
for (const clone of clones) {
shapes[clone.id] = clone
}
}
for (const { id, point } of initialShapes) {
const shape = shapes[id]
2021-05-25 11:38:21 +00:00
getShapeUtils(shape).translateTo(shape, point)
2021-05-19 21:24:41 +00:00
data.selectedIds.add(id)
2021-05-13 06:44:52 +00:00
}
},
undo(data) {
const { initialShapes, clones, currentPageId } = before
const { shapes } = getPage(data, currentPageId)
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
2021-05-20 09:25:14 +00:00
if (isCloning) {
for (const { id } of clones) {
delete shapes[id]
2021-05-19 21:24:41 +00:00
}
2021-05-13 06:44:52 +00:00
}
2021-05-20 09:25:14 +00:00
for (const { id, point } of initialShapes) {
const shape = shapes[id]
2021-05-25 11:38:21 +00:00
getShapeUtils(shape).translateTo(shape, point)
2021-05-20 09:25:14 +00:00
data.selectedIds.add(id)
}
2021-05-13 06:44:52 +00:00
},
})
)
}