Преглед на файлове

Fix bug in translating

main
Steve Ruiz преди 4 години
родител
ревизия
4a36ff7341

+ 0
- 2
components/canvas/page.tsx Целия файл

@@ -51,8 +51,6 @@ export default function Page() {
51 51
       .map((shape) => shape.id)
52 52
   }, deepCompareArrays)
53 53
 
54
-  console.log(currentPageShapeIds.length)
55
-
56 54
   const isSelecting = useSelector((s) => s.isIn('selecting'))
57 55
 
58 56
   return (

+ 0
- 5
lib/shape-utils/group.tsx Целия файл

@@ -58,11 +58,6 @@ const group = registerShapeUtils<GroupShape>({
58 58
     )
59 59
   },
60 60
 
61
-  translateTo(shape, point) {
62
-    shape.point = point
63
-    return this
64
-  },
65
-
66 61
   getBounds(shape) {
67 62
     if (!this.boundsCache.has(shape)) {
68 63
       const [width, height] = shape.size

+ 2
- 2
lib/shape-utils/index.tsx Целия файл

@@ -222,12 +222,12 @@ function getDefaultShapeUtil<T extends Shape>(): ShapeUtility<T> {
222 222
     },
223 223
 
224 224
     translateBy(shape, delta) {
225
-      shape.point = vec.add(shape.point, delta)
225
+      shape.point = vec.round(vec.add(shape.point, delta))
226 226
       return this
227 227
     },
228 228
 
229 229
     translateTo(shape, point) {
230
-      shape.point = point
230
+      shape.point = vec.round(point)
231 231
       return this
232 232
     },
233 233
 

+ 4
- 2
state/commands/translate.ts Целия файл

@@ -72,8 +72,10 @@ export default function translateCommand(
72 72
 
73 73
         // Move shapes back to where they started
74 74
         for (const { id, point } of initialShapes) {
75
-          const shape = shapes[id]
76
-          getShapeUtils(shape).translateTo(shape, point)
75
+          getDocumentBranch(data, id).forEach((id) => {
76
+            const shape = shapes[id]
77
+            getShapeUtils(shape).translateTo(shape, point)
78
+          })
77 79
         }
78 80
 
79 81
         // Delete clones

+ 14
- 0
state/hacks.ts Целия файл

@@ -88,7 +88,21 @@ export function fastPinchCamera(
88 88
 
89 89
 export function fastBrushSelect(point: number[]) {
90 90
   const data = { ...state.data }
91
+
91 92
   session.current.update(data, screenToWorld(point, data))
92 93
 
93 94
   state.forceData(Object.freeze(data))
94 95
 }
96
+
97
+export function fastTranslate(info: PointerInfo) {
98
+  const data = { ...state.data }
99
+
100
+  session.current.update(
101
+    data,
102
+    screenToWorld(info.point, data),
103
+    info.shiftKey,
104
+    info.altKey
105
+  )
106
+
107
+  state.forceData(Object.freeze(data))
108
+}

+ 11
- 8
state/sessions/draw-session.ts Целия файл

@@ -24,7 +24,7 @@ export default class BrushSession extends BaseSession {
24 24
     // Add a first point but don't update the shape yet. We'll update
25 25
     // when the draw session ends; if the user hasn't added additional
26 26
     // points, this single point will be interpreted as a "dot" shape.
27
-    this.points = [[0, 0]]
27
+    this.points = [[0, 0, 0.5]]
28 28
 
29 29
     const shape = getPage(data).shapes[id]
30 30
 
@@ -103,13 +103,14 @@ export default class BrushSession extends BaseSession {
103 103
     // Update the points and update the shape's parents.
104 104
     const shape = getShape(data, snapshot.id) as DrawShape
105 105
 
106
-    if (newPoint[0] < 0 || newPoint[1] < 0) {
107
-      const offset = [Math.min(newPoint[0], 0), Math.min(newPoint[1], 0)]
108
-
109
-      this.points = this.points.map((pt) => vec.sub(pt, offset))
110
-
111
-      this.origin = vec.sub(this.origin, offset)
106
+    // Offset the points and shapes to avoid negative numbers
107
+    const ox = Math.min(newPoint[0], 0)
108
+    const oy = Math.min(newPoint[1], 0)
112 109
 
110
+    if (ox < 0 || oy < 0) {
111
+      const offset = [ox, oy]
112
+      this.points = this.points.map((pt) => [...vec.sub(pt, offset), pt[2]])
113
+      this.origin = vec.add(this.origin, offset)
113 114
       getShapeUtils(shape).translateBy(shape, offset)
114 115
     }
115 116
 
@@ -121,6 +122,7 @@ export default class BrushSession extends BaseSession {
121 122
   cancel = (data: Data) => {
122 123
     const { snapshot } = this
123 124
     const shape = getShape(data, snapshot.id) as DrawShape
125
+    getShapeUtils(shape).translateTo(shape, snapshot.point)
124 126
     getShapeUtils(shape).setProperty(shape, 'points', snapshot.points)
125 127
     updateParents(data, [shape.id])
126 128
   }
@@ -144,10 +146,11 @@ export default class BrushSession extends BaseSession {
144 146
 
145 147
 export function getDrawSnapshot(data: Data, shapeId: string) {
146 148
   const page = getPage(current(data))
147
-  const { points } = page.shapes[shapeId] as DrawShape
149
+  const { points, point } = page.shapes[shapeId] as DrawShape
148 150
 
149 151
   return {
150 152
     id: shapeId,
153
+    point,
151 154
     points,
152 155
   }
153 156
 }

+ 0
- 2
state/sessions/transform-session.ts Целия файл

@@ -12,8 +12,6 @@ import {
12 12
   getPage,
13 13
   getRelativeTransformedBoundingBox,
14 14
   getSelectedIds,
15
-  getSelectedShapes,
16
-  getShapes,
17 15
   getTransformedBoundingBox,
18 16
   setToArray,
19 17
   updateParents,

+ 27
- 10
state/sessions/translate-session.ts Целия файл

@@ -8,8 +8,10 @@ import {
8 8
   getChildIndexAbove,
9 9
   getDocumentBranch,
10 10
   getPage,
11
+  getPageState,
11 12
   getSelectedShapes,
12 13
   setSelectedIds,
14
+  setToArray,
13 15
   updateParents,
14 16
 } from 'utils/utils'
15 17
 import { getShapeUtils } from 'lib/shape-utils'
@@ -50,25 +52,28 @@ export default class TranslateSession extends BaseSession {
50 52
       if (!this.isCloning) {
51 53
         this.isCloning = true
52 54
 
53
-        for (const { id } of initialShapes) {
55
+        // Move original shapes back to start
56
+        for (const { id, point } of initialShapes) {
54 57
           const shape = shapes[id]
55
-          getShapeUtils(shape).translateBy(shape, trueDelta)
58
+          getShapeUtils(shape).translateTo(shape, point)
56 59
         }
57 60
 
58 61
         for (const clone of clones) {
59
-          shapes[clone.id] = { ...clone }
60
-          const parent = shapes[clone.parentId]
62
+          shapes[clone.id] = { ...clone, point: [...clone.point] }
63
+
64
+          const shape = shapes[clone.id]
65
+
66
+          getShapeUtils(shape).translateBy(shape, delta)
67
+
68
+          const parent = shapes[shape.parentId]
69
+
61 70
           if (!parent) continue
71
+
62 72
           getShapeUtils(parent).setProperty(parent, 'children', [
63 73
             ...parent.children,
64
-            clone.id,
74
+            shape.id,
65 75
           ])
66 76
         }
67
-
68
-        setSelectedIds(
69
-          data,
70
-          clones.map((c) => c.id)
71
-        )
72 77
       }
73 78
 
74 79
       for (const { id } of clones) {
@@ -76,6 +81,11 @@ export default class TranslateSession extends BaseSession {
76 81
         getShapeUtils(shape).translateBy(shape, trueDelta)
77 82
       }
78 83
 
84
+      setSelectedIds(
85
+        data,
86
+        clones.map((c) => c.id)
87
+      )
88
+
79 89
       updateParents(
80 90
         data,
81 91
         clones.map((c) => c.id)
@@ -93,6 +103,13 @@ export default class TranslateSession extends BaseSession {
93 103
           delete shapes[clone.id]
94 104
         }
95 105
 
106
+        for (const initialShape of initialShapes) {
107
+          getDocumentBranch(data, initialShape.id).forEach((id) => {
108
+            const shape = shapes[id]
109
+            getShapeUtils(shape).translateBy(shape, delta)
110
+          })
111
+        }
112
+
96 113
         initialParents.forEach(
97 114
           (parent) =>
98 115
             ((shapes[parent.id] as GroupShape).children = parent.children)

+ 1
- 1
state/state.ts Целия файл

@@ -876,7 +876,7 @@ const state = createState({
876 876
     createShape(data, payload, type: ShapeType) {
877 877
       const shape = createShape(type, {
878 878
         parentId: data.currentPageId,
879
-        point: screenToWorld(payload.point, data),
879
+        point: vec.round(screenToWorld(payload.point, data)),
880 880
         style: getCurrent(data.currentStyle),
881 881
       })
882 882
 

+ 0
- 1
state/storage.ts Целия файл

@@ -166,7 +166,6 @@ class Storage {
166 166
     const savedPage = localStorage.getItem(storageId(fileId, 'page', pageId))
167 167
 
168 168
     if (savedPage !== null) {
169
-      console.log(lzw_decode(savedPage))
170 169
       data.document.pages[pageId] = JSON.parse(lzw_decode(savedPage))
171 170
     } else {
172 171
       data.document.pages[pageId] = {

Loading…
Отказ
Запис