@handaotech-design/bom 0.0.26 → 0.0.28

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.
package/README.md CHANGED
@@ -1,4 +1,14 @@
1
1
  # Bom 组件库
2
+
3
+ ## 安装
4
+ ### 依赖安装
5
+ ```shell
6
+ yarn add @handaotech-design/bom @handaotech-design/vue
7
+ ```
8
+ ### 样式引入
9
+ ```typescript
10
+ import '@handaotech-design/bom/dist/style.css';
11
+ ```
2
12
  ## 组件列表
3
13
  ### HdBomTree
4
14
  bom树组件
@@ -35,7 +35,7 @@ const treeContainerHeight = ref<number>(0)
35
35
  const treeContainerId = 'treeContainer'
36
36
  let keyToNodeMap = new Map<string, TreeNodeWithMeta>()
37
37
  const selectedKeys = ref<string[]>([])
38
- const keepTreeState = ref<boolean>(false)
38
+ const isPreservingTreeState = ref<boolean>(false)
39
39
  const initTreeState = () => {
40
40
  searchValue.value = ''
41
41
  selectedKeys.value = []
@@ -91,20 +91,23 @@ const filterTreeData = (data: TreeProps['treeData'], filteredKeys: string[]) =>
91
91
  }
92
92
 
93
93
  // 获取直接父节点的 key
94
- const getParentKey = (key: string): string[] | undefined => {
94
+ const getParentKeys = (key: string): string[] | undefined => {
95
95
  const treeNode = keyToNodeMap.get(key)
96
96
  return treeNode?.parentKeys || []
97
97
  }
98
98
 
99
- const selectFirstSelectableNode = () => {
99
+ const selectFirstSelectableNode = (preserveExpanded = false) => {
100
100
  const firstNode: any = _.find(Array.from(keyToNodeMap.values()), node => (node as any)?.selectable)
101
101
  if (!firstNode) {
102
102
  return
103
103
  }
104
104
 
105
105
  nextTick(() => {
106
- expandedKeys.value = getParentKey(firstNode.key as string) as any
107
- selectedKeys.value = firstNode ? [firstNode.key] as string[] : []
106
+ const parentKeys = getParentKeys(firstNode.key as string) as string[]
107
+ expandedKeys.value = preserveExpanded
108
+ ? Array.from(new Set([...expandedKeys.value, ...parentKeys])) // 取并集
109
+ : parentKeys
110
+ selectedKeys.value = [firstNode.key]
108
111
  })
109
112
  }
110
113
 
@@ -136,8 +139,8 @@ watch(
136
139
  filteredTreeData.value = _treeData.value
137
140
  keyToNodeMap = buildKeyToNodeMap(_treeData.value ?? [])
138
141
  initTreeState()
139
- if (!keepTreeState.value) {
140
- selectFirstSelectableNode()
142
+ if (!isPreservingTreeState.value) {
143
+ selectFirstSelectableNode(true)
141
144
  }
142
145
  },
143
146
  {
@@ -149,7 +152,7 @@ watchDebounced(
149
152
  searchValue, (value: string) => {
150
153
  let expanded: any[]
151
154
  if (_.isEmpty(value)) {
152
- expanded = getParentKey(selectedKeys.value[0]) || []
155
+ expanded = getParentKeys(selectedKeys.value[0]) || []
153
156
  filteredTreeData.value = _treeData.value
154
157
  }
155
158
  else {
@@ -158,7 +161,7 @@ watchDebounced(
158
161
  .map((item: any) => {
159
162
  if (hasSearchMatch(item.title)) {
160
163
  filteredNodes.push(item)
161
- return getParentKey(item.key)
164
+ return getParentKeys(item.key)
162
165
  }
163
166
  return null
164
167
  })
@@ -199,10 +202,10 @@ function hasSearchMatch(title: string) {
199
202
  return searchValue.value && title.includes(searchValue.value)
200
203
  }
201
204
 
202
- async function withKeepTreeState(updateDataFn: () => Promise<void>) {
205
+ async function updateTreeWithPreservedState(updateDataFn: () => Promise<void>) {
203
206
  const prevSelected = selectedKeys.value?.[0]
204
207
  const prevExpanded = [...expandedKeys.value]
205
- keepTreeState.value = true
208
+ isPreservingTreeState.value = true
206
209
  await updateDataFn()
207
210
  await nextTick(() => {
208
211
  expandedKeys.value = prevExpanded.filter(key => keyToNodeMap.has(key as string))
@@ -211,14 +214,21 @@ async function withKeepTreeState(updateDataFn: () => Promise<void>) {
211
214
  selectedKeys.value = [prevSelected]
212
215
  }
213
216
  else {
214
- selectFirstSelectableNode()
217
+ selectFirstSelectableNode(true)
215
218
  }
216
- keepTreeState.value = false
219
+ isPreservingTreeState.value = false
217
220
  })
218
221
  }
219
222
 
223
+ async function getDepth(key: string) {
224
+ const parentKeys = getParentKeys(key)
225
+ return parentKeys!.length + 1
226
+ }
227
+
220
228
  defineExpose({
221
- withKeepTreeState,
229
+ updateTreeWithPreservedState,
230
+ getParentKeys,
231
+ getDepth,
222
232
  })
223
233
  </script>
224
234
 
@@ -247,7 +257,7 @@ defineExpose({
247
257
  @expand="onExpand"
248
258
  @select="onSelected"
249
259
  >
250
- <template #title="{ title, dataRef, data }">
260
+ <template #title="{ title, dataRef }">
251
261
  <div :class="`tree-node-tittle flex items-center ${dataRef.selectable ? 'selectable' : 'not-selectable'}`">
252
262
  <div
253
263
  v-if="!!dataRef.icon"
@@ -40,12 +40,22 @@ watch(
40
40
  { immediate: true },
41
41
  )
42
42
 
43
- async function withKeepTreeState(fn: any) {
44
- treeRef.value.withKeepTreeState(fn)
43
+ async function updateTreeWithPreservedState(fn: any) {
44
+ treeRef.value.updateTreeWithPreservedState(fn)
45
+ }
46
+
47
+ function getParentKeys(key: string) {
48
+ return treeRef.value.getParentKeys(key)
49
+ }
50
+
51
+ function getDepth(key: string) {
52
+ return treeRef.value.getDepth(key)
45
53
  }
46
54
 
47
55
  defineExpose({
48
- withKeepTreeState,
56
+ updateTreeWithPreservedState,
57
+ getParentKeys,
58
+ getDepth,
49
59
  })
50
60
  </script>
51
61
 
@@ -35,7 +35,7 @@ const treeContainerHeight = ref<number>(0)
35
35
  const treeContainerId = 'treeContainer'
36
36
  let keyToNodeMap = new Map<string, TreeNodeWithMeta>()
37
37
  const selectedKeys = ref<string[]>([])
38
- const keepTreeState = ref<boolean>(false)
38
+ const isPreservingTreeState = ref<boolean>(false)
39
39
  const initTreeState = () => {
40
40
  searchValue.value = ''
41
41
  selectedKeys.value = []
@@ -91,20 +91,23 @@ const filterTreeData = (data: TreeProps['treeData'], filteredKeys: string[]) =>
91
91
  }
92
92
 
93
93
  // 获取直接父节点的 key
94
- const getParentKey = (key: string): string[] | undefined => {
94
+ const getParentKeys = (key: string): string[] | undefined => {
95
95
  const treeNode = keyToNodeMap.get(key)
96
96
  return treeNode?.parentKeys || []
97
97
  }
98
98
 
99
- const selectFirstSelectableNode = () => {
99
+ const selectFirstSelectableNode = (preserveExpanded = false) => {
100
100
  const firstNode: any = _.find(Array.from(keyToNodeMap.values()), node => (node as any)?.selectable)
101
101
  if (!firstNode) {
102
102
  return
103
103
  }
104
104
 
105
105
  nextTick(() => {
106
- expandedKeys.value = getParentKey(firstNode.key as string) as any
107
- selectedKeys.value = firstNode ? [firstNode.key] as string[] : []
106
+ const parentKeys = getParentKeys(firstNode.key as string) as string[]
107
+ expandedKeys.value = preserveExpanded
108
+ ? Array.from(new Set([...expandedKeys.value, ...parentKeys])) // 取并集
109
+ : parentKeys
110
+ selectedKeys.value = [firstNode.key]
108
111
  })
109
112
  }
110
113
 
@@ -136,8 +139,8 @@ watch(
136
139
  filteredTreeData.value = _treeData.value
137
140
  keyToNodeMap = buildKeyToNodeMap(_treeData.value ?? [])
138
141
  initTreeState()
139
- if (!keepTreeState.value) {
140
- selectFirstSelectableNode()
142
+ if (!isPreservingTreeState.value) {
143
+ selectFirstSelectableNode(true)
141
144
  }
142
145
  },
143
146
  {
@@ -149,7 +152,7 @@ watchDebounced(
149
152
  searchValue, (value: string) => {
150
153
  let expanded: any[]
151
154
  if (_.isEmpty(value)) {
152
- expanded = getParentKey(selectedKeys.value[0]) || []
155
+ expanded = getParentKeys(selectedKeys.value[0]) || []
153
156
  filteredTreeData.value = _treeData.value
154
157
  }
155
158
  else {
@@ -158,7 +161,7 @@ watchDebounced(
158
161
  .map((item: any) => {
159
162
  if (hasSearchMatch(item.title)) {
160
163
  filteredNodes.push(item)
161
- return getParentKey(item.key)
164
+ return getParentKeys(item.key)
162
165
  }
163
166
  return null
164
167
  })
@@ -199,10 +202,10 @@ function hasSearchMatch(title: string) {
199
202
  return searchValue.value && title.includes(searchValue.value)
200
203
  }
201
204
 
202
- async function withKeepTreeState(updateDataFn: () => Promise<void>) {
205
+ async function updateTreeWithPreservedState(updateDataFn: () => Promise<void>) {
203
206
  const prevSelected = selectedKeys.value?.[0]
204
207
  const prevExpanded = [...expandedKeys.value]
205
- keepTreeState.value = true
208
+ isPreservingTreeState.value = true
206
209
  await updateDataFn()
207
210
  await nextTick(() => {
208
211
  expandedKeys.value = prevExpanded.filter(key => keyToNodeMap.has(key as string))
@@ -211,14 +214,21 @@ async function withKeepTreeState(updateDataFn: () => Promise<void>) {
211
214
  selectedKeys.value = [prevSelected]
212
215
  }
213
216
  else {
214
- selectFirstSelectableNode()
217
+ selectFirstSelectableNode(true)
215
218
  }
216
- keepTreeState.value = false
219
+ isPreservingTreeState.value = false
217
220
  })
218
221
  }
219
222
 
223
+ async function getDepth(key: string) {
224
+ const parentKeys = getParentKeys(key)
225
+ return parentKeys!.length + 1
226
+ }
227
+
220
228
  defineExpose({
221
- withKeepTreeState,
229
+ updateTreeWithPreservedState,
230
+ getParentKeys,
231
+ getDepth,
222
232
  })
223
233
  </script>
224
234
 
@@ -247,7 +257,7 @@ defineExpose({
247
257
  @expand="onExpand"
248
258
  @select="onSelected"
249
259
  >
250
- <template #title="{ title, dataRef, data }">
260
+ <template #title="{ title, dataRef }">
251
261
  <div :class="`tree-node-tittle flex items-center ${dataRef.selectable ? 'selectable' : 'not-selectable'}`">
252
262
  <div
253
263
  v-if="!!dataRef.icon"
@@ -40,12 +40,22 @@ watch(
40
40
  { immediate: true },
41
41
  )
42
42
 
43
- async function withKeepTreeState(fn: any) {
44
- treeRef.value.withKeepTreeState(fn)
43
+ async function updateTreeWithPreservedState(fn: any) {
44
+ treeRef.value.updateTreeWithPreservedState(fn)
45
+ }
46
+
47
+ function getParentKeys(key: string) {
48
+ return treeRef.value.getParentKeys(key)
49
+ }
50
+
51
+ function getDepth(key: string) {
52
+ return treeRef.value.getDepth(key)
45
53
  }
46
54
 
47
55
  defineExpose({
48
- withKeepTreeState,
56
+ updateTreeWithPreservedState,
57
+ getParentKeys,
58
+ getDepth,
49
59
  })
50
60
  </script>
51
61
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handaotech-design/bom",
3
- "version": "0.0.26",
3
+ "version": "0.0.28",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "exports": {
@@ -25,7 +25,10 @@
25
25
  "build:unocss": "unocss '**/*.{vue,ts}' -o dist/style.css",
26
26
  "stub": "pnpm run build:unocss && unbuild --stub"
27
27
  },
28
- "dependencies": {
28
+ "dependencies": {},
29
+ "devDependencies": {
30
+ "sass": "*",
31
+ "unocss": "*",
29
32
  "vue": "*",
30
33
  "vue-types": "*",
31
34
  "ant-design-vue": "*",
@@ -33,11 +36,7 @@
33
36
  "@handaotech-design/vue": "workspace:*",
34
37
  "lodash-es": "*"
35
38
  },
36
- "devDependencies": {
37
- "sass": "*",
38
- "unocss": "*"
39
- },
40
- "peerDependenciesMeta": {
39
+ "peerDependencies": {
41
40
  "vue": "*",
42
41
  "vue-types": "*",
43
42
  "ant-design-vue": "*",
@@ -46,10 +45,10 @@
46
45
  "lodash-es": "*"
47
46
  },
48
47
  "author": "HD",
49
- "homepage": "http://git.handao.cloud/infra/hd-design/-/blob/bom/README.md",
48
+ "homepage": "https://www.npmjs.com/package/@handaotech-design/bom",
50
49
  "repository": {
51
50
  "type": "git",
52
- "url": "git+http://git.handao.cloud/infra/hd-design.git",
51
+ "url": "git+http://esgit.handao.cloud/infra/hd-design.git",
53
52
  "directory": "packages/bom"
54
53
  },
55
54
  "publishConfig": {