@tmagic/utils 1.8.0-beta.4 → 1.8.0-beta.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.
package/dist/es/index.js CHANGED
@@ -20,47 +20,48 @@ var emptyFn = () => void 0;
20
20
  * @param {Array} data 要查找的根容器节点
21
21
  * @return {Array} 组件在data中的子孙路径
22
22
  */
23
- var getNodePath = (id, data = []) => {
23
+ var getNodePath = (id, data = [], skip) => {
24
24
  const path = [];
25
- const get = function(id, data) {
25
+ const targetId = `${id}`;
26
+ const get = function(data) {
26
27
  if (!Array.isArray(data)) return null;
27
28
  for (let i = 0, l = data.length; i < l; i++) {
28
29
  const item = data[i];
29
30
  path.push(item);
30
- if (`${item.id}` === `${id}`) return item;
31
- if (item.items) {
32
- const node = get(id, item.items);
31
+ if (`${item.id}` === targetId) return item;
32
+ if (item.items && item !== skip) {
33
+ const node = get(item.items);
33
34
  if (node) return node;
34
35
  }
35
36
  path.pop();
36
37
  }
37
38
  return null;
38
39
  };
39
- get(id, data);
40
+ get(data);
40
41
  return path;
41
42
  };
42
- var getNodeInfo = (id, root) => {
43
+ var getNodeInfo = (id, root, skip) => {
43
44
  const info = {
44
45
  node: null,
45
46
  parent: null,
46
- page: null
47
+ page: null,
48
+ path: []
47
49
  };
48
50
  if (!root) return info;
49
51
  if (id === root.id) {
50
52
  info.node = root;
51
53
  return info;
52
54
  }
53
- const path = getNodePath(id, root.items);
55
+ const path = getNodePath(id, root.items, skip);
56
+ info.path = path;
54
57
  if (!path.length) return info;
55
58
  path.unshift(root);
56
59
  info.node = path[path.length - 1];
57
60
  info.parent = path[path.length - 2];
58
- path.forEach((item) => {
59
- if (isPage(item) || isPageFragment(item)) {
60
- info.page = item;
61
- return;
62
- }
63
- });
61
+ for (const item of path) if (isPage(item) || isPageFragment(item)) {
62
+ info.page = item;
63
+ break;
64
+ }
64
65
  return info;
65
66
  };
66
67
  var filterXSS = (str) => str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
@@ -2345,47 +2345,48 @@
2345
2345
  * @param {Array} data 要查找的根容器节点
2346
2346
  * @return {Array} 组件在data中的子孙路径
2347
2347
  */
2348
- var getNodePath = (id, data = []) => {
2348
+ var getNodePath = (id, data = [], skip) => {
2349
2349
  const path = [];
2350
- const get = function(id, data) {
2350
+ const targetId = `${id}`;
2351
+ const get = function(data) {
2351
2352
  if (!Array.isArray(data)) return null;
2352
2353
  for (let i = 0, l = data.length; i < l; i++) {
2353
2354
  const item = data[i];
2354
2355
  path.push(item);
2355
- if (`${item.id}` === `${id}`) return item;
2356
- if (item.items) {
2357
- const node = get(id, item.items);
2356
+ if (`${item.id}` === targetId) return item;
2357
+ if (item.items && item !== skip) {
2358
+ const node = get(item.items);
2358
2359
  if (node) return node;
2359
2360
  }
2360
2361
  path.pop();
2361
2362
  }
2362
2363
  return null;
2363
2364
  };
2364
- get(id, data);
2365
+ get(data);
2365
2366
  return path;
2366
2367
  };
2367
- var getNodeInfo = (id, root) => {
2368
+ var getNodeInfo = (id, root, skip) => {
2368
2369
  const info = {
2369
2370
  node: null,
2370
2371
  parent: null,
2371
- page: null
2372
+ page: null,
2373
+ path: []
2372
2374
  };
2373
2375
  if (!root) return info;
2374
2376
  if (id === root.id) {
2375
2377
  info.node = root;
2376
2378
  return info;
2377
2379
  }
2378
- const path = getNodePath(id, root.items);
2380
+ const path = getNodePath(id, root.items, skip);
2381
+ info.path = path;
2379
2382
  if (!path.length) return info;
2380
2383
  path.unshift(root);
2381
2384
  info.node = path[path.length - 1];
2382
2385
  info.parent = path[path.length - 2];
2383
- path.forEach((item) => {
2384
- if (isPage(item) || isPageFragment(item)) {
2385
- info.page = item;
2386
- return;
2387
- }
2388
- });
2386
+ for (const item of path) if (isPage(item) || isPageFragment(item)) {
2387
+ info.page = item;
2388
+ break;
2389
+ }
2389
2390
  return info;
2390
2391
  };
2391
2392
  var filterXSS = (str) => str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.0-beta.4",
2
+ "version": "1.8.0-beta.6",
3
3
  "name": "@tmagic/utils",
4
4
  "type": "module",
5
5
  "sideEffects": false,
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "peerDependencies": {
37
37
  "typescript": "^6.0.3",
38
- "@tmagic/schema": "1.8.0-beta.4"
38
+ "@tmagic/schema": "1.8.0-beta.6"
39
39
  },
40
40
  "peerDependenciesMeta": {
41
41
  "typescript": {
package/src/index.ts CHANGED
@@ -23,7 +23,6 @@ import type {
23
23
  DataSchema,
24
24
  DataSourceDeps,
25
25
  Id,
26
- MApp,
27
26
  MComponent,
28
27
  MContainer,
29
28
  MNode,
@@ -80,10 +79,12 @@ export const emptyFn = (): any => undefined;
80
79
  * @param {Array} data 要查找的根容器节点
81
80
  * @return {Array} 组件在data中的子孙路径
82
81
  */
83
- export const getNodePath = (id: Id, data: MNode[] = []): MNode[] => {
82
+ export const getNodePath = (id: Id, data: MNode[] = [], skip?: MNode | null): MNode[] => {
84
83
  const path: MNode[] = [];
84
+ // 目标 id 字符串只计算一次,避免在递归遍历中对每个节点重复拼接
85
+ const targetId = `${id}`;
85
86
 
86
- const get = function (id: number | string, data: MNode[]): MNode | null {
87
+ const get = function (data: MNode[]): MNode | null {
87
88
  if (!Array.isArray(data)) {
88
89
  return null;
89
90
  }
@@ -92,12 +93,13 @@ export const getNodePath = (id: Id, data: MNode[] = []): MNode[] => {
92
93
  const item = data[i];
93
94
 
94
95
  path.push(item);
95
- if (`${item.id}` === `${id}`) {
96
+ if (`${item.id}` === targetId) {
96
97
  return item;
97
98
  }
98
99
 
99
- if (item.items) {
100
- const node = get(id, item.items);
100
+ // skip 用于跳过已经搜索过的子树(如已优先搜索过的当前页面),避免重复遍历
101
+ if (item.items && item !== skip) {
102
+ const node = get(item.items);
101
103
  if (node) {
102
104
  return node;
103
105
  }
@@ -109,16 +111,17 @@ export const getNodePath = (id: Id, data: MNode[] = []): MNode[] => {
109
111
  return null;
110
112
  };
111
113
 
112
- get(id, data);
114
+ get(data);
113
115
 
114
116
  return path;
115
117
  };
116
118
 
117
- export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) => {
119
+ export const getNodeInfo = (id: Id, root: { id: Id; items?: MNode[] } | null, skip?: MNode | null) => {
118
120
  const info: EditorNodeInfo = {
119
121
  node: null,
120
122
  parent: null,
121
123
  page: null,
124
+ path: [],
122
125
  };
123
126
 
124
127
  if (!root) return info;
@@ -128,7 +131,8 @@ export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) =>
128
131
  return info;
129
132
  }
130
133
 
131
- const path = getNodePath(id, root.items);
134
+ const path = getNodePath(id, root.items, skip);
135
+ info.path = path;
132
136
 
133
137
  if (!path.length) return info;
134
138
 
@@ -137,12 +141,12 @@ export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) =>
137
141
  info.node = path[path.length - 1] as MComponent;
138
142
  info.parent = path[path.length - 2] as MContainer;
139
143
 
140
- path.forEach((item) => {
144
+ for (const item of path) {
141
145
  if (isPage(item) || isPageFragment(item)) {
142
146
  info.page = item as MPage | MPageFragment;
143
- return;
147
+ break;
144
148
  }
145
- });
149
+ }
146
150
 
147
151
  return info;
148
152
  };
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { DataSchema, DataSourceDeps, Id, MApp, MComponent, MNode, MNodeInstance } from "@tmagic/schema";
1
+ import { DataSchema, DataSourceDeps, Id, MComponent, MNode, MNodeInstance } from "@tmagic/schema";
2
2
  import { MContainer, MNode as MNode$1, MPage, MPageFragment } from "@tmagic/core";
3
3
 
4
4
  //#region temp/packages/editor/src/type.d.ts
@@ -6,6 +6,7 @@ interface EditorNodeInfo {
6
6
  node: MNode$1 | null;
7
7
  parent: MContainer | null;
8
8
  page: MPage | MPageFragment | null;
9
+ path: MNode$1[];
9
10
  }
10
11
  //#endregion
11
12
  //#region temp/packages/utils/src/dom.d.ts
@@ -51,8 +52,11 @@ declare const emptyFn: () => any;
51
52
  * @param {Array} data 要查找的根容器节点
52
53
  * @return {Array} 组件在data中的子孙路径
53
54
  */
54
- declare const getNodePath: (id: Id, data?: MNode[]) => MNode[];
55
- declare const getNodeInfo: (id: Id, root: Pick<MApp, "id" | "items"> | null) => EditorNodeInfo;
55
+ declare const getNodePath: (id: Id, data?: MNode[], skip?: MNode | null) => MNode[];
56
+ declare const getNodeInfo: (id: Id, root: {
57
+ id: Id;
58
+ items?: MNode[];
59
+ } | null, skip?: MNode | null) => EditorNodeInfo;
56
60
  declare const filterXSS: (str: string) => string;
57
61
  declare const getUrlParam: (param: string, url?: string) => string;
58
62
  /**