refactor: remove dependency of nodeSymbol

This commit is contained in:
JackLian 2023-01-29 17:35:24 +08:00 committed by 刘菊萍(絮黎)
parent 1b74e71353
commit 67a5e724fd
3 changed files with 67 additions and 9 deletions

View File

@ -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**
**@since v1.1.0**

View File

@ -1149,7 +1149,7 @@ export class Node<Schema extends IPublicTypeNodeSchema = IPublicTypeNodeSchema>
}
/**
* 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;

View File

@ -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);