feat: 补充 component-meta 相关 API & 将 getXxx 改成 get xxx

This commit is contained in:
lihao.ylh 2021-12-23 11:12:31 +08:00
parent b1383fa773
commit a52fcafc45
15 changed files with 240 additions and 62 deletions

View File

@ -145,7 +145,6 @@ export class ComponentMeta {
} }
@computed get icon() { @computed get icon() {
// TODO: 标记下。这块需要康师傅加一下API页面正常渲染。
// give Slot default icon // give Slot default icon
// if _title is TitleConfig get _title.icon // if _title is TitleConfig get _title.icon
return ( return (

View File

@ -512,6 +512,10 @@ export class Designer {
return meta; return meta;
} }
getComponentMetasMap() {
return this._componentMetasMap;
}
@computed get componentsMap(): { [key: string]: NpmInfo | Component } { @computed get componentsMap(): { [key: string]: NpmInfo | Component } {
const maps: any = {}; const maps: any = {};
const designer = this; const designer = this;

View File

@ -347,6 +347,10 @@ export class NodeChildren {
return this.children.find(fn); return this.children.find(fn);
} }
reduce(fn: (acc: any, cur: Node) => any, initialValue: any) {
return this.children.reduce(fn, initialValue);
}
mergeChildren( mergeChildren(
remover: (node: Node, idx: number) => boolean, remover: (node: Node, idx: number) => boolean,
adder: (children: Node[]) => NodeData[] | null, adder: (children: Node[]) => NodeData[] | null,

View File

@ -15,11 +15,6 @@ import {
import { getLogger, Logger } from '@ali/lowcode-utils'; import { getLogger, Logger } from '@ali/lowcode-utils';
import { ILowCodePluginContext, PluginContextOptions } from './plugin-types'; import { ILowCodePluginContext, PluginContextOptions } from './plugin-types';
/**
* API
* 1. on[Will|Did]VerbNoun? https://code.visualstudio.com/api/references/vscode-api#events
* 2. Disposable
*/
export default class PluginContext implements ILowCodePluginContext { export default class PluginContext implements ILowCodePluginContext {
private readonly [editorSymbol]: Editor; private readonly [editorSymbol]: Editor;
private readonly [designerSymbol]: Designer; private readonly [designerSymbol]: Designer;

View File

@ -0,0 +1,75 @@
import { Editor } from '@ali/lowcode-editor-core';
import {
DocumentModel as InnerDocumentModel,
Node as InnerNode,
ParentalNode,
IOnChangeOptions as InnerIOnChangeOptions,
PropChangeOptions as InnerPropChangeOptions,
ComponentMeta as InnerComponentMeta,
} from '@ali/lowcode-designer';
import { TransformStage, RootSchema, NodeSchema, NodeData, GlobalEvent } from '@ali/lowcode-types';
import Node from './node';
import Selection from './selection';
import Detecting from './detecting';
import History from './history';
import Project from './project';
import Prop from './prop';
import { componentMetaSymbol } from './symbols';
export default class ComponentMeta {
private readonly [componentMetaSymbol]: InnerComponentMeta;
constructor(componentMeta: InnerComponentMeta) {
this[componentMetaSymbol] = componentMeta;
}
static create(componentMeta: InnerComponentMeta | null) {
if (!componentMeta) return null;
return new ComponentMeta(componentMeta);
}
get componentName(): string {
return this[componentMetaSymbol].componentName;
}
get isContainer(): boolean {
return this[componentMetaSymbol].isContainer;
}
get isMinimalRenderUnit(): boolean {
return this[componentMetaSymbol].isMinimalRenderUnit;
}
get isModal(): boolean {
return this[componentMetaSymbol].isModal;
}
get configure() {
return this[componentMetaSymbol].configure;
}
get title() {
return this[componentMetaSymbol].title;
}
get icon() {
return this[componentMetaSymbol].icon;
}
get npm() {
return this[componentMetaSymbol].npm;
}
get acceptable(): boolean {
return this[componentMetaSymbol].acceptable;
}
setNpm(npm: any) {
this[componentMetaSymbol].setNpm(npm);
}
getMetadata() {
return this[componentMetaSymbol].getMetadata();
}
}

View File

@ -52,7 +52,7 @@ export default class DocumentModel {
* project * project
* @returns * @returns
*/ */
getProject() { get project() {
return Project.create(this[documentSymbol].project); return Project.create(this[documentSymbol].project);
} }
@ -60,7 +60,7 @@ export default class DocumentModel {
* *
* @returns * @returns
*/ */
getRoot() { get root(): Node | null {
return Node.create(this[documentSymbol].getRoot()); return Node.create(this[documentSymbol].getRoot());
} }
@ -68,7 +68,7 @@ export default class DocumentModel {
* *
* @returns * @returns
*/ */
getNodesMap() { get nodesMap() {
const map = new Map<string, Node>(); const map = new Map<string, Node>();
for (let id in this[documentSymbol].nodesMap.keys()) { for (let id in this[documentSymbol].nodesMap.keys()) {
map.set(id, this.getNodeById(id)!); map.set(id, this.getNodeById(id)!);
@ -125,6 +125,15 @@ export default class DocumentModel {
return Node.create(node); return Node.create(node);
} }
/**
*
* @param data
* @returns
*/
createNode(data: any) {
return Node.create(this[documentSymbol].createNode(data));
}
/** /**
* /id * /id
* @param idOrNode * @param idOrNode

View File

@ -2,7 +2,7 @@ import { Editor as InnerEditor } from '@ali/lowcode-editor-core';
import { getLogger } from '@ali/lowcode-utils'; import { getLogger } from '@ali/lowcode-utils';
import { editorSymbol } from './symbols'; import { editorSymbol } from './symbols';
const logger = getLogger({ level: 'warn', bizName: 'shell:event:' }); const logger = getLogger({ level: 'warn', bizName: 'shell:event' });
type EventOptions = { type EventOptions = {
prefix: string; prefix: string;

View File

@ -13,6 +13,12 @@ import Hotkey from './hotkey';
import Skeleton from './skeleton'; import Skeleton from './skeleton';
export * from './symbols'; export * from './symbols';
/**
* shell API
* 1. on[Will|Did]VerbNoun? https://code.visualstudio.com/api/references/vscode-api#events
* 2. Disposable
* 3. .xxx getter 使 .getXxx()
*/
export { export {
DocumentModel, DocumentModel,
Detecting, Detecting,

View File

@ -11,6 +11,7 @@ import {
import { AssetsJson } from '@ali/lowcode-utils'; import { AssetsJson } from '@ali/lowcode-utils';
import { ComponentAction } from '@ali/lowcode-types'; import { ComponentAction } from '@ali/lowcode-types';
import { editorSymbol, designerSymbol } from './symbols'; import { editorSymbol, designerSymbol } from './symbols';
import ComponentMeta from './component-meta';
export default class Material { export default class Material {
private readonly [editorSymbol]: Editor; private readonly [editorSymbol]: Editor;
@ -21,6 +22,10 @@ export default class Material {
this[designerSymbol] = editor.get('designer')!; this[designerSymbol] = editor.get('designer')!;
} }
get componentsMap() {
return this[designerSymbol].componentsMap;
}
setAssets(assets: AssetsJson) { setAssets(assets: AssetsJson) {
return this[editorSymbol].setAssets(assets); return this[editorSymbol].setAssets(assets);
} }
@ -46,11 +51,16 @@ export default class Material {
} }
getComponentMeta(componentName: string) { getComponentMeta(componentName: string) {
return this[designerSymbol].getComponentMeta(componentName); return ComponentMeta.create(this[designerSymbol].getComponentMeta(componentName));
} }
getComponentsMap() { getComponentMetasMap() {
return this[designerSymbol].componentsMap; const map = new Map<string, ComponentMeta>();
const originalMap = this[designerSymbol].getComponentMetasMap();
for (let componentName in originalMap.keys()) {
map.set(componentName, this.getComponentMeta(componentName)!);
}
return map;
} }
addBuiltinComponentAction(action: ComponentAction) { addBuiltinComponentAction(action: ComponentAction) {

View File

@ -15,7 +15,7 @@ export default class NodeChildren {
return new NodeChildren(nodeChldren); return new NodeChildren(nodeChldren);
} }
getOwner() { get owner(): Node | null {
return Node.create(this[nodeChildrenSymbol].owner); return Node.create(this[nodeChildrenSymbol].owner);
} }
@ -91,6 +91,12 @@ export default class NodeChildren {
); );
} }
reduce(fn: (acc: any, cur: Node) => any, initialValue: any) {
return this[nodeChildrenSymbol].reduce((acc: any, cur: InnerNode) => {
return fn(acc, Node.create(cur)!);
}, initialValue);
}
mergeChildren( mergeChildren(
remover: (node: Node, idx: number) => boolean, remover: (node: Node, idx: number) => boolean,
adder: (children: Node[]) => any, adder: (children: Node[]) => any,

View File

@ -7,6 +7,7 @@ import { CompositeValue, NodeSchema, TransformStage } from '@ali/lowcode-types';
import Prop from './prop'; import Prop from './prop';
import DocumentModel from './document-model'; import DocumentModel from './document-model';
import NodeChildren from './node-children'; import NodeChildren from './node-children';
import ComponentMeta from './component-meta';
import { documentSymbol, nodeSymbol } from './symbols'; import { documentSymbol, nodeSymbol } from './symbols';
export default class Node { export default class Node {
@ -30,6 +31,50 @@ export default class Node {
return this[nodeSymbol].id; return this[nodeSymbol].id;
} }
get title() {
return this[nodeSymbol].title;
}
get isContainer() {
return this[nodeSymbol].isContainer();
}
get isRoot() {
return this[nodeSymbol].isRoot();
}
get isPage() {
return this[nodeSymbol].isPage();
}
get isComponent() {
return this[nodeSymbol].isComponent();
}
get isSlot() {
return this[nodeSymbol].isSlot();
}
get isParental() {
return this[nodeSymbol].isParental();
}
get isLeaf() {
return this[nodeSymbol].isLeaf();
}
get index() {
return this[nodeSymbol].index;
}
get icon() {
return this[nodeSymbol].icon;
}
get zLevel() {
return this[nodeSymbol].zLevel;
}
/** /**
* componentName * componentName
*/ */
@ -37,14 +82,70 @@ export default class Node {
return this[nodeSymbol].componentName; return this[nodeSymbol].componentName;
} }
get componentMeta() {
return ComponentMeta.create(this[nodeSymbol].componentMeta);
}
/** /**
* *
* @returns * @returns
*/ */
getDocumentModel() { get document() {
return DocumentModel.create(this[documentSymbol]); return DocumentModel.create(this[documentSymbol]);
} }
/**
*
* @returns
*/
get prevSibling(): Node | null {
return Node.create(this[nodeSymbol].prevSibling);
}
/**
*
* @returns
*/
get nextSibling(): Node | null {
return Node.create(this[nodeSymbol].nextSibling);
}
/**
*
* @returns
*/
get parent(): Node | null {
return Node.create(this[nodeSymbol].parent);
}
/**
*
* @returns
*/
get children() {
return NodeChildren.create(this[nodeSymbol].children);
}
get slots(): Node[] {
return this[nodeSymbol].slots.map((node: InnerNode) => Node.create(node)!);
}
get slotFor() {
return Prop.create(this[nodeSymbol].slotFor);
}
hasSlots() {
return this[nodeSymbol].hasSlots();
}
hasCondition() {
return this[nodeSymbol].hasCondition();
}
hasLoop() {
return this[nodeSymbol].hasLoop();
}
/** /**
* path * path
* @param path a / a.b / a.0 * @param path a / a.b / a.0
@ -103,38 +204,6 @@ export default class Node {
return this.getExtraProp(path)?.setValue(value); return this.getExtraProp(path)?.setValue(value);
} }
/**
*
* @returns
*/
getPrevSibling() {
return Node.create(this[nodeSymbol].prevSibling);
}
/**
*
* @returns
*/
getNextSibling() {
return Node.create(this[nodeSymbol].nextSibling);
}
/**
*
* @returns
*/
getParent() {
return Node.create(this[nodeSymbol].parent);
}
/**
*
* @returns
*/
getChildren() {
return NodeChildren.create(this[nodeSymbol].children);
}
/** /**
* *
* @param data * @param data

View File

@ -22,6 +22,14 @@ export default class Project {
return new Project(project); return new Project(project);
} }
/**
* project documents
* @returns
*/
get documents(): DocumentModel[] {
return this[projectSymbol].documents.map((doc) => DocumentModel.create(doc)!);
}
/** /**
* document * document
* @param doc * @param doc
@ -61,14 +69,6 @@ export default class Project {
return DocumentModel.create(this[projectSymbol].getDocument(id)); return DocumentModel.create(this[projectSymbol].getDocument(id));
} }
/**
* project documents
* @returns
*/
getDocuments(): DocumentModel[] {
return this[projectSymbol].documents.map((doc) => DocumentModel.create(doc)!);
}
/** /**
* project * project
* @returns * @returns

View File

@ -15,15 +15,15 @@ export default class Prop {
return new Prop(prop); return new Prop(prop);
} }
getId() { get id() {
return this[propSymbol].id; return this[propSymbol].id;
} }
getKey() { get key() {
return this[propSymbol].key; return this[propSymbol].key;
} }
getNode() { get node(): Node | null {
return Node.create(this[propSymbol].getNode()); return Node.create(this[propSymbol].getNode());
} }

View File

@ -15,6 +15,10 @@ export default class Selection {
this[selectionSymbol] = document.selection; this[selectionSymbol] = document.selection;
} }
get selected() {
return this[selectionSymbol].selected;
}
select(id: string) { select(id: string) {
this[selectionSymbol].select(id); this[selectionSymbol].select(id);
} }
@ -23,10 +27,6 @@ export default class Selection {
this[selectionSymbol].selectAll(ids); this[selectionSymbol].selectAll(ids);
} }
getSelected() {
return this[selectionSymbol].selected;
}
remove(id: string) { remove(id: string) {
this[selectionSymbol].remove(id); this[selectionSymbol].remove(id);
} }

View File

@ -13,5 +13,6 @@ export const propSymbol = Symbol('prop');
export const detectingSymbol = Symbol('detecting'); export const detectingSymbol = Symbol('detecting');
export const selectionSymbol = Symbol('selection'); export const selectionSymbol = Symbol('selection');
export const historySymbol = Symbol('history'); export const historySymbol = Symbol('history');
export const componentMetaSymbol = Symbol('componentMeta');
export const simulatorHostSymbol = Symbol('simulatorHost'); export const simulatorHostSymbol = Symbol('simulatorHost');
export const simulatorRendererSymbol = Symbol('simulatorRenderer'); export const simulatorRendererSymbol = Symbol('simulatorRenderer');