@electerm/electerm-react 3.7.16 → 3.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ import createName from '../../common/create-title'
2
+
3
+ function isTopLevelGroup (group) {
4
+ return !group?.level || group.level < 2
5
+ }
6
+
7
+ export function buildVisibleTreeRows ({
8
+ bookmarkGroups,
9
+ bookmarkGroupTree,
10
+ bookmarksMap,
11
+ expandedKeys,
12
+ keyword
13
+ }) {
14
+ const groupTree = bookmarkGroupTree || {}
15
+ const rows = []
16
+ const matchedRowKeys = []
17
+ const expandedKeySet = new Set(expandedKeys || [])
18
+ const lowerKeyword = (keyword || '').toLowerCase()
19
+ const bookmarkMatchCache = new Map()
20
+ const groupMatchCache = new Map()
21
+
22
+ const bookmarkMatches = (bookmarkId) => {
23
+ if (bookmarkMatchCache.has(bookmarkId)) {
24
+ return bookmarkMatchCache.get(bookmarkId)
25
+ }
26
+ const item = bookmarksMap.get(bookmarkId)
27
+ const matched = Boolean(
28
+ item &&
29
+ (!lowerKeyword || createName(item).toLowerCase().includes(lowerKeyword))
30
+ )
31
+ bookmarkMatchCache.set(bookmarkId, matched)
32
+ return matched
33
+ }
34
+
35
+ const groupHasMatchedBookmarks = (group) => {
36
+ if (!lowerKeyword) {
37
+ return true
38
+ }
39
+ if (!group) {
40
+ return false
41
+ }
42
+ if (groupMatchCache.has(group.id)) {
43
+ return groupMatchCache.get(group.id)
44
+ }
45
+ const hasMatch = (group.bookmarkIds || []).some(bookmarkMatches) ||
46
+ (group.bookmarkGroupIds || []).some(id => {
47
+ return groupHasMatchedBookmarks(groupTree[id])
48
+ })
49
+ groupMatchCache.set(group.id, hasMatch)
50
+ return hasMatch
51
+ }
52
+
53
+ const visitGroup = (group, parentId = '', depth = 1) => {
54
+ if (!group || (lowerKeyword && !groupHasMatchedBookmarks(group))) {
55
+ return
56
+ }
57
+
58
+ rows.push({
59
+ key: `group:${group.id}`,
60
+ item: group,
61
+ isGroup: true,
62
+ parentId,
63
+ depth
64
+ })
65
+
66
+ if (!lowerKeyword && !expandedKeySet.has(group.id)) {
67
+ return
68
+ }
69
+
70
+ const nextParentId = parentId
71
+ ? `${parentId}#${group.id}`
72
+ : `#${group.id}`
73
+
74
+ for (const groupId of group.bookmarkGroupIds || []) {
75
+ visitGroup(groupTree[groupId], nextParentId, depth + 1)
76
+ }
77
+
78
+ for (const bookmarkId of group.bookmarkIds || []) {
79
+ const item = bookmarksMap.get(bookmarkId)
80
+ if (!item || (lowerKeyword && !bookmarkMatches(bookmarkId))) {
81
+ continue
82
+ }
83
+ const rowKey = `bookmark:${item.id}`
84
+ rows.push({
85
+ key: rowKey,
86
+ item,
87
+ isGroup: false,
88
+ parentId: nextParentId,
89
+ depth
90
+ })
91
+ if (lowerKeyword) {
92
+ matchedRowKeys.push(rowKey)
93
+ }
94
+ }
95
+ }
96
+
97
+ for (const group of bookmarkGroups || []) {
98
+ if (isTopLevelGroup(group)) {
99
+ visitGroup(group)
100
+ }
101
+ }
102
+
103
+ return {
104
+ rows,
105
+ matchedRowKeys
106
+ }
107
+ }