2021-05-31 19:13:43 +00:00
|
|
|
import { Data } from 'types'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-05-31 19:13:43 +00:00
|
|
|
import BaseSession from './base-session'
|
|
|
|
import commands from 'state/commands'
|
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-06-29 14:54:46 +00:00
|
|
|
import { deepClone } from 'utils'
|
2021-05-31 19:13:43 +00:00
|
|
|
|
|
|
|
export default class HandleSession extends BaseSession {
|
|
|
|
delta = [0, 0]
|
|
|
|
origin: number[]
|
|
|
|
snapshot: HandleSnapshot
|
|
|
|
|
|
|
|
constructor(data: Data, shapeId: string, handleId: string, point: number[]) {
|
|
|
|
super(data)
|
|
|
|
this.origin = point
|
|
|
|
this.snapshot = getHandleSnapshot(data, shapeId, handleId)
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
update(data: Data, point: number[], isAligned: boolean): void {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { handleId, initialShape } = this.snapshot
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getPage(data).shapes[initialShape.id]
|
2021-05-31 19:13:43 +00:00
|
|
|
|
|
|
|
const delta = vec.vec(this.origin, point)
|
|
|
|
|
|
|
|
if (isAligned) {
|
|
|
|
if (Math.abs(delta[0]) < Math.abs(delta[1])) {
|
|
|
|
delta[0] = 0
|
|
|
|
} else {
|
|
|
|
delta[1] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handles = initialShape.handles
|
|
|
|
|
2021-06-05 14:29:49 +00:00
|
|
|
// rotate the delta ?
|
|
|
|
// rotate the handle ?
|
|
|
|
// rotate the shape around the previous center point
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
getShapeUtils(shape).onHandleChange(shape, {
|
2021-05-31 19:13:43 +00:00
|
|
|
[handleId]: {
|
|
|
|
...handles[handleId],
|
2021-06-05 14:29:49 +00:00
|
|
|
point: vec.add(handles[handleId].point, delta), // vec.rot(delta, shape.rotation)),
|
2021-05-31 19:13:43 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
cancel(data: Data): void {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { initialShape } = this.snapshot
|
2021-06-29 12:00:59 +00:00
|
|
|
tld.getPage(data).shapes[initialShape.id] = initialShape
|
2021-05-31 19:13:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
complete(data: Data): void {
|
2021-05-31 19:13:43 +00:00
|
|
|
commands.handle(
|
|
|
|
data,
|
|
|
|
this.snapshot,
|
|
|
|
getHandleSnapshot(
|
|
|
|
data,
|
|
|
|
this.snapshot.initialShape.id,
|
|
|
|
this.snapshot.handleId
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-05-31 19:13:43 +00:00
|
|
|
export function getHandleSnapshot(
|
|
|
|
data: Data,
|
|
|
|
shapeId: string,
|
|
|
|
handleId: string
|
|
|
|
) {
|
2021-06-29 14:54:46 +00:00
|
|
|
const initialShape = deepClone(tld.getShape(data, shapeId))
|
2021-05-31 19:13:43 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
currentPageId: data.currentPageId,
|
|
|
|
handleId,
|
|
|
|
initialShape,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type HandleSnapshot = ReturnType<typeof getHandleSnapshot>
|