Tldraw/state/commands/translate.ts

52 wiersze
1.3 KiB
TypeScript
Czysty Zwykły widok Historia

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"
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
2021-05-13 06:44:52 +00:00
const { shapes } = data.document.pages[after.currentPageId]
2021-05-20 08:19:13 +00:00
const { initialShapes } = after
const { clones } = before // !
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
2021-05-13 06:44:52 +00:00
2021-05-19 21:24:41 +00:00
for (let id in initialShapes) {
2021-05-20 08:19:13 +00:00
shapes[id].point = initialShapes[id].point
shapes[clones[id].id] = clones[id]
2021-05-19 21:24:41 +00:00
data.selectedIds.add(id)
2021-05-13 06:44:52 +00:00
}
},
undo(data) {
const { shapes } = data.document.pages[before.currentPageId]
2021-05-19 21:24:41 +00:00
const { initialShapes, clones } = before
data.selectedIds.clear()
for (let id in initialShapes) {
shapes[id].point = initialShapes[id].point
data.selectedIds.add(id)
2021-05-13 06:44:52 +00:00
2021-05-19 21:24:41 +00:00
if (isCloning) {
const clone = clones[id]
delete shapes[clone.id]
}
2021-05-13 06:44:52 +00:00
}
},
})
)
}