feat: update props ts defined

This commit is contained in:
liujuping 2023-03-24 14:32:51 +08:00 committed by 林熠
parent 80fc9766b1
commit 8c073c3c95
4 changed files with 52 additions and 40 deletions

View File

@ -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<IPublicModelProp<
INode
>, '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<IPublicModelProp<
isUnset(): boolean;
key: string | number | undefined;
purge(): void;
setupItems(): IProp[] | null;
get type(): ValueTypes;
get size(): number;
get code(): string;
}
export type ValueTypes = 'unset' | 'literal' | 'map' | 'list' | 'expression' | 'slot';
@ -126,15 +136,15 @@ export class Prop implements IProp, IPropParent {
this._code = code;
}
private _slotNode?: INode;
private _slotNode?: INode | null;
get slotNode(): INode | null {
return this._slotNode || null;
}
@obx.shallow private _items: Prop[] | null = null;
@obx.shallow private _items: IProp[] | null = null;
@obx.shallow private _maps: Map<string | number, Prop> | null = null;
@obx.shallow private _maps: Map<string | number, IProp> | 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<string | number, Prop> | null = null;
private _prevMaps: Map<string | number, IProp> | 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<string, Prop>();
const maps = new Map<string, IProp>();
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<string | number, Prop> | null {
@computed private get maps(): Map<string | number, IProp> | 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<ISlotNode>(slotSchema);
this._slotNode = owner.document?.createNode<ISlotNode>(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<T>(fn: (item: Prop, key: number | string | undefined) => T): T[] | null {
map<T>(fn: (item: IProp, key: number | string | undefined) => T): T[] | null {
const { items } = this;
if (!items) {
return null;

View File

@ -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<IBaseModelProps<IProp>, | 'getExtraProp' | 'getExtraPropValue' | 'setExtraPropValue' | 'node'> {
export interface IProps extends Omit<IBaseModelProps<IProp>, | '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<IBaseModelProps<IProp>, | '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<IBaseModelProps<IProp>, | '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<string, Prop> {
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<T>(fn: (item: Prop, key: number | string | undefined) => T): T[] | null {
map<T>(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;
}

View File

@ -108,7 +108,7 @@ export interface IPublicModelDocumentModel<
* @param data
* @returns
*/
createNode(data: IPublicTypeNodeSchema): Node | null;
createNode<T = Node>(data: IPublicTypeNodeSchema): T | null;
/**
* /id

View File

@ -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;
}