@splicetree/plugin-lazy-load 0.3.0 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # @splicetree/plugin-lazy-load
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 为懒加载插件新增“加载中”状态与渲染联动:
8
+ - 新增 `loadingKeys: Set<string>` 与 `isLoading(id)` 实例方法;节点新增 `isLoading()`。
9
+ - 在 `load(id)` 开始/结束时派发 `visibility` 事件,触发适配层刷新,便于 UI 立刻显示 Loading。
10
+ - 文档示例在加载中禁用展开按钮,并以旋转图标显示 Loading。
11
+
12
+ 此为向后兼容更新;如需在加载中禁止交互,请在 UI 侧判断 `item.isLoading()`。
13
+
14
+ ## 1.0.0
15
+
3
16
  ## 0.3.0
4
17
 
5
18
  ## 0.2.0
package/dist/index.d.ts CHANGED
@@ -19,7 +19,9 @@ declare module '@splicetree/core' {
19
19
  /**
20
20
  * 实例扩展(Lazy-Load)
21
21
  * - loadedKeys:已加载集合
22
+ * - loadingKeys:加载中的集合
22
23
  * - isLoaded:查询是否已加载
24
+ * - isLoading:查询是否正在加载
23
25
  * - load:加载指定节点的子节点
24
26
  */
25
27
  interface SpliceTreeInstance {
@@ -27,10 +29,18 @@ declare module '@splicetree/core' {
27
29
  * 已完成加载的节点 id 集合
28
30
  */
29
31
  loadedKeys: Set<string>;
32
+ /**
33
+ * 正在加载的节点 id 集合
34
+ */
35
+ loadingKeys: Set<string>;
30
36
  /**
31
37
  * 查询指定节点是否已完成子节点加载
32
38
  */
33
39
  isLoaded: (id: string) => boolean;
40
+ /**
41
+ * 查询指定节点是否正在加载
42
+ */
43
+ isLoading: (id: string) => boolean;
34
44
  /**
35
45
  * 加载指定节点的子节点并追加到树中
36
46
  * @param id 目标节点 id
@@ -43,6 +53,7 @@ declare module '@splicetree/core' {
43
53
  */
44
54
  interface SpliceTreeNode {
45
55
  isLoaded: () => boolean;
56
+ isLoading: () => boolean;
46
57
  }
47
58
  }
48
59
  declare const lazyLoad: SpliceTreePlugin;
package/dist/index.js CHANGED
@@ -4,7 +4,9 @@ const lazyLoad = {
4
4
  setup(ctx) {
5
5
  const { loadChildren } = ctx.options?.configuration?.lazyLoad ?? {};
6
6
  const loadedKeys = /* @__PURE__ */ new Set();
7
+ const loadingKeys = /* @__PURE__ */ new Set();
7
8
  const isLoaded = (id) => loadedKeys.has(id);
9
+ const isLoading = (id) => loadingKeys.has(id);
8
10
  /**
9
11
  * 为节点应用懒加载相关的覆盖方法
10
12
  * - isLoaded:读取实例的已加载状态
@@ -12,6 +14,7 @@ const lazyLoad = {
12
14
  */
13
15
  const applyLazyOverrides = (node) => {
14
16
  node.isLoaded = () => ctx.tree.isLoaded?.(node.id);
17
+ node.isLoading = () => ctx.tree.isLoading?.(node.id);
15
18
  node.hasChildren = () => {
16
19
  if (!ctx.tree.isLoaded?.(node.id)) return true;
17
20
  return !!node.getChildren()?.length;
@@ -29,6 +32,12 @@ const lazyLoad = {
29
32
  if (loadedKeys.has(id)) return;
30
33
  const node = ctx.tree.getNode(id);
31
34
  if (!node) return;
35
+ if (loadingKeys.has(id)) return;
36
+ loadingKeys.add(id);
37
+ ctx.events.emit({
38
+ name: "visibility",
39
+ keys: ctx.tree.expandedKeys()
40
+ });
32
41
  const children = await loadChildren(node);
33
42
  if (children?.length) {
34
43
  ctx.tree.appendChildren(id, children);
@@ -39,6 +48,11 @@ const lazyLoad = {
39
48
  }
40
49
  }
41
50
  loadedKeys.add(id);
51
+ loadingKeys.delete(id);
52
+ ctx.events.emit({
53
+ name: "visibility",
54
+ keys: ctx.tree.expandedKeys()
55
+ });
42
56
  ctx.events.emit({
43
57
  name: "lazyload",
44
58
  keys: Array.from(loadedKeys)
@@ -70,7 +84,9 @@ const lazyLoad = {
70
84
  };
71
85
  return {
72
86
  loadedKeys,
87
+ loadingKeys,
73
88
  isLoaded,
89
+ isLoading,
74
90
  load,
75
91
  expand,
76
92
  toggleExpand
@@ -78,6 +94,7 @@ const lazyLoad = {
78
94
  },
79
95
  extendNode(node, ctx) {
80
96
  node.isLoaded = () => ctx.tree.isLoaded?.(node.id);
97
+ node.isLoading = () => ctx.tree.isLoading?.(node.id);
81
98
  node.hasChildren = () => {
82
99
  if (!ctx.tree.isLoaded?.(node.id)) return true;
83
100
  return !!node.getChildren()?.length;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@splicetree/plugin-lazy-load",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "1.1.0",
5
5
  "author": {
6
6
  "email": "michael.cocova@gmail.com",
7
7
  "name": "Michael Cocova"
@@ -23,7 +23,7 @@
23
23
  "access": "public"
24
24
  },
25
25
  "devDependencies": {
26
- "@splicetree/core": "0.3.0"
26
+ "@splicetree/core": "1.1.0"
27
27
  },
28
28
  "scripts": {
29
29
  "dev": "tsdown --watch",
package/src/index.ts CHANGED
@@ -19,7 +19,9 @@ declare module '@splicetree/core' {
19
19
  /**
20
20
  * 实例扩展(Lazy-Load)
21
21
  * - loadedKeys:已加载集合
22
+ * - loadingKeys:加载中的集合
22
23
  * - isLoaded:查询是否已加载
24
+ * - isLoading:查询是否正在加载
23
25
  * - load:加载指定节点的子节点
24
26
  */
25
27
  interface SpliceTreeInstance {
@@ -27,10 +29,18 @@ declare module '@splicetree/core' {
27
29
  * 已完成加载的节点 id 集合
28
30
  */
29
31
  loadedKeys: Set<string>
32
+ /**
33
+ * 正在加载的节点 id 集合
34
+ */
35
+ loadingKeys: Set<string>
30
36
  /**
31
37
  * 查询指定节点是否已完成子节点加载
32
38
  */
33
39
  isLoaded: (id: string) => boolean
40
+ /**
41
+ * 查询指定节点是否正在加载
42
+ */
43
+ isLoading: (id: string) => boolean
34
44
  /**
35
45
  * 加载指定节点的子节点并追加到树中
36
46
  * @param id 目标节点 id
@@ -44,6 +54,7 @@ declare module '@splicetree/core' {
44
54
  */
45
55
  interface SpliceTreeNode {
46
56
  isLoaded: () => boolean
57
+ isLoading: () => boolean
47
58
  }
48
59
  }
49
60
 
@@ -61,7 +72,9 @@ export const lazyLoad: SpliceTreePlugin = {
61
72
  }
62
73
  const { loadChildren } = cfg
63
74
  const loadedKeys = new Set<string>()
75
+ const loadingKeys = new Set<string>()
64
76
  const isLoaded = (id: string) => loadedKeys.has(id)
77
+ const isLoading = (id: string) => loadingKeys.has(id)
65
78
 
66
79
  /**
67
80
  * 为节点应用懒加载相关的覆盖方法
@@ -70,6 +83,7 @@ export const lazyLoad: SpliceTreePlugin = {
70
83
  */
71
84
  const applyLazyOverrides = (node: any) => {
72
85
  node.isLoaded = () => ctx.tree.isLoaded?.(node.id)
86
+ node.isLoading = () => ctx.tree.isLoading?.(node.id)
73
87
  node.hasChildren = () => {
74
88
  const loaded = ctx.tree.isLoaded?.(node.id)
75
89
  if (!loaded) {
@@ -97,6 +111,11 @@ export const lazyLoad: SpliceTreePlugin = {
97
111
  if (!node) {
98
112
  return
99
113
  }
114
+ if (loadingKeys.has(id)) {
115
+ return
116
+ }
117
+ loadingKeys.add(id)
118
+ ctx.events.emit({ name: 'visibility', keys: ctx.tree.expandedKeys() })
100
119
  const children = await loadChildren(node)
101
120
  if (children?.length) {
102
121
  ctx.tree.appendChildren(id, children)
@@ -109,6 +128,8 @@ export const lazyLoad: SpliceTreePlugin = {
109
128
  }
110
129
  }
111
130
  loadedKeys.add(id)
131
+ loadingKeys.delete(id)
132
+ ctx.events.emit({ name: 'visibility', keys: ctx.tree.expandedKeys() })
112
133
  ctx.events.emit({ name: 'lazyload', keys: Array.from(loadedKeys) })
113
134
  }
114
135
 
@@ -148,7 +169,9 @@ export const lazyLoad: SpliceTreePlugin = {
148
169
 
149
170
  return {
150
171
  loadedKeys,
172
+ loadingKeys,
151
173
  isLoaded,
174
+ isLoading,
152
175
  load,
153
176
  expand,
154
177
  toggleExpand,
@@ -161,6 +184,7 @@ export const lazyLoad: SpliceTreePlugin = {
161
184
  */
162
185
  extendNode(node, ctx) {
163
186
  node.isLoaded = () => ctx.tree.isLoaded?.(node.id)
187
+ node.isLoading = () => ctx.tree.isLoading?.(node.id)
164
188
  node.hasChildren = () => {
165
189
  const loaded = ctx.tree.isLoaded?.(node.id)
166
190
  if (!loaded) {