Browse Source

Fixes bug on drawn dots

main
Steve Ruiz 4 years ago
parent
commit
68577d1838

+ 1
- 1
lib/shape-utils/draw.tsx View File

57
         pathCache.set(
57
         pathCache.set(
58
           shape,
58
           shape,
59
           getSvgPathFromStroke(
59
           getSvgPathFromStroke(
60
-            getStroke(points, { size: +style.strokeWidth * 2 })
60
+            getStroke(points, { size: +style.strokeWidth * 2, thinning: 0.9 })
61
           )
61
           )
62
         )
62
         )
63
       }
63
       }

+ 6
- 3
state/commands/draw.ts View File

5
 import { getShapeUtils } from 'lib/shape-utils'
5
 import { getShapeUtils } from 'lib/shape-utils'
6
 import { current } from 'immer'
6
 import { current } from 'immer'
7
 
7
 
8
-export default function drawCommand(data: Data, id: string, after: number[][]) {
8
+export default function drawCommand(
9
+  data: Data,
10
+  id: string,
11
+  points: number[][]
12
+) {
9
   const restoreShape = current(getPage(data)).shapes[id] as DrawShape
13
   const restoreShape = current(getPage(data)).shapes[id] as DrawShape
10
-
11
-  getShapeUtils(restoreShape).setProperty!(restoreShape, 'points', after)
14
+  getShapeUtils(restoreShape).setProperty(restoreShape, 'points', points)
12
 
15
 
13
   history.execute(
16
   history.execute(
14
     data,
17
     data,

+ 10
- 10
state/sessions/draw-session.ts View File

11
   previous: number[]
11
   previous: number[]
12
   points: number[][]
12
   points: number[][]
13
   snapshot: DrawSnapshot
13
   snapshot: DrawSnapshot
14
-  shapeId: string
15
 
14
 
16
   constructor(data: Data, id: string, point: number[]) {
15
   constructor(data: Data, id: string, point: number[]) {
17
     super(data)
16
     super(data)
18
-    this.shapeId = id
19
     this.origin = point
17
     this.origin = point
20
     this.previous = point
18
     this.previous = point
21
     this.points = []
19
     this.points = []
27
   }
25
   }
28
 
26
 
29
   update = (data: Data, point: number[]) => {
27
   update = (data: Data, point: number[]) => {
30
-    const { shapeId } = this
28
+    const { snapshot } = this
31
 
29
 
32
     const lp = vec.med(this.previous, vec.toPrecision(point))
30
     const lp = vec.med(this.previous, vec.toPrecision(point))
33
     this.points.push(vec.sub(lp, this.origin))
31
     this.points.push(vec.sub(lp, this.origin))
34
     this.previous = lp
32
     this.previous = lp
35
 
33
 
36
     const page = getPage(data)
34
     const page = getPage(data)
37
-    const shape = page.shapes[shapeId]
38
-    getShapeUtils(shape).setPoints!(shape, [...this.points])
35
+    const shape = page.shapes[snapshot.id] as DrawShape
36
+    getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
39
   }
37
   }
40
 
38
 
41
   cancel = (data: Data) => {
39
   cancel = (data: Data) => {
42
-    const { shapeId, snapshot } = this
40
+    const { snapshot } = this
43
     const page = getPage(data)
41
     const page = getPage(data)
44
-    const shape = page.shapes[shapeId]
45
-    getShapeUtils(shape).setPoints!(shape, snapshot.points)
42
+    const shape = page.shapes[snapshot.id] as DrawShape
43
+    getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
46
   }
44
   }
47
 
45
 
48
   complete = (data: Data) => {
46
   complete = (data: Data) => {
49
-    commands.draw(data, this.shapeId, this.snapshot.points, this.points)
47
+    commands.draw(data, this.snapshot.id, this.points)
50
   }
48
   }
51
 }
49
 }
52
 
50
 
53
 export function getDrawSnapshot(data: Data, shapeId: string) {
51
 export function getDrawSnapshot(data: Data, shapeId: string) {
54
   const page = getPage(current(data))
52
   const page = getPage(current(data))
55
-  const { points } = page.shapes[shapeId] as DrawShape
53
+  const { points, style } = page.shapes[shapeId] as DrawShape
56
   return {
54
   return {
55
+    id: shapeId,
57
     points,
56
     points,
57
+    strokeWidth: style.strokeWidth,
58
   }
58
   }
59
 }
59
 }
60
 
60
 

+ 9
- 3
state/sessions/rotate-session.ts View File

70
   }
70
   }
71
 
71
 
72
   complete(data: Data) {
72
   complete(data: Data) {
73
+    if (!this.snapshot.hasUnlockedShapes) return
73
     commands.rotate(data, this.snapshot, getRotateSnapshot(data))
74
     commands.rotate(data, this.snapshot, getRotateSnapshot(data))
74
   }
75
   }
75
 }
76
 }
76
 
77
 
77
 export function getRotateSnapshot(data: Data) {
78
 export function getRotateSnapshot(data: Data) {
78
-  const shapes = getSelectedShapes(current(data))
79
+  const initialShapes = getSelectedShapes(current(data)).filter(
80
+    (shape) => !shape.isLocked
81
+  )
82
+
83
+  const hasUnlockedShapes = initialShapes.length > 0
79
 
84
 
80
   const shapesBounds = Object.fromEntries(
85
   const shapesBounds = Object.fromEntries(
81
-    shapes.map((shape) => [shape.id, getShapeBounds(shape)])
86
+    initialShapes.map((shape) => [shape.id, getShapeBounds(shape)])
82
   )
87
   )
83
 
88
 
84
   const bounds = getCommonBounds(...Object.values(shapesBounds))
89
   const bounds = getCommonBounds(...Object.values(shapesBounds))
85
 
90
 
86
   return {
91
   return {
92
+    hasUnlockedShapes,
87
     currentPageId: data.currentPageId,
93
     currentPageId: data.currentPageId,
88
     boundsRotation: data.boundsRotation,
94
     boundsRotation: data.boundsRotation,
89
     boundsCenter: getBoundsCenter(bounds),
95
     boundsCenter: getBoundsCenter(bounds),
90
-    shapes: shapes.map(({ id, point, rotation }) => {
96
+    shapes: initialShapes.map(({ id, point, rotation }) => {
91
       const bounds = shapesBounds[id]
97
       const bounds = shapesBounds[id]
92
       const offset = [bounds.width / 2, bounds.height / 2]
98
       const offset = [bounds.width / 2, bounds.height / 2]
93
       const center = getBoundsCenter(bounds)
99
       const center = getBoundsCenter(bounds)

+ 7
- 6
state/sessions/transform-session.ts View File

95
   }
95
   }
96
 
96
 
97
   complete(data: Data) {
97
   complete(data: Data) {
98
+    if (!this.snapshot.hasUnlockedShapes) return
99
+
98
     commands.transform(
100
     commands.transform(
99
       data,
101
       data,
100
       this.snapshot,
102
       this.snapshot,
110
   const initialShapes = getSelectedShapes(cData).filter(
112
   const initialShapes = getSelectedShapes(cData).filter(
111
     (shape) => !shape.isLocked
113
     (shape) => !shape.isLocked
112
   )
114
   )
113
-  const hasShapes = initialShapes.length > 0
114
 
115
 
115
-  // A mapping of selected shapes and their bounds
116
+  const hasUnlockedShapes = initialShapes.length > 0
117
+
116
   const shapesBounds = Object.fromEntries(
118
   const shapesBounds = Object.fromEntries(
117
     initialShapes.map((shape) => [
119
     initialShapes.map((shape) => [
118
       shape.id,
120
       shape.id,
122
 
124
 
123
   const boundsArr = Object.values(shapesBounds)
125
   const boundsArr = Object.values(shapesBounds)
124
 
126
 
125
-  // The common (exterior) bounds of the selected shapes
126
-  const bounds = getCommonBounds(...boundsArr)
127
+  const commonBounds = getCommonBounds(...boundsArr)
127
 
128
 
128
   const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
129
   const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
129
 
130
 
131
   // positions of the shape's bounds within the common bounds shape.
132
   // positions of the shape's bounds within the common bounds shape.
132
   return {
133
   return {
133
     type: transformType,
134
     type: transformType,
134
-    hasShapes,
135
+    hasUnlockedShapes,
135
     currentPageId,
136
     currentPageId,
136
-    initialBounds: bounds,
137
+    initialBounds: commonBounds,
137
     shapeBounds: Object.fromEntries(
138
     shapeBounds: Object.fromEntries(
138
       initialShapes.map((shape) => {
139
       initialShapes.map((shape) => {
139
         const initialShapeBounds = shapesBounds[shape.id]
140
         const initialShapeBounds = shapesBounds[shape.id]

+ 10
- 7
state/sessions/transform-single-session.ts View File

1
-import { Data, Edge, Corner } from "types"
2
-import * as vec from "utils/vec"
3
-import BaseSession from "./base-session"
4
-import commands from "state/commands"
5
-import { current } from "immer"
6
-import { getShapeUtils } from "lib/shape-utils"
1
+import { Data, Edge, Corner } from 'types'
2
+import * as vec from 'utils/vec'
3
+import BaseSession from './base-session'
4
+import commands from 'state/commands'
5
+import { current } from 'immer'
6
+import { getShapeUtils } from 'lib/shape-utils'
7
 import {
7
 import {
8
   getTransformedBoundingBox,
8
   getTransformedBoundingBox,
9
   getCommonBounds,
9
   getCommonBounds,
12
   getPage,
12
   getPage,
13
   getShape,
13
   getShape,
14
   getSelectedShapes,
14
   getSelectedShapes,
15
-} from "utils/utils"
15
+} from 'utils/utils'
16
 
16
 
17
 export default class TransformSingleSession extends BaseSession {
17
 export default class TransformSingleSession extends BaseSession {
18
   transformType: Edge | Corner
18
   transformType: Edge | Corner
79
   }
79
   }
80
 
80
 
81
   complete(data: Data) {
81
   complete(data: Data) {
82
+    if (!this.snapshot.hasUnlockedShape) return
83
+
82
     commands.transformSingle(
84
     commands.transformSingle(
83
       data,
85
       data,
84
       this.snapshot,
86
       this.snapshot,
99
 
101
 
100
   return {
102
   return {
101
     id: shape.id,
103
     id: shape.id,
104
+    hasUnlockedShape: !shape.isLocked,
102
     currentPageId: data.currentPageId,
105
     currentPageId: data.currentPageId,
103
     type: transformType,
106
     type: transformType,
104
     initialShape: shape,
107
     initialShape: shape,

+ 3
- 3
state/sessions/translate-session.ts View File

89
   }
89
   }
90
 
90
 
91
   complete(data: Data) {
91
   complete(data: Data) {
92
-    if (!this.snapshot.hasShapes) return
92
+    if (!this.snapshot.hasUnlockedShapes) return
93
 
93
 
94
     commands.translate(
94
     commands.translate(
95
       data,
95
       data,
103
 export function getTranslateSnapshot(data: Data) {
103
 export function getTranslateSnapshot(data: Data) {
104
   const cData = current(data)
104
   const cData = current(data)
105
   const shapes = getSelectedShapes(cData).filter((shape) => !shape.isLocked)
105
   const shapes = getSelectedShapes(cData).filter((shape) => !shape.isLocked)
106
-  const hasShapes = shapes.length > 0
106
+  const hasUnlockedShapes = shapes.length > 0
107
 
107
 
108
   return {
108
   return {
109
+    hasUnlockedShapes,
109
     currentPageId: data.currentPageId,
110
     currentPageId: data.currentPageId,
110
     initialShapes: shapes.map(({ id, point }) => ({ id, point })),
111
     initialShapes: shapes.map(({ id, point }) => ({ id, point })),
111
     clones: shapes.map((shape) => ({
112
     clones: shapes.map((shape) => ({
113
       id: uuid(),
114
       id: uuid(),
114
       childIndex: getChildIndexAbove(cData, shape.id),
115
       childIndex: getChildIndexAbove(cData, shape.id),
115
     })),
116
     })),
116
-    hasShapes,
117
   }
117
   }
118
 }
118
 }
119
 
119
 

+ 1
- 1
utils/utils.ts View File

978
   let maxX = -Infinity
978
   let maxX = -Infinity
979
   let maxY = -Infinity
979
   let maxY = -Infinity
980
 
980
 
981
-  if (points.length === 0) {
981
+  if (points.length < 2) {
982
     minX = 0
982
     minX = 0
983
     minY = 0
983
     minY = 0
984
     maxX = 1
984
     maxX = 1

Loading…
Cancel
Save