Pārlūkot izejas kodu

Improves translating, breaks save/load again

main
Steve Ruiz 4 gadus atpakaļ
vecāks
revīzija
94fcca1685

+ 10
- 4
hooks/useCanvasEvents.ts Parādīt failu

@@ -1,6 +1,6 @@
1 1
 import { MutableRefObject, useCallback } from 'react'
2 2
 import state from 'state'
3
-import { fastBrushSelect, fastDrawUpdate } from 'state/hacks'
3
+import { fastBrushSelect, fastDrawUpdate, fastTranslate } from 'state/hacks'
4 4
 import inputs from 'state/inputs'
5 5
 import { isMobile } from 'utils/utils'
6 6
 
@@ -30,18 +30,24 @@ export default function useCanvasEvents(
30 30
   const handlePointerMove = useCallback((e: React.PointerEvent) => {
31 31
     if (!inputs.canAccept(e.pointerId)) return
32 32
 
33
+    const info = inputs.pointerMove(e)
34
+
33 35
     if (state.isIn('draw.editing')) {
34
-      fastDrawUpdate(inputs.pointerMove(e))
36
+      fastDrawUpdate(info)
35 37
       return
36 38
     }
37 39
 
38 40
     if (state.isIn('brushSelecting')) {
39
-      const info = inputs.pointerMove(e)
40 41
       fastBrushSelect(info.point)
41 42
       return
42 43
     }
43 44
 
44
-    state.send('MOVED_POINTER', inputs.pointerMove(e))
45
+    if (state.isIn('translatingSelection')) {
46
+      fastTranslate(info)
47
+      return
48
+    }
49
+
50
+    state.send('MOVED_POINTER', info)
45 51
   }, [])
46 52
 
47 53
   const handlePointerUp = useCallback((e: React.PointerEvent) => {

+ 6
- 4
hooks/useShapeEvents.ts Parādīt failu

@@ -14,14 +14,16 @@ export default function useShapeEvents(
14 14
       e.stopPropagation()
15 15
       rGroup.current.setPointerCapture(e.pointerId)
16 16
 
17
+      const info = inputs.pointerDown(e, id)
18
+
17 19
       if (e.button === 0) {
18
-        if (inputs.isDoubleClick()) {
19
-          state.send('DOUBLE_POINTED_SHAPE', inputs.pointerDown(e, id))
20
+        if (inputs.isDoubleClick() && !(info.altKey || info.metaKey)) {
21
+          state.send('DOUBLE_POINTED_SHAPE', info)
20 22
         }
21 23
 
22
-        state.send('POINTED_SHAPE', inputs.pointerDown(e, id))
24
+        state.send('POINTED_SHAPE', info)
23 25
       } else {
24
-        state.send('RIGHT_POINTED', inputs.pointerDown(e, id))
26
+        state.send('RIGHT_POINTED', info)
25 27
       }
26 28
     },
27 29
     [id]

+ 4
- 4
lib/shape-utils/rectangle.tsx Parādīt failu

@@ -12,7 +12,7 @@ import {
12 12
 import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
13 13
 import getStroke from 'perfect-freehand'
14 14
 
15
-const pathCache = new WeakMap<RectangleShape, string>([])
15
+const pathCache = new WeakMap<number[], string>([])
16 16
 
17 17
 const rectangle = registerShapeUtils<RectangleShape>({
18 18
   boundsCache: new WeakMap([]),
@@ -42,11 +42,11 @@ const rectangle = registerShapeUtils<RectangleShape>({
42 42
     const { id, size, radius, style } = shape
43 43
     const styles = getShapeStyle(style)
44 44
 
45
-    if (!pathCache.has(shape)) {
45
+    if (!pathCache.has(shape.size)) {
46 46
       renderPath(shape)
47 47
     }
48 48
 
49
-    const path = pathCache.get(shape)
49
+    const path = pathCache.get(shape.size)
50 50
 
51 51
     return (
52 52
       <g id={id}>
@@ -163,5 +163,5 @@ function renderPath(shape: RectangleShape) {
163 163
     }
164 164
   )
165 165
 
166
-  pathCache.set(shape, getSvgPathFromStroke(stroke))
166
+  pathCache.set(shape.size, getSvgPathFromStroke(stroke))
167 167
 }

+ 13
- 12
state/sessions/translate-session.ts Parādīt failu

@@ -56,6 +56,7 @@ export default class TranslateSession extends BaseSession {
56 56
         for (const { id, point } of initialShapes) {
57 57
           const shape = shapes[id]
58 58
           getShapeUtils(shape).translateTo(shape, point)
59
+          shapes[shape.id] = { ...shape }
59 60
         }
60 61
 
61 62
         for (const clone of clones) {
@@ -65,6 +66,8 @@ export default class TranslateSession extends BaseSession {
65 66
 
66 67
           getShapeUtils(shape).translateBy(shape, delta)
67 68
 
69
+          shapes[clone.id] = { ...shape }
70
+
68 71
           const parent = shapes[shape.parentId]
69 72
 
70 73
           if (!parent) continue
@@ -73,12 +76,15 @@ export default class TranslateSession extends BaseSession {
73 76
             ...parent.children,
74 77
             shape.id,
75 78
           ])
79
+
80
+          shapes[shape.parentId] = { ...parent }
76 81
         }
77 82
       }
78 83
 
79 84
       for (const { id } of clones) {
80 85
         const shape = shapes[id]
81 86
         getShapeUtils(shape).translateBy(shape, trueDelta)
87
+        shapes[id] = { ...shape }
82 88
       }
83 89
 
84 90
       setSelectedIds(
@@ -107,19 +113,22 @@ export default class TranslateSession extends BaseSession {
107 113
           getDocumentBranch(data, initialShape.id).forEach((id) => {
108 114
             const shape = shapes[id]
109 115
             getShapeUtils(shape).translateBy(shape, delta)
116
+            shapes[id] = { ...shape }
110 117
           })
111 118
         }
112 119
 
113
-        initialParents.forEach(
114
-          (parent) =>
115
-            ((shapes[parent.id] as GroupShape).children = parent.children)
116
-        )
120
+        initialParents.forEach((parent) => {
121
+          const shape = shapes[parent.id] as GroupShape
122
+          shapes[parent.id] = { ...shape, children: parent.children }
123
+        })
117 124
       }
118 125
 
119 126
       for (const initialShape of initialShapes) {
120 127
         getDocumentBranch(data, initialShape.id).forEach((id) => {
121 128
           const shape = shapes[id]
122 129
           getShapeUtils(shape).translateBy(shape, trueDelta)
130
+
131
+          shapes[id] = { ...shape }
123 132
         })
124 133
       }
125 134
 
@@ -204,7 +213,6 @@ export function getTranslateSnapshot(data: Data) {
204 213
     clones: selectedShapes
205 214
       .filter((shape) => shape.type !== ShapeType.Group)
206 215
       .flatMap((shape) => {
207
-        // TODO: Clone children recursively
208 216
         const clone = {
209 217
           ...shape,
210 218
           id: uuid(),
@@ -214,13 +222,6 @@ export function getTranslateSnapshot(data: Data) {
214 222
         }
215 223
 
216 224
         return clone
217
-
218
-        // cloneGroup(cData, {
219
-        //   ...shape,
220
-        //   id: uuid(),
221
-        //   parentId: shape.parentId,
222
-        //   childIndex: getChildIndexAbove(cData, shape.id),
223
-        // })
224 225
       }),
225 226
   }
226 227
 }

+ 2
- 2
state/storage.ts Parādīt failu

@@ -6,7 +6,7 @@ import { current } from 'immer'
6 6
 import { v4 as uuid } from 'uuid'
7 7
 import * as idb from 'idb-keyval'
8 8
 
9
-const CURRENT_VERSION = 'code_slate_0.0.6'
9
+const CURRENT_VERSION = 'code_slate_0.0.7'
10 10
 const DOCUMENT_ID = '0001'
11 11
 
12 12
 function storageId(fileId: string, label: string, id?: string) {
@@ -27,7 +27,7 @@ class Storage {
27 27
 
28 28
     this.loadPage(data, data.currentPageId)
29 29
 
30
-    // this.saveToLocalStorage(data, data.document.id)
30
+    this.saveToLocalStorage(data, data.document.id)
31 31
 
32 32
     localStorage.setItem(`${CURRENT_VERSION}_lastOpened`, data.document.id)
33 33
   }

+ 5
- 0
utils/utils.ts Parādīt failu

@@ -1636,6 +1636,8 @@ export function updateParents(data: Data, changedShapeIds: string[]) {
1636 1636
       parent,
1637 1637
       parent.children.map((id) => shapes[id])
1638 1638
     )
1639
+
1640
+    shapes[parentId] = { ...parent }
1639 1641
   }
1640 1642
 
1641 1643
   updateParents(data, parentToUpdateIds)
@@ -1770,6 +1772,7 @@ export function getPoint(
1770 1772
 }
1771 1773
 
1772 1774
 export function lzw_encode(s: string) {
1775
+  return s
1773 1776
   const dict = {}
1774 1777
   const data = (s + '').split('')
1775 1778
 
@@ -1803,6 +1806,8 @@ export function lzw_encode(s: string) {
1803 1806
 
1804 1807
 // Decompress an LZW-encoded string
1805 1808
 export function lzw_decode(s: string) {
1809
+  return s
1810
+
1806 1811
   const dict = {}
1807 1812
   const data = (s + '').split('')
1808 1813
 

Notiek ielāde…
Atcelt
Saglabāt