|
9 | 9 | IPublicEnumDragObjectType, |
10 | 10 | IPublicTypeDragNodeObject, |
11 | 11 | } from '@alilc/lowcode-types'; |
12 | | -import symbols from '../modules/symbols'; |
13 | | - |
14 | | -const { nodeSymbol } = symbols; |
15 | 12 |
|
16 | 13 | function insertChild( |
17 | 14 | container: IPublicModelNode, |
@@ -163,6 +160,67 @@ function getPrevForSelect(prev: IPublicModelNode | null, head?: any, parent?: IP |
163 | 160 | return null; |
164 | 161 | } |
165 | 162 |
|
| 163 | +function getSuitablePlaceForNode(targetNode: IPublicModelNode, node: IPublicModelNode, ref: any): any { |
| 164 | + const { document } = targetNode; |
| 165 | + if (!document) { |
| 166 | + return null; |
| 167 | + } |
| 168 | + |
| 169 | + const dragNodeObject: IPublicTypeDragNodeObject = { |
| 170 | + type: IPublicEnumDragObjectType.Node, |
| 171 | + nodes: [node], |
| 172 | + }; |
| 173 | + |
| 174 | + const focusNode = document?.focusNode; |
| 175 | + // 如果节点是模态框,插入到根节点下 |
| 176 | + if (node?.componentMeta?.isModal) { |
| 177 | + return { container: focusNode, ref }; |
| 178 | + } |
| 179 | + const canDropInFn = document.checkNesting; |
| 180 | + |
| 181 | + if (!ref && focusNode && targetNode.contains(focusNode)) { |
| 182 | + if (canDropInFn(focusNode, dragNodeObject)) { |
| 183 | + return { container: focusNode }; |
| 184 | + } |
| 185 | + |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + if (targetNode.isRootNode && targetNode.children) { |
| 190 | + const dropElement = targetNode.children.filter((c) => { |
| 191 | + if (!c.isContainerNode) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + if (canDropInFn(c, dragNodeObject)) { |
| 195 | + return true; |
| 196 | + } |
| 197 | + return false; |
| 198 | + })[0]; |
| 199 | + |
| 200 | + if (dropElement) { |
| 201 | + return { container: dropElement, ref }; |
| 202 | + } |
| 203 | + |
| 204 | + if (canDropInFn(targetNode, dragNodeObject)) { |
| 205 | + return { container: targetNode, ref }; |
| 206 | + } |
| 207 | + |
| 208 | + return null; |
| 209 | + } |
| 210 | + |
| 211 | + if (targetNode.isContainerNode) { |
| 212 | + if (canDropInFn(targetNode, dragNodeObject)) { |
| 213 | + return { container: targetNode, ref }; |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + if (targetNode.parent) { |
| 218 | + return getSuitablePlaceForNode(targetNode.parent, node, { index: targetNode.index }); |
| 219 | + } |
| 220 | + |
| 221 | + return null; |
| 222 | +} |
| 223 | + |
166 | 224 | // 注册默认的 setters |
167 | 225 | export const builtinHotkey = (ctx: IPublicModelPluginContext) => { |
168 | 226 | return { |
@@ -426,14 +484,14 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => { |
426 | 484 | const silbing = firstNode.prevSibling; |
427 | 485 | if (silbing) { |
428 | 486 | if (silbing.isContainerNode) { |
429 | | - const place = (silbing as any)[nodeSymbol].getSuitablePlace(firstNode, null); |
| 487 | + const place = getSuitablePlaceForNode(silbing, firstNode, null); |
430 | 488 | silbing.insertAfter(firstNode, place.ref, true); |
431 | 489 | } else { |
432 | 490 | parent.insertBefore(firstNode, silbing, true); |
433 | 491 | } |
434 | 492 | firstNode?.select(); |
435 | 493 | } else { |
436 | | - const place = (parent as any)[nodeSymbol].getSuitablePlace(firstNode, null); // upwards |
| 494 | + const place = getSuitablePlaceForNode(parent, firstNode, null); // upwards |
437 | 495 | if (place) { |
438 | 496 | const container = place.container.internalToShellNode(); |
439 | 497 | container.insertBefore(firstNode, place.ref); |
@@ -474,7 +532,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => { |
474 | 532 | } |
475 | 533 | firstNode?.select(); |
476 | 534 | } else { |
477 | | - const place = (parent as any)[nodeSymbol].getSuitablePlace(firstNode, null); // upwards |
| 535 | + const place = getSuitablePlaceForNode(parent, firstNode, null); // upwards |
478 | 536 | if (place) { |
479 | 537 | const container = place.container.internalToShellNode(); |
480 | 538 | container.insertAfter(firstNode, place.ref, true); |
|
0 commit comments