diff --git a/packages/editor/src/Editor.vue b/packages/editor/src/Editor.vue index 9e6800b46..aa5a2bd65 100644 --- a/packages/editor/src/Editor.vue +++ b/packages/editor/src/Editor.vue @@ -25,6 +25,10 @@ + + diff --git a/packages/editor/src/components/Tree.vue b/packages/editor/src/components/Tree.vue index e7f488a03..bfad5f022 100644 --- a/packages/editor/src/components/Tree.vue +++ b/packages/editor/src/components/Tree.vue @@ -6,6 +6,10 @@ + + @@ -28,6 +32,7 @@ import TreeNode from './TreeNode.vue'; defineSlots<{ 'tree-node-content'(props: { data: TreeNodeData }): any; + 'tree-node-label'(props: { data: TreeNodeData }): any; 'tree-node-tool'(props: { data: TreeNodeData }): any; }>(); diff --git a/packages/editor/src/components/TreeNode.vue b/packages/editor/src/components/TreeNode.vue index ce662f04f..95e501526 100644 --- a/packages/editor/src/components/TreeNode.vue +++ b/packages/editor/src/components/TreeNode.vue @@ -25,7 +25,9 @@
-
{{ `${data.name} (${data.id})` }}
+
+ {{ `${data.name} (${data.id})` }} +
@@ -44,6 +46,9 @@ + @@ -63,6 +68,7 @@ import type { LayerNodeStatus, TreeNodeData } from '@editor/type'; import { updateStatus } from '@editor/utils/tree'; defineSlots<{ + 'tree-node-label'(props: { data: TreeNodeData }): any; 'tree-node-tool'(props: { data: TreeNodeData }): any; 'tree-node-content'(props: { data: TreeNodeData }): any; }>(); diff --git a/packages/editor/src/layouts/sidebar/layer/use-filter.ts b/packages/editor/src/hooks/use-filter.ts similarity index 69% rename from packages/editor/src/layouts/sidebar/layer/use-filter.ts rename to packages/editor/src/hooks/use-filter.ts index 60680f81d..e4f0f2e3d 100644 --- a/packages/editor/src/layouts/sidebar/layer/use-filter.ts +++ b/packages/editor/src/hooks/use-filter.ts @@ -1,18 +1,16 @@ -import { computed, type ComputedRef, ref } from 'vue'; +import { type Ref, ref } from 'vue'; -import { Id, MNode } from '@tmagic/schema'; +import type { Id, MNode } from '@tmagic/schema'; -import { LayerNodeStatus, Services } from '@editor/type'; +import type { LayerNodeStatus, TreeNodeData } from '@editor/type'; import { traverseNode } from '@editor/utils'; import { updateStatus } from '@editor/utils/tree'; export const useFilter = ( - services: Services | undefined, - nodeStatusMap: ComputedRef | undefined>, + nodeData: Ref, + nodeStatusMap: Ref | undefined>, filterNodeMethod: (value: string, data: MNode) => boolean, ) => { - const page = computed(() => services?.editorService.get('page')); - // tree方法:对树节点进行筛选时执行的方法 const filterIsMatch = (value: string | string[], data: MNode): boolean => { const string = !Array.isArray(value) ? [value] : value; @@ -25,18 +23,14 @@ export const useFilter = ( }; const filter = (text: string | string[]) => { - if (!page.value?.items?.length) return; + if (!nodeData.value.length) return; - page.value.items.forEach((node) => { + nodeData.value.forEach((node) => { traverseNode(node, (node: MNode, parents: MNode[]) => { if (!nodeStatusMap.value) return; const visible = filterIsMatch(text, node); if (visible && parents.length) { - console.log( - node.id, - parents.map((a) => a.id), - ); parents.forEach((parent) => { updateStatus(nodeStatusMap.value!, parent.id, { visible, diff --git a/packages/editor/src/hooks/use-node-status.ts b/packages/editor/src/hooks/use-node-status.ts new file mode 100644 index 000000000..df9880f49 --- /dev/null +++ b/packages/editor/src/hooks/use-node-status.ts @@ -0,0 +1,48 @@ +import { ComputedRef, ref, watch } from 'vue'; + +import type { Id, MNode } from '@tmagic/schema'; + +import { LayerNodeStatus, TreeNodeData } from '@editor/type'; +import { traverseNode } from '@editor/utils'; + +const createPageNodeStatus = (nodeData: TreeNodeData[], initalLayerNodeStatus?: Map) => { + const map = new Map(); + + nodeData.forEach((node: MNode) => + traverseNode(node, (node) => { + map.set( + node.id, + initalLayerNodeStatus?.get(node.id) || { + visible: true, + expand: false, + selected: false, + draggable: false, + }, + ); + }), + ); + + return map; +}; + +export const useNodeStatus = (nodeData: ComputedRef) => { + /** 所有页面的节点状态 */ + const nodeStatusMap = ref(new Map()); + + // 切换页面或者新增页面,重新生成节点状态 + watch( + nodeData, + (nodeData) => { + // 生成节点状态 + nodeStatusMap.value = createPageNodeStatus(nodeData, nodeStatusMap.value); + }, + { + immediate: true, + deep: true, + }, + ); + + return { + nodeStatusMap, + }; +}; diff --git a/packages/editor/src/layouts/sidebar/Sidebar.vue b/packages/editor/src/layouts/sidebar/Sidebar.vue index 812c15696..cefec9f0d 100644 --- a/packages/editor/src/layouts/sidebar/Sidebar.vue +++ b/packages/editor/src/layouts/sidebar/Sidebar.vue @@ -74,6 +74,11 @@ + +