tldraw/state/commands/handle.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-31 19:13:43 +00:00
import Command from './command'
import history from '../history'
import { Data } from 'types'
import { getPage } from 'utils/utils'
import { HandleSnapshot } from 'state/sessions/handle-session'
import { getShapeUtils } from 'lib/shape-utils'
2021-06-05 06:36:39 +00:00
import * as vec from 'utils/vec'
2021-05-31 19:13:43 +00:00
export default function handleCommand(
data: Data,
before: HandleSnapshot,
after: HandleSnapshot
) {
history.execute(
data,
new Command({
name: 'moved_handle',
category: 'canvas',
do(data, isInitial) {
2021-06-05 06:36:39 +00:00
// if (isInitial) return
2021-05-31 19:13:43 +00:00
const { initialShape, currentPageId } = after
2021-06-05 06:36:39 +00:00
const page = getPage(data, currentPageId)
const shape = page.shapes[initialShape.id]
2021-05-31 19:13:43 +00:00
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).onHandleChange(shape, initialShape.handles)
2021-06-05 06:36:39 +00:00
const bounds = getShapeUtils(shape).getBounds(shape)
const offset = vec.sub([bounds.minX, bounds.minY], shape.point)
getShapeUtils(shape).translateTo(shape, vec.add(shape.point, offset))
const { start, end, bend } = page.shapes[initialShape.id].handles
start.point = vec.sub(start.point, offset)
end.point = vec.sub(end.point, offset)
bend.point = vec.sub(bend.point, offset)
2021-05-31 19:13:43 +00:00
},
undo(data) {
const { initialShape, currentPageId } = before
const shape = getPage(data, currentPageId).shapes[initialShape.id]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).onHandleChange(shape, initialShape.handles)
2021-05-31 19:13:43 +00:00
},
})
)
}