Skip to content

Commit cd191f6

Browse files
parismaroymondchen
parisma
authored and
roymondchen
committed
feat(editor): 复制组件时收集依赖性能优化改造
1 parent 50d238a commit cd191f6

File tree

8 files changed

+28
-47
lines changed

8 files changed

+28
-47
lines changed

packages/dep/src/Watcher.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ export default class Watcher {
114114
* @param deep 是否需要收集子节点
115115
* @param type 强制收集指定类型的依赖
116116
*/
117-
public collect(nodes: TargetNode[], depExtendedData: DepExtendedData = {}, deep = false, type?: DepTargetType) {
117+
public collect(
118+
nodes: TargetNode[],
119+
depExtendedData: DepExtendedData = {},
120+
deep = false,
121+
type?: DepTargetType | string,
122+
) {
118123
this.collectByCallback(nodes, type, ({ node, target }) => {
119124
this.removeTargetDep(target, node);
120125
this.collectItem(node, target, depExtendedData, deep);
@@ -123,7 +128,7 @@ export default class Watcher {
123128

124129
public collectByCallback(
125130
nodes: TargetNode[],
126-
type: DepTargetType | undefined,
131+
type: DepTargetType | string | undefined,
127132
cb: (data: { node: TargetNode; target: Target }) => void,
128133
) {
129134
traverseTarget(
@@ -144,7 +149,7 @@ export default class Watcher {
144149
* 清除所有目标的依赖
145150
* @param nodes 需要清除依赖的节点
146151
*/
147-
public clear(nodes?: TargetNode[], type?: DepTargetType) {
152+
public clear(nodes?: TargetNode[], type?: DepTargetType | string) {
148153
let { targetsList } = this;
149154

150155
if (type) {
@@ -179,7 +184,7 @@ export default class Watcher {
179184
* @param type 类型
180185
* @param nodes 需要清除依赖的节点
181186
*/
182-
public clearByType(type: DepTargetType, nodes?: TargetNode[]) {
187+
public clearByType(type: DepTargetType | string, nodes?: TargetNode[]) {
183188
this.clear(nodes, type);
184189
}
185190

packages/dep/src/types.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,6 @@ export interface TargetOptions {
2828
isCollectByDefault?: boolean;
2929
}
3030

31-
export interface CustomTargetOptions {
32-
isTarget: IsTarget;
33-
name?: string;
34-
initialDeps?: DepData;
35-
/** 是否默认收集,默认为true,当值为false时需要传入type参数给collect方法才会被收集 */
36-
isCollectByDefault?: boolean;
37-
}
38-
3931
export interface TargetList {
4032
[type: string]: {
4133
[targetId: string | number]: Target;

packages/dep/src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,11 @@ export const createDataSourceMethodTarget = (ds: DataSourceSchema, initialDeps:
193193
},
194194
});
195195

196-
export const traverseTarget = (targetsList: TargetList, cb: (target: Target) => void, type?: DepTargetType) => {
196+
export const traverseTarget = (
197+
targetsList: TargetList,
198+
cb: (target: Target) => void,
199+
type?: DepTargetType | string,
200+
) => {
197201
Object.values(targetsList).forEach((targets) => {
198202
Object.values(targets).forEach((target) => {
199203
if (type && target.type !== type) {

packages/editor/src/services/codeBlock.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { reactive } from 'vue';
2020
import { cloneDeep, get, keys, pick } from 'lodash-es';
2121
import type { Writable } from 'type-fest';
2222

23-
import { type CustomTargetOptions, Target, Watcher } from '@tmagic/dep';
23+
import { Target, type TargetOptions, Watcher } from '@tmagic/dep';
2424
import type { ColumnConfig } from '@tmagic/form';
2525
import type { CodeBlockContent, CodeBlockDSL, Id, MNode } from '@tmagic/schema';
2626

@@ -257,25 +257,20 @@ class CodeBlock extends BaseService {
257257
* @param config 组件节点配置
258258
* @returns
259259
*/
260-
public copyWithRelated(config: MNode | MNode[], collectorOptions?: CustomTargetOptions): void {
260+
public copyWithRelated(config: MNode | MNode[], collectorOptions?: TargetOptions): void {
261261
const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
262262
const copyData: CodeBlockDSL = {};
263263

264264
if (collectorOptions && typeof collectorOptions.isTarget === 'function') {
265265
const customTarget = new Target({
266-
id: 'related-code-when-copy',
267266
...collectorOptions,
268267
});
269268

270269
const coperWatcher = new Watcher();
271270

272271
coperWatcher.addTarget(customTarget);
273272

274-
coperWatcher.collect(
275-
copyNodes.map((node) => ({ id: `${node.id}`, name: `${node.name || node.id}` })),
276-
{},
277-
true,
278-
);
273+
coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
279274

280275
Object.keys(customTarget.deps).forEach((nodeId: Id) => {
281276
const node = editorService.getNodeById(nodeId);

packages/editor/src/services/dataSource.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { cloneDeep, get } from 'lodash-es';
33
import { Writable } from 'type-fest';
44

55
import type { EventOption } from '@tmagic/core';
6-
import { type CustomTargetOptions, Target, Watcher } from '@tmagic/dep';
6+
import { Target, type TargetOptions, Watcher } from '@tmagic/dep';
77
import type { FormConfig } from '@tmagic/form';
88
import type { DataSourceSchema, Id, MNode } from '@tmagic/schema';
99
import { guid, toLine } from '@tmagic/utils';
@@ -162,25 +162,20 @@ class DataSource extends BaseService {
162162
* @param config 组件节点配置
163163
* @returns
164164
*/
165-
public copyWithRelated(config: MNode | MNode[], collectorOptions?: CustomTargetOptions): void {
165+
public copyWithRelated(config: MNode | MNode[], collectorOptions?: TargetOptions): void {
166166
const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
167167
const copyData: DataSourceSchema[] = [];
168168

169169
if (collectorOptions && typeof collectorOptions.isTarget === 'function') {
170170
const customTarget = new Target({
171-
id: 'related-ds-when-copy',
172171
...collectorOptions,
173172
});
174173

175174
const coperWatcher = new Watcher();
176175

177176
coperWatcher.addTarget(customTarget);
178177

179-
coperWatcher.collect(
180-
copyNodes.map((node) => ({ id: `${node.id}`, name: `${node.name || node.id}` })),
181-
{},
182-
true,
183-
);
178+
coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
184179

185180
Object.keys(customTarget.deps).forEach((nodeId: Id) => {
186181
const node = editorService.getNodeById(nodeId);

packages/editor/src/services/editor.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { reactive, toRaw } from 'vue';
2020
import { cloneDeep, get, isObject, mergeWith, uniq } from 'lodash-es';
2121
import { Writable } from 'type-fest';
2222

23-
import { type CustomTargetOptions, Target, Watcher } from '@tmagic/dep';
23+
import { Target, type TargetOptions, Watcher } from '@tmagic/dep';
2424
import type { Id, MApp, MComponent, MContainer, MNode, MPage, MPageFragment } from '@tmagic/schema';
2525
import { NodeType } from '@tmagic/schema';
2626
import { calcValueByFontsize, getNodePath, isNumber, isPage, isPageFragment, isPop } from '@tmagic/utils';
@@ -660,26 +660,20 @@ class Editor extends BaseService {
660660
* @param config 组件节点配置
661661
* @returns
662662
*/
663-
public copyWithRelated(config: MNode | MNode[], collectorOptions?: CustomTargetOptions): void {
663+
public copyWithRelated(config: MNode | MNode[], collectorOptions?: TargetOptions): void {
664664
const copyNodes: MNode[] = Array.isArray(config) ? config : [config];
665665

666666
// 初始化复制组件相关的依赖收集器
667667
if (collectorOptions && typeof collectorOptions.isTarget === 'function') {
668668
const customTarget = new Target({
669-
id: 'related-comp-when-copy',
670669
...collectorOptions,
671670
});
672671

673672
const coperWatcher = new Watcher();
674673

675674
coperWatcher.addTarget(customTarget);
676675

677-
coperWatcher.collect(
678-
copyNodes.map((node) => ({ id: `${node.id}`, name: `${node.name || node.id}` })),
679-
{},
680-
true,
681-
);
682-
676+
coperWatcher.collect(copyNodes, {}, true, collectorOptions.type);
683677
Object.keys(customTarget.deps).forEach((nodeId: Id) => {
684678
const node = this.getNodeById(nodeId);
685679
if (!node) return;
@@ -706,10 +700,7 @@ class Editor extends BaseService {
706700
* @param position 粘贴的坐标
707701
* @returns 添加后的组件节点配置
708702
*/
709-
public async paste(
710-
position: PastePosition = {},
711-
collectorOptions?: CustomTargetOptions,
712-
): Promise<MNode | MNode[] | void> {
703+
public async paste(position: PastePosition = {}, collectorOptions?: TargetOptions): Promise<MNode | MNode[] | void> {
713704
const config: MNode[] = storageService.getItem(COPY_STORAGE_KEY);
714705
if (!Array.isArray(config)) return;
715706

packages/editor/src/services/props.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { reactive } from 'vue';
2020
import { cloneDeep, mergeWith } from 'lodash-es';
2121
import { Writable } from 'type-fest';
2222

23-
import { type CustomTargetOptions, Target, Watcher } from '@tmagic/dep';
23+
import { Target, type TargetOptions, Watcher } from '@tmagic/dep';
2424
import type { FormConfig } from '@tmagic/form';
2525
import type { Id, MComponent, MNode } from '@tmagic/schema';
2626
import { getNodePath, getValueByKeyPath, guid, setValueByKeyPath, toLine } from '@tmagic/utils';
@@ -195,20 +195,19 @@ class Props extends BaseService {
195195
* @param originConfigs 原组件配置
196196
* @param targetConfigs 待替换的组件配置
197197
*/
198-
public replaceRelateId(originConfigs: MNode[], targetConfigs: MNode[], collectorOptions: CustomTargetOptions) {
198+
public replaceRelateId(originConfigs: MNode[], targetConfigs: MNode[], collectorOptions: TargetOptions) {
199199
const relateIdMap = this.getRelateIdMap();
200200

201201
if (Object.keys(relateIdMap).length === 0) return;
202202

203203
const target = new Target({
204-
id: 'related-comp-when-copy',
205204
...collectorOptions,
206205
});
207206

208207
const coperWatcher = new Watcher();
209208

210209
coperWatcher.addTarget(target);
211-
coperWatcher.collect(originConfigs);
210+
coperWatcher.collect(originConfigs, {}, true, collectorOptions.type);
212211

213212
originConfigs.forEach((config: MNode) => {
214213
const newId = relateIdMap[config.id];

playground/src/pages/Editor.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const collectorOptions = {
104104
name: '蒙层',
105105
isTarget: (key: string | number, value: any) =>
106106
typeof key === 'string' && typeof value === 'string' && key.includes('events') && value.startsWith('overlay_'),
107-
autoCollect: false,
107+
isCollectByDefault: false,
108108
};
109109
110110
const usePasteMenu = (menu?: Ref<InstanceType<typeof ContentMenu> | undefined>): MenuButton => ({

0 commit comments

Comments
 (0)