浏览代码

Fixes bug on drawn dots

main
Steve Ruiz 4 年前
父节点
当前提交
68577d1838

+ 1
- 1
lib/shape-utils/draw.tsx 查看文件

@@ -57,7 +57,7 @@ const draw = registerShapeUtils<DrawShape>({
57 57
         pathCache.set(
58 58
           shape,
59 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 查看文件

@@ -5,10 +5,13 @@ import { getPage } from 'utils/utils'
5 5
 import { getShapeUtils } from 'lib/shape-utils'
6 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 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 16
   history.execute(
14 17
     data,

+ 10
- 10
state/sessions/draw-session.ts 查看文件

@@ -11,11 +11,9 @@ export default class BrushSession extends BaseSession {
11 11
   previous: number[]
12 12
   points: number[][]
13 13
   snapshot: DrawSnapshot
14
-  shapeId: string
15 14
 
16 15
   constructor(data: Data, id: string, point: number[]) {
17 16
     super(data)
18
-    this.shapeId = id
19 17
     this.origin = point
20 18
     this.previous = point
21 19
     this.points = []
@@ -27,34 +25,36 @@ export default class BrushSession extends BaseSession {
27 25
   }
28 26
 
29 27
   update = (data: Data, point: number[]) => {
30
-    const { shapeId } = this
28
+    const { snapshot } = this
31 29
 
32 30
     const lp = vec.med(this.previous, vec.toPrecision(point))
33 31
     this.points.push(vec.sub(lp, this.origin))
34 32
     this.previous = lp
35 33
 
36 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 39
   cancel = (data: Data) => {
42
-    const { shapeId, snapshot } = this
40
+    const { snapshot } = this
43 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 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 51
 export function getDrawSnapshot(data: Data, shapeId: string) {
54 52
   const page = getPage(current(data))
55
-  const { points } = page.shapes[shapeId] as DrawShape
53
+  const { points, style } = page.shapes[shapeId] as DrawShape
56 54
   return {
55
+    id: shapeId,
57 56
     points,
57
+    strokeWidth: style.strokeWidth,
58 58
   }
59 59
 }
60 60
 

+ 9
- 3
state/sessions/rotate-session.ts 查看文件

@@ -70,24 +70,30 @@ export default class RotateSession extends BaseSession {
70 70
   }
71 71
 
72 72
   complete(data: Data) {
73
+    if (!this.snapshot.hasUnlockedShapes) return
73 74
     commands.rotate(data, this.snapshot, getRotateSnapshot(data))
74 75
   }
75 76
 }
76 77
 
77 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 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 89
   const bounds = getCommonBounds(...Object.values(shapesBounds))
85 90
 
86 91
   return {
92
+    hasUnlockedShapes,
87 93
     currentPageId: data.currentPageId,
88 94
     boundsRotation: data.boundsRotation,
89 95
     boundsCenter: getBoundsCenter(bounds),
90
-    shapes: shapes.map(({ id, point, rotation }) => {
96
+    shapes: initialShapes.map(({ id, point, rotation }) => {
91 97
       const bounds = shapesBounds[id]
92 98
       const offset = [bounds.width / 2, bounds.height / 2]
93 99
       const center = getBoundsCenter(bounds)

+ 7
- 6
state/sessions/transform-session.ts 查看文件

@@ -95,6 +95,8 @@ export default class TransformSession extends BaseSession {
95 95
   }
96 96
 
97 97
   complete(data: Data) {
98
+    if (!this.snapshot.hasUnlockedShapes) return
99
+
98 100
     commands.transform(
99 101
       data,
100 102
       this.snapshot,
@@ -110,9 +112,9 @@ export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
110 112
   const initialShapes = getSelectedShapes(cData).filter(
111 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 118
   const shapesBounds = Object.fromEntries(
117 119
     initialShapes.map((shape) => [
118 120
       shape.id,
@@ -122,8 +124,7 @@ export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
122 124
 
123 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 129
   const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
129 130
 
@@ -131,9 +132,9 @@ export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
131 132
   // positions of the shape's bounds within the common bounds shape.
132 133
   return {
133 134
     type: transformType,
134
-    hasShapes,
135
+    hasUnlockedShapes,
135 136
     currentPageId,
136
-    initialBounds: bounds,
137
+    initialBounds: commonBounds,
137 138
     shapeBounds: Object.fromEntries(
138 139
       initialShapes.map((shape) => {
139 140
         const initialShapeBounds = shapesBounds[shape.id]

+ 10
- 7
state/sessions/transform-single-session.ts 查看文件

@@ -1,9 +1,9 @@
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 7
 import {
8 8
   getTransformedBoundingBox,
9 9
   getCommonBounds,
@@ -12,7 +12,7 @@ import {
12 12
   getPage,
13 13
   getShape,
14 14
   getSelectedShapes,
15
-} from "utils/utils"
15
+} from 'utils/utils'
16 16
 
17 17
 export default class TransformSingleSession extends BaseSession {
18 18
   transformType: Edge | Corner
@@ -79,6 +79,8 @@ export default class TransformSingleSession extends BaseSession {
79 79
   }
80 80
 
81 81
   complete(data: Data) {
82
+    if (!this.snapshot.hasUnlockedShape) return
83
+
82 84
     commands.transformSingle(
83 85
       data,
84 86
       this.snapshot,
@@ -99,6 +101,7 @@ export function getTransformSingleSnapshot(
99 101
 
100 102
   return {
101 103
     id: shape.id,
104
+    hasUnlockedShape: !shape.isLocked,
102 105
     currentPageId: data.currentPageId,
103 106
     type: transformType,
104 107
     initialShape: shape,

+ 3
- 3
state/sessions/translate-session.ts 查看文件

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

+ 1
- 1
utils/utils.ts 查看文件

@@ -978,7 +978,7 @@ export function getBoundsFromPoints(points: number[][]): Bounds {
978 978
   let maxX = -Infinity
979 979
   let maxY = -Infinity
980 980
 
981
-  if (points.length === 0) {
981
+  if (points.length < 2) {
982 982
     minX = 0
983 983
     minY = 0
984 984
     maxX = 1

正在加载...
取消
保存