浏览代码

Improves cloning

main
Steve Ruiz 4 年前
父节点
当前提交
eea9af5f31
共有 3 个文件被更改,包括 77 次插入49 次删除
  1. 16
    10
      state/commands/translate.ts
  2. 44
    29
      state/sessions/translate-session.ts
  3. 17
    10
      state/state.ts

+ 16
- 10
state/commands/translate.ts 查看文件

@@ -24,9 +24,14 @@ export default function translateCommand(
24 24
 
25 25
         data.selectedIds.clear()
26 26
 
27
-        for (let id in initialShapes) {
28
-          shapes[id].point = initialShapes[id].point
29
-          shapes[clones[id].id] = clones[id]
27
+        if (isCloning) {
28
+          for (const clone of clones) {
29
+            shapes[clone.id] = clone
30
+          }
31
+        }
32
+
33
+        for (const { id, point } of initialShapes) {
34
+          shapes[id].point = point
30 35
           data.selectedIds.add(id)
31 36
         }
32 37
       },
@@ -36,15 +41,16 @@ export default function translateCommand(
36 41
 
37 42
         data.selectedIds.clear()
38 43
 
39
-        for (let id in initialShapes) {
40
-          shapes[id].point = initialShapes[id].point
41
-          data.selectedIds.add(id)
42
-
43
-          if (isCloning) {
44
-            const clone = clones[id]
45
-            delete shapes[clone.id]
44
+        if (isCloning) {
45
+          for (const { id } of clones) {
46
+            delete shapes[id]
46 47
           }
47 48
         }
49
+
50
+        for (const { id, point } of initialShapes) {
51
+          shapes[id].point = point
52
+          data.selectedIds.add(id)
53
+        }
48 54
       },
49 55
     })
50 56
   )

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

@@ -20,6 +20,7 @@ export default class TranslateSession extends BaseSession {
20 20
   update(data: Data, point: number[], isAligned: boolean, isCloning: boolean) {
21 21
     const { currentPageId, clones, initialShapes } = this.snapshot
22 22
     const { shapes } = data.document.pages[currentPageId]
23
+
23 24
     const delta = vec.vec(this.origin, point)
24 25
 
25 26
     if (isAligned) {
@@ -30,22 +31,41 @@ export default class TranslateSession extends BaseSession {
30 31
       }
31 32
     }
32 33
 
33
-    if (isCloning && !this.isCloning) {
34
-      this.isCloning = true
35
-      for (let id in clones) {
36
-        const clone = clones[id]
37
-        shapes[clone.id] = clone
34
+    if (isCloning) {
35
+      if (!this.isCloning) {
36
+        this.isCloning = true
37
+        data.selectedIds.clear()
38
+
39
+        for (const { id, point } of initialShapes) {
40
+          shapes[id].point = point
41
+        }
42
+
43
+        for (const clone of clones) {
44
+          shapes[clone.id] = { ...clone }
45
+          data.selectedIds.add(clone.id)
46
+        }
38 47
       }
39
-    } else if (!isCloning && this.isCloning) {
40
-      this.isCloning = false
41
-      for (let id in clones) {
42
-        const clone = clones[id]
43
-        delete shapes[clone.id]
48
+
49
+      for (const { id, point } of clones) {
50
+        shapes[id].point = vec.add(point, delta)
44 51
       }
45
-    }
52
+    } else {
53
+      if (this.isCloning) {
54
+        this.isCloning = false
55
+        data.selectedIds.clear()
56
+
57
+        for (const { id } of initialShapes) {
58
+          data.selectedIds.add(id)
59
+        }
46 60
 
47
-    for (let id in initialShapes) {
48
-      shapes[id].point = vec.add(initialShapes[id].point, delta)
61
+        for (const clone of clones) {
62
+          delete shapes[clone.id]
63
+        }
64
+      }
65
+
66
+      for (const { id, point } of initialShapes) {
67
+        shapes[id].point = vec.add(point, delta)
68
+      }
49 69
     }
50 70
   }
51 71
 
@@ -53,9 +73,12 @@ export default class TranslateSession extends BaseSession {
53 73
     const { initialShapes, clones, currentPageId } = this.snapshot
54 74
     const { shapes } = data.document.pages[currentPageId]
55 75
 
56
-    for (let id in initialShapes) {
57
-      shapes[id].point = initialShapes[id].point
58
-      delete shapes[clones[id].id]
76
+    for (const { id, point } of initialShapes) {
77
+      shapes[id].point = point
78
+    }
79
+
80
+    for (const { id } of clones) {
81
+      delete shapes[id]
59 82
     }
60 83
   }
61 84
 
@@ -70,24 +93,16 @@ export default class TranslateSession extends BaseSession {
70 93
 }
71 94
 
72 95
 export function getTranslateSnapshot(data: Data) {
73
-  const {
74
-    document: { pages },
75
-    currentPageId,
76
-  } = current(data)
96
+  const { document, selectedIds, currentPageId } = current(data)
77 97
 
78
-  const shapes = Array.from(data.selectedIds.values()).map(
79
-    (id) => pages[currentPageId].shapes[id]
98
+  const shapes = Array.from(selectedIds.values()).map(
99
+    (id) => document.pages[currentPageId].shapes[id]
80 100
   )
81 101
 
82
-  // Clones and shapes are keyed under the same id, though the clone itself
83
-  // has a different id.
84
-
85 102
   return {
86 103
     currentPageId,
87
-    initialShapes: Object.fromEntries(shapes.map((shape) => [shape.id, shape])),
88
-    clones: Object.fromEntries(
89
-      shapes.map((shape) => [shape.id, { ...shape, id: uuid() }])
90
-    ),
104
+    initialShapes: shapes.map(({ id, point }) => ({ id, point })),
105
+    clones: shapes.map((shape) => ({ ...shape, id: uuid() })),
91 106
   }
92 107
 }
93 108
 

+ 17
- 10
state/state.ts 查看文件

@@ -112,16 +112,18 @@ const state = createState({
112 112
                 },
113 113
                 UNHOVERED_SHAPE: "clearHoveredId",
114 114
                 POINTED_SHAPE: [
115
-                  "setPointedId",
116 115
                   {
117
-                    if: "isPressingShiftKey",
118
-                    unless: "isPointedShapeSelected",
119
-                    do: ["pushPointedIdToSelectedIds", "clearPointedId"],
120
-                    to: "pointingBounds",
116
+                    if: "isPressingMetaKey",
117
+                    to: "brushSelecting",
121 118
                   },
119
+                  "setPointedId",
122 120
                   {
123 121
                     unless: "isPointedShapeSelected",
124
-                    do: ["clearSelectedIds", "pushPointedIdToSelectedIds"],
122
+                    then: {
123
+                      if: "isPressingShiftKey",
124
+                      do: ["pushPointedIdToSelectedIds", "clearPointedId"],
125
+                      else: ["clearSelectedIds", "pushPointedIdToSelectedIds"],
126
+                    },
125 127
                   },
126 128
                   {
127 129
                     to: "pointingBounds",
@@ -139,7 +141,7 @@ const state = createState({
139 141
                       do: "pullPointedIdFromSelectedIds",
140 142
                     },
141 143
                     else: {
142
-                      if: "isPointingBounds",
144
+                      unless: "isPointingBounds",
143 145
                       do: ["clearSelectedIds", "pushPointedIdToSelectedIds"],
144 146
                     },
145 147
                   },
@@ -185,7 +187,10 @@ const state = createState({
185 187
             },
186 188
             brushSelecting: {
187 189
               onEnter: [
188
-                { unless: "isPressingShiftKey", do: "clearSelectedIds" },
190
+                {
191
+                  unless: ["isPressingMetaKey", "isPressingShiftKey"],
192
+                  do: "clearSelectedIds",
193
+                },
189 194
                 "clearBoundsRotation",
190 195
                 "startBrushSession",
191 196
               ],
@@ -403,9 +408,12 @@ const state = createState({
403 408
     isPointedShapeSelected(data) {
404 409
       return data.selectedIds.has(data.pointedId)
405 410
     },
406
-    isPressingShiftKey(data, payload: { shiftKey: boolean }) {
411
+    isPressingShiftKey(data, payload: PointerInfo) {
407 412
       return payload.shiftKey
408 413
     },
414
+    isPressingMetaKey(data, payload: PointerInfo) {
415
+      return payload.metaKey
416
+    },
409 417
     shapeIsHovered(data, payload: { target: string }) {
410 418
       return data.hoveredId === payload.target
411 419
     },
@@ -543,7 +551,6 @@ const state = createState({
543 551
       )
544 552
     },
545 553
     updateTranslateSession(data, payload: PointerInfo) {
546
-      console.log(payload.altKey)
547 554
       session.update(
548 555
         data,
549 556
         screenToWorld(payload.point, data),

正在加载...
取消
保存