2021-05-28 14:37:23 +00:00
|
|
|
import { current } from 'immer'
|
|
|
|
import { Data, DrawShape } from 'types'
|
|
|
|
import BaseSession from './base-session'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
2021-06-04 16:08:43 +00:00
|
|
|
import { getPage, getShape, updateParents } from 'utils/utils'
|
2021-05-28 14:37:23 +00:00
|
|
|
import * as vec from 'utils/vec'
|
|
|
|
import commands from 'state/commands'
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
let prevEndPoint: number[]
|
|
|
|
|
2021-05-27 17:59:40 +00:00
|
|
|
export default class BrushSession extends BaseSession {
|
|
|
|
origin: number[]
|
2021-05-27 20:11:48 +00:00
|
|
|
previous: number[]
|
2021-05-27 17:59:40 +00:00
|
|
|
points: number[][]
|
|
|
|
snapshot: DrawSnapshot
|
2021-05-30 21:24:31 +00:00
|
|
|
isLocked: boolean
|
|
|
|
lockedDirection: 'horizontal' | 'vertical'
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
constructor(data: Data, id: string, point: number[], isLocked = false) {
|
2021-05-27 17:59:40 +00:00
|
|
|
super(data)
|
|
|
|
this.origin = point
|
2021-05-27 20:11:48 +00:00
|
|
|
this.previous = point
|
|
|
|
this.points = []
|
2021-05-27 17:59:40 +00:00
|
|
|
this.snapshot = getDrawSnapshot(data, id)
|
|
|
|
|
|
|
|
const page = getPage(data)
|
|
|
|
const shape = page.shapes[id]
|
2021-06-04 16:08:43 +00:00
|
|
|
getShapeUtils(shape).translateTo(shape, point)
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
update = (data: Data, point: number[], isLocked = false) => {
|
2021-05-29 12:54:33 +00:00
|
|
|
const { snapshot } = this
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-05-30 21:24:31 +00:00
|
|
|
const delta = vec.vec(this.origin, point)
|
|
|
|
|
|
|
|
if (isLocked) {
|
|
|
|
if (!this.isLocked && this.points.length > 1) {
|
|
|
|
this.isLocked = true
|
|
|
|
const returning = [...this.previous]
|
|
|
|
|
|
|
|
if (Math.abs(delta[0]) < Math.abs(delta[1])) {
|
|
|
|
this.lockedDirection = 'vertical'
|
|
|
|
returning[0] = this.origin[0]
|
|
|
|
} else {
|
|
|
|
this.lockedDirection = 'horizontal'
|
|
|
|
returning[1] = this.origin[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.previous = returning
|
|
|
|
this.points.push(vec.sub(returning, this.origin))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.isLocked) {
|
|
|
|
this.isLocked = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isLocked) {
|
|
|
|
if (this.lockedDirection === 'vertical') {
|
|
|
|
point[0] = this.origin[0]
|
|
|
|
} else {
|
|
|
|
point[1] = this.origin[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.previous) {
|
|
|
|
point = vec.med(this.previous, point)
|
|
|
|
}
|
|
|
|
|
|
|
|
prevEndPoint = [...point]
|
|
|
|
const next = vec.sub(point, this.origin)
|
|
|
|
|
|
|
|
this.points.push(next)
|
|
|
|
this.previous = point
|
2021-05-27 17:59:40 +00:00
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = getShape(data, snapshot.id) as DrawShape
|
2021-05-29 12:54:33 +00:00
|
|
|
getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
|
2021-06-04 16:14:01 +00:00
|
|
|
updateParents(data, [shape.id])
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel = (data: Data) => {
|
2021-05-29 12:54:33 +00:00
|
|
|
const { snapshot } = this
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = getShape(data, snapshot.id) as DrawShape
|
2021-05-29 12:54:33 +00:00
|
|
|
getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
|
2021-06-04 16:14:01 +00:00
|
|
|
updateParents(data, [shape.id])
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete = (data: Data) => {
|
2021-05-29 22:27:19 +00:00
|
|
|
if (this.points.length > 1) {
|
|
|
|
let minX = Infinity
|
|
|
|
let minY = Infinity
|
|
|
|
const pts = [...this.points]
|
|
|
|
|
|
|
|
for (let pt of pts) {
|
|
|
|
minX = Math.min(pt[0], minX)
|
|
|
|
minY = Math.min(pt[1], minY)
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let pt of pts) {
|
|
|
|
pt[0] -= minX
|
|
|
|
pt[1] -= minY
|
|
|
|
}
|
|
|
|
|
|
|
|
const { snapshot } = this
|
|
|
|
const page = getPage(data)
|
|
|
|
const shape = page.shapes[snapshot.id] as DrawShape
|
|
|
|
|
|
|
|
getShapeUtils(shape)
|
|
|
|
.setProperty(shape, 'points', pts)
|
2021-06-02 15:05:44 +00:00
|
|
|
.setProperty(shape, 'point', vec.add(shape.point, [minX, minY]))
|
2021-05-29 22:27:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-29 12:54:33 +00:00
|
|
|
commands.draw(data, this.snapshot.id, this.points)
|
2021-05-27 17:59:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDrawSnapshot(data: Data, shapeId: string) {
|
|
|
|
const page = getPage(current(data))
|
2021-06-02 11:50:34 +00:00
|
|
|
const { points } = page.shapes[shapeId] as DrawShape
|
|
|
|
|
2021-05-27 17:59:40 +00:00
|
|
|
return {
|
2021-05-29 12:54:33 +00:00
|
|
|
id: shapeId,
|
2021-05-27 17:59:40 +00:00
|
|
|
points,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type DrawSnapshot = ReturnType<typeof getDrawSnapshot>
|