1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
import type {
BoxSelectionChange,
SelectionState,
} from "@/selection/types";
function dedupeIds({ ids }: { ids: string[] }) {
return [...new Set(ids)];
}
function getRangeIds({
orderedIds,
anchorId,
targetId,
}: {
orderedIds: string[];
anchorId: string;
targetId: string;
}) {
const anchorIndex = orderedIds.indexOf(anchorId);
const targetIndex = orderedIds.indexOf(targetId);
if (anchorIndex === -1 || targetIndex === -1) {
return [targetId];
}
const rangeStart = Math.min(anchorIndex, targetIndex);
const rangeEnd = Math.max(anchorIndex, targetIndex);
return orderedIds.slice(rangeStart, rangeEnd + 1);
}
export function replaceSelection({
ids,
anchorId,
}: {
ids: string[];
anchorId?: string | null;
}): SelectionState {
const selectedIds = dedupeIds({ ids });
return {
selectedIds,
anchorId: anchorId ?? selectedIds[selectedIds.length - 1] ?? null,
};
}
export function clearSelection(): SelectionState {
return {
selectedIds: [],
anchorId: null,
};
}
export function pruneSelection({
state,
orderedIds,
}: {
state: SelectionState;
orderedIds: string[];
}): SelectionState {
const validIds = new Set(orderedIds);
const selectedIds = state.selectedIds.filter((id) => validIds.has(id));
const anchorId =
state.anchorId && validIds.has(state.anchorId)
? state.anchorId
: selectedIds[selectedIds.length - 1] ?? null;
const isUnchanged =
selectedIds.length === state.selectedIds.length &&
anchorId === state.anchorId;
if (isUnchanged) {
return state;
}
return {
selectedIds,
anchorId,
};
}
export function isSelected({
state,
id,
}: {
state: SelectionState;
id: string;
}) {
return state.selectedIds.includes(id);
}
export function toggleSelection({
state,
id,
}: {
state: SelectionState;
id: string;
}): SelectionState {
if (isSelected({ state, id })) {
const selectedIds = state.selectedIds.filter((selectedId) => selectedId !== id);
const anchorId =
state.anchorId === id
? selectedIds[selectedIds.length - 1] ?? null
: state.anchorId;
return replaceSelection({
ids: selectedIds,
anchorId,
});
}
return replaceSelection({
ids: [...state.selectedIds, id],
anchorId: id,
});
}
export function selectRange({
state,
orderedIds,
targetId,
isAdditive,
}: {
state: SelectionState;
orderedIds: string[];
targetId: string;
isAdditive: boolean;
}): SelectionState {
const anchorId =
state.anchorId ??
state.selectedIds[state.selectedIds.length - 1] ??
targetId;
const rangeIds = getRangeIds({
orderedIds,
anchorId,
targetId,
});
const selectedIds = isAdditive
? dedupeIds({
ids: [...state.selectedIds, ...rangeIds],
})
: rangeIds;
return replaceSelection({
ids: selectedIds,
anchorId,
});
}
export function applyBoxSelection({
intersectedIds,
initialSelectedIds,
initialAnchorId,
isAdditive,
}: BoxSelectionChange): SelectionState {
const selectedIds = isAdditive
? dedupeIds({
ids: [...initialSelectedIds, ...intersectedIds],
})
: intersectedIds;
const anchorId = isAdditive
? initialAnchorId ?? intersectedIds[intersectedIds.length - 1] ?? null
: intersectedIds[intersectedIds.length - 1] ?? null;
return replaceSelection({
ids: selectedIds,
anchorId,
});
}
|