Skip to content

Commit 0b0015c

Browse files
JackLianliujuping
authored andcommitted
refactor: remove dependecy of designer.focusing for refactoring of hotkey plugin as a standalone one
1 parent 8c7f57a commit 0b0015c

File tree

5 files changed

+63
-28
lines changed

5 files changed

+63
-28
lines changed

packages/designer/src/designer/designer.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import { ActiveTracker, IActiveTracker } from './active-tracker';
3030
import { Detecting } from './detecting';
3131
import { DropLocation } from './location';
3232
import { OffsetObserver, createOffsetObserver } from './offset-observer';
33-
import { focusing } from './focusing';
3433
import { SettingTopEntry } from './setting';
3534
import { BemToolsManager } from '../builtin-simulator/bem-tools/manager';
3635
import { ComponentActions } from '../component-actions';
@@ -241,9 +240,6 @@ export class Designer implements IDesigner {
241240
this.postEvent('init', this);
242241
this.setupSelection();
243242
setupHistory();
244-
245-
// TODO: 先简单实现,后期通过焦点赋值
246-
focusing.focusDesigner = this;
247243
}
248244

249245
setupSelection = () => {
@@ -341,6 +337,7 @@ export class Designer implements IDesigner {
341337

342338
/**
343339
* 获得合适的插入位置
340+
* @deprecated
344341
*/
345342
getSuitableInsertion(
346343
insertNode?: INode | IPublicTypeNodeSchema | IPublicTypeNodeSchema[],

packages/designer/src/designer/focusing.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/designer/src/designer/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ export * from './offset-observer';
77
export * from './scroller';
88
export * from './setting';
99
export * from './active-tracker';
10-
export * from './focusing';
1110
export * from '../document';
1211
export * from './clipboard';

packages/designer/src/document/node/node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,11 +1396,11 @@ export function comparePosition(node1: Node, node2: Node): PositionNO {
13961396

13971397
export function insertChild(
13981398
container: INode,
1399-
thing: Node | IPublicTypeNodeData,
1399+
thing: INode | IPublicTypeNodeData,
14001400
at?: number | null,
14011401
copy?: boolean,
1402-
): Node {
1403-
let node: Node;
1402+
): INode {
1403+
let node: INode;
14041404
if (isNode(thing) && (copy || thing.isSlot())) {
14051405
thing = thing.export(IPublicEnumTransformStage.Clone);
14061406
}
@@ -1410,20 +1410,20 @@ export function insertChild(
14101410
node = container.document.createNode(thing);
14111411
}
14121412

1413-
container.children.internalInsert(node, at);
1413+
container.children.insert(node, at);
14141414

14151415
return node;
14161416
}
14171417

14181418
export function insertChildren(
14191419
container: INode,
1420-
nodes: Node[] | IPublicTypeNodeData[],
1420+
nodes: INode[] | IPublicTypeNodeData[],
14211421
at?: number | null,
14221422
copy?: boolean,
1423-
): Node[] {
1423+
): INode[] {
14241424
let index = at;
14251425
let node: any;
1426-
const results: Node[] = [];
1426+
const results: INode[] = [];
14271427
// eslint-disable-next-line no-cond-assign
14281428
while ((node = nodes.pop())) {
14291429
node = insertChild(container, node, index, copy);

packages/engine/src/inner-plugins/builtin-hotkey.ts

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,67 @@
11
/* eslint-disable max-len */
2-
import { isFormEvent } from '@alilc/lowcode-utils';
2+
import { isFormEvent, isNodeSchema } from '@alilc/lowcode-utils';
33
import {
4-
focusing,
54
insertChildren,
65
clipboard,
76
} from '@alilc/lowcode-designer';
87
import {
98
IPublicModelPluginContext,
109
IPublicEnumTransformStage,
1110
IPublicModelNode,
11+
IPublicTypeNodeSchema,
1212
} from '@alilc/lowcode-types';
1313
import symbols from '../modules/symbols';
1414

1515
const { nodeSymbol, documentSymbol } = symbols;
1616

17+
/**
18+
* 获得合适的插入位置
19+
*/
20+
function getSuitableInsertion(
21+
pluginContext: IPublicModelPluginContext,
22+
insertNode?: IPublicModelNode | IPublicTypeNodeSchema | IPublicTypeNodeSchema[],
23+
): { target: IPublicModelNode; index?: number } | null {
24+
const { project, material } = pluginContext;
25+
const activeDoc = project.currentDocument;
26+
if (!activeDoc) {
27+
return null;
28+
}
29+
if (
30+
Array.isArray(insertNode) &&
31+
isNodeSchema(insertNode[0]) &&
32+
material.getComponentMeta(insertNode[0].componentName)?.isModal
33+
) {
34+
if (!activeDoc.root) {
35+
return null;
36+
}
37+
38+
return {
39+
target: activeDoc.root,
40+
};
41+
}
42+
43+
const focusNode = activeDoc.focusNode!;
44+
const nodes = activeDoc.selection.getNodes();
45+
const refNode = nodes.find((item) => focusNode.contains(item));
46+
let target;
47+
let index: number | undefined;
48+
if (!refNode || refNode === focusNode) {
49+
target = focusNode;
50+
} else if (refNode.componentMeta?.isContainer) {
51+
target = refNode;
52+
} else {
53+
// FIXME!!, parent maybe null
54+
target = refNode.parent!;
55+
index = refNode.index + 1;
56+
}
57+
58+
if (target && insertNode && !target.componentMeta?.checkNestingDown(target, insertNode)) {
59+
return null;
60+
}
61+
62+
return { target, index };
63+
}
64+
1765
/* istanbul ignore next */
1866
function getNextForSelect(next: IPublicModelNode | null, head?: any, parent?: IPublicModelNode | null): any {
1967
if (next) {
@@ -108,11 +156,11 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
108156

109157
hotkey.bind('escape', (e: KeyboardEvent, action) => {
110158
logger.info(`action ${action} is triggered`);
111-
// const currentFocus = focusing.current;
159+
112160
if (canvas.isInLiveEditing) {
113161
return;
114162
}
115-
const sel = focusing.focusDesigner?.currentDocument?.selection;
163+
const sel = project.currentDocument?.selection;
116164
if (isFormEvent(e) || !sel) {
117165
return;
118166
}
@@ -168,15 +216,14 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
168216
return;
169217
}
170218
// TODO
171-
const designer = focusing.focusDesigner;
172219
const doc = project?.currentDocument;
173-
if (isFormEvent(e) || !designer || !doc) {
220+
if (isFormEvent(e) || !doc) {
174221
return;
175222
}
176223
/* istanbul ignore next */
177224
clipboard.waitPasteData(e, ({ componentsTree }) => {
178225
if (componentsTree) {
179-
const { target, index } = designer.getSuitableInsertion(componentsTree) || {};
226+
const { target, index } = getSuitableInsertion(ctx, componentsTree) || {};
180227
if (!target) {
181228
return;
182229
}
@@ -187,7 +234,7 @@ export const builtinHotkey = (ctx: IPublicModelPluginContext) => {
187234
const nodes = insertChildren(target, canAddComponentsTree, index);
188235
if (nodes) {
189236
doc.selection.selectAll(nodes.map((o) => o.id));
190-
setTimeout(() => designer.activeTracker.track(nodes[0]), 10);
237+
setTimeout(() => canvas.activeTracker?.track(nodes[0]), 10);
191238
}
192239
}
193240
});

0 commit comments

Comments
 (0)