2021-06-04 18:49:27 +00:00
|
|
|
import { Data, GroupShape, Shape, 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,
|
2021-06-05 19:36:46 +00:00
|
|
|
getDocumentBranch,
|
2021-06-04 16:08:43 +00:00
|
|
|
getPage,
|
|
|
|
getSelectedShapes,
|
2021-06-07 11:18:50 +00:00
|
|
|
setSelectedIds,
|
2021-06-04 16:08:43 +00:00
|
|
|
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]
|
2021-06-05 19:36:46 +00:00
|
|
|
prev = [0, 0]
|
2021-05-13 06:44:52 +00:00
|
|
|
origin: number[]
|
|
|
|
snapshot: TranslateSnapshot
|
2021-05-19 21:24:41 +00:00
|
|
|
isCloning = false
|
2021-05-13 06:44:52 +00:00
|
|
|
|
2021-05-30 21:24:31 +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
|
2021-05-22 15:45:24 +00:00
|
|
|
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-06-05 19:36:46 +00:00
|
|
|
const trueDelta = vec.sub(delta, this.prev)
|
|
|
|
this.delta = delta
|
|
|
|
this.prev = delta
|
2021-05-13 06:44:52 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
for (const { id, point } of initialShapes) {
|
2021-05-25 09:00:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
for (const clone of clones) {
|
2021-06-04 16:08:43 +00:00
|
|
|
shapes[clone.id] = { ...clone }
|
2021-06-04 18:49:27 +00:00
|
|
|
const parent = shapes[clone.parentId]
|
|
|
|
if (!parent) continue
|
|
|
|
getShapeUtils(parent).setProperty(parent, 'children', [
|
|
|
|
...parent.children,
|
|
|
|
clone.id,
|
|
|
|
])
|
2021-05-20 09:25:14 +00:00
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
|
|
|
setSelectedIds(
|
|
|
|
data,
|
|
|
|
clones.map((c) => c.id)
|
|
|
|
)
|
2021-05-19 21:24:41 +00:00
|
|
|
}
|
2021-05-20 09:25:14 +00:00
|
|
|
|
|
|
|
for (const { id, point } of clones) {
|
2021-05-25 09:00:59 +00:00
|
|
|
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
|
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
setSelectedIds(
|
|
|
|
data,
|
|
|
|
initialShapes.map((c) => c.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) {
|
2021-06-05 19:36:46 +00:00
|
|
|
getDocumentBranch(data, initialShape.id).forEach((id) => {
|
|
|
|
const shape = shapes[id]
|
|
|
|
getShapeUtils(shape).translateBy(shape, trueDelta)
|
|
|
|
})
|
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
|
2021-05-22 15:45:24 +00:00
|
|
|
const { shapes } = getPage(data, currentPageId)
|
2021-05-19 21:24:41 +00:00
|
|
|
|
2021-06-05 19:36:46 +00:00
|
|
|
for (const { id } of initialShapes) {
|
|
|
|
getDocumentBranch(data, id).forEach((id) => {
|
|
|
|
const shape = shapes[id]
|
|
|
|
getShapeUtils(shape).translateBy(shape, vec.neg(this.delta))
|
|
|
|
})
|
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-05 19:36:46 +00:00
|
|
|
|
|
|
|
// Get selected shapes
|
|
|
|
// Filter out the locked shapes
|
|
|
|
// Collect the branch children for each remaining shape
|
|
|
|
// Filter out doubles using a set
|
|
|
|
// End up with an array of ids for all of the shapes that will change
|
|
|
|
// Map into shapes from data snapshot
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const page = getPage(cData)
|
|
|
|
const selectedShapes = getSelectedShapes(cData).filter(
|
|
|
|
(shape) => !shape.isLocked
|
|
|
|
)
|
2021-06-04 18:49:27 +00:00
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
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,
|
2021-05-22 15:45:24 +00:00
|
|
|
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,
|
|
|
|
})),
|
2021-06-04 18:49:27 +00:00
|
|
|
clones: selectedShapes
|
|
|
|
.filter((shape) => shape.type !== ShapeType.Group)
|
|
|
|
.flatMap((shape) => {
|
|
|
|
const clone = {
|
|
|
|
...shape,
|
|
|
|
id: uuid(),
|
|
|
|
parentId: shape.parentId,
|
|
|
|
childIndex: getChildIndexAbove(cData, shape.id),
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
|
|
|
|
// cloneGroup(cData, {
|
|
|
|
// ...shape,
|
|
|
|
// id: uuid(),
|
|
|
|
// parentId: shape.parentId,
|
|
|
|
// childIndex: getChildIndexAbove(cData, shape.id),
|
|
|
|
// })
|
|
|
|
}),
|
2021-05-13 06:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>
|
2021-06-04 18:49:27 +00:00
|
|
|
|
|
|
|
function cloneGroup(data: Data, clone: Shape): Shape[] {
|
|
|
|
if (clone.type !== ShapeType.Group) {
|
|
|
|
return [clone]
|
|
|
|
}
|
|
|
|
|
|
|
|
const page = getPage(data)
|
|
|
|
const childClones = clone.children.flatMap((id) => {
|
|
|
|
const newId = uuid()
|
|
|
|
const source = page.shapes[id]
|
|
|
|
const next = { ...source, id: newId, parentId: clone.id }
|
|
|
|
|
|
|
|
if (next.type === ShapeType.Group) {
|
|
|
|
return [next, ...cloneGroup(data, next)]
|
|
|
|
}
|
|
|
|
|
|
|
|
return [next]
|
|
|
|
})
|
|
|
|
|
|
|
|
return [clone, ...childClones]
|
|
|
|
}
|