-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpansionManager.ts
More file actions
100 lines (88 loc) · 2.81 KB
/
Copy pathExpansionManager.ts
File metadata and controls
100 lines (88 loc) · 2.81 KB
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
import { TagTreeNode } from "./utils";
export class ExpansionManager {
private expansionState = new Map<string, boolean>();
private preSearchState = new Map<string, boolean>();
/**
* Restore expansion state for a tree of tag nodes
*/
restoreExpansionState(treeRoots: Map<string, TagTreeNode>, searchQuery: string): void {
const isSearching = Boolean(searchQuery);
const restoreNode = (node: TagTreeNode) => {
const savedState = this.expansionState.get(node.full);
if (isSearching) {
// When searching, expand all nodes but remember the pre-search state
if (!this.preSearchState.has(node.full)) {
// Store the current state before overriding for search
this.preSearchState.set(node.full, savedState !== undefined ? savedState : false);
}
node.expanded = true;
} else {
// When not searching, restore the pre-search state if it exists
if (this.preSearchState.has(node.full)) {
// Restore from pre-search state and clean it up
const preSearchExpanded = this.preSearchState.get(node.full)!;
node.expanded = preSearchExpanded;
this.expansionState.set(node.full, preSearchExpanded);
this.preSearchState.delete(node.full);
} else if (savedState !== undefined) {
// Use saved state
node.expanded = savedState;
} else {
// Default to collapsed for new nodes when not searching
node.expanded = false;
this.expansionState.set(node.full, false);
}
}
// Recursively restore children
for (const child of node.children.values()) {
restoreNode(child);
}
};
for (const rootNode of treeRoots.values()) {
restoreNode(rootNode);
}
}
/**
* Save expansion state for a specific node
*/
saveExpansionState(node: TagTreeNode): void {
this.expansionState.set(node.full, node.expanded || false);
}
/**
* Clear all expansion states
*/
clear(): void {
this.expansionState.clear();
this.preSearchState.clear();
}
/**
* Expand all tags
*/
expandAll(): void {
for (const [tagPath] of this.expansionState) {
this.expansionState.set(tagPath, true);
}
}
/**
* Collapse all tags
*/
collapseAll(): void {
for (const [tagPath] of this.expansionState) {
this.expansionState.set(tagPath, false);
}
}
/**
* Get current expansion state
*/
getState(): Map<string, boolean> {
return new Map(this.expansionState);
}
/**
* Check if most nodes are expanded (for toggle logic)
*/
isMostlyExpanded(): boolean {
const expandedCount = Array.from(this.expansionState.values()).filter(Boolean).length;
const totalCount = this.expansionState.size;
return expandedCount > totalCount / 2;
}
}