@handaotech-design/bom 0.0.23 → 0.0.25

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.
@@ -35,9 +35,11 @@ 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 initTreeState = async () => {
38
+ const keepTreeState = ref<boolean>(false)
39
+ const initTreeState = () => {
39
40
  searchValue.value = ''
40
- filteredTreeData.value = _treeData.value
41
+ selectedKeys.value = []
42
+ expandedKeys.value = []
41
43
  }
42
44
 
43
45
  const onExpand = (keys: Key[]) => {
@@ -131,15 +133,12 @@ onBeforeUnmount(() => {
131
133
  watch(
132
134
  () => _treeData.value,
133
135
  async () => {
134
- if (!_.isEmpty(_treeData.value)) {
135
- keyToNodeMap = buildKeyToNodeMap(_treeData.value)
136
- await initTreeState()
136
+ filteredTreeData.value = _treeData.value
137
+ keyToNodeMap = buildKeyToNodeMap(_treeData.value ?? [])
138
+ initTreeState()
139
+ if (!keepTreeState.value) {
137
140
  selectFirstSelectableNode()
138
141
  }
139
- else {
140
- keyToNodeMap.clear()
141
- await initTreeState()
142
- }
143
142
  },
144
143
  {
145
144
  immediate: true,
@@ -199,6 +198,28 @@ function getTitlePart(title: string, partType: 'before' | 'after') {
199
198
  function hasSearchMatch(title: string) {
200
199
  return searchValue.value && title.includes(searchValue.value)
201
200
  }
201
+
202
+ async function withKeepTreeState(updateDataFn: () => Promise<void>) {
203
+ const prevSelected = selectedKeys.value?.[0]
204
+ const prevExpanded = [...expandedKeys.value]
205
+ keepTreeState.value = true
206
+ await updateDataFn()
207
+ await nextTick(() => {
208
+ expandedKeys.value = prevExpanded.filter(key => keyToNodeMap.has(key as string))
209
+
210
+ if (prevSelected && keyToNodeMap.has(prevSelected)) {
211
+ selectedKeys.value = [prevSelected]
212
+ }
213
+ else {
214
+ selectFirstSelectableNode()
215
+ }
216
+ keepTreeState.value = false
217
+ })
218
+ }
219
+
220
+ defineExpose({
221
+ withKeepTreeState,
222
+ })
202
223
  </script>
203
224
 
204
225
  <template>
@@ -8,6 +8,7 @@ import HdBomTree from '../bom-tree/index'
8
8
 
9
9
  const props = defineProps<Props>()
10
10
  const COMPONENT_NAME = 'HdBomWorkbench'
11
+ const treeRef = ref()
11
12
  defineOptions({
12
13
  name: COMPONENT_NAME,
13
14
  })
@@ -38,6 +39,14 @@ watch(
38
39
  },
39
40
  { immediate: true },
40
41
  )
42
+
43
+ async function withKeepTreeState(fn: any) {
44
+ treeRef.value.withKeepTreeState(fn)
45
+ }
46
+
47
+ defineExpose({
48
+ withKeepTreeState,
49
+ })
41
50
  </script>
42
51
 
43
52
  <template>
@@ -48,8 +57,10 @@ watch(
48
57
  >
49
58
  <template #left>
50
59
  <div class="left-content">
60
+ <slot name="tree-header" />
51
61
  <div class="bom-tree-container">
52
62
  <HdBomTree
63
+ ref="treeRef"
53
64
  :tree-data="bomDataForTree"
54
65
  :config="props.treeConfig"
55
66
  @select="(data: BomNode) => onSelected(data)"
@@ -35,9 +35,11 @@ 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 initTreeState = async () => {
38
+ const keepTreeState = ref<boolean>(false)
39
+ const initTreeState = () => {
39
40
  searchValue.value = ''
40
- filteredTreeData.value = _treeData.value
41
+ selectedKeys.value = []
42
+ expandedKeys.value = []
41
43
  }
42
44
 
43
45
  const onExpand = (keys: Key[]) => {
@@ -131,15 +133,12 @@ onBeforeUnmount(() => {
131
133
  watch(
132
134
  () => _treeData.value,
133
135
  async () => {
134
- if (!_.isEmpty(_treeData.value)) {
135
- keyToNodeMap = buildKeyToNodeMap(_treeData.value)
136
- await initTreeState()
136
+ filteredTreeData.value = _treeData.value
137
+ keyToNodeMap = buildKeyToNodeMap(_treeData.value ?? [])
138
+ initTreeState()
139
+ if (!keepTreeState.value) {
137
140
  selectFirstSelectableNode()
138
141
  }
139
- else {
140
- keyToNodeMap.clear()
141
- await initTreeState()
142
- }
143
142
  },
144
143
  {
145
144
  immediate: true,
@@ -199,6 +198,28 @@ function getTitlePart(title: string, partType: 'before' | 'after') {
199
198
  function hasSearchMatch(title: string) {
200
199
  return searchValue.value && title.includes(searchValue.value)
201
200
  }
201
+
202
+ async function withKeepTreeState(updateDataFn: () => Promise<void>) {
203
+ const prevSelected = selectedKeys.value?.[0]
204
+ const prevExpanded = [...expandedKeys.value]
205
+ keepTreeState.value = true
206
+ await updateDataFn()
207
+ await nextTick(() => {
208
+ expandedKeys.value = prevExpanded.filter(key => keyToNodeMap.has(key as string))
209
+
210
+ if (prevSelected && keyToNodeMap.has(prevSelected)) {
211
+ selectedKeys.value = [prevSelected]
212
+ }
213
+ else {
214
+ selectFirstSelectableNode()
215
+ }
216
+ keepTreeState.value = false
217
+ })
218
+ }
219
+
220
+ defineExpose({
221
+ withKeepTreeState,
222
+ })
202
223
  </script>
203
224
 
204
225
  <template>
@@ -8,6 +8,7 @@ import HdBomTree from '../bom-tree/index'
8
8
 
9
9
  const props = defineProps<Props>()
10
10
  const COMPONENT_NAME = 'HdBomWorkbench'
11
+ const treeRef = ref()
11
12
  defineOptions({
12
13
  name: COMPONENT_NAME,
13
14
  })
@@ -38,6 +39,14 @@ watch(
38
39
  },
39
40
  { immediate: true },
40
41
  )
42
+
43
+ async function withKeepTreeState(fn: any) {
44
+ treeRef.value.withKeepTreeState(fn)
45
+ }
46
+
47
+ defineExpose({
48
+ withKeepTreeState,
49
+ })
41
50
  </script>
42
51
 
43
52
  <template>
@@ -48,8 +57,10 @@ watch(
48
57
  >
49
58
  <template #left>
50
59
  <div class="left-content">
60
+ <slot name="tree-header" />
51
61
  <div class="bom-tree-container">
52
62
  <HdBomTree
63
+ ref="treeRef"
53
64
  :tree-data="bomDataForTree"
54
65
  :config="props.treeConfig"
55
66
  @select="(data: BomNode) => onSelected(data)"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@handaotech-design/bom",
3
- "version": "0.0.23",
3
+ "version": "0.0.25",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "exports": {
@@ -21,14 +21,9 @@
21
21
  "module": "./dist/es/index.js",
22
22
  "types": "./dist/es/index.d.ts",
23
23
  "scripts": {
24
- "build": "unocss '**/*.{vue,ts}' -o dist/style.css && unbuild",
25
- "build:unbuild": "unbuild",
26
- "build:vite": "vite build && pnpm run generate:types",
27
- "build:rollup": "rollup --config rollup.config.ts --configPlugin typescript",
28
- "generate:types": "pnpm run generate:es:types && pnpm run generate:lib:types",
29
- "generate:es:types": "vue-tsc --declaration --emitDeclarationOnly --outdir ./es",
30
- "generate:lib:types": "vue-tsc --declaration --emitDeclarationOnly --outdir ./lib",
31
- "stub": "unbuild --stub"
24
+ "build": "unbuild && pnpm run build:unocss",
25
+ "build:unocss": "unocss '**/*.{vue,ts}' -o dist/style.css",
26
+ "stub": "pnpm run build:unocss && unbuild --stub"
32
27
  },
33
28
  "dependencies": {
34
29
  "@handaotech-design/vue": "^0.2.11"