From 67a5e724fd2934f6773456542cf9dbc40081d12a Mon Sep 17 00:00:00 2001 From: JackLian Date: Sun, 29 Jan 2023 17:35:24 +0800 Subject: [PATCH] refactor: remove dependency of nodeSymbol --- docs/docs/api/model/node.md | 4 +- packages/designer/src/document/node/node.ts | 2 +- .../src/inner-plugins/builtin-hotkey.ts | 70 +++++++++++++++++-- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/docs/docs/api/model/node.md b/docs/docs/api/model/node.md index fbc88034b..333e973f0 100644 --- a/docs/docs/api/model/node.md +++ b/docs/docs/api/model/node.md @@ -160,7 +160,7 @@ sidebar_position: 1 `@type {IPublicModelComponentMeta | null}` -相关类型:[IPublicTypeIconType](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/component-meta.ts) +相关类型:[IPublicModelComponentMeta](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/component-meta.ts) ### document @@ -644,4 +644,4 @@ isConditionalVisible(): boolean | undefined; setConditionalVisible(): void; ``` -**@since v1.1.0** \ No newline at end of file +**@since v1.1.0** diff --git a/packages/designer/src/document/node/node.ts b/packages/designer/src/document/node/node.ts index f52d7e7bf..1d294663b 100644 --- a/packages/designer/src/document/node/node.ts +++ b/packages/designer/src/document/node/node.ts @@ -1149,7 +1149,7 @@ export class Node } /** - * TODO: replace non standard metas with standard ones. + * @deprecated no one is using this, will be removed in a future release */ getSuitablePlace(node: INode, ref: any): any { const focusNode = this.document?.focusNode; diff --git a/packages/engine/src/inner-plugins/builtin-hotkey.ts b/packages/engine/src/inner-plugins/builtin-hotkey.ts index b6d3a0950..73aac64aa 100644 --- a/packages/engine/src/inner-plugins/builtin-hotkey.ts +++ b/packages/engine/src/inner-plugins/builtin-hotkey.ts @@ -9,9 +9,6 @@ import { IPublicEnumDragObjectType, IPublicTypeDragNodeObject, } from '@alilc/lowcode-types'; -import symbols from '../modules/symbols'; - -const { nodeSymbol } = symbols; function insertChild( container: IPublicModelNode, @@ -163,6 +160,67 @@ function getPrevForSelect(prev: IPublicModelNode | null, head?: any, parent?: IP return null; } +function getSuitablePlaceForNode(targetNode: IPublicModelNode, node: IPublicModelNode, ref: any): any { + const { document } = targetNode; + if (!document) { + return null; + } + + const dragNodeObject: IPublicTypeDragNodeObject = { + type: IPublicEnumDragObjectType.Node, + nodes: [node], + }; + + const focusNode = document?.focusNode; + // 如果节点是模态框,插入到根节点下 + if (node?.componentMeta?.isModal) { + return { container: focusNode, ref }; + } + const canDropInFn = document.checkNesting; + + if (!ref && focusNode && targetNode.contains(focusNode)) { + if (canDropInFn(focusNode, dragNodeObject)) { + return { container: focusNode }; + } + + return null; + } + + if (targetNode.isRootNode && targetNode.children) { + const dropElement = targetNode.children.filter((c) => { + if (!c.isContainerNode) { + return false; + } + if (canDropInFn(c, dragNodeObject)) { + return true; + } + return false; + })[0]; + + if (dropElement) { + return { container: dropElement, ref }; + } + + if (canDropInFn(targetNode, dragNodeObject)) { + return { container: targetNode, ref }; + } + + return null; + } + + if (targetNode.isContainerNode) { + if (canDropInFn(targetNode, dragNodeObject)) { + return { container: targetNode, ref }; + } + } + + if (targetNode.parent) { + return getSuitablePlaceForNode(targetNode.parent, node, { index: targetNode.index }); + } + + return null; +} + // 注册默认的 setters export const builtinHotkey = (ctx: IPublicModelPluginContext) => { return { @@ -426,14 +484,14 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => { const silbing = firstNode.prevSibling; if (silbing) { if (silbing.isContainerNode) { - const place = (silbing as any)[nodeSymbol].getSuitablePlace(firstNode, null); + const place = getSuitablePlaceForNode(silbing, firstNode, null); silbing.insertAfter(firstNode, place.ref, true); } else { parent.insertBefore(firstNode, silbing, true); } firstNode?.select(); } else { - const place = (parent as any)[nodeSymbol].getSuitablePlace(firstNode, null); // upwards + const place = getSuitablePlaceForNode(parent, firstNode, null); // upwards if (place) { const container = place.container.internalToShellNode(); container.insertBefore(firstNode, place.ref); @@ -474,7 +532,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => { } firstNode?.select(); } else { - const place = (parent as any)[nodeSymbol].getSuitablePlace(firstNode, null); // upwards + const place = getSuitablePlaceForNode(parent, firstNode, null); // upwards if (place) { const container = place.container.internalToShellNode(); container.insertAfter(firstNode, place.ref, true);