Skip to content

Commit bcdec09

Browse files
JackLianliujuping
authored andcommitted
refactor: use callbacks to replace events
1 parent 840e70e commit bcdec09

38 files changed

+586
-528
lines changed

docs/docs/api/model/document-model.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ sidebar_position: 0
4242

4343
参见 [模态节点管理](./modal-nodes-manager)
4444

45+
### dropLocation
46+
文档的 dropLocation
47+
相关类型:[IPublicModelDropLocation](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/drop-location.ts)
48+
49+
**@since v1.1.0**
50+
4551
## 方法签名
4652
### getNodeById
4753

@@ -90,6 +96,24 @@ removeNode(idOrNode: string | Node)
9096
function checkNesting(dropTarget: Node, dragObject: DragNodeObject | DragNodeDataObject): boolean {}
9197
```
9298

99+
### isDetectingNode
100+
检查拖拽放置的目标节点是否可以放置该拖拽对象
101+
102+
```typescript
103+
/**
104+
* 判断是否当前节点处于被探测状态
105+
* check is node being detected
106+
* @param node
107+
* @since v1.1.0
108+
*/
109+
isDetectingNode(node: IPublicModelNode): boolean;
110+
```
111+
相关类型:[IPublicModelNode](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/node.ts)
112+
113+
114+
**@since v1.1.0**
115+
116+
93117
## 事件
94118
### onAddNode
95119

@@ -144,7 +168,53 @@ onChangeNodeProp(fn: (info: IPublicTypePropChangeOptions) => void)
144168

145169
### onImportSchema
146170
当前 document 导入新的 schema 事件
147-
版本 >= 1.0.15
148171
```typescript
149-
onImportSchema(fn: (schema: any) => void)
150-
```
172+
/**
173+
* import schema event
174+
* @param fn
175+
* @since v1.0.15
176+
*/
177+
onImportSchema(fn: (schema: IPublicTypeRootSchema) => void): IPublicTypeDisposable;
178+
```
179+
相关类型:
180+
- [IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
181+
- [IPublicTypeRootSchema](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/root-schema.ts)
182+
183+
**@since v1.0.15**
184+
185+
### onFocusNodeChanged
186+
设置聚焦节点变化的回调
187+
188+
```typescript
189+
/**
190+
* 设置聚焦节点变化的回调
191+
* triggered focused node is set mannually from plugin
192+
* @param fn
193+
* @since v1.1.0
194+
*/
195+
onFocusNodeChanged(
196+
fn: (doc: IPublicModelDocumentModel, focusNode: IPublicModelNode) => void,
197+
): IPublicTypeDisposable;
198+
```
199+
相关类型:
200+
- [IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
201+
- [IPublicModelNode](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/node.ts)
202+
203+
**@since v1.1.0**
204+
205+
### onDropLocationChanged
206+
设置 DropLocation 变化的回调
207+
208+
```typescript
209+
/**
210+
* 设置 DropLocation 变化的回调
211+
* triggered when drop location changed
212+
* @param fn
213+
* @since v1.1.0
214+
*/
215+
onDropLocationChanged(fn: (doc: IPublicModelDocumentModel) => void): IPublicTypeDisposable;
216+
```
217+
218+
相关类型:[IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts)
219+
220+
**@since v1.1.0**

packages/designer/src/designer/active-tracker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import {
77
import { isNode } from '@alilc/lowcode-utils';
88

99
export interface IActiveTracker extends IPublicModelActiveTracker {
10-
10+
track(originalTarget: IPublicTypeActiveTarget | INode): void;
1111
}
12+
1213
export class ActiveTracker implements IActiveTracker {
1314
private emitter: IEventBus = createModuleEventBus('ActiveTracker');
1415

packages/designer/src/designer/location.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,11 @@ export interface IDropLocation extends IPublicModelDropLocation {
103103

104104
readonly target: INode;
105105

106-
readonly detail: IPublicTypeLocationDetail;
107-
108106
readonly event: ILocateEvent;
109107

110108
readonly source: string;
111109

112110
get document(): IPublicModelDocumentModel;
113-
114-
clone(event: ILocateEvent): IDropLocation;
115111
}
116112

117113
export class DropLocation implements IDropLocation {

packages/designer/src/document/document-model.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
IPublicModelNode,
1515
IPublicApiProject,
1616
IPublicModelDropLocation,
17-
IPublicEnumEventNames,
1817
IPublicEnumTransformStage,
1918
} from '@alilc/lowcode-types';
2019
import { Project } from '../project';
@@ -326,7 +325,7 @@ export class DocumentModel implements IDocumentModel {
326325
this._dropLocation = loc;
327326
// pub event
328327
this.designer.editor.eventBus.emit(
329-
IPublicEnumEventNames.DOCUMENT_DROPLOCATION_CHANGED,
328+
'document.dropLocation.changed',
330329
{ document: this, location: loc },
331330
);
332331
}

packages/editor-skeleton/src/skeleton.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import {
2626
PluginClassSet,
2727
IPublicTypeWidgetBaseConfig,
2828
IPublicTypeWidgetConfigArea,
29-
IPublicEnumEventNames,
3029
} from '@alilc/lowcode-types';
3130

3231
const logger = new Logger({ level: 'warn', bizName: 'skeleton' });
@@ -174,7 +173,7 @@ export class Skeleton {
174173
*/
175174
setupEvents() {
176175
// adjust pinned status when panel shown
177-
this.editor.eventBus.on(IPublicEnumEventNames.SKELETON_PANEL_SHOW, (panelName, panel) => {
176+
this.editor.eventBus.on(SkeletonEvents.PANEL_SHOW, (panelName, panel) => {
178177
const panelNameKey = `${panelName}-pinned-status-isFloat`;
179178
const isInFloatAreaPreferenceExists = engineConfig.getPreference()?.contains(panelNameKey, 'skeleton');
180179
if (isInFloatAreaPreferenceExists) {

packages/engine/src/engine-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export async function init(
171171
engineConfig.setEngineOptions(engineOptions as any);
172172

173173
// 注册一批内置插件
174-
await plugins.register(OutlinePlugin);
174+
await plugins.register(OutlinePlugin, {}, { autoInit: true });
175175
await plugins.register(componentMetaParser(designer));
176176
await plugins.register(setterRegistry, {}, { autoInit: true });
177177
await plugins.register(defaultPanelRegistry(editor, designer));

packages/plugin-outline-pane/src/controllers/pane-controller.ts

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
isLocationChildrenDetail,
88
} from '@alilc/lowcode-utils';
99
import {
10-
DragObject,
10+
IPublicModelDragObject,
1111
IPublicModelScrollable,
1212
ISensor,
1313
IPublicTypeLocationChildrenDetail,
@@ -50,7 +50,6 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
5050
setup();
5151
}
5252

53-
5453
/** -------------------- ISensor begin -------------------- */
5554

5655
private indentTrack = new IndentTrack();
@@ -107,12 +106,12 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
107106
const document = project.getCurrentDocument();
108107
const pos = getPosFromEvent(e, this._shell);
109108
const irect = this.getInsertionRect();
110-
const originLoc = document.dropLocation;
109+
const originLoc = document?.dropLocation;
111110

112-
const componentMeta = e.dragObject.nodes ? e.dragObject.nodes[0].componentMeta : null;
113-
if (e.dragObject.type === 'node' && componentMeta && componentMeta.isModal) {
111+
const componentMeta = e.dragObject?.nodes ? e.dragObject.nodes[0].componentMeta : null;
112+
if (e.dragObject?.type === 'node' && componentMeta && componentMeta.isModal && document?.focusNode) {
114113
return canvas.createLocation({
115-
target: document.focusNode,
114+
target: document?.focusNode,
116115
detail: {
117116
type: IPublicTypeLocationDetailType.Children,
118117
index: 0,
@@ -123,7 +122,9 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
123122
});
124123
}
125124

126-
if (originLoc && ((pos && pos === 'unchanged') || (irect && globalY >= irect.top && globalY <= irect.bottom))) {
125+
if (originLoc
126+
&& ((pos && pos === 'unchanged') || (irect && globalY >= irect.top && globalY <= irect.bottom))
127+
&& dragObject) {
127128
const loc = originLoc.clone(e);
128129
const indented = this.indentTrack.getIndentParent(originLoc, loc);
129130
if (indented) {
@@ -138,7 +139,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
138139
detail: {
139140
type: IPublicTypeLocationDetailType.Children,
140141
index,
141-
valid: document.checkNesting(parent, e.dragObject as any),
142+
valid: document?.checkNesting(parent, e.dragObject as any),
142143
},
143144
});
144145
}
@@ -177,7 +178,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
177178
}
178179
}
179180
if (p !== node) {
180-
node = p || document.focusNode;
181+
node = p || document?.focusNode;
181182
treeNode = tree.getTreeNode(node);
182183
focusSlots = false;
183184
}
@@ -258,7 +259,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
258259
cancelIdleCallback(this.tryScrollAgain);
259260
this.tryScrollAgain = null;
260261
}
261-
if (this.sensing || !this.bounds || !this.scroller || !this.scrollTarget) {
262+
if (!this.bounds || !this.scroller || !this.scrollTarget) {
262263
// is a active sensor
263264
return;
264265
}
@@ -305,7 +306,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
305306
focus = { type: 'slots' };
306307
} else {
307308
index = 0;
308-
valid = document.checkNesting(target, event.dragObject as any);
309+
valid = !!document?.checkNesting(target, event.dragObject as any);
309310
}
310311
canvas.createLocation({
311312
target,
@@ -320,23 +321,28 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
320321
});
321322
});
322323

323-
private getNear(treeNode: TreeNode, e: IPublicModelLocateEvent, index?: number, rect?: DOMRect) {
324+
private getNear(treeNode: TreeNode, e: IPublicModelLocateEvent, originalIndex?: number, originalRect?: DOMRect) {
324325
const { canvas, project } = this.pluginContext;
325326
const document = project.getCurrentDocument();
326327
const { globalY, dragObject } = e;
328+
if (!dragObject) {
329+
return null;
330+
}
327331
// TODO: check dragObject is anyData
328332
const { node, expanded } = treeNode;
333+
let rect = originalRect;
329334
if (!rect) {
330335
rect = this.getTreeNodeRect(treeNode);
331336
if (!rect) {
332337
return null;
333338
}
334339
}
340+
let index = originalIndex;
335341
if (index == null) {
336342
index = node.index;
337343
}
338344

339-
if (node.isSlot) {
345+
if (node.isSlotNode) {
340346
// 是个插槽根节点
341347
if (!treeNode.isContainer() && !treeNode.hasSlots()) {
342348
return canvas.createLocation({
@@ -385,7 +391,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
385391
detail: {
386392
type: IPublicTypeLocationDetailType.Children,
387393
index,
388-
valid: document.checkNesting(node.parent!, dragObject as any),
394+
valid: document?.checkNesting(node.parent!, dragObject as any),
389395
near: { node, pos: 'before' },
390396
focus: checkRecursion(focusNode, dragObject) ? { type: 'node', node: focusNode } : undefined,
391397
},
@@ -412,7 +418,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
412418
detail: {
413419
type: IPublicTypeLocationDetailType.Children,
414420
index: index + 1,
415-
valid: document.checkNesting(node.parent!, dragObject as any),
421+
valid: document?.checkNesting(node.parent!, dragObject as any),
416422
near: { node, pos: 'after' },
417423
focus: checkRecursion(focusNode, dragObject) ? { type: 'node', node: focusNode } : undefined,
418424
},
@@ -423,6 +429,9 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
423429
const { canvas, project } = this.pluginContext;
424430
const document = project.getCurrentDocument();
425431
const { dragObject, globalY } = e;
432+
if (!dragObject) {
433+
return null;
434+
}
426435

427436
if (!checkRecursion(treeNode.node, dragObject)) {
428437
return null;
@@ -454,7 +463,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
454463
detail.valid = false;
455464
} else {
456465
detail.index = 0;
457-
detail.valid = document.checkNesting(container, dragObject);
466+
detail.valid = document?.checkNesting(container, dragObject);
458467
}
459468
}
460469

@@ -526,7 +535,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
526535
} else {
527536
detail.index = l;
528537
}
529-
detail.valid = document.checkNesting(container, dragObject);
538+
detail.valid = document?.checkNesting(container, dragObject);
530539
}
531540

532541
return canvas.createLocation(locationData);
@@ -572,9 +581,26 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
572581
return;
573582
}
574583
this._shell = shell;
584+
const { canvas, project } = this.pluginContext;
575585
if (shell) {
576-
this._scrollTarget = this.pluginContext.canvas.createScrollTarget(shell);
586+
this._scrollTarget = canvas.createScrollTarget(shell);
577587
this._sensorAvailable = true;
588+
589+
// check if there is current selection and scroll to it
590+
const selection = project.currentDocument?.selection;
591+
const topNodes = selection?.getTopNodes(true);
592+
const tree = this.treeMaster?.currentTree;
593+
if (topNodes && topNodes[0] && tree) {
594+
const treeNode = tree.getTreeNodeById(topNodes[0].id);
595+
if (treeNode) {
596+
// at this moment, it is possible that pane is not ready yet, so
597+
// put ui related operations to the next loop
598+
setTimeout(() => {
599+
tree.setNodeSelected(treeNode.id);
600+
this.scrollToNode(treeNode, null, 4);
601+
}, 0);
602+
}
603+
}
578604
} else {
579605
this._scrollTarget = undefined;
580606
this._sensorAvailable = false;
@@ -610,7 +636,7 @@ export class PaneController implements ISensor, ITreeBoard, IPublicModelScrollab
610636
}
611637
}
612638

613-
function checkRecursion(parent: IPublicModelNode | undefined | null, dragObject: DragObject): boolean {
639+
function checkRecursion(parent: IPublicModelNode | undefined | null, dragObject: IPublicModelDragObject): boolean {
614640
if (!parent) {
615641
return false;
616642
}
@@ -633,14 +659,14 @@ function getPosFromEvent(
633659
if (target.matches('.insertion')) {
634660
return 'unchanged';
635661
}
636-
target = target.closest('[data-id]');
637-
if (!target || !stop.contains(target)) {
662+
const closest = target.closest('[data-id]');
663+
if (!closest || !stop.contains(closest)) {
638664
return null;
639665
}
640666

641-
const nodeId = (target as HTMLDivElement).dataset.id!;
667+
const nodeId = (closest as HTMLDivElement).dataset.id!;
642668
return {
643-
focusSlots: target.matches('.tree-node-slots'),
669+
focusSlots: closest.matches('.tree-node-slots'),
644670
nodeId,
645671
};
646672
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'ric-shim';

0 commit comments

Comments
 (0)