tldraw/state/commands/translate.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-05-29 12:40:41 +00:00
import Command from './command'
import history from '../history'
import { TranslateSnapshot } from 'state/sessions/translate-session'
2021-06-21 21:35:28 +00:00
import { Data } from 'types'
import {
getDocumentBranch,
getPage,
setSelectedIds,
updateParents,
2021-06-24 08:18:14 +00:00
} from 'utils'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/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-06-21 21:35:28 +00:00
): void {
2021-05-13 06:44:52 +00:00
history.execute(
data,
new Command({
2021-05-29 12:40:41 +00:00
name: isCloning ? 'clone_shapes' : 'translate_shapes',
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-13 06:44:52 +00:00
2021-06-04 16:08:43 +00:00
// Restore clones to document
2021-05-20 09:25:14 +00:00
if (isCloning) {
2021-06-04 16:08:43 +00:00
for (const clone of before.clones) {
2021-05-20 09:25:14 +00:00
shapes[clone.id] = clone
2021-06-04 16:08:43 +00:00
if (clone.parentId !== data.currentPageId) {
const parent = shapes[clone.parentId]
getShapeUtils(parent).setProperty(parent, 'children', [
...parent.children,
clone.id,
])
2021-06-04 18:49:27 +00:00
2021-06-21 21:35:28 +00:00
// if (clone.type === ShapeType.Group) {
// }
2021-06-04 16:08:43 +00:00
}
2021-05-20 09:25:14 +00:00
}
}
2021-06-04 16:08:43 +00:00
// Move shapes (these initialShapes will include clones if any)
2021-05-20 09:25:14 +00:00
for (const { id, point } of initialShapes) {
2021-06-05 19:36:46 +00:00
getDocumentBranch(data, id).forEach((id) => {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, point)
})
2021-05-13 06:44:52 +00:00
}
2021-06-04 16:08:43 +00:00
// Set selected shapes
setSelectedIds(
data,
initialShapes.map((s) => s.id)
)
2021-06-04 16:08:43 +00:00
// Update parents
updateParents(
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
},
undo(data) {
2021-06-04 16:08:43 +00:00
const { initialShapes, clones, currentPageId, initialParents } = before
const { shapes } = getPage(data, currentPageId)
2021-05-19 21:24:41 +00:00
2021-06-04 16:08:43 +00:00
// Move shapes back to where they started
2021-05-20 09:25:14 +00:00
for (const { id, point } of initialShapes) {
2021-06-13 13:55:37 +00:00
getDocumentBranch(data, id).forEach((id) => {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, point)
})
2021-05-20 09:25:14 +00:00
}
2021-06-04 16:08:43 +00:00
// Delete clones
if (isCloning) for (const { id } of clones) delete shapes[id]
// Set selected shapes
setSelectedIds(
data,
initialShapes.map((s) => s.id)
)
2021-06-04 16:08:43 +00:00
// Restore children on parents
initialParents.forEach(({ id, children }) => {
const parent = shapes[id]
getShapeUtils(parent).setProperty(parent, 'children', children)
})
// Update parents
updateParents(
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
},
})
)
}