Skip to content

Commit

Permalink
refactor(editor,utils): 将traverseNode方法挪到utils中
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Sep 19, 2024
1 parent 0d1e28b commit eae4892
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/editor/src/fields/EventSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ import { ActionType } from '@tmagic/core';
import { TMagicButton } from '@tmagic/design';
import type { CascaderOption, ChildConfig, FieldProps, FormState, PanelConfig } from '@tmagic/form';
import { MContainer as MFormContainer, MPanel } from '@tmagic/form';
import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX } from '@tmagic/utils';
import { DATA_SOURCE_FIELDS_CHANGE_EVENT_PREFIX, traverseNode } from '@tmagic/utils';
import type { CodeSelectColConfig, DataSourceMethodSelectConfig, EventSelectConfig, Services } from '@editor/type';
import { getCascaderOptionsFromFields, traverseNode } from '@editor/utils';
import { getCascaderOptionsFromFields } from '@editor/utils';
defineOptions({
name: 'MFieldsEventSelect',
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/hooks/use-filter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type Ref, ref } from 'vue';

import type { Id, MNode } from '@tmagic/core';
import { traverseNode } from '@tmagic/utils';

import type { LayerNodeStatus, TreeNodeData } from '@editor/type';
import { traverseNode } from '@editor/utils';
import { updateStatus } from '@editor/utils/tree';

export const useFilter = (
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/hooks/use-node-status.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ComputedRef, ref, watch } from 'vue';
import { type ComputedRef, ref, watch } from 'vue';

import type { Id, MNode } from '@tmagic/core';
import { traverseNode } from '@tmagic/utils';

import { LayerNodeStatus, TreeNodeData } from '@editor/type';
import { traverseNode } from '@editor/utils';

const createPageNodeStatus = (nodeData: TreeNodeData[], initialLayerNodeStatus?: Map<Id, LayerNodeStatus>) => {
const map = new Map<Id, LayerNodeStatus>();
Expand Down
9 changes: 4 additions & 5 deletions packages/editor/src/initService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { onBeforeUnmount, reactive, toRaw, watch } from 'vue';
import { cloneDeep } from 'lodash-es';
import { cloneDeep, debounce } from 'lodash-es';

import type {
CodeBlockContent,
Expand All @@ -19,12 +19,11 @@ import {
DepTargetType,
Target,
} from '@tmagic/core';
import { isPage } from '@tmagic/utils';
import { isPage, traverseNode } from '@tmagic/utils';

import PropsPanel from './layouts/PropsPanel.vue';
import { EditorProps } from './editorProps';
import { Services } from './type';
import { traverseNode } from './utils';

export declare type LooseRequired<T> = {
[P in string & keyof T]: T[P];
Expand Down Expand Up @@ -292,7 +291,7 @@ export const initServiceEvents = (
}
};

const collectedHandler = (nodes: MNode[], deep: boolean) => {
const collectedHandler = debounce((nodes: MNode[], deep: boolean) => {
const root = editorService.get('root');
const stage = editorService.get('stage');

Expand Down Expand Up @@ -336,7 +335,7 @@ export const initServiceEvents = (
});
});
});
};
}, 300);

depService.on('add-target', targetAddHandler);
depService.on('remove-target', targetRemoveHandler);
Expand Down
5 changes: 2 additions & 3 deletions packages/editor/src/layouts/sidebar/layer/use-node-status.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { computed, ref, watch } from 'vue';

import type { Id, MNode, MPage, MPageFragment } from '@tmagic/core';
import { getNodePath, isPage, isPageFragment } from '@tmagic/utils';
import { getNodePath, isPage, isPageFragment, traverseNode } from '@tmagic/utils';

import { LayerNodeStatus, Services } from '@editor/type';
import { traverseNode } from '@editor/utils';
import type { LayerNodeStatus, Services } from '@editor/type';
import { updateStatus } from '@editor/utils/tree';

const createPageNodeStatus = (page: MPage | MPageFragment, initialLayerNodeStatus?: Map<Id, LayerNodeStatus>) => {
Expand Down
20 changes: 0 additions & 20 deletions packages/editor/src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,26 +272,6 @@ export const serializeConfig = (config: any) =>
unsafe: true,
}).replace(/"(\w+)":\s/g, '$1: ');

export interface NodeItem {
items?: NodeItem[];
[key: string]: any;
}

export const traverseNode = <T extends NodeItem = NodeItem>(
node: T,
cb: (node: T, parents: T[]) => void,
parents: T[] = [],
) => {
cb(node, parents);

if (Array.isArray(node.items) && node.items.length) {
parents.push(node);
node.items.forEach((item) => {
traverseNode(item as T, cb, [...parents]);
});
}
};

export const moveItemsInContainer = (sourceIndices: number[], parent: MContainer, targetIndex: number) => {
sourceIndices.sort((a, b) => a - b);
for (let i = sourceIndices.length - 1; i >= 0; i--) {
Expand Down
20 changes: 20 additions & 0 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,23 @@ export const dataSourceTemplateRegExp = /\$\{([\s\S]+?)\}/g;

export const isDslNode = (config: MNodeInstance) =>
typeof config[IS_DSL_NODE_KEY] === 'undefined' || config[IS_DSL_NODE_KEY] === true;

export interface NodeItem {
items?: NodeItem[];
[key: string]: any;
}

export const traverseNode = <T extends NodeItem = NodeItem>(
node: T,
cb: (node: T, parents: T[]) => void,
parents: T[] = [],
) => {
cb(node, parents);

if (Array.isArray(node.items) && node.items.length) {
parents.push(node);
node.items.forEach((item) => {
traverseNode(item as T, cb, [...parents]);
});
}
};

0 comments on commit eae4892

Please sign in to comment.