@d-matrix/utils 1.24.0 → 1.26.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/dist/algorithm/tree.d.ts +9 -1
- package/dist/algorithm/tree.js +28 -1
- package/dist/file.js +2 -0
- package/dist/react/index.d.ts +1 -0
- package/dist/react/index.js +1 -0
- package/dist/react/useIsFirstRender.d.ts +6 -0
- package/dist/react/useIsFirstRender.js +14 -0
- package/package.json +1 -1
- package/readme.md +21 -0
package/dist/algorithm/tree.d.ts
CHANGED
|
@@ -20,6 +20,14 @@ export declare const findNode: <T extends Record<string, any>>(tree: T[], predic
|
|
|
20
20
|
* @param child 子节点
|
|
21
21
|
* @param indentityKey 节点唯一id字段名称
|
|
22
22
|
* @param childrenKey 节点的子节点的字段名称
|
|
23
|
-
* @returns
|
|
23
|
+
* @returns 父节点
|
|
24
24
|
*/
|
|
25
25
|
export declare const findParent: <T extends Record<string, any>>(tree: T | undefined, child: T | undefined, indentityKey?: string, childrenKey?: string) => T | null;
|
|
26
|
+
/**
|
|
27
|
+
* 查找节点路径
|
|
28
|
+
* @param tree
|
|
29
|
+
* @param func 符合条件的节点函数,返回 boolean
|
|
30
|
+
* @param childrenKey 子节点key
|
|
31
|
+
* @returns 节点路径
|
|
32
|
+
*/
|
|
33
|
+
export declare function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey?: string): T[] | null;
|
package/dist/algorithm/tree.js
CHANGED
|
@@ -47,7 +47,7 @@ export const findNode = (tree, predicate, childrenKey = 'children') => {
|
|
|
47
47
|
* @param child 子节点
|
|
48
48
|
* @param indentityKey 节点唯一id字段名称
|
|
49
49
|
* @param childrenKey 节点的子节点的字段名称
|
|
50
|
-
* @returns
|
|
50
|
+
* @returns 父节点
|
|
51
51
|
*/
|
|
52
52
|
export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'children') => {
|
|
53
53
|
if (tree === undefined || child === undefined) {
|
|
@@ -68,3 +68,30 @@ export const findParent = (tree, child, indentityKey = 'id', childrenKey = 'chil
|
|
|
68
68
|
}
|
|
69
69
|
return null;
|
|
70
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* 查找节点路径
|
|
73
|
+
* @param tree
|
|
74
|
+
* @param func 符合条件的节点函数,返回 boolean
|
|
75
|
+
* @param childrenKey 子节点key
|
|
76
|
+
* @returns 节点路径
|
|
77
|
+
*/
|
|
78
|
+
export function findPath(tree, func, childrenKey = 'children') {
|
|
79
|
+
const path = [];
|
|
80
|
+
const list = [...tree];
|
|
81
|
+
const visitedSet = new Set();
|
|
82
|
+
while (list.length) {
|
|
83
|
+
const node = list[0];
|
|
84
|
+
if (visitedSet.has(node)) {
|
|
85
|
+
path.pop();
|
|
86
|
+
list.shift();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
visitedSet.add(node);
|
|
90
|
+
Array.isArray(node[childrenKey]) && list.unshift(...node[childrenKey]);
|
|
91
|
+
path.push(node);
|
|
92
|
+
if (func(node))
|
|
93
|
+
return path;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
package/dist/file.js
CHANGED
|
@@ -109,6 +109,8 @@ export function download(source, fileName = '', target) {
|
|
|
109
109
|
link.target = target;
|
|
110
110
|
}
|
|
111
111
|
else {
|
|
112
|
+
// https://stackoverflow.com/questions/33909763/download-attribute-with-a-file-name-not-working
|
|
113
|
+
// download 只在同源 URL 或 blob:、data: 协议起作用, 如果下载不同源的oss文件,download属性无效
|
|
112
114
|
link.download = fileName;
|
|
113
115
|
}
|
|
114
116
|
link.href = source;
|
package/dist/react/index.d.ts
CHANGED
package/dist/react/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useRef } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* 使用 useIsFirstRender 区分第一次和后续渲染
|
|
4
|
+
* useIsFirstRender 对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个钩子特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
|
|
5
|
+
* @returns boolean true 表示第一次渲染,false 表示其他渲染
|
|
6
|
+
*/
|
|
7
|
+
export function useIsFirstRender() {
|
|
8
|
+
const renderRef = useRef(true);
|
|
9
|
+
if (renderRef.current === true) {
|
|
10
|
+
renderRef.current = false;
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
return renderRef.current;
|
|
14
|
+
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -198,6 +198,10 @@ export default function Component() {
|
|
|
198
198
|
}
|
|
199
199
|
```
|
|
200
200
|
|
|
201
|
+
- `useIsFirstRender(): boolean`
|
|
202
|
+
|
|
203
|
+
对于确定当前渲染是否是组件的第一个渲染很有用。当你想在初始渲染时有条件地执行某些逻辑或渲染特定组件时,这个 hook 特别有价值,提供了一种有效的方法来区分第一次和后续渲染。
|
|
204
|
+
|
|
201
205
|
### dom
|
|
202
206
|
|
|
203
207
|
- `scrollToTop(element: Element | null | undefined): void`
|
|
@@ -407,6 +411,23 @@ const actual = tree.findParent(treeData, treeData.subs[1].subs[2], 'code', 'subs
|
|
|
407
411
|
expect(actual).to.be.deep.equal(treeData.subs[1]);
|
|
408
412
|
```
|
|
409
413
|
|
|
414
|
+
- `function findPath<T extends Record<string, any>>(tree: T[], func: (node: T) => boolean, childrenKey = 'children'): T[] | null`
|
|
415
|
+
|
|
416
|
+
查找节点路径, 返回节点数组或`null`
|
|
417
|
+
|
|
418
|
+
```ts
|
|
419
|
+
const treeData = {
|
|
420
|
+
code: 1,
|
|
421
|
+
subs: [
|
|
422
|
+
{ code: 2, subs: [{ code: 21 }, { code: 22 }, { code: 23 }] },
|
|
423
|
+
{ code: 3, subs: [{ code: 31 }, { code: 32 }, { code: 33 }] },
|
|
424
|
+
],
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const actual = tree.findPath([root], (node) => node.id === 33);
|
|
428
|
+
expect(actual).to.be.deep.equal([root, root.children[1], root.children[1].children[2]]);
|
|
429
|
+
```
|
|
430
|
+
|
|
410
431
|
### file
|
|
411
432
|
|
|
412
433
|
- `toImage(file: BlobPart | FileURL, options?: BlobPropertyBag): Promise<HTMLImageElement>`
|