|
|
@@ -2,34 +2,81 @@ import { current } from 'immer'
|
|
2
|
2
|
import { Data, DrawShape } from 'types'
|
|
3
|
3
|
import BaseSession from './base-session'
|
|
4
|
4
|
import { getShapeUtils } from 'lib/shape-utils'
|
|
5
|
|
-import { getPage, simplify } from 'utils/utils'
|
|
|
5
|
+import { getPage } from 'utils/utils'
|
|
6
|
6
|
import * as vec from 'utils/vec'
|
|
7
|
7
|
import commands from 'state/commands'
|
|
8
|
8
|
|
|
|
9
|
+let prevEndPoint: number[]
|
|
|
10
|
+
|
|
9
|
11
|
export default class BrushSession extends BaseSession {
|
|
10
|
12
|
origin: number[]
|
|
11
|
13
|
previous: number[]
|
|
12
|
14
|
points: number[][]
|
|
13
|
15
|
snapshot: DrawSnapshot
|
|
|
16
|
+ isLocked: boolean
|
|
|
17
|
+ lockedDirection: 'horizontal' | 'vertical'
|
|
14
|
18
|
|
|
15
|
|
- constructor(data: Data, id: string, point: number[]) {
|
|
|
19
|
+ constructor(data: Data, id: string, point: number[], isLocked = false) {
|
|
16
|
20
|
super(data)
|
|
17
|
21
|
this.origin = point
|
|
18
|
22
|
this.previous = point
|
|
19
|
23
|
this.points = []
|
|
20
|
24
|
this.snapshot = getDrawSnapshot(data, id)
|
|
21
|
25
|
|
|
|
26
|
+ // if (isLocked && prevEndPoint) {
|
|
|
27
|
+ // const continuedPt = vec.sub([...prevEndPoint], this.origin)
|
|
|
28
|
+ // this.points.push(continuedPt)
|
|
|
29
|
+ // }
|
|
|
30
|
+
|
|
22
|
31
|
const page = getPage(data)
|
|
23
|
32
|
const shape = page.shapes[id]
|
|
24
|
33
|
getShapeUtils(shape).translateTo(shape, point)
|
|
25
|
34
|
}
|
|
26
|
35
|
|
|
27
|
|
- update = (data: Data, point: number[]) => {
|
|
|
36
|
+ update = (data: Data, point: number[], isLocked = false) => {
|
|
28
|
37
|
const { snapshot } = this
|
|
29
|
38
|
|
|
30
|
|
- const lp = vec.med(this.previous, vec.toPrecision(point))
|
|
31
|
|
- this.points.push(vec.sub(lp, this.origin))
|
|
32
|
|
- this.previous = lp
|
|
|
39
|
+ const delta = vec.vec(this.origin, point)
|
|
|
40
|
+
|
|
|
41
|
+ if (isLocked) {
|
|
|
42
|
+ if (!this.isLocked && this.points.length > 1) {
|
|
|
43
|
+ this.isLocked = true
|
|
|
44
|
+ const returning = [...this.previous]
|
|
|
45
|
+
|
|
|
46
|
+ if (Math.abs(delta[0]) < Math.abs(delta[1])) {
|
|
|
47
|
+ this.lockedDirection = 'vertical'
|
|
|
48
|
+ returning[0] = this.origin[0]
|
|
|
49
|
+ } else {
|
|
|
50
|
+ this.lockedDirection = 'horizontal'
|
|
|
51
|
+ returning[1] = this.origin[1]
|
|
|
52
|
+ }
|
|
|
53
|
+
|
|
|
54
|
+ this.previous = returning
|
|
|
55
|
+ this.points.push(vec.sub(returning, this.origin))
|
|
|
56
|
+ }
|
|
|
57
|
+ } else {
|
|
|
58
|
+ if (this.isLocked) {
|
|
|
59
|
+ this.isLocked = false
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+
|
|
|
63
|
+ if (this.isLocked) {
|
|
|
64
|
+ if (this.lockedDirection === 'vertical') {
|
|
|
65
|
+ point[0] = this.origin[0]
|
|
|
66
|
+ } else {
|
|
|
67
|
+ point[1] = this.origin[1]
|
|
|
68
|
+ }
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ if (this.previous) {
|
|
|
72
|
+ point = vec.med(this.previous, point)
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ prevEndPoint = [...point]
|
|
|
76
|
+ const next = vec.sub(point, this.origin)
|
|
|
77
|
+
|
|
|
78
|
+ this.points.push(next)
|
|
|
79
|
+ this.previous = point
|
|
33
|
80
|
|
|
34
|
81
|
const page = getPage(data)
|
|
35
|
82
|
const shape = page.shapes[snapshot.id] as DrawShape
|