Skip to content

Commit c885587

Browse files
committed
chore: 修复类型定义问题
- 移除dragon中不再支持的util,修改相关引用
1 parent dd25dcc commit c885587

File tree

17 files changed

+143
-123
lines changed

17 files changed

+143
-123
lines changed

packages/designer/src/context-menu-actions.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import './context-menu-actions.scss';
1919
export interface IContextMenuActions {
2020
actions: IPublicTypeContextMenuAction[];
2121

22-
adjustMenuLayoutFn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[];
22+
// adjustMenuLayoutFn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[];
2323

2424
addMenuAction: IPublicApiMaterial['addContextMenuOption'];
2525

@@ -57,7 +57,11 @@ export class GlobalContextMenuActions {
5757
event.preventDefault();
5858

5959
const actions: IPublicTypeContextMenuAction[] = [];
60-
let contextMenu: ContextMenuActions = this.contextMenuActionsMap.values().next().value;
60+
let contextMenu: ContextMenuActions | undefined = this.contextMenuActionsMap
61+
.values()
62+
.next().value;
63+
if (!contextMenu) return;
64+
6165
this.contextMenuActionsMap.forEach((contextMenu) => {
6266
actions.push(...contextMenu.actions);
6367
});
@@ -90,12 +94,17 @@ export class GlobalContextMenuActions {
9094
pluginContext,
9195
});
9296

93-
const target = event.target;
97+
const target = event.target as HTMLElement;
98+
99+
if (!target) {
100+
console.warn('context menu target is null');
101+
return;
102+
}
94103

95-
const { top, left } = target?.getBoundingClientRect();
104+
const { top, left } = target.getBoundingClientRect();
96105

97106
const menuInstance = Menu.create({
98-
target: event.target,
107+
target: target,
99108
offset: [event.clientX - left, event.clientY - top],
100109
children: menuNode,
101110
className: 'engine-context-menu',
@@ -177,7 +186,7 @@ export class ContextMenuActions implements IContextMenuActions {
177186
) as IPublicModelPluginContext;
178187

179188
const menus: IPublicTypeContextMenuItem[] = parseContextMenuProperties(actions, {
180-
nodes: nodes.map((d) => designer.shellModelFactory.createNode(d)!),
189+
nodes: nodes.map((d) => designer.shellModelFactory!.createNode(d)!),
181190
destroy,
182191
event,
183192
pluginContext,
@@ -191,7 +200,7 @@ export class ContextMenuActions implements IContextMenuActions {
191200

192201
const menuNode = parseContextMenuAsReactNode(layoutMenu, {
193202
destroy,
194-
nodes: nodes.map((d) => designer.shellModelFactory.createNode(d)!),
203+
nodes: nodes.map((d) => designer.shellModelFactory!.createNode(d)!),
195204
pluginContext,
196205
});
197206

@@ -210,10 +219,10 @@ export class ContextMenuActions implements IContextMenuActions {
210219
originalEvent.stopPropagation();
211220
originalEvent.preventDefault();
212221
// 如果右键的节点不在 当前选中的节点中,选中该节点
213-
if (!designer.currentSelection.has(node.id)) {
214-
designer.currentSelection.select(node.id);
222+
if (!designer.currentSelection!.has(node.id)) {
223+
designer.currentSelection!.select(node.id);
215224
}
216-
const nodes = designer.currentSelection.getNodes();
225+
const nodes = designer.currentSelection!.getNodes();
217226
this.handleContextMenu(nodes, originalEvent);
218227
},
219228
),

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { obx, IEventBus, createModuleEventBus } from '@felce/lowcode-editor-core
33
import { IPublicTypeActiveTarget, IPublicModelActiveTracker } from '@felce/lowcode-types';
44
import { isNode } from '@felce/lowcode-utils';
55

6-
export interface IActiveTracker extends Omit<IPublicModelActiveTracker, 'track' | 'onChange'> {
7-
_target: ActiveTarget | INode;
8-
6+
export interface IActiveTracker
7+
extends Omit<IPublicModelActiveTracker, 'track' | 'onChange' | 'target'> {
98
track(originalTarget: ActiveTarget | INode): void;
109

1110
onChange(fn: (target: ActiveTarget) => void): () => void;
@@ -16,7 +15,7 @@ export interface ActiveTarget extends Omit<IPublicTypeActiveTarget, 'node'> {
1615
}
1716

1817
export class ActiveTracker implements IActiveTracker {
19-
@obx.ref private _target?: ActiveTarget | INode;
18+
@obx.ref private _target: ActiveTarget | INode;
2019

2120
private emitter: IEventBus = createModuleEventBus('ActiveTracker');
2221

packages/designer/src/designer/designer.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BuiltinSimulatorProps } from './../builtin-simulator/host';
12
import { ComponentType } from 'react';
23
import {
34
obx,
@@ -55,13 +56,13 @@ const logger = new Logger({ level: 'warn', bizName: 'designer' });
5556
export interface DesignerProps {
5657
[key: string]: any;
5758
editor: IPublicModelEditor;
58-
shellModelFactory: IShellModelFactory;
59+
shellModelFactory?: IShellModelFactory;
5960
className?: string;
6061
style?: object;
6162
defaultSchema?: IPublicTypeProjectSchema;
6263
hotkeys?: object;
6364
viewName?: string;
64-
simulatorProps?: Record<string, any> | ((document: DocumentModel) => object);
65+
simulatorProps?: ((project: Project) => BuiltinSimulatorProps) | BuiltinSimulatorProps;
6566
simulatorComponent?: ComponentType<any>;
6667
dragGhostComponent?: ComponentType<any>;
6768
suspensed?: boolean;
@@ -71,13 +72,13 @@ export interface DesignerProps {
7172
onDragstart?: (e: IPublicModelLocateEvent) => void;
7273
onDrag?: (e: IPublicModelLocateEvent) => void;
7374
onDragend?: (
74-
e: { dragObject: IPublicModelDragObject; copy: boolean },
75+
e: { dragObject: IPublicModelDragObject<INode>; copy?: boolean },
7576
loc?: DropLocation,
7677
) => void;
7778
}
7879

7980
export interface IDesigner {
80-
readonly shellModelFactory: IShellModelFactory;
81+
readonly shellModelFactory?: IShellModelFactory;
8182

8283
viewName: string | undefined;
8384

@@ -89,15 +90,15 @@ export interface IDesigner {
8990

9091
get componentActions(): ComponentActions;
9192

92-
get contextMenuActions(): ContextMenuActions;
93+
get contextMenuActions(): IContextMenuActions;
9394

9495
get editor(): IPublicModelEditor;
9596

9697
get detecting(): Detecting;
9798

9899
get simulatorComponent(): ComponentType<any> | undefined;
99100

100-
get currentSelection(): ISelection;
101+
get currentSelection(): ISelection | undefined;
101102

102103
createScroller(scrollable: IPublicTypeScrollable): IPublicModelScroller;
103104

@@ -162,7 +163,7 @@ export class Designer implements IDesigner {
162163

163164
readonly bemToolsManager = new BemToolsManager(this);
164165

165-
readonly shellModelFactory: IShellModelFactory;
166+
readonly shellModelFactory?: IShellModelFactory;
166167

167168
private _dropLocation?: DropLocation;
168169

@@ -448,9 +449,10 @@ export class Designer implements IDesigner {
448449
this._simulatorComponent = props.simulatorComponent;
449450
}
450451
if (props.simulatorProps !== this.props.simulatorProps) {
452+
const oldDesignMode = this.simulatorProps?.designMode;
451453
this._simulatorProps = props.simulatorProps;
452454
// 重新 setupSelection
453-
if (props.simulatorProps?.designMode !== this.props.simulatorProps?.designMode) {
455+
if (this.simulatorProps?.designMode !== oldDesignMode) {
454456
this.setupSelection();
455457
}
456458
}

packages/designer/src/designer/drag-ghost/index.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { Component, ReactElement } from 'react';
22
import { observer, obx, Title, makeObservable } from '@felce/lowcode-editor-core';
33
import { Designer } from '../designer';
4-
import { isDragNodeObject } from '../dragon';
54
import { isSimulatorHost } from '../../simulator';
65
import './ghost.less';
76
import {
87
IPublicTypeI18nData,
98
IPublicTypeNodeSchema,
109
IPublicModelDragObject,
1110
} from '@felce/lowcode-types';
11+
import { isDragNodeObject } from '@felce/lowcode-utils';
1212

1313
type offBinding = () => any;
1414

1515
@observer
1616
export default class DragGhost extends Component<{ designer: Designer }> {
1717
private dispose: offBinding[] = [];
1818

19-
@obx.ref private titles: (string | IPublicTypeI18nData | ReactElement)[] | null = null;
19+
@obx.ref private titles: (string | IPublicTypeI18nData | ReactElement | null)[] | null = null;
2020

2121
@obx.ref private x = 0;
2222

@@ -58,17 +58,19 @@ export default class DragGhost extends Component<{ designer: Designer }> {
5858
];
5959
}
6060

61-
getTitles(dragObject: IPublicModelDragObject) {
61+
getTitles(dragObject: IPublicModelDragObject | null) {
62+
if (!dragObject) return null;
63+
6264
if (isDragNodeObject(dragObject)) {
63-
return dragObject.nodes.map((node) => node.title);
65+
return dragObject.nodes.map((node) => node?.title || null);
6466
}
6567

6668
const dataList = Array.isArray(dragObject.data) ? dragObject.data : [dragObject.data];
6769

68-
return dataList.map(
69-
(item: IPublicTypeNodeSchema, i) =>
70-
this.props.designer.getComponentMeta(item.componentName).title,
71-
);
70+
return dataList.map((item: IPublicTypeNodeSchema | null) => {
71+
if (!item) return null;
72+
return this.props.designer.getComponentMeta(item.componentName).title;
73+
});
7274
}
7375

7476
componentWillUnmount() {

packages/designer/src/designer/dragon.ts

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { obx, makeObservable, IEventBus, createModuleEventBus } from '@felce/lowcode-editor-core';
1+
import { IEventBus, createModuleEventBus, makeObservable, obx } from '@felce/lowcode-editor-core';
22
import {
3-
IPublicTypeDragNodeObject,
4-
IPublicTypeDragAnyObject,
5-
IPublicEnumDragObjectType,
6-
IPublicTypeDragNodeDataObject,
73
IPublicModelDragObject,
8-
IPublicModelNode,
94
IPublicModelDragon,
105
IPublicModelLocateEvent,
6+
IPublicModelNode,
117
IPublicModelSensor,
128
} from '@felce/lowcode-types';
13-
import { setNativeSelection, cursor } from '@felce/lowcode-utils';
9+
import { cursor, isDragNodeObject, setNativeSelection } from '@felce/lowcode-utils';
1410
import { INode, Node } from '../document';
1511
import { ISimulatorHost, isSimulatorHost } from '../simulator';
16-
import { IDesigner } from './designer';
1712
import { makeEventsHandler } from '../utils/misc';
13+
import { IDesigner } from './designer';
1814

1915
export interface ILocateEvent extends IPublicModelLocateEvent {
2016
readonly type: 'LocateEvent';
@@ -25,31 +21,6 @@ export interface ILocateEvent extends IPublicModelLocateEvent {
2521
sensor?: IPublicModelSensor;
2622
}
2723

28-
/**
29-
* @deprecated use same function in @felce/lowcode-utils
30-
*/
31-
export function isDragNodeObject(obj: any): obj is IPublicTypeDragNodeObject {
32-
return obj && obj.type === IPublicEnumDragObjectType.Node;
33-
}
34-
35-
/**
36-
* @deprecated use same function in @felce/lowcode-utils
37-
*/
38-
export function isDragNodeDataObject(obj: any): obj is IPublicTypeDragNodeDataObject {
39-
return obj && obj.type === IPublicEnumDragObjectType.NodeData;
40-
}
41-
42-
/**
43-
* @deprecated use same function in @felce/lowcode-utils
44-
*/
45-
export function isDragAnyObject(obj: any): obj is IPublicTypeDragAnyObject {
46-
return (
47-
obj &&
48-
obj.type !== IPublicEnumDragObjectType.NodeData &&
49-
obj.type !== IPublicEnumDragObjectType.Node
50-
);
51-
}
52-
5324
export function isLocateEvent(e: any): e is ILocateEvent {
5425
return e && e.type === 'LocateEvent';
5526
}
@@ -88,7 +59,7 @@ export function setShaken(e: any) {
8859
e.shaken = true;
8960
}
9061

91-
function getSourceSensor(dragObject: IPublicModelDragObject): ISimulatorHost | null {
62+
function getSourceSensor(dragObject: IPublicModelDragObject<INode>): ISimulatorHost | null {
9263
if (!isDragNodeObject(dragObject)) {
9364
return null;
9465
}
@@ -144,7 +115,7 @@ export class Dragon implements IDragon {
144115
* @param shell container element
145116
* @param boost boost got a drag object
146117
*/
147-
from(shell: Element, boost: (e: MouseEvent) => IPublicModelDragObject | null) {
118+
from(shell: Element, boost: (e: MouseEvent) => IPublicModelDragObject<INode> | null) {
148119
const mousedown = (e: MouseEvent) => {
149120
// ESC or RightClick
150121
if (e.which === 3 || e.button === 2) {
@@ -172,7 +143,7 @@ export class Dragon implements IDragon {
172143
* @param boostEvent 拖拽初始时事件
173144
*/
174145
boost(
175-
dragObject: IPublicModelDragObject,
146+
dragObject: IPublicModelDragObject<INode>,
176147
boostEvent: MouseEvent | DragEvent,
177148
fromRglNode?: INode | IPublicModelNode,
178149
) {
@@ -257,7 +228,7 @@ export class Dragon implements IDragon {
257228
/* istanbul ignore next */
258229
if (isRGL) {
259230
// 禁止被拖拽元素的阻断
260-
const nodeInst = dragObject.nodes[0].getDOMNode();
231+
const nodeInst = dragObject.nodes?.[0]?.getDOMNode();
261232
if (nodeInst && nodeInst.style) {
262233
this.nodeInstPointerEvents = true;
263234
nodeInst.style.pointerEvents = 'none';
@@ -275,7 +246,7 @@ export class Dragon implements IDragon {
275246
this.emitter.emit('rgl.add.placeholder', {
276247
rglNode,
277248
fromRglNode,
278-
node: locateEvent.dragObject?.nodes[0],
249+
node: locateEvent.dragObject?.nodes?.[0],
279250
event: e,
280251
});
281252
designer.clearLocation();
@@ -349,7 +320,7 @@ export class Dragon implements IDragon {
349320
const over = (e?: any) => {
350321
// 禁止被拖拽元素的阻断
351322
if (this.nodeInstPointerEvents) {
352-
const nodeInst = dragObject.nodes[0].getDOMNode();
323+
const nodeInst = dragObject.nodes?.[0]?.getDOMNode();
353324
if (nodeInst && nodeInst.style) {
354325
nodeInst.style.pointerEvents = '';
355326
}
@@ -361,7 +332,7 @@ export class Dragon implements IDragon {
361332
const { isRGL, rglNode } = getRGL(e);
362333
/* istanbul ignore next */
363334
if (isRGL && this._canDrop && this._dragging) {
364-
const tarNode = dragObject.nodes[0];
335+
const tarNode = dragObject.nodes?.[0];
365336
if (rglNode.id !== tarNode.id) {
366337
// 避免死循环
367338
this.emitter.emit('rgl.drop', {
@@ -489,6 +460,7 @@ export class Dragon implements IDragon {
489460
} else if (e.sensor) {
490461
sensor = e.sensor;
491462
} else if (sourceSensor) {
463+
// FIXME: 这里为什么会把simulator拿过来?
492464
sensor = sourceSensor;
493465
}
494466
}
@@ -640,7 +612,7 @@ export class Dragon implements IDragon {
640612
};
641613
}
642614

643-
onDragend(func: (x: { dragObject: IPublicModelDragObject; copy: boolean }) => any) {
615+
onDragend(func: (x: { dragObject: IPublicModelDragObject<INode>; copy: boolean }) => any) {
644616
this.emitter.on('dragend', func);
645617
return () => {
646618
this.emitter.removeListener('dragend', func);

packages/designer/src/document/document-view.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { Component } from 'react';
2-
import classNames from 'classnames';
31
import { observer } from '@felce/lowcode-editor-core';
4-
import { DocumentModel, IDocumentModel } from './document-model';
2+
import classNames from 'classnames';
3+
import { Component } from 'react';
54
import { BuiltinSimulatorHostView } from '../builtin-simulator';
5+
import { IDocumentModel } from './document-model';
66

7+
/**
8+
* @deprecated 暂时没用到
9+
*/
710
@observer
811
export class DocumentView extends Component<{ document: IDocumentModel }> {
912
render() {

packages/designer/tests/designer/dragon.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import { DocumentModel } from '../../src/document/document-model';
55
import { Designer } from '../../src/designer/designer';
66
import {
77
Dragon,
8-
isDragNodeObject,
9-
isDragNodeDataObject,
10-
isDragAnyObject,
118
isLocateEvent,
129
isShaken,
1310
setShaken,
@@ -18,6 +15,7 @@ import { IPublicEnumDragObjectType } from '@felce/lowcode-types';
1815
import formSchema from '../fixtures/schema/form';
1916
import { fireEvent } from '@testing-library/react';
2017
import { shellModelFactory } from '../../../engine/src/modules/shell-model-factory';
18+
import { isDragAnyObject, isDragNodeDataObject, isDragNodeObject } from '@felce/lowcode-utils';
2119

2220
describe('Dragon 测试', () => {
2321
let editor: Editor;

0 commit comments

Comments
 (0)