@tmagic/utils 1.8.0-beta.0 → 1.8.0-beta.10

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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.0-beta.0",
2
+ "version": "1.8.0-beta.10",
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.0"
38
+ "@tmagic/schema": "1.8.0-beta.10"
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
  /**