From 0d92011863b52b87a8657ba53c2ac2ed97c5ef79 Mon Sep 17 00:00:00 2001 From: liujuping Date: Tue, 27 Feb 2024 10:51:03 +0800 Subject: [PATCH] chore: update ts defined --- packages/designer/src/context-menu-actions.ts | 36 ++-- .../designer/src/designer/designer-view.tsx | 6 +- packages/designer/src/designer/designer.ts | 80 ++------ packages/designer/src/designer/dragon.ts | 14 +- .../designer/src/designer/offset-observer.ts | 34 ++-- .../designer/src/document/document-model.ts | 128 +++---------- .../src/document/node/node-children.ts | 89 +++------ packages/designer/src/document/node/node.ts | 171 ++++-------------- .../designer/src/document/node/props/prop.ts | 6 +- .../designer/src/document/node/props/props.ts | 25 +-- .../designer/src/plugin/plugin-context.ts | 2 + packages/editor-skeleton/src/skeleton.ts | 87 +++------ packages/engine/src/engine-core.ts | 2 +- .../src/utils/misc.ts | 51 +++--- packages/shell/src/api/workspace.ts | 4 +- packages/shell/src/model/resource.ts | 2 +- packages/types/src/shell/api/common.ts | 4 +- .../types/src/shell/model/document-model.ts | 2 +- packages/types/src/shell/model/prop.ts | 4 +- packages/types/src/shell/model/resource.ts | 4 +- packages/types/src/shell/type/metadata.ts | 2 +- .../types/src/shell/type/props-transducer.ts | 6 +- .../src/shell/type/widget-base-config.ts | 2 +- packages/utils/src/is-plain-object.ts | 2 +- .../src/__snapshots__/is-react.test.tsx.snap | 0 .../src/__snapshots__/schema.test.ts.snap | 0 .../build-components/buildComponents.test.tsx | 0 .../build-components/getProjectUtils.test.ts | 0 .../build-components/getSubComponent.test.ts | 0 .../src/check-prop-types.test.ts | 0 .../is-action-content-object.test.ts | 0 .../check-types/is-basic-prop-type.test.ts | 0 .../src/check-types/is-custom-view.test.tsx | 0 .../src/check-types/is-dom-text.test.ts | 0 .../check-types/is-drag-any-object.test.ts | 0 .../is-drag-node-data-object.test.ts | 0 .../check-types/is-drag-node-object.test.ts | 0 .../src/check-types/is-dynamic-setter.test.ts | 0 .../src/check-types/is-i18n-data.test.ts | 0 .../src/check-types/is-isfunction.test.ts | 0 .../src/check-types/is-jsblock.test.ts | 0 .../src/check-types/is-jsexpression.test.ts | 0 .../src/check-types/is-jsslot.test.ts | 0 .../is-location-children-detail.test.ts | 0 .../src/check-types/is-location-data.test.ts | 0 .../is-lowcode-component-type.test.ts | 0 .../is-lowcode-project-schema.test.ts | 0 .../src/check-types/is-node-schema.test.ts | 0 .../src/check-types/is-node.test.ts | 0 .../is-procode-component-type.test.ts | 0 .../src/check-types/is-project-schema.test.ts | 0 .../check-types/is-required-prop-type.test.ts | 0 .../src/check-types/is-setter-config.test.ts | 0 .../src/check-types/is-setting-field.test.ts | 0 .../src/check-types/is-title-config.test.ts | 0 .../{test => tests}/src/clone-deep.test.ts | 0 .../src/clone-enumerable-property.test.ts | 0 .../src/create-content.test.tsx | 0 .../{test => tests}/src/create-defer.test.ts | 0 .../{test => tests}/src/is-object.test.ts | 0 .../{test => tests}/src/is-react.test.tsx | 0 .../{test => tests}/src/is-shaken.test.ts | 0 .../utils/{test => tests}/src/misc.test.ts | 0 .../src/navtive-selection.test.ts | 0 .../utils/{test => tests}/src/schema.test.ts | 0 .../utils/{test => tests}/src/script.test.ts | 0 .../{test => tests}/src/svg-icon.test.tsx | 0 .../src/transaction-manager.test.ts | 0 .../{test => tests}/src/unique-id.test.ts | 0 .../workspace/src/context/base-context.ts | 26 +-- packages/workspace/src/resource-type.ts | 10 +- packages/workspace/src/resource.ts | 27 +-- packages/workspace/src/view/window-view.tsx | 4 +- packages/workspace/src/window.ts | 22 +-- packages/workspace/src/workspace.ts | 57 +----- 75 files changed, 226 insertions(+), 683 deletions(-) rename packages/utils/{test => tests}/src/__snapshots__/is-react.test.tsx.snap (100%) rename packages/utils/{test => tests}/src/__snapshots__/schema.test.ts.snap (100%) rename packages/utils/{test => tests}/src/build-components/buildComponents.test.tsx (100%) rename packages/utils/{test => tests}/src/build-components/getProjectUtils.test.ts (100%) rename packages/utils/{test => tests}/src/build-components/getSubComponent.test.ts (100%) rename packages/utils/{test => tests}/src/check-prop-types.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-action-content-object.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-basic-prop-type.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-custom-view.test.tsx (100%) rename packages/utils/{test => tests}/src/check-types/is-dom-text.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-drag-any-object.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-drag-node-data-object.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-drag-node-object.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-dynamic-setter.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-i18n-data.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-isfunction.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-jsblock.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-jsexpression.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-jsslot.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-location-children-detail.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-location-data.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-lowcode-component-type.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-lowcode-project-schema.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-node-schema.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-node.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-procode-component-type.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-project-schema.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-required-prop-type.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-setter-config.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-setting-field.test.ts (100%) rename packages/utils/{test => tests}/src/check-types/is-title-config.test.ts (100%) rename packages/utils/{test => tests}/src/clone-deep.test.ts (100%) rename packages/utils/{test => tests}/src/clone-enumerable-property.test.ts (100%) rename packages/utils/{test => tests}/src/create-content.test.tsx (100%) rename packages/utils/{test => tests}/src/create-defer.test.ts (100%) rename packages/utils/{test => tests}/src/is-object.test.ts (100%) rename packages/utils/{test => tests}/src/is-react.test.tsx (100%) rename packages/utils/{test => tests}/src/is-shaken.test.ts (100%) rename packages/utils/{test => tests}/src/misc.test.ts (100%) rename packages/utils/{test => tests}/src/navtive-selection.test.ts (100%) rename packages/utils/{test => tests}/src/schema.test.ts (100%) rename packages/utils/{test => tests}/src/script.test.ts (100%) rename packages/utils/{test => tests}/src/svg-icon.test.tsx (100%) rename packages/utils/{test => tests}/src/transaction-manager.test.ts (100%) rename packages/utils/{test => tests}/src/unique-id.test.ts (100%) diff --git a/packages/designer/src/context-menu-actions.ts b/packages/designer/src/context-menu-actions.ts index c88e03ac6..3b0283fc4 100644 --- a/packages/designer/src/context-menu-actions.ts +++ b/packages/designer/src/context-menu-actions.ts @@ -5,18 +5,6 @@ import { Menu } from '@alifd/next'; import { engineConfig } from '@alilc/lowcode-editor-core'; import './context-menu-actions.scss'; -export interface IContextMenuActions { - actions: IPublicTypeContextMenuAction[]; - - adjustMenuLayoutFn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]; - - addMenuAction: IPublicApiMaterial['addContextMenuOption']; - - removeMenuAction: IPublicApiMaterial['removeContextMenuOption']; - - adjustMenuLayout: IPublicApiMaterial['adjustContextMenuLayout']; -} - let adjustMenuLayoutFn: Function = (actions: IPublicTypeContextMenuAction[]) => actions; export class GlobalContextMenuActions { @@ -116,7 +104,7 @@ export class GlobalContextMenuActions { const globalContextMenuActions = new GlobalContextMenuActions(); -export class ContextMenuActions implements IContextMenuActions { +export class ContextMenuActions { actions: IPublicTypeContextMenuAction[] = []; designer: IDesigner; @@ -204,30 +192,32 @@ export class ContextMenuActions implements IContextMenuActions { originalEvent.stopPropagation(); originalEvent.preventDefault(); // 如果右键的节点不在 当前选中的节点中,选中该节点 - if (!designer.currentSelection.has(node.id)) { - designer.currentSelection.select(node.id); + if (!designer.currentSelection?.has(node.id)) { + designer.currentSelection?.select(node.id); } - const nodes = designer.currentSelection.getNodes(); + const nodes = designer.currentSelection?.getNodes(); this.handleContextMenu(nodes, originalEvent); }), ); } - addMenuAction(action: IPublicTypeContextMenuAction) { + addMenuAction: IPublicApiMaterial['addContextMenuOption'] = (action: IPublicTypeContextMenuAction) => { this.actions.push({ type: IPublicEnumContextMenuType.MENU_ITEM, ...action, }); - } + }; - removeMenuAction(name: string) { + removeMenuAction: IPublicApiMaterial['removeContextMenuOption'] = (name: string) => { const i = this.actions.findIndex((action) => action.name === name); if (i > -1) { this.actions.splice(i, 1); } - } + }; - adjustMenuLayout(fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]) { + adjustMenuLayout: IPublicApiMaterial['adjustContextMenuLayout'] = (fn: (actions: IPublicTypeContextMenuItem[]) => IPublicTypeContextMenuItem[]) => { adjustMenuLayoutFn = fn; - } -} \ No newline at end of file + }; +} + +export interface IContextMenuActions extends ContextMenuActions {} \ No newline at end of file diff --git a/packages/designer/src/designer/designer-view.tsx b/packages/designer/src/designer/designer-view.tsx index aaf0c9583..851decf14 100644 --- a/packages/designer/src/designer/designer-view.tsx +++ b/packages/designer/src/designer/designer-view.tsx @@ -1,16 +1,16 @@ import { Component } from 'react'; import classNames from 'classnames'; import BuiltinDragGhostComponent from './drag-ghost'; -import { Designer, DesignerProps } from './designer'; +import { Designer, DesignerProps, IDesigner } from './designer'; import { ProjectView } from '../project'; import './designer.less'; type IProps = DesignerProps & { - designer?: Designer; + designer?: IDesigner; }; export class DesignerView extends Component { - readonly designer: Designer; + readonly designer: IDesigner; readonly viewName: string | undefined; constructor(props: IProps) { diff --git a/packages/designer/src/designer/designer.ts b/packages/designer/src/designer/designer.ts index 1dd4bc04e..48c163493 100644 --- a/packages/designer/src/designer/designer.ts +++ b/packages/designer/src/designer/designer.ts @@ -6,7 +6,6 @@ import { IPublicTypeComponentAction, IPublicTypeNpmInfo, IPublicModelEditor, - IPublicTypeCompositeObject, IPublicTypePropsList, IPublicTypeNodeSchema, IPublicTypePropsTransducer, @@ -17,15 +16,16 @@ import { IPublicTypeLocationData, IPublicEnumTransformStage, IPublicModelLocateEvent, + IPublicTypePropsMap, } from '@alilc/lowcode-types'; import { mergeAssets, IPublicTypeAssetsJson, isNodeSchema, isDragNodeObject, isDragNodeDataObject, isLocationChildrenDetail, Logger } from '@alilc/lowcode-utils'; import { IProject, Project } from '../project'; -import { Node, DocumentModel, insertChildren, INode, ISelection } from '../document'; +import { Node, DocumentModel, insertChildren, INode } from '../document'; import { ComponentMeta, IComponentMeta } from '../component-meta'; import { INodeSelector, Component } from '../simulator'; import { Scroller } from './scroller'; import { Dragon, IDragon } from './dragon'; -import { ActiveTracker, IActiveTracker } from './active-tracker'; +import { ActiveTracker } from './active-tracker'; import { Detecting } from './detecting'; import { DropLocation } from './location'; import { OffsetObserver, createOffsetObserver } from './offset-observer'; @@ -47,7 +47,7 @@ export interface DesignerProps { viewName?: string; simulatorProps?: Record | ((document: DocumentModel) => object); simulatorComponent?: ComponentType; - dragGhostComponent?: ComponentType; + dragGhostComponent?: ComponentType<{ designer: IDesigner }>; suspensed?: boolean; componentMetadatas?: IPublicTypeComponentMetadata[]; globalComponentActions?: IPublicTypeComponentAction[]; @@ -60,70 +60,10 @@ export interface DesignerProps { ) => void; } -export interface IDesigner { - readonly shellModelFactory: IShellModelFactory; - - viewName: string | undefined; - - readonly project: IProject; - - get dragon(): IDragon; - - get activeTracker(): IActiveTracker; - - get componentActions(): ComponentActions; - - get contextMenuActions(): ContextMenuActions; - - get editor(): IPublicModelEditor; - - get detecting(): Detecting; - - get simulatorComponent(): ComponentType | undefined; - - get currentSelection(): ISelection; - - createScroller(scrollable: IPublicTypeScrollable): IPublicModelScroller; - - refreshComponentMetasMap(): void; - - createOffsetObserver(nodeInstance: INodeSelector): OffsetObserver | null; - - /** - * 创建插入位置,考虑放到 dragon 中 - */ - createLocation(locationData: IPublicTypeLocationData): DropLocation; - - get componentsMap(): { [key: string]: IPublicTypeNpmInfo | Component }; - - loadIncrementalAssets(incrementalAssets: IPublicTypeAssetsJson): Promise; - - getComponentMeta( - componentName: string, - generateMetadata?: () => IPublicTypeComponentMetadata | null, - ): IComponentMeta; - - clearLocation(): void; - - createComponentMeta(data: IPublicTypeComponentMetadata): IComponentMeta | null; - - getComponentMetasMap(): Map; - - addPropsReducer(reducer: IPublicTypePropsTransducer, stage: IPublicEnumTransformStage): void; - - postEvent(event: string, ...args: any[]): void; - - transformProps(props: IPublicTypeCompositeObject | IPublicTypePropsList, node: Node, stage: IPublicEnumTransformStage): IPublicTypeCompositeObject | IPublicTypePropsList; - - createSettingEntry(nodes: INode[]): ISettingTopEntry; - - autorun(effect: (reaction: IReactionPublic) => void, options?: IReactionOptions): IReactionDisposer; -} - -export class Designer implements IDesigner { +export class Designer { dragon: IDragon; - viewName: string | undefined; + readonly viewName: string | undefined; readonly componentActions = new ComponentActions(); @@ -423,7 +363,7 @@ export class Designer implements IDesigner { if (props.simulatorProps !== this.props.simulatorProps) { this._simulatorProps = props.simulatorProps; // 重新 setupSelection - if (props.simulatorProps?.designMode !== this.props.simulatorProps?.designMode) { + if ((props.simulatorProps as any)?.designMode !== (this.props.simulatorProps as any)?.designMode) { this.setupSelection(); } } @@ -612,7 +552,7 @@ export class Designer implements IDesigner { return maps; } - transformProps(props: IPublicTypeCompositeObject | IPublicTypePropsList, node: Node, stage: IPublicEnumTransformStage) { + transformProps(props: IPublicTypePropsMap | IPublicTypePropsList, node: Node, stage: IPublicEnumTransformStage): IPublicTypePropsMap | IPublicTypePropsList { if (Array.isArray(props)) { // current not support, make this future return props; @@ -623,7 +563,7 @@ export class Designer implements IDesigner { return props; } - return reducers.reduce((xprops, reducer) => { + return reducers.reduce((xprops, reducer: IPublicTypePropsTransducer) => { try { return reducer(xprops, node.internalToShellNode() as any, { stage }); } catch (e) { @@ -655,3 +595,5 @@ export class Designer implements IDesigner { // TODO: } } + +export interface IDesigner extends Designer {} diff --git a/packages/designer/src/designer/dragon.ts b/packages/designer/src/designer/dragon.ts index 8dcce2b4a..5c4d0648d 100644 --- a/packages/designer/src/designer/dragon.ts +++ b/packages/designer/src/designer/dragon.ts @@ -95,17 +95,13 @@ function isDragEvent(e: any): e is DragEvent { return e?.type?.startsWith('drag'); } -export interface IDragon extends IPublicModelDragon< - INode, - ILocateEvent -> { - emitter: IEventBus; -} - /** * Drag-on 拖拽引擎 */ -export class Dragon implements IDragon { +export class Dragon implements IPublicModelDragon< + INode, + ILocateEvent +> { private sensors: IPublicModelSensor[] = []; private nodeInstPointerEvents: boolean; @@ -637,3 +633,5 @@ export class Dragon implements IDragon { }; } } + +export interface IDragon extends Dragon { } diff --git a/packages/designer/src/designer/offset-observer.ts b/packages/designer/src/designer/offset-observer.ts index 2cf5bfee2..3c37be02d 100644 --- a/packages/designer/src/designer/offset-observer.ts +++ b/packages/designer/src/designer/offset-observer.ts @@ -28,11 +28,11 @@ export class OffsetObserver { @obx private _bottom = 0; @computed get height() { - return this.isRoot ? this.viewport.height : this._height * this.scale; + return this.isRoot ? this.viewport?.height : this._height * this.scale; } @computed get width() { - return this.isRoot ? this.viewport.width : this._width * this.scale; + return this.isRoot ? this.viewport?.width : this._width * this.scale; } @computed get top() { @@ -44,51 +44,51 @@ export class OffsetObserver { } @computed get bottom() { - return this.isRoot ? this.viewport.height : this._bottom * this.scale; + return this.isRoot ? this.viewport?.height : this._bottom * this.scale; } @computed get right() { - return this.isRoot ? this.viewport.width : this._right * this.scale; + return this.isRoot ? this.viewport?.width : this._right * this.scale; } @obx hasOffset = false; @computed get offsetLeft() { if (this.isRoot) { - return this.viewport.scrollX * this.scale; + return (this.viewport?.scrollX || 0) * this.scale; } - if (!this.viewport.scrolling || this.lastOffsetLeft == null) { - this.lastOffsetLeft = this.left + this.viewport.scrollX * this.scale; + if (!this.viewport?.scrolling || this.lastOffsetLeft == null) { + this.lastOffsetLeft = this.left + (this.viewport?.scrollX || 0) * this.scale; } return this.lastOffsetLeft; } @computed get offsetTop() { if (this.isRoot) { - return this.viewport.scrollY * this.scale; + return (this.viewport?.scrollY || 0) * this.scale; } - if (!this.viewport.scrolling || this.lastOffsetTop == null) { - this.lastOffsetTop = this.top + this.viewport.scrollY * this.scale; + if (!this.viewport?.scrolling || this.lastOffsetTop == null) { + this.lastOffsetTop = this.top + (this.viewport?.scrollY || 0) * this.scale; } return this.lastOffsetTop; } @computed get offsetHeight() { - if (!this.viewport.scrolling || this.lastOffsetHeight == null) { - this.lastOffsetHeight = this.isRoot ? this.viewport.height : this.height; + if (!this.viewport?.scrolling || this.lastOffsetHeight == null) { + this.lastOffsetHeight = this.isRoot ? (this.viewport?.height || 0) : this.height; } return this.lastOffsetHeight; } @computed get offsetWidth() { - if (!this.viewport.scrolling || this.lastOffsetWidth == null) { - this.lastOffsetWidth = this.isRoot ? this.viewport.width : this.width; + if (!(this.viewport?.scrolling || 0) || this.lastOffsetWidth == null) { + this.lastOffsetWidth = this.isRoot ? (this.viewport?.width || 0) : this.width; } return this.lastOffsetWidth; } @computed get scale() { - return this.viewport.scale; + return this.viewport?.scale || 0; } private pid: number | undefined; @@ -124,11 +124,11 @@ export class OffsetObserver { return; } - const rect = host.computeComponentInstanceRect(instance!, node.componentMeta.rootSelector); + const rect = host?.computeComponentInstanceRect(instance!, node.componentMeta.rootSelector); if (!rect) { this.hasOffset = false; - } else if (!this.viewport.scrolling || !this.hasOffset) { + } else if (!this.viewport?.scrolling || !this.hasOffset) { this._height = rect.height; this._width = rect.width; this._left = rect.left; diff --git a/packages/designer/src/document/document-model.ts b/packages/designer/src/document/document-model.ts index edca8fd81..2938395a9 100644 --- a/packages/designer/src/document/document-model.ts +++ b/packages/designer/src/document/document-model.ts @@ -56,104 +56,30 @@ export type GetDataType = T extends undefined : any : T; -export interface IDocumentModel extends Omit, - 'detecting' | - 'checkNesting' | - 'getNodeById' | - // 以下属性在内部的 document 中不存在 - 'exportSchema' | - 'importSchema' | - 'onAddNode' | - 'onRemoveNode' | - 'onChangeDetecting' | - 'onChangeSelection' | - 'onChangeNodeProp' | - 'onImportSchema' | - 'isDetectingNode' | - 'onFocusNodeChanged' | - 'onDropLocationChanged' +'detecting' | +'checkNesting' | +'getNodeById' | +// 以下属性在内部的 document 中不存在 +'exportSchema' | +'importSchema' | +'onAddNode' | +'onRemoveNode' | +'onChangeDetecting' | +'onChangeSelection' | +'onChangeNodeProp' | +'onImportSchema' | +'isDetectingNode' | +'onFocusNodeChanged' | +'onDropLocationChanged' > { - - readonly designer: IDesigner; - - selection: ISelection; - - get rootNode(): INode | null; - - get simulator(): ISimulatorHost | null; - - get active(): boolean; - - get nodesMap(): Map; - - /** - * 是否为非激活状态 - */ - get suspensed(): boolean; - - get fileName(): string; - - get currentRoot(): INode | null; - - isBlank(): boolean; - - /** - * 根据 id 获取节点 - */ - getNode(id: string): INode | null; - - getRoot(): INode | null; - - getHistory(): IHistory; - - checkNesting( - dropTarget: INode, - dragObject: IPublicTypeDragNodeObject | IPublicTypeNodeSchema | INode | IPublicTypeDragNodeDataObject, - ): boolean; - - getNodeCount(): number; - - nextId(possibleId: string | undefined): string; - - import(schema: IPublicTypeRootSchema, checkId?: boolean): void; - - export(stage: IPublicEnumTransformStage): IPublicTypeRootSchema | undefined; - - onNodeCreate(func: (node: INode) => void): IPublicTypeDisposable; - - onNodeDestroy(func: (node: INode) => void): IPublicTypeDisposable; - - onChangeNodeVisible(fn: (node: INode, visible: boolean) => void): IPublicTypeDisposable; - - addWillPurge(node: INode): void; - - removeWillPurge(node: INode): void; - - getComponentMeta(componentName: string): IComponentMeta; - - insertNodes(parent: INode, thing: INode[] | IPublicTypeNodeData[], at?: number | null, copy?: boolean): INode[]; - - open(): IDocumentModel; - - remove(): void; - - suspense(): void; - - close(): void; - - unlinkNode(node: INode): void; - - destroyNode(node: INode): void; -} - -export class DocumentModel implements IDocumentModel { /** * 根节点 类型有:Page/Component/Block */ @@ -322,7 +248,7 @@ export class DocumentModel implements IDocumentModel { // 兼容 vision this.id = project.getSchema()?.id || this.id; - this.rootNode = this.createNode( + this.rootNode = this.createNode( schema || { componentName: 'Page', id: 'root', @@ -425,7 +351,7 @@ export class DocumentModel implements IDocumentModel { * 根据 schema 创建一个节点 */ @action - createNode(data: GetDataType): T { + createNode(data: S): T { let schema: any; if (isDOMText(data) || isJSExpression(data)) { schema = { @@ -529,7 +455,7 @@ export class DocumentModel implements IDocumentModel { return null; } const wrapper = this.createNode(schema); - if (wrapper.isParental()) { + if (wrapper?.isParental()) { const first = nodes[0]; // TODO: check nesting rules x 2 insertChild(first.parent!, wrapper, first.index); @@ -538,7 +464,7 @@ export class DocumentModel implements IDocumentModel { return wrapper; } - this.removeNode(wrapper); + wrapper && this.removeNode(wrapper); return null; } @@ -928,3 +854,5 @@ export function isDocumentModel(obj: any): obj is IDocumentModel { export function isPageSchema(obj: any): obj is IPublicTypePageSchema { return obj?.componentName === 'Page'; } + +export interface IDocumentModel extends DocumentModel {} \ No newline at end of file diff --git a/packages/designer/src/document/node/node-children.ts b/packages/designer/src/document/node/node-children.ts index 65210fe62..b7f03d9fe 100644 --- a/packages/designer/src/document/node/node-children.ts +++ b/packages/designer/src/document/node/node-children.ts @@ -10,63 +10,12 @@ export interface IOnChangeOptions { node: Node; } -export interface INodeChildren extends Omit, - 'importSchema' | - 'exportSchema' | - 'isEmpty' | - 'notEmpty' +export class NodeChildren implements Omit, +'importSchema' | +'exportSchema' | +'isEmpty' | +'notEmpty' > { - children: INode[]; - - get owner(): INode; - - get length(): number; - - unlinkChild(node: INode): void; - - /** - * 删除一个节点 - */ - internalDelete( - node: INode, - purge: boolean, - useMutator: boolean, - options: NodeRemoveOptions - ): boolean; - - /** - * 插入一个节点,返回新长度 - */ - internalInsert(node: INode, at?: number | null, useMutator?: boolean): void; - - import(data?: IPublicTypeNodeData | IPublicTypeNodeData[], checkId?: boolean): void; - - /** - * 导出 schema - */ - export(stage: IPublicEnumTransformStage): IPublicTypeNodeData[]; - - /** following methods are overriding super interface, using different param types */ - /** overriding methods start */ - - forEach(fn: (item: INode, index: number) => void): void; - - /** - * 根据索引获得节点 - */ - get(index: number): INode | null; - - isEmpty(): boolean; - - notEmpty(): boolean; - - internalInitParent(): void; - - onChange(fn: (info?: IOnChangeOptions) => void): IPublicTypeDisposable; - - /** overriding methods end */ -} -export class NodeChildren implements INodeChildren { @obx.shallow children: INode[]; private emitter: IEventBus = createModuleEventBus('NodeChildren'); @@ -99,11 +48,10 @@ export class NodeChildren implements INodeChildren { constructor( readonly owner: INode, data: IPublicTypeNodeData | IPublicTypeNodeData[], - options: any = {}, ) { makeObservable(this); this.children = (Array.isArray(data) ? data : [data]).filter(child => !!child).map((child) => { - return this.owner.document?.createNode(child, options.checkId); + return this.owner.document?.createNode(child); }); } @@ -142,9 +90,12 @@ export class NodeChildren implements INodeChildren { node = child; node.import(item); } else { - node = this.owner.document?.createNode(item, checkId); + node = this.owner.document?.createNode(item); + } + + if (node) { + children[i] = node; } - children[i] = node; } this.children = children; @@ -176,13 +127,13 @@ export class NodeChildren implements INodeChildren { /** * 回收销毁 */ - purge(useMutator = true) { + purge() { if (this.purged) { return; } this.purged = true; this.children.forEach((child) => { - child.purge(useMutator); + child.purge(); }); } @@ -212,7 +163,7 @@ export class NodeChildren implements INodeChildren { node.internalPurgeStart(); if (node.isParentalNode) { foreachReverse( - node.children, + node.children!, (subNode: Node) => { subNode.remove(useMutator, purge, options); }, @@ -459,11 +410,11 @@ export class NodeChildren implements INodeChildren { const items = adder(this.children); if (items && items.length > 0) { items.forEach((child: IPublicTypeNodeData) => { - const node: INode = this.owner.document?.createNode(child); - this.children.push(node); - node.internalSetParent(this.owner); + const node: INode | null = this.owner.document?.createNode(child); + node && this.children.push(node); + node?.internalSetParent(this.owner); /* istanbul ignore next */ - const editor = node.document?.designer.editor; + const editor = node?.document?.designer.editor; editor?.eventBus.emit('node.add', { node }); }); changed = true; @@ -504,7 +455,7 @@ export class NodeChildren implements INodeChildren { try { callbacks?.onSubtreeModified.call( node.internalToShellNode(), - owner.internalToShellNode(), + owner.internalToShellNode()!, options, ); } catch (e) { @@ -517,3 +468,5 @@ export class NodeChildren implements INodeChildren { } } } + +export interface INodeChildren extends NodeChildren {} \ No newline at end of file diff --git a/packages/designer/src/document/node/node.ts b/packages/designer/src/document/node/node.ts index c8363d058..0b698831e 100644 --- a/packages/designer/src/document/node/node.ts +++ b/packages/designer/src/document/node/node.ts @@ -13,7 +13,6 @@ import { GlobalEvent, IPublicTypeComponentAction, IPublicModelNode, - IPublicModelExclusiveGroup, IPublicEnumTransformStage, IPublicTypeDisposable, IBaseModelNode, @@ -37,132 +36,7 @@ export interface NodeStatus { inPlaceEditing: boolean; } -export interface IBaseNode extends Omit, - 'isRoot' | - 'isPage' | - 'isComponent' | - 'isModal' | - 'isSlot' | - 'isParental' | - 'isLeaf' | - 'settingEntry' | - // 在内部的 node 模型中不存在 - 'getExtraPropValue' | - 'setExtraPropValue' | - 'exportSchema' | - 'visible' | - 'importSchema' | - // 内外实现有差异 - 'isContainer' | - 'isEmpty' -> { - isNode: boolean; - - get componentMeta(): IComponentMeta; - - get settingEntry(): ISettingTopEntry; - - get isPurged(): boolean; - - get index(): number | undefined; - - get isPurging(): boolean; - - getId(): string; - - getParent(): INode | null; - - /** - * 内部方法,请勿使用 - * @param useMutator 是否触发联动逻辑 - */ - internalSetParent(parent: INode | null, useMutator?: boolean): void; - - setConditionGroup(grp: IPublicModelExclusiveGroup | string | null): void; - - internalToShellNode(): IPublicModelNode | null; - - internalPurgeStart(): void; - - unlinkSlot(slotNode: INode): void; - - /** - * 导出 schema - */ - export(stage: IPublicEnumTransformStage, options?: any): T; - - emitPropChange(val: IPublicTypePropChangeOptions): void; - - import(data: Schema, checkId?: boolean): void; - - internalSetSlotFor(slotFor: Prop | null | undefined): void; - - addSlot(slotNode: INode): void; - - onVisibleChange(func: (flag: boolean) => any): () => void; - - getSuitablePlace(node: INode, ref: any): any; - - onChildrenChange(fn: (param?: { type: string; node: INode }) => void): IPublicTypeDisposable | undefined; - - onPropChange(func: (info: IPublicTypePropChangeOptions) => void): IPublicTypeDisposable; - - isModal(): boolean; - - isRoot(): boolean; - - isPage(): boolean; - - isComponent(): boolean; - - isSlot(): boolean; - - isParental(): boolean; - - isLeaf(): boolean; - - isContainer(): boolean; - - isEmpty(): boolean; - - remove( - useMutator?: boolean, - purge?: boolean, - options?: NodeRemoveOptions, - ): void; - - didDropIn(dragment: INode): void; - - didDropOut(dragment: INode): void; - - purge(): void; - - removeSlot(slotNode: INode): boolean; - - setVisible(flag: boolean): void; - - getVisible(): boolean; - - getChildren(): INodeChildren | null; - - clearPropValue(path: string | number): void; - - setProps(props?: IPublicTypePropsMap | IPublicTypePropsList | Props | null): void; - - mergeProps(props: IPublicTypePropsMap): void; - - /** 是否可以选中 */ - canSelect(): boolean; -} +export interface IBaseNode extends Node {} /** * 基础节点 @@ -212,7 +86,34 @@ export interface IBaseNode