-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathv2codebehind.ts
194 lines (182 loc) · 7.44 KB
/
v2codebehind.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type { TREE_TYPE, TagFolderSettings, TagInfoDict, ViewItem } from "./types";
import { V2FI_IDX_CHILDREN, type V2FolderItem, trimPrefix, parseTagName, pathMatch, getExtraTags, getViewItemFromPath, V2FI_IDX_TAG, V2FI_IDX_TAGNAME, V2FI_IDX_TAGDISP, waitForRequestAnimationFrame } from "./util";
export function performSortExactFirst(_items: ViewItem[], children: V2FolderItem[], leftOverItems: ViewItem[]) {
// const m2 = measure("new");
const childrenPathsArr = children.map((e) =>
(e[V2FI_IDX_CHILDREN]).map((ee) => ee.path)).flat()
const childrenPaths = new Set(
childrenPathsArr
);
const exactHerePaths = new Set(_items.map((e) => e.path));
childrenPaths.forEach((path) => exactHerePaths.delete(path));
//const isHerePaths =
const wk2 = [...leftOverItems].sort((a, b) => {
const aIsInChildren = exactHerePaths.has(a.path);
const bIsInChildren = exactHerePaths.has(b.path);
return (aIsInChildren ? -1 : 0) + (bIsInChildren ? 1 : 0);
});
// m2();
return [...wk2];
}
function delay() {
return new Promise<void>(res => setTimeout(() => res(), 5));
}
function nextTick() {
return new Promise<void>(res => setTimeout(() => res(), 0));
}
const delays = [nextTick, delay, nextTick, waitForRequestAnimationFrame];
let delayIdx = 0;
export async function collectChildren(previousTrail: string, tags: string[], _tagInfo: TagInfoDict, _items: ViewItem[]) {
const previousTrailLC = previousTrail.toLowerCase();
const children: V2FolderItem[] = [];
const tagPerItem = new Map<string, ViewItem[]>();
const lowercaseMap = new Map<string, string>();
for (const item of _items) {
const itemTags = item.tags;
itemTags.forEach(itemTag => {
const tagLc = lowercaseMap.get(itemTag) ?? lowercaseMap.set(itemTag, itemTag.toLowerCase()).get(itemTag)!;
if (!tagPerItem.has(tagLc)) tagPerItem.set(tagLc, []);
tagPerItem.get(tagLc)!.push(item);
})
}
for (const tag of tags) {
const tagLC = tag.toLowerCase();
const tagNestedLC = trimPrefix(tagLC, previousTrailLC);
const items: ViewItem[] = [];
for (const [itemTag, tempItems] of tagPerItem) {
if (pathMatch(itemTag, tagLC)) {
items.push(...tempItems);
} else if (pathMatch(itemTag, tagNestedLC)) {
items.push(...tempItems);
}
}
children.push(
[
tag,
...parseTagName(tag, _tagInfo),
[...new Set(items)]
]
)
// Prevent UI freezing.
delayIdx++; delayIdx %= 4;
await (delays[delayIdx])();
}
return children;
}
export async function collectTreeChildren(
{ key,
expandLimit, depth, tags, trailLower, _setting, isMainTree,
isSuppressibleLevel, viewType, previousTrail, _tagInfo, _items, linkedItems, isRoot,
sortFunc }
:
{
key: string,
expandLimit: number, depth: number, tags: string[], trailLower: string[], _setting: TagFolderSettings,
isMainTree: boolean, isSuppressibleLevel: boolean, viewType: TREE_TYPE, previousTrail: string,
_tagInfo: TagInfoDict, _items: ViewItem[], linkedItems: Map<string, ViewItem[]>, isRoot: boolean,
sortFunc: (a: V2FolderItem, b: V2FolderItem) => number
}
): Promise<{ suppressLevels: string[], children: V2FolderItem[] }> {
let suppressLevels: string[] = []; // This will be shown as chip.
let children: V2FolderItem[] = [];
if (expandLimit && depth >= expandLimit) {
// If expand limit had been configured and we have reached it,
// suppress sub-folders and show that information as extraTags.
children = [];
suppressLevels = getExtraTags(
tags,
trailLower,
_setting.reduceNestedParent
);
} else if (!isMainTree) {
// If not in main tree, suppress sub-folders.
children = [];
} else if (isSuppressibleLevel) {
// If we determined it was a suppressible,
// suppress sub-folders and show that information as extraTags.
children = [];
suppressLevels = getExtraTags(
tags,
trailLower,
_setting.reduceNestedParent
);
} else {
let wChildren = [] as V2FolderItem[];
if (viewType == "tags") {
wChildren = await collectChildren(
previousTrail,
tags,
_tagInfo,
_items
);
} else if (viewType == "links") {
// We made the list in the previous step.
wChildren = tags.map((tag) => {
const selfInfo = getViewItemFromPath(tag);
const dispName = !selfInfo ? tag : selfInfo.displayName;
const children = linkedItems.get(tag) ?? [];
return [
tag,
dispName,
[dispName],
children,
] as V2FolderItem;
});
}
if (viewType == "tags") {
// -- Check redundant combination if configured.
if (_setting.mergeRedundantCombination) {
const out = [] as typeof wChildren;
const isShown = new Set<string>();
for (const [tag, tagName, tagsDisp, items] of wChildren) {
const list = [] as ViewItem[];
for (const v of items) {
if (!isShown.has(v.path)) {
list.push(v);
isShown.add(v.path);
}
}
if (list.length != 0)
out.push([tag, tagName, tagsDisp, list]);
}
wChildren = out;
}
// -- MainTree and Root specific structure modification.
if (isMainTree && isRoot) {
// Remove all items which have been already archived except is on the root.
const archiveTags = _setting.archiveTags
.toLowerCase()
.replace(/[\n ]/g, "")
.split(",");
wChildren = wChildren
.map((e) =>
archiveTags.some((aTag) =>
`${aTag}//`.startsWith(
e[V2FI_IDX_TAG].toLowerCase() + "/"
)
)
? e
: ([
e[V2FI_IDX_TAG],
e[V2FI_IDX_TAGNAME],
e[V2FI_IDX_TAGDISP],
e[V2FI_IDX_CHILDREN].filter(
(items) =>
!items.tags.some((e) =>
archiveTags.contains(
e.toLowerCase()
)
)
),
] as V2FolderItem)
)
.filter(
(child) => child[V2FI_IDX_CHILDREN].length != 0
);
}
}
wChildren = wChildren.sort(sortFunc);
children = wChildren;
}
return { suppressLevels, children }
}