From 8c073c3c95585ea426d28dbe43d9b58021b74115 Mon Sep 17 00:00:00 2001 From: liujuping Date: Fri, 24 Mar 2023 14:32:51 +0800 Subject: [PATCH] feat: update props ts defined --- .../designer/src/document/node/props/prop.ts | 52 +++++++++++-------- .../designer/src/document/node/props/props.ts | 35 +++++++------ .../types/src/shell/model/document-model.ts | 2 +- packages/types/src/shell/type/slot-schema.ts | 3 +- 4 files changed, 52 insertions(+), 40 deletions(-) diff --git a/packages/designer/src/document/node/props/prop.ts b/packages/designer/src/document/node/props/prop.ts index f93f2b608..74e93c106 100644 --- a/packages/designer/src/document/node/props/prop.ts +++ b/packages/designer/src/document/node/props/prop.ts @@ -3,7 +3,8 @@ import { GlobalEvent, IPublicEnumTransformStage } from '@alilc/lowcode-types'; import type { IPublicTypeCompositeValue, IPublicTypeJSSlot, IPublicTypeSlotSchema, IPublicModelProp } from '@alilc/lowcode-types'; import { uniqueId, isPlainObject, hasOwnProperty, compatStage, isJSExpression, isJSSlot, isNodeSchema } from '@alilc/lowcode-utils'; import { valueToSource } from './value-to-source'; -import { IProps, IPropParent } from './props'; +import { IPropParent } from './props'; +import type { IProps } from './props'; import { ISlotNode, INode } from '../node'; // import { TransformStage } from '../transform-stage'; @@ -14,13 +15,14 @@ export type UNSET = typeof UNSET; export interface IProp extends Omit, 'exportSchema' | 'node'> { +>, 'exportSchema' | 'node'>, IPropParent { + key: string | number | undefined; readonly props: IProps; readonly owner: INode; - delete(prop: Prop): void; + delete(prop: IProp): void; export(stage: IPublicEnumTransformStage): IPublicTypeCompositeValue; @@ -36,7 +38,15 @@ export interface IProp extends Omit | null = null; + @obx.shallow private _maps: Map | null = null; /** * 作为 _maps 的一层缓存机制,主要是复用部分已存在的 Prop,保持响应式关系,比如: @@ -142,15 +152,15 @@ export class Prop implements IProp, IPropParent { * 导致假如外部有 mobx reaction(常见于 observer),此时响应式链路会被打断, * 因为 reaction 监听的是原 Prop(a) 的 _value,而不是新 Prop(a) 的 _value。 */ - private _prevMaps: Map | null = null; + private _prevMaps: Map | null = null; /** * 构造 items 属性,同时构造 maps 属性 */ - private get items(): Prop[] | null { + private get items(): IProp[] | null { if (this._items) return this._items; return runInAction(() => { - let items: Prop[] | null = null; + let items: IProp[] | null = null; if (this._type === 'list') { const data = this._value; data.forEach((item: any, idx: number) => { @@ -160,10 +170,10 @@ export class Prop implements IProp, IPropParent { this._maps = null; } else if (this._type === 'map') { const data = this._value; - const maps = new Map(); + const maps = new Map(); const keys = Object.keys(data); for (const key of keys) { - let prop: Prop; + let prop: IProp; if (this._prevMaps?.has(key)) { prop = this._prevMaps.get(key)!; prop.setValue(data[key]); @@ -184,7 +194,7 @@ export class Prop implements IProp, IPropParent { }); } - @computed private get maps(): Map | null { + @computed private get maps(): Map | null { if (!this.items) { return null; } @@ -434,7 +444,7 @@ export class Prop implements IProp, IPropParent { this._slotNode.import(slotSchema); } else { const { owner } = this.props; - this._slotNode = owner.document.createNode(slotSchema); + this._slotNode = owner.document?.createNode(slotSchema); if (this._slotNode) { owner.addSlot(this._slotNode); this._slotNode.internalSetSlotFor(this); @@ -465,7 +475,7 @@ export class Prop implements IProp, IPropParent { /** * @returns 0: the same 1: maybe & like 2: not the same */ - compare(other: Prop | null): number { + compare(other: IProp | null): number { if (!other || other.isUnset()) { return this.isUnset() ? 0 : 2; } @@ -489,7 +499,7 @@ export class Prop implements IProp, IPropParent { * @param createIfNone 当没有的时候,是否创建一个 */ @action - get(path: string | number, createIfNone = true): Prop | null { + get(path: string | number, createIfNone = true): IProp | null { const type = this._type; if (type !== 'map' && type !== 'list' && type !== 'unset' && !createIfNone) { return null; @@ -548,7 +558,7 @@ export class Prop implements IProp, IPropParent { * 删除项 */ @action - delete(prop: Prop): void { + delete(prop: IProp): void { /* istanbul ignore else */ if (this._items) { const i = this._items.indexOf(prop); @@ -582,7 +592,7 @@ export class Prop implements IProp, IPropParent { * @param force 强制 */ @action - add(value: IPublicTypeCompositeValue, force = false): Prop | null { + add(value: IPublicTypeCompositeValue, force = false): IProp | null { const type = this._type; if (type !== 'list' && type !== 'unset' && !force) { return null; @@ -688,7 +698,7 @@ export class Prop implements IProp, IPropParent { /** * 迭代器 */ - [Symbol.iterator](): { next(): { value: Prop } } { + [Symbol.iterator](): { next(): { value: IProp } } { let index = 0; const { items } = this; const length = items?.length || 0; @@ -712,7 +722,7 @@ export class Prop implements IProp, IPropParent { * 遍历 */ @action - forEach(fn: (item: Prop, key: number | string | undefined) => void): void { + forEach(fn: (item: IProp, key: number | string | undefined) => void): void { const { items } = this; if (!items) { return; @@ -727,7 +737,7 @@ export class Prop implements IProp, IPropParent { * 遍历 */ @action - map(fn: (item: Prop, key: number | string | undefined) => T): T[] | null { + map(fn: (item: IProp, key: number | string | undefined) => T): T[] | null { const { items } = this; if (!items) { return null; diff --git a/packages/designer/src/document/node/props/props.ts b/packages/designer/src/document/node/props/props.ts index 09c90849d..213592a5d 100644 --- a/packages/designer/src/document/node/props/props.ts +++ b/packages/designer/src/document/node/props/props.ts @@ -2,7 +2,8 @@ import { computed, makeObservable, obx, action } from '@alilc/lowcode-editor-cor import { IPublicTypePropsList, IPublicTypeCompositeValue, IPublicEnumTransformStage, IBaseModelProps } from '@alilc/lowcode-types'; import type { IPublicTypePropsMap } from '@alilc/lowcode-types'; import { uniqueId, compatStage } from '@alilc/lowcode-utils'; -import { Prop, IProp, UNSET } from './prop'; +import { Prop, UNSET } from './prop'; +import type { IProp } from './prop'; import { INode } from '../node'; // import { TransformStage } from '../transform-stage'; @@ -27,23 +28,23 @@ export function getOriginalExtraKey(key: string): string { export interface IPropParent { - readonly props: Props; + readonly props: IProps; readonly owner: INode; get path(): string[]; - delete(prop: Prop): void; + delete(prop: IProp): void; } -export interface IProps extends Omit, | 'getExtraProp' | 'getExtraPropValue' | 'setExtraPropValue' | 'node'> { +export interface IProps extends Omit, | 'getExtraProp' | 'getExtraPropValue' | 'setExtraPropValue' | 'node'>, IPropParent { /** * 获取 props 对应的 node */ getNode(): INode; - get(path: string, createIfNone?: boolean): Prop | null; + get(path: string, createIfNone?: boolean): IProp | null; export(stage?: IPublicEnumTransformStage): { props?: IPublicTypePropsMap | IPublicTypePropsList; @@ -54,7 +55,7 @@ export interface IProps extends Omit, | 'getExtraProp' | purge(): void; - query(path: string, createIfNone: boolean): Prop | null; + query(path: string, createIfNone: boolean): IProp | null; import(value?: IPublicTypePropsMap | IPublicTypePropsList | null, extras?: ExtrasObject): void; } @@ -62,7 +63,7 @@ export interface IProps extends Omit, | 'getExtraProp' | export class Props implements IProps, IPropParent { readonly id = uniqueId('props'); - @obx.shallow private items: Prop[] = []; + @obx.shallow private items: IProp[] = []; @computed private get maps(): Map { const maps = new Map(); @@ -78,7 +79,7 @@ export class Props implements IProps, IPropParent { readonly path = []; - get props(): Props { + get props(): IProps { return this; } @@ -229,7 +230,7 @@ export class Props implements IProps, IPropParent { * @param createIfNone 当没有的时候,是否创建一个 */ @action - query(path: string, createIfNone = true): Prop | null { + query(path: string, createIfNone = true): IProp | null { return this.get(path, createIfNone); } @@ -238,7 +239,7 @@ export class Props implements IProps, IPropParent { * @param createIfNone 当没有的时候,是否创建一个 */ @action - get(path: string, createIfNone = false): Prop | null { + get(path: string, createIfNone = false): IProp | null { let entry = path; let nest = ''; const i = path.indexOf('.'); @@ -266,7 +267,7 @@ export class Props implements IProps, IPropParent { * 删除项 */ @action - delete(prop: Prop): void { + delete(prop: IProp): void { const i = this.items.indexOf(prop); if (i > -1) { this.items.splice(i, 1); @@ -298,7 +299,7 @@ export class Props implements IProps, IPropParent { key?: string | number, spread = false, options: any = {}, - ): Prop { + ): IProp { const prop = new Prop(this, value, key, spread, options); this.items.push(prop); return prop; @@ -314,7 +315,7 @@ export class Props implements IProps, IPropParent { /** * 迭代器 */ - [Symbol.iterator](): { next(): { value: Prop } } { + [Symbol.iterator](): { next(): { value: IProp } } { let index = 0; const { items } = this; const length = items.length || 0; @@ -338,7 +339,7 @@ export class Props implements IProps, IPropParent { * 遍历 */ @action - forEach(fn: (item: Prop, key: number | string | undefined) => void): void { + forEach(fn: (item: IProp, key: number | string | undefined) => void): void { this.items.forEach((item) => { return fn(item, item.key); }); @@ -348,14 +349,14 @@ export class Props implements IProps, IPropParent { * 遍历 */ @action - map(fn: (item: Prop, key: number | string | undefined) => T): T[] | null { + map(fn: (item: IProp, key: number | string | undefined) => T): T[] | null { return this.items.map((item) => { return fn(item, item.key); }); } @action - filter(fn: (item: Prop, key: number | string | undefined) => boolean) { + filter(fn: (item: IProp, key: number | string | undefined) => boolean) { return this.items.filter((item) => { return fn(item, item.key); }); @@ -378,7 +379,7 @@ export class Props implements IProps, IPropParent { * @param createIfNone 当没有的时候,是否创建一个 */ @action - getProp(path: string, createIfNone = true): Prop | null { + getProp(path: string, createIfNone = true): IProp | null { return this.query(path, createIfNone) || null; } diff --git a/packages/types/src/shell/model/document-model.ts b/packages/types/src/shell/model/document-model.ts index b11ac6f08..2ef0b532b 100644 --- a/packages/types/src/shell/model/document-model.ts +++ b/packages/types/src/shell/model/document-model.ts @@ -108,7 +108,7 @@ export interface IPublicModelDocumentModel< * @param data * @returns */ - createNode(data: IPublicTypeNodeSchema): Node | null; + createNode(data: IPublicTypeNodeSchema): T | null; /** * 移除指定节点/节点id diff --git a/packages/types/src/shell/type/slot-schema.ts b/packages/types/src/shell/type/slot-schema.ts index d4c1f5d97..8928a9824 100644 --- a/packages/types/src/shell/type/slot-schema.ts +++ b/packages/types/src/shell/type/slot-schema.ts @@ -1,3 +1,4 @@ +import { IPublicTypeNodeData } from './node-data'; import { IPublicTypeNodeSchema } from './node-schema'; /** @@ -13,5 +14,5 @@ export interface IPublicTypeSlotSchema extends IPublicTypeNodeSchema { slotName?: string; slotParams?: string[]; }; - children?: IPublicTypeNodeSchema[]; + children?: IPublicTypeNodeData[] | IPublicTypeNodeData; }