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

Fixes selection for shapes in a group

main
Steve Ruiz преди 4 години
родител
ревизия
fcb11f19e3
променени са 6 файла, в които са добавени 51 реда и са изтрити 43 реда
  1. 13
    7
      state/commands/delete-selected.ts
  2. 10
    5
      state/commands/style.ts
  3. 1
    1
      state/history.ts
  4. 12
    29
      state/sessions/brush-session.ts
  5. 3
    1
      state/state.ts
  6. 12
    0
      utils/utils.ts

+ 13
- 7
state/commands/delete-selected.ts Целия файл

@@ -1,8 +1,8 @@
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'
5
-import { getPage, updateParents } from 'utils/utils'
4
+import { Data, ShapeType } from 'types'
5
+import { getDocumentBranch, getPage, updateParents } from 'utils/utils'
6 6
 import { current } from 'immer'
7 7
 import { getShapeUtils } from 'lib/shape-utils'
8 8
 
@@ -13,7 +13,9 @@ export default function deleteSelected(data: Data) {
13 13
 
14 14
   const page = getPage(current(data))
15 15
 
16
-  const shapes = selectedIds.map((id) => page.shapes[id])
16
+  const childrenToDelete = selectedIds
17
+    .flatMap((id) => getDocumentBranch(data, id))
18
+    .map((id) => page.shapes[id])
17 19
 
18 20
   data.selectedIds.clear()
19 21
 
@@ -41,18 +43,22 @@ export default function deleteSelected(data: Data) {
41 43
                 parent.children.map((id) => page.shapes[id])
42 44
               )
43 45
           }
44
-          delete page.shapes[id]
46
+
47
+          for (let shape of childrenToDelete) {
48
+            delete page.shapes[shape.id]
49
+          }
45 50
         }
46 51
 
47 52
         data.selectedIds.clear()
48 53
       },
49 54
       undo(data) {
50 55
         const page = getPage(data, currentPageId)
51
-        data.selectedIds.clear()
52
-        for (let shape of shapes) {
56
+
57
+        for (let shape of childrenToDelete) {
53 58
           page.shapes[shape.id] = shape
54
-          data.selectedIds.add(shape.id)
55 59
         }
60
+
61
+        data.selectedIds = new Set(selectedIds)
56 62
       },
57 63
     })
58 64
   )

+ 10
- 5
state/commands/style.ts Целия файл

@@ -1,13 +1,18 @@
1 1
 import Command from './command'
2 2
 import history from '../history'
3 3
 import { Data, ShapeStyles } from 'types'
4
-import { getPage, getSelectedShapes } from 'utils/utils'
4
+import { getDocumentBranch, getPage, getSelectedShapes } from 'utils/utils'
5 5
 import { getShapeUtils } from 'lib/shape-utils'
6 6
 import { current } from 'immer'
7 7
 
8 8
 export default function styleCommand(data: Data, styles: Partial<ShapeStyles>) {
9
-  const { currentPageId } = data
10
-  const initialShapes = getSelectedShapes(current(data))
9
+  const cData = current(data)
10
+  const page = getPage(cData)
11
+  const { currentPageId } = cData
12
+
13
+  const shapesToStyle = Array.from(data.selectedIds.values())
14
+    .flatMap((id) => getDocumentBranch(data, id))
15
+    .map((id) => page.shapes[id])
11 16
 
12 17
   history.execute(
13 18
     data,
@@ -18,7 +23,7 @@ export default function styleCommand(data: Data, styles: Partial<ShapeStyles>) {
18 23
       do(data) {
19 24
         const { shapes } = getPage(data, currentPageId)
20 25
 
21
-        for (const { id } of initialShapes) {
26
+        for (const { id } of shapesToStyle) {
22 27
           const shape = shapes[id]
23 28
           getShapeUtils(shape).applyStyles(shape, styles)
24 29
         }
@@ -26,7 +31,7 @@ export default function styleCommand(data: Data, styles: Partial<ShapeStyles>) {
26 31
       undo(data) {
27 32
         const { shapes } = getPage(data, currentPageId)
28 33
 
29
-        for (const { id, style } of initialShapes) {
34
+        for (const { id, style } of shapesToStyle) {
30 35
           const shape = shapes[id]
31 36
           getShapeUtils(shape).applyStyles(shape, style)
32 37
         }

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

@@ -1,7 +1,7 @@
1 1
 import { Data } from 'types'
2 2
 import { BaseCommand } from './commands/command'
3 3
 
4
-const CURRENT_VERSION = 'code_slate_0.0.2'
4
+const CURRENT_VERSION = 'code_slate_0.0.3'
5 5
 
6 6
 // A singleton to manage history changes.
7 7
 

+ 12
- 29
state/sessions/brush-session.ts Целия файл

@@ -22,25 +22,21 @@ export default class BrushSession extends BaseSession {
22 22
 
23 23
     const brushBounds = getBoundsFromPoints([origin, point])
24 24
 
25
+    const hits = new Set<string>([])
26
+
25 27
     for (let id in snapshot.shapeHitTests) {
26 28
       const { test, selectId } = snapshot.shapeHitTests[id]
27
-      if (test(brushBounds)) {
28
-        // When brushing a shape, select its top group parent.
29
-        if (!data.selectedIds.has(selectId)) {
30
-          data.selectedIds.add(selectId)
29
+      if (!hits.has(selectId)) {
30
+        if (test(brushBounds)) {
31
+          hits.add(selectId)
32
+
33
+          // When brushing a shape, select its top group parent.
34
+          if (!data.selectedIds.has(selectId)) {
35
+            data.selectedIds.add(selectId)
36
+          }
37
+        } else if (data.selectedIds.has(selectId)) {
38
+          data.selectedIds.delete(selectId)
31 39
         }
32
-
33
-        // Possibly... select all of the top group parent's children too?
34
-        // const selectedId = getTopParentId(data, id)
35
-        // const idsToSelect = collectChildIds(data, selectedId)
36
-
37
-        // for (let id in idsToSelect) {
38
-        //   if (!data.selectedIds.has(id)) {
39
-        //     data.selectedIds.add(id)
40
-        //   }
41
-        // }
42
-      } else if (data.selectedIds.has(selectId)) {
43
-        data.selectedIds.delete(selectId)
44 40
       }
45 41
     }
46 42
 
@@ -89,16 +85,3 @@ function getTopParentId(data: Data, id: string): string {
89 85
     ? id
90 86
     : getTopParentId(data, shape.parentId)
91 87
 }
92
-
93
-function collectChildIds(data: Data, id: string): string[] {
94
-  const shape = getPage(data).shapes[id]
95
-
96
-  if (shape.type === ShapeType.Group) {
97
-    return [
98
-      id,
99
-      ...shape.children.flatMap((childId) => collectChildIds(data, childId)),
100
-    ]
101
-  }
102
-
103
-  return [id]
104
-}

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

@@ -1027,7 +1027,9 @@ const state = createState({
1027 1027
       const page = getPage(data)
1028 1028
       selectedIds.clear()
1029 1029
       for (let id in page.shapes) {
1030
-        selectedIds.add(id)
1030
+        if (page.shapes[id].parentId === data.currentPageId) {
1031
+          selectedIds.add(id)
1032
+        }
1031 1033
       }
1032 1034
     },
1033 1035
     setHoveredId(data, payload: PointerInfo) {

+ 12
- 0
utils/utils.ts Целия файл

@@ -1610,6 +1610,8 @@ export function getCurrentCamera(data: Data) {
1610 1610
 //   })
1611 1611
 // }
1612 1612
 
1613
+/* --------------------- Groups --------------------- */
1614
+
1613 1615
 export function updateParents(data: Data, changedShapeIds: string[]) {
1614 1616
   if (changedShapeIds.length === 0) return
1615 1617
 
@@ -1647,3 +1649,13 @@ export function getParentRotation(
1647 1649
     ? rotation + shape.rotation
1648 1650
     : getParentRotation(data, shape.parentId, rotation + shape.rotation)
1649 1651
 }
1652
+
1653
+export function getDocumentBranch(data: Data, id: string): string[] {
1654
+  const shape = getPage(data).shapes[id]
1655
+  if (shape.type !== ShapeType.Group) return [id]
1656
+
1657
+  return [
1658
+    id,
1659
+    ...shape.children.flatMap((childId) => getDocumentBranch(data, childId)),
1660
+  ]
1661
+}

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