tldraw/state/sessions/handle-session.ts

67 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Data, Shape } from 'types'
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[]
shiftKey: boolean
initialShape: Shape
handleId: string
2021-05-31 19:13:43 +00:00
constructor(data: Data, shapeId: string, handleId: string, point: number[]) {
super(data)
this.origin = point
this.handleId = handleId
this.initialShape = deepClone(tld.getShape(data, shapeId))
2021-05-31 19:13:43 +00:00
}
update(
data: Data,
point: number[],
shiftKey: boolean,
altKey: boolean,
metaKey: boolean
): void {
const shape = tld.getPage(data).shapes[this.initialShape.id]
2021-05-31 19:13:43 +00:00
this.shiftKey = shiftKey
2021-05-31 19:13:43 +00:00
const delta = vec.vec(this.origin, point)
2021-05-31 19:13:43 +00:00
const handles = this.initialShape.handles
2021-06-05 14:29:49 +00:00
getShapeUtils(shape).onHandleChange(
shape,
{
[this.handleId]: {
...handles[this.handleId],
point: vec.round(vec.add(handles[this.handleId].point, delta)), // vec.rot(delta, shape.rotation)),
},
2021-05-31 19:13:43 +00:00
},
{ delta, shiftKey, altKey, metaKey }
)
2021-05-31 19:13:43 +00:00
}
2021-06-21 21:35:28 +00:00
cancel(data: Data): void {
tld.getPage(data).shapes[this.initialShape.id] = this.initialShape
2021-05-31 19:13:43 +00:00
}
2021-06-21 21:35:28 +00:00
complete(data: Data): void {
const before = this.initialShape
const after = deepClone(tld.getShape(data, before.id))
commands.mutate(data, [before], [after])
2021-05-31 19:13:43 +00:00
}
}
2021-06-21 21:35:28 +00:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function getHandleSnapshot(data: Data, shapeId: string) {
return deepClone(tld.getShape(data, shapeId))
2021-05-31 19:13:43 +00:00
}
export type HandleSnapshot = ReturnType<typeof getHandleSnapshot>