@tmagic/utils 1.8.0-beta.3 → 1.8.0-beta.5

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,26 +20,27 @@ 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,
@@ -50,17 +51,15 @@ var getNodeInfo = (id, root) => {
50
51
  info.node = root;
51
52
  return info;
52
53
  }
53
- const path = getNodePath(id, root.items);
54
+ const path = getNodePath(id, root.items, skip);
54
55
  if (!path.length) return info;
55
56
  path.unshift(root);
56
57
  info.node = path[path.length - 1];
57
58
  info.parent = path[path.length - 2];
58
- path.forEach((item) => {
59
- if (isPage(item) || isPageFragment(item)) {
60
- info.page = item;
61
- return;
62
- }
63
- });
59
+ for (const item of path) if (isPage(item) || isPageFragment(item)) {
60
+ info.page = item;
61
+ break;
62
+ }
64
63
  return info;
65
64
  };
66
65
  var filterXSS = (str) => str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
@@ -2345,26 +2345,27 @@
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,
@@ -2375,17 +2376,15 @@
2375
2376
  info.node = root;
2376
2377
  return info;
2377
2378
  }
2378
- const path = getNodePath(id, root.items);
2379
+ const path = getNodePath(id, root.items, skip);
2379
2380
  if (!path.length) return info;
2380
2381
  path.unshift(root);
2381
2382
  info.node = path[path.length - 1];
2382
2383
  info.parent = path[path.length - 2];
2383
- path.forEach((item) => {
2384
- if (isPage(item) || isPageFragment(item)) {
2385
- info.page = item;
2386
- return;
2387
- }
2388
- });
2384
+ for (const item of path) if (isPage(item) || isPageFragment(item)) {
2385
+ info.page = item;
2386
+ break;
2387
+ }
2389
2388
  return info;
2390
2389
  };
2391
2390
  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.3",
2
+ "version": "1.8.0-beta.5",
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.3"
38
+ "@tmagic/schema": "1.8.0-beta.5"
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,12 +111,12 @@ 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,
@@ -128,7 +130,7 @@ export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) =>
128
130
  return info;
129
131
  }
130
132
 
131
- const path = getNodePath(id, root.items);
133
+ const path = getNodePath(id, root.items, skip);
132
134
 
133
135
  if (!path.length) return info;
134
136
 
@@ -137,12 +139,12 @@ export const getNodeInfo = (id: Id, root: Pick<MApp, 'id' | 'items'> | null) =>
137
139
  info.node = path[path.length - 1] as MComponent;
138
140
  info.parent = path[path.length - 2] as MContainer;
139
141
 
140
- path.forEach((item) => {
142
+ for (const item of path) {
141
143
  if (isPage(item) || isPageFragment(item)) {
142
144
  info.page = item as MPage | MPageFragment;
143
- return;
145
+ break;
144
146
  }
145
- });
147
+ }
146
148
 
147
149
  return info;
148
150
  };
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
@@ -51,8 +51,11 @@ declare const emptyFn: () => any;
51
51
  * @param {Array} data 要查找的根容器节点
52
52
  * @return {Array} 组件在data中的子孙路径
53
53
  */
54
- declare const getNodePath: (id: Id, data?: MNode[]) => MNode[];
55
- declare const getNodeInfo: (id: Id, root: Pick<MApp, "id" | "items"> | null) => EditorNodeInfo;
54
+ declare const getNodePath: (id: Id, data?: MNode[], skip?: MNode | null) => MNode[];
55
+ declare const getNodeInfo: (id: Id, root: {
56
+ id: Id;
57
+ items?: MNode[];
58
+ } | null, skip?: MNode | null) => EditorNodeInfo;
56
59
  declare const filterXSS: (str: string) => string;
57
60
  declare const getUrlParam: (param: string, url?: string) => string;
58
61
  /**