2021-06-07 11:18:50 +00:00
|
|
|
import { Data, LineShape, RayShape } from 'types'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-07 11:18:50 +00:00
|
|
|
import BaseSession from './base-session'
|
|
|
|
import commands from 'state/commands'
|
|
|
|
import { current } from 'immer'
|
|
|
|
import { getPage, getSelectedIds } from 'utils/utils'
|
2021-05-15 15:20:21 +00:00
|
|
|
|
|
|
|
export default class DirectionSession extends BaseSession {
|
|
|
|
delta = [0, 0]
|
|
|
|
origin: number[]
|
|
|
|
snapshot: DirectionSnapshot
|
|
|
|
|
|
|
|
constructor(data: Data, point: number[]) {
|
|
|
|
super(data)
|
|
|
|
this.origin = point
|
|
|
|
this.snapshot = getDirectionSnapshot(data)
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
update(data: Data, point: number[]): void {
|
2021-05-22 15:45:24 +00:00
|
|
|
const { shapes } = this.snapshot
|
|
|
|
|
|
|
|
const page = getPage(data)
|
2021-05-15 15:20:21 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const { id } of shapes) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = page.shapes[id] as RayShape | LineShape
|
2021-05-15 15:20:21 +00:00
|
|
|
|
|
|
|
shape.direction = vec.uni(vec.vec(shape.point, point))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
cancel(data: Data): void {
|
2021-05-22 15:45:24 +00:00
|
|
|
const page = getPage(data, this.snapshot.currentPageId)
|
2021-05-15 15:20:21 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
for (const { id, direction } of this.snapshot.shapes) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = page.shapes[id] as RayShape | LineShape
|
2021-05-15 15:20:21 +00:00
|
|
|
shape.direction = direction
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
complete(data: Data): void {
|
2021-05-17 21:27:18 +00:00
|
|
|
commands.direct(data, this.snapshot, getDirectionSnapshot(data))
|
2021-05-15 15:20:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-05-15 15:20:21 +00:00
|
|
|
export function getDirectionSnapshot(data: Data) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const { shapes } = getPage(current(data))
|
2021-05-15 15:20:21 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
const snapshapes: { id: string; direction: number[] }[] = []
|
2021-05-15 15:20:21 +00:00
|
|
|
|
2021-06-07 11:18:50 +00:00
|
|
|
getSelectedIds(data).forEach((id) => {
|
2021-05-15 15:20:21 +00:00
|
|
|
const shape = shapes[id]
|
2021-06-07 11:18:50 +00:00
|
|
|
if ('direction' in shape) {
|
2021-05-15 15:20:21 +00:00
|
|
|
snapshapes.push({ id: shape.id, direction: shape.direction })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
2021-05-22 15:45:24 +00:00
|
|
|
currentPageId: data.currentPageId,
|
2021-05-15 15:20:21 +00:00
|
|
|
shapes: snapshapes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DirectionSnapshot = ReturnType<typeof getDirectionSnapshot>
|