tldraw/state/commands/translate.ts

99 lines
2.7 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'
2021-06-29 12:00:59 +00:00
import tld from 'utils/tld'
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
2021-06-24 12:34:43 +00:00
const { initialShapes } = after
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
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-29 12:00:59 +00:00
tld.getDocumentBranch(data, id).forEach((id) => {
2021-06-05 19:36:46 +00:00
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
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(
data,
initialShapes.map((s) => s.id)
)
2021-06-04 16:08:43 +00:00
// Update parents
2021-06-29 12:00:59 +00:00
tld.updateParents(
2021-06-04 16:08:43 +00:00
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
},
undo(data) {
2021-06-24 12:34:43 +00:00
const { initialShapes, clones, initialParents } = before
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
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-29 12:00:59 +00:00
tld.getDocumentBranch(data, id).forEach((id) => {
2021-06-13 13:55:37 +00:00
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
2021-06-29 12:00:59 +00:00
tld.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
2021-06-29 12:00:59 +00:00
tld.updateParents(
2021-06-04 16:08:43 +00:00
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
},
})
)
}