浏览代码

Fixes deleting undos

main
Steve Ruiz 4 年前
父节点
当前提交
9626461ea7
共有 4 个文件被更改,包括 66 次插入16 次删除
  1. 12
    0
      state/commands/delete-selected.ts
  2. 5
    1
      state/commands/translate.ts
  3. 48
    14
      state/sessions/translate-session.ts
  4. 1
    1
      state/state.ts

+ 12
- 0
state/commands/delete-selected.ts 查看文件

@@ -58,6 +58,18 @@ export default function deleteSelected(data: Data) {
58 58
           page.shapes[shape.id] = shape
59 59
         }
60 60
 
61
+        for (let shape of childrenToDelete) {
62
+          if (shape.parentId !== data.currentPageId) {
63
+            const parent = page.shapes[shape.parentId]
64
+            getShapeUtils(parent)
65
+              .setProperty(parent, 'children', [...parent.children, shape.id])
66
+              .onChildrenChange(
67
+                parent,
68
+                parent.children.map((id) => page.shapes[id])
69
+              )
70
+          }
71
+        }
72
+
61 73
         data.selectedIds = new Set(selectedIds)
62 74
       },
63 75
     })

+ 5
- 1
state/commands/translate.ts 查看文件

@@ -1,9 +1,10 @@
1 1
 import Command from './command'
2 2
 import history from '../history'
3 3
 import { TranslateSnapshot } from 'state/sessions/translate-session'
4
-import { Data } from 'types'
4
+import { Data, GroupShape, Shape, ShapeType } from 'types'
5 5
 import { getPage, updateParents } from 'utils/utils'
6 6
 import { getShapeUtils } from 'lib/shape-utils'
7
+import { v4 as uuid } from 'uuid'
7 8
 
8 9
 export default function translateCommand(
9 10
   data: Data,
@@ -33,6 +34,9 @@ export default function translateCommand(
33 34
                 ...parent.children,
34 35
                 clone.id,
35 36
               ])
37
+
38
+              if (clone.type === ShapeType.Group) {
39
+              }
36 40
             }
37 41
           }
38 42
         }

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

@@ -1,4 +1,4 @@
1
-import { Data, GroupShape, ShapeType } from 'types'
1
+import { Data, GroupShape, Shape, ShapeType } from 'types'
2 2
 import * as vec from 'utils/vec'
3 3
 import BaseSession from './base-session'
4 4
 import commands from 'state/commands'
@@ -54,13 +54,12 @@ export default class TranslateSession extends BaseSession {
54 54
         for (const clone of clones) {
55 55
           data.selectedIds.add(clone.id)
56 56
           shapes[clone.id] = { ...clone }
57
-          if (clone.parentId !== data.currentPageId) {
58
-            const parent = shapes[clone.parentId]
59
-            getShapeUtils(parent).setProperty(parent, 'children', [
60
-              ...parent.children,
61
-              clone.id,
62
-            ])
63
-          }
57
+          const parent = shapes[clone.parentId]
58
+          if (!parent) continue
59
+          getShapeUtils(parent).setProperty(parent, 'children', [
60
+            ...parent.children,
61
+            clone.id,
62
+          ])
64 63
         }
65 64
       }
66 65
 
@@ -164,6 +163,7 @@ export function getTranslateSnapshot(data: Data) {
164 163
   const selectedShapes = getSelectedShapes(cData).filter(
165 164
     (shape) => !shape.isLocked
166 165
   )
166
+
167 167
   const hasUnlockedShapes = selectedShapes.length > 0
168 168
 
169 169
   const parents = Array.from(
@@ -181,13 +181,47 @@ export function getTranslateSnapshot(data: Data) {
181 181
       point,
182 182
       parentId,
183 183
     })),
184
-    clones: selectedShapes.map((shape) => ({
185
-      ...shape,
186
-      id: uuid(),
187
-      parentId: shape.parentId,
188
-      childIndex: getChildIndexAbove(cData, shape.id),
189
-    })),
184
+    clones: selectedShapes
185
+      .filter((shape) => shape.type !== ShapeType.Group)
186
+      .flatMap((shape) => {
187
+        const clone = {
188
+          ...shape,
189
+          id: uuid(),
190
+          parentId: shape.parentId,
191
+          childIndex: getChildIndexAbove(cData, shape.id),
192
+        }
193
+
194
+        return clone
195
+
196
+        // cloneGroup(cData, {
197
+        //   ...shape,
198
+        //   id: uuid(),
199
+        //   parentId: shape.parentId,
200
+        //   childIndex: getChildIndexAbove(cData, shape.id),
201
+        // })
202
+      }),
190 203
   }
191 204
 }
192 205
 
193 206
 export type TranslateSnapshot = ReturnType<typeof getTranslateSnapshot>
207
+
208
+function cloneGroup(data: Data, clone: Shape): Shape[] {
209
+  if (clone.type !== ShapeType.Group) {
210
+    return [clone]
211
+  }
212
+
213
+  const page = getPage(data)
214
+  const childClones = clone.children.flatMap((id) => {
215
+    const newId = uuid()
216
+    const source = page.shapes[id]
217
+    const next = { ...source, id: newId, parentId: clone.id }
218
+
219
+    if (next.type === ShapeType.Group) {
220
+      return [next, ...cloneGroup(data, next)]
221
+    }
222
+
223
+    return [next]
224
+  })
225
+
226
+  return [clone, ...childClones]
227
+}

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

@@ -1385,7 +1385,7 @@ const state = createState({
1385 1385
     },
1386 1386
 
1387 1387
     restoreSavedData(data) {
1388
-      history.load(data)
1388
+      // history.load(data)
1389 1389
     },
1390 1390
 
1391 1391
     clearBoundsRotation(data) {

正在加载...
取消
保存