Procházet zdrojové kódy

Fixes selection for shapes in a group

main
Steve Ruiz před 4 roky
rodič
revize
fcb11f19e3

+ 13
- 7
state/commands/delete-selected.ts Zobrazit soubor

1
 import Command from './command'
1
 import Command from './command'
2
 import history from '../history'
2
 import history from '../history'
3
 import { TranslateSnapshot } from 'state/sessions/translate-session'
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
 import { current } from 'immer'
6
 import { current } from 'immer'
7
 import { getShapeUtils } from 'lib/shape-utils'
7
 import { getShapeUtils } from 'lib/shape-utils'
8
 
8
 
13
 
13
 
14
   const page = getPage(current(data))
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
   data.selectedIds.clear()
20
   data.selectedIds.clear()
19
 
21
 
41
                 parent.children.map((id) => page.shapes[id])
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
         data.selectedIds.clear()
52
         data.selectedIds.clear()
48
       },
53
       },
49
       undo(data) {
54
       undo(data) {
50
         const page = getPage(data, currentPageId)
55
         const page = getPage(data, currentPageId)
51
-        data.selectedIds.clear()
52
-        for (let shape of shapes) {
56
+
57
+        for (let shape of childrenToDelete) {
53
           page.shapes[shape.id] = shape
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 Zobrazit soubor

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

+ 1
- 1
state/history.ts Zobrazit soubor

1
 import { Data } from 'types'
1
 import { Data } from 'types'
2
 import { BaseCommand } from './commands/command'
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
 // A singleton to manage history changes.
6
 // A singleton to manage history changes.
7
 
7
 

+ 12
- 29
state/sessions/brush-session.ts Zobrazit soubor

22
 
22
 
23
     const brushBounds = getBoundsFromPoints([origin, point])
23
     const brushBounds = getBoundsFromPoints([origin, point])
24
 
24
 
25
+    const hits = new Set<string>([])
26
+
25
     for (let id in snapshot.shapeHitTests) {
27
     for (let id in snapshot.shapeHitTests) {
26
       const { test, selectId } = snapshot.shapeHitTests[id]
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
     ? id
85
     ? id
90
     : getTopParentId(data, shape.parentId)
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 Zobrazit soubor

1027
       const page = getPage(data)
1027
       const page = getPage(data)
1028
       selectedIds.clear()
1028
       selectedIds.clear()
1029
       for (let id in page.shapes) {
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
     setHoveredId(data, payload: PointerInfo) {
1035
     setHoveredId(data, payload: PointerInfo) {

+ 12
- 0
utils/utils.ts Zobrazit soubor

1610
 //   })
1610
 //   })
1611
 // }
1611
 // }
1612
 
1612
 
1613
+/* --------------------- Groups --------------------- */
1614
+
1613
 export function updateParents(data: Data, changedShapeIds: string[]) {
1615
 export function updateParents(data: Data, changedShapeIds: string[]) {
1614
   if (changedShapeIds.length === 0) return
1616
   if (changedShapeIds.length === 0) return
1615
 
1617
 
1647
     ? rotation + shape.rotation
1649
     ? rotation + shape.rotation
1648
     : getParentRotation(data, shape.parentId, rotation + shape.rotation)
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
+}

Načítá se…
Zrušit
Uložit