tldraw/state/sessions/translate-session.ts

194 lines
5 KiB
TypeScript
Raw Normal View History

2021-06-04 16:08:43 +00:00
import { Data, GroupShape, ShapeType } from 'types'
2021-05-29 12:40:41 +00:00
import * as vec from 'utils/vec'
import BaseSession from './base-session'
import commands from 'state/commands'
import { current } from 'immer'
import { v4 as uuid } from 'uuid'
2021-06-04 16:08:43 +00:00
import {
getChildIndexAbove,
getPage,
getSelectedShapes,
updateParents,
} from 'utils/utils'
2021-05-29 12:40:41 +00:00
import { getShapeUtils } from 'lib/shape-utils'
2021-05-13 06:44:52 +00:00
export default class TranslateSession extends BaseSession {
delta = [0, 0]
origin: number[]
snapshot: TranslateSnapshot
2021-05-19 21:24:41 +00:00
isCloning = false
2021-05-13 06:44:52 +00:00
constructor(data: Data, point: number[]) {
2021-05-13 06:44:52 +00:00
super(data)
this.origin = point
this.snapshot = getTranslateSnapshot(data)
}
2021-05-20 08:19:13 +00:00
update(data: Data, point: number[], isAligned: boolean, isCloning: boolean) {
2021-06-04 16:08:43 +00:00
const { currentPageId, clones, initialShapes, initialParents } =
this.snapshot
const { shapes } = getPage(data, currentPageId)
2021-05-20 09:25:14 +00:00
2021-05-13 06:44:52 +00:00
const delta = vec.vec(this.origin, point)
2021-05-20 08:19:13 +00:00
if (isAligned) {
if (Math.abs(delta[0]) < Math.abs(delta[1])) {
delta[0] = 0
} else {
delta[1] = 0
}
}
2021-05-20 09:25:14 +00:00
if (isCloning) {
if (!this.isCloning) {
this.isCloning = true
data.selectedIds.clear()
for (const { id, point } of initialShapes) {
const shape = shapes[id]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, point)
2021-05-20 09:25:14 +00:00
}
2021-06-04 16:08:43 +00:00
data.selectedIds.clear()
2021-05-20 09:25:14 +00:00
for (const clone of clones) {
data.selectedIds.add(clone.id)
2021-06-04 16:08:43 +00:00
shapes[clone.id] = { ...clone }
if (clone.parentId !== data.currentPageId) {
const parent = shapes[clone.parentId]
getShapeUtils(parent).setProperty(parent, 'children', [
...parent.children,
clone.id,
])
}
2021-05-20 09:25:14 +00:00
}
2021-05-19 21:24:41 +00:00
}
2021-05-20 09:25:14 +00:00
for (const { id, point } of clones) {
const shape = shapes[id]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, vec.add(point, delta))
2021-05-19 21:24:41 +00:00
}
2021-06-04 16:08:43 +00:00
updateParents(
data,
clones.map((c) => c.id)
)
2021-05-20 09:25:14 +00:00
} else {
if (this.isCloning) {
this.isCloning = false
data.selectedIds.clear()
for (const { id } of initialShapes) {
data.selectedIds.add(id)
}
2021-05-19 21:24:41 +00:00
2021-05-20 09:25:14 +00:00
for (const clone of clones) {
delete shapes[clone.id]
}
2021-06-04 16:08:43 +00:00
initialParents.forEach(
(parent) =>
((shapes[parent.id] as GroupShape).children = parent.children)
)
2021-05-20 09:25:14 +00:00
}
2021-06-04 16:08:43 +00:00
for (const initialShape of initialShapes) {
const shape = shapes[initialShape.id]
const next = vec.add(initialShape.point, delta)
const deltaForShape = vec.sub(next, shape.point)
getShapeUtils(shape).translateTo(shape, next)
if (shape.type === ShapeType.Group) {
for (let childId of shape.children) {
const childShape = shapes[childId]
getShapeUtils(childShape).translateBy(childShape, deltaForShape)
}
}
2021-05-20 09:25:14 +00:00
}
2021-06-04 16:08:43 +00:00
updateParents(
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
}
}
cancel(data: Data) {
2021-06-04 16:08:43 +00:00
const { initialShapes, initialParents, clones, currentPageId } =
this.snapshot
const { shapes } = getPage(data, currentPageId)
2021-05-19 21:24:41 +00:00
2021-05-20 09:25:14 +00:00
for (const { id, point } of initialShapes) {
const shape = shapes[id]
2021-06-04 16:08:43 +00:00
const deltaForShape = vec.sub(point, shape.point)
getShapeUtils(shape).translateTo(shape, point)
if (shape.type === ShapeType.Group) {
for (let childId of shape.children) {
const childShape = shapes[childId]
getShapeUtils(childShape).translateBy(childShape, deltaForShape)
}
}
2021-05-20 09:25:14 +00:00
}
for (const { id } of clones) {
delete shapes[id]
2021-05-13 06:44:52 +00:00
}
2021-06-04 16:08:43 +00:00
initialParents.forEach(({ id, children }) => {
const shape = shapes[id]
getShapeUtils(shape).setProperty(shape, 'children', children)
})
updateParents(
data,
initialShapes.map((s) => s.id)
)
2021-05-13 06:44:52 +00:00
}
complete(data: Data) {
2021-05-29 12:54:33 +00:00
if (!this.snapshot.hasUnlockedShapes) return
2021-05-29 12:40:41 +00:00
2021-05-19 21:24:41 +00:00
commands.translate(
data,
this.snapshot,
getTranslateSnapshot(data),
this.isCloning
)
2021-05-13 06:44:52 +00:00
}
}
export function getTranslateSnapshot(data: Data) {
2021-05-23 13:46:04 +00:00
const cData = current(data)
2021-06-04 16:08:43 +00:00
const page = getPage(cData)
const selectedShapes = getSelectedShapes(cData).filter(
(shape) => !shape.isLocked
)
const hasUnlockedShapes = selectedShapes.length > 0
const parents = Array.from(
new Set(selectedShapes.map((s) => s.parentId)).values()
)
.filter((id) => id !== data.currentPageId)
.map((id) => page.shapes[id])
2021-05-19 21:24:41 +00:00
2021-05-13 06:44:52 +00:00
return {
2021-05-29 12:54:33 +00:00
hasUnlockedShapes,
currentPageId: data.currentPageId,
2021-06-04 16:08:43 +00:00
initialParents: parents.map(({ id, children }) => ({ id, children })),
initialShapes: selectedShapes.map(({ id, point, parentId }) => ({
id,
point,
parentId,
})),
clones: selectedShapes.map((shape) => ({
2021-05-23 13:46:04 +00:00
...shape,
id: uuid(),
2021-06-04 16:08:43 +00:00
parentId: shape.parentId,
2021-05-23 13:46:04 +00:00
childIndex: getChildIndexAbove(cData, shape.id),
})),
2021-05-13 06:44:52 +00:00
}
}
export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>