Line data Source code
1 : import { isEqual } from 'es-toolkit'
2 : import { defineStore, getActivePinia } from 'pinia'
3 : import type { Pinia } from 'pinia'
4 : import { reactive, ref } from 'vue'
5 :
6 : import type {
7 : SelectableKey,
8 : SelectionCommand
9 : } from '@/core/selection/selectionState'
10 : import type {
11 : GraphScope,
12 : OwningGraphId,
13 : RootGraphId
14 : } from '@comfyorg/graph-model/graphScopeId'
15 :
16 : type SelectionRoots = ReadonlyMap<
17 : RootGraphId,
18 : ReadonlyMap<OwningGraphId, ReadonlySet<SelectableKey>>
19 : >
20 :
21 2356 : export const useSelectionStore = defineStore('selection', () => {
22 2355 : const roots = reactive(
23 : new Map<RootGraphId, Map<OwningGraphId, Set<SelectableKey>>>()
24 : )
25 2355 : const revision = ref(0)
26 :
27 1331 : function setSelection(
28 : scope: GraphScope,
29 : selection: Set<SelectableKey>
30 : ): void {
31 1331 : const owners = roots.get(scope.rootGraphId)
32 1331 : if (owners) owners.set(scope.owningGraphId, selection)
33 : else
34 692 : roots.set(scope.rootGraphId, new Map([[scope.owningGraphId, selection]]))
35 : }
36 :
37 0 : function addSelection(
38 : scope: GraphScope,
39 : current: Set<SelectableKey> | undefined,
40 : key: SelectableKey
41 : ): boolean {
42 0 : if (current) {
43 0 : const size = current.size
44 0 : current.add(key)
45 0 : return current.size !== size
46 : }
47 0 : setSelection(scope, new Set([key]))
48 0 : return true
49 : }
50 :
51 13689 : function clearSelection(current: Set<SelectableKey> | undefined): boolean {
52 13689 : if (!current?.size) return false
53 277 : current.clear()
54 277 : return true
55 : }
56 :
57 1519 : function replaceSelection(
58 : scope: GraphScope,
59 : current: Set<SelectableKey> | undefined,
60 : keys: readonly SelectableKey[]
61 : ): boolean {
62 1519 : const next = new Set(keys)
63 1519 : if (isEqual([...(current ?? [])], [...next])) return false
64 1331 : setSelection(scope, next)
65 1331 : return true
66 : }
67 :
68 15426 : function applyCommand(scope: GraphScope, command: SelectionCommand): boolean {
69 15426 : const current = roots.get(scope.rootGraphId)?.get(scope.owningGraphId)
70 15426 : switch (command.type) {
71 : case 'selection.add':
72 0 : return addSelection(scope, current, command.key)
73 : case 'selection.remove':
74 218 : return current?.delete(command.key) ?? false
75 : case 'selection.clear':
76 13689 : return clearSelection(current)
77 : case 'selection.replace':
78 1519 : return replaceSelection(scope, current, command.keys)
79 : }
80 : }
81 :
82 15426 : function apply(scope: GraphScope, command: SelectionCommand): void {
83 15426 : if (applyCommand(scope, command)) revision.value++
84 : }
85 :
86 7828 : function clearRoot(rootGraphId: RootGraphId): void {
87 7828 : if (roots.delete(rootGraphId)) revision.value++
88 : }
89 :
90 170069 : function getRevision(): number {
91 170069 : return revision.value
92 : }
93 :
94 33459 : function selectedKeys(scope: GraphScope): readonly SelectableKey[] {
95 33459 : return [...(roots.get(scope.rootGraphId)?.get(scope.owningGraphId) ?? [])]
96 : }
97 :
98 1094 : function isSelected(scope: GraphScope, key: SelectableKey): boolean {
99 1094 : return (
100 : roots.get(scope.rootGraphId)?.get(scope.owningGraphId)?.has(key) ?? false
101 : )
102 : }
103 :
104 : const readonlyRoots: SelectionRoots = roots
105 :
106 2355 : return {
107 : roots: readonlyRoots,
108 : apply,
109 : clearRoot,
110 : getRevision,
111 : selectedKeys,
112 : isSelected
113 : }
114 : })
115 :
116 : /**
117 : * Item `selected` accessors run once per item per frame, so they skip
118 : * pinia's store lookup and action wrapper: the store instance is memoized per
119 : * active pinia and membership is read straight from its state.
120 : */
121 : type SelectionStore = ReturnType<typeof useSelectionStore>
122 :
123 2356 : let memoized: { pinia: Pinia | undefined; store: SelectionStore } | undefined
124 :
125 211012 : function selectionStore(): SelectionStore {
126 211012 : const pinia = getActivePinia()
127 211012 : if (memoized && memoized.pinia === pinia) return memoized.store
128 2348 : memoized = { pinia, store: useSelectionStore() }
129 2348 : return memoized.store
130 : }
131 :
132 211012 : export function isSelectedIn(
133 : scope: GraphScope | undefined,
134 : key: SelectableKey
135 : ): boolean {
136 211012 : return (
137 : scope !== undefined &&
138 : (selectionStore()
139 : .roots.get(scope.rootGraphId)
140 : ?.get(scope.owningGraphId)
141 : ?.has(key) ??
142 : false)
143 : )
144 : }
145 :
146 0 : export function setSelectedIn(
147 : scope: GraphScope | undefined,
148 : key: SelectableKey,
149 : selected: boolean
150 : ): void {
151 0 : if (!scope) return
152 0 : selectionStore().apply(scope, {
153 : type: selected ? 'selection.add' : 'selection.remove',
154 : key
155 : })
156 : }
|