feat: modify the usage of resource registration

This commit is contained in:
liujuping 2023-01-10 10:49:36 +08:00 committed by 林熠
parent d3cc39ec9c
commit 7d526d1e83
29 changed files with 297 additions and 308 deletions

View File

@ -1,26 +0,0 @@
---
title: ResourceType
sidebar_position: 12
---
> **[@experimental](./#experimental)**<br/>
> **@types** [IPublicModelWindow](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/model/resource-type.ts)<br/>
> **@since** v1.1.0
## 变量
### description
资源描述
`@type {string}`
### icon
资源 icon
`@type {ReactElement}`
### name
`@type {string}`

View File

@ -15,6 +15,24 @@ sidebar_position: 12
`@type {string}`
### name
资源名字
`@type {string}`
### type
资源类型
`@type {string}`
### category
资源分类
`@type {string}`
### icon
资源 icon
@ -25,12 +43,4 @@ sidebar_position: 12
资源配置信息
`@type {Object}`
### resourceType
资源所属的资源类型
`@type {IPublicModelResourceType}`
关联模型 [IPublicModelResourceType](./resource-type)
`@type {Object}`

View File

@ -30,13 +30,13 @@ sidebar_position: 12
`@type {ReactElement}`
### resourceType
### resource
窗口资源类型
窗口对应资源
`@type {IPublicModelResourceType}`
`@type {IPublicModelResource}`
关联模型 [IPublicModelResourceType](./resource-type)
关联模型 [IPublicModelResource](./resource)
## 方法签名

View File

@ -64,10 +64,10 @@ get resourceList(): IPublicModelResource;
```typescript
/** 注册资源 */
registerResourceType(name: string, type: 'editor', options: IPublicResourceOptions): void;
registerResourceType(resourceTypeModel: IPublicTypeResourceType): void;
```
相关类型:[IPublicResourceOptions](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-options.ts)
相关类型:[IPublicTypeResourceType](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/resource-type.ts)
### onChangeWindows

View File

@ -39,12 +39,14 @@ import { ComponentActions } from '../component-actions';
const logger = new Logger({ level: 'warn', bizName: 'designer' });
export interface DesignerProps {
[key: string]: any;
editor: IPublicModelEditor;
shellModelFactory: IShellModelFactory;
className?: string;
style?: object;
defaultSchema?: IPublicTypeProjectSchema;
hotkeys?: object;
viewName?: string;
simulatorProps?: object | ((document: DocumentModel) => object);
simulatorComponent?: ComponentType<any>;
dragGhostComponent?: ComponentType<any>;
@ -55,8 +57,6 @@ export interface DesignerProps {
onDragstart?: (e: ILocateEvent) => void;
onDrag?: (e: ILocateEvent) => void;
onDragend?: (e: { dragObject: IPublicModelDragObject; copy: boolean }, loc?: DropLocation) => void;
viewName?: string;
[key: string]: any;
}
export interface IDesigner {
@ -71,7 +71,9 @@ export interface IDesigner {
}
export class Designer implements IDesigner {
dragon: Dragon;
public dragon: Dragon;
public viewName: string | undefined;
readonly componentActions = new ComponentActions();
@ -87,6 +89,24 @@ export class Designer implements IDesigner {
readonly shellModelFactory: IShellModelFactory;
private _dropLocation?: DropLocation;
private propsReducers = new Map<IPublicEnumTransformStage, IPublicTypePropsTransducer[]>();
private _lostComponentMetasMap = new Map<string, ComponentMeta>();
private props?: DesignerProps;
private oobxList: OffsetObserver[] = [];
@obx.ref private _componentMetasMap = new Map<string, ComponentMeta>();
@obx.ref private _simulatorComponent?: ComponentType<any>;
@obx.ref private _simulatorProps?: object | ((project: Project) => object);
@obx.ref private _suspensed = false;
get currentDocument() {
return this.project.currentDocument;
}
@ -99,8 +119,6 @@ export class Designer implements IDesigner {
return this.currentDocument?.selection;
}
viewName: string | undefined;
constructor(props: DesignerProps) {
makeObservable(this);
const { editor, viewName, shellModelFactory } = props;
@ -234,8 +252,6 @@ export class Designer implements IDesigner {
this.editor.eventBus.emit(`designer.${event}`, ...args);
}
private _dropLocation?: DropLocation;
get dropLocation() {
return this._dropLocation;
}
@ -270,8 +286,6 @@ export class Designer implements IDesigner {
return new Scroller(scrollable);
}
private oobxList: OffsetObserver[] = [];
createOffsetObserver(nodeInstance: INodeSelector): OffsetObserver | null {
const oobx = createOffsetObserver(nodeInstance);
this.clearOobxList();
@ -342,8 +356,6 @@ export class Designer implements IDesigner {
return { target, index };
}
private props?: DesignerProps;
setProps(nextProps: DesignerProps) {
const props = this.props ? { ...this.props, ...nextProps } : nextProps;
if (this.props) {
@ -421,14 +433,10 @@ export class Designer implements IDesigner {
return this.props?.[key];
}
@obx.ref private _simulatorComponent?: ComponentType<any>;
@computed get simulatorComponent(): ComponentType<any> | undefined {
return this._simulatorComponent;
}
@obx.ref private _simulatorProps?: object | ((project: Project) => object);
@computed get simulatorProps(): object {
if (typeof this._simulatorProps === 'function') {
return this._simulatorProps(this.project);
@ -451,8 +459,6 @@ export class Designer implements IDesigner {
};
}
@obx.ref private _suspensed = false;
get suspensed(): boolean {
return this._suspensed;
}
@ -473,16 +479,15 @@ export class Designer implements IDesigner {
this.project.load(schema);
}
@obx.ref private _componentMetasMap = new Map<string, ComponentMeta>();
private _lostComponentMetasMap = new Map<string, ComponentMeta>();
buildComponentMetasMap(metas: IPublicTypeComponentMetadata[]) {
metas.forEach((data) => this.createComponentMeta(data));
}
createComponentMeta(data: IPublicTypeComponentMetadata): ComponentMeta {
createComponentMeta(data: IPublicTypeComponentMetadata): ComponentMeta | null {
const key = data.componentName;
if (!key) {
return null;
}
let meta = this._componentMetasMap.get(key);
if (meta) {
meta.setMetadata(data);
@ -552,8 +557,6 @@ export class Designer implements IDesigner {
return maps;
}
private propsReducers = new Map<IPublicEnumTransformStage, IPublicTypePropsTransducer[]>();
transformProps(props: IPublicTypeCompositeObject | IPublicTypePropsList, node: Node, stage: IPublicEnumTransformStage) {
if (Array.isArray(props)) {
// current not support, make this future

View File

@ -1,4 +1,4 @@
import { IPublicApiWorkspace, IPublicResourceList, IPublicResourceOptions } from '@alilc/lowcode-types';
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeResourceType } from '@alilc/lowcode-types';
import { Workspace as InnerWorkSpace } from '@alilc/lowcode-workspace';
import { Plugins } from '@alilc/lowcode-shell';
import { Window } from '../model/window';
@ -32,8 +32,8 @@ export class Workspace implements IPublicApiWorkspace {
return new Window(this[workspaceSymbol].window);
}
registerResourceType(name: string, type: 'editor', options: IPublicResourceOptions): void {
this[workspaceSymbol].registerResourceType(name, type, options);
registerResourceType(resourceTypeModel: IPublicTypeResourceType): void {
this[workspaceSymbol].registerResourceType(resourceTypeModel);
}
openEditorWindow(resourceName: string, title: string, extra: Object, viewName?: string) {

View File

@ -14,5 +14,4 @@ export * from './props';
export * from './selection';
export * from './setting-prop-entry';
export * from './setting-top-entry';
export * from './resource';
export * from './resource-type';
export * from './resource';

View File

@ -1,22 +0,0 @@
import { IPublicModelResourceType } from '@alilc/lowcode-types';
import { ResourceType as InnerResourceType } from '@alilc/lowcode-workspace';
import { resourceTypeSymbol } from '../symbols';
export class ResourceType implements IPublicModelResourceType {
readonly [resourceTypeSymbol]: InnerResourceType;
constructor(resourceType: InnerResourceType) {
this[resourceTypeSymbol] = resourceType;
}
get name() {
return this[resourceTypeSymbol].name;
}
get description() {
return this[resourceTypeSymbol].options.description;
}
get icon() {
return this[resourceTypeSymbol].options.icon;
}
}

View File

@ -22,7 +22,15 @@ export class Resource implements IPublicModelResource {
return this[resourceSymbol].options;
}
get resourceType() {
return new ResourceType(this[resourceSymbol].resourceType);
get name() {
return this[resourceSymbol].resourceType.name;
}
get type() {
return this[resourceSymbol].resourceType.type;
}
get category() {
return this[resourceSymbol].category;
}
}

View File

@ -1,13 +1,13 @@
import { windowSymbol } from '../symbols';
import { IPublicModelWindow } from '@alilc/lowcode-types';
import { IPublicModelResource, IPublicModelWindow } from '@alilc/lowcode-types';
import { EditorWindow } from '@alilc/lowcode-workspace';
import { ResourceType } from './resource-type';
import { Resource } from './resource';
export class Window implements IPublicModelWindow {
private readonly [windowSymbol]: EditorWindow;
get id() {
return this[windowSymbol].id;
return this[windowSymbol]?.id;
}
get title() {
@ -18,8 +18,8 @@ export class Window implements IPublicModelWindow {
return this[windowSymbol].icon;
}
get resourceType(): ResourceType {
return new ResourceType(this[windowSymbol].resourceType);
get resource(): IPublicModelResource {
return new Resource(this[windowSymbol].resource);
}
constructor(editorWindow: EditorWindow) {

View File

@ -1,6 +1,5 @@
import { IPublicModelWindow } from '../model';
import { IPublicResourceOptions } from '../type';
import { IPublicApiPlugins, IPublicModelResource, IPublicResourceList } from '@alilc/lowcode-types';
import { IPublicApiPlugins, IPublicModelResource, IPublicResourceList, IPublicTypeResourceType } from '@alilc/lowcode-types';
export interface IPublicApiWorkspace {
@ -10,6 +9,11 @@ export interface IPublicApiWorkspace {
/** 当前设计器窗口 */
window: IPublicModelWindow;
plugins: IPublicApiPlugins;
/** 当前设计器的编辑窗口 */
windows: IPublicModelWindow[];
/** 获取资源树列表 */
get resourceList(): IPublicModelResource[];
@ -20,7 +24,7 @@ export interface IPublicApiWorkspace {
onResourceListChange(fn: (resourceList: IPublicResourceList) => void): () => void;
/** 注册资源 */
registerResourceType(resourceName: string, type: 'editor', options: IPublicResourceOptions): void;
registerResourceType(resourceTypeModel: IPublicTypeResourceType): void;
/** 打开视图窗口 */
openEditorWindow(resourceName: string, title: string, extra: Object, viewName?: string): void;
@ -34,14 +38,9 @@ export interface IPublicApiWorkspace {
/** 通过视图 id 移除窗口 */
removeEditorWindowById(id: string): void;
plugins: IPublicApiPlugins;
/** 当前设计器的编辑窗口 */
windows: IPublicModelWindow[];
/** 窗口新增/删除的事件 */
onChangeWindows: (fn: () => void) => void;
onChangeWindows(fn: () => void): void;
/** active 窗口变更事件 */
onChangeActiveWindow: (fn: () => void) => void;
onChangeActiveWindow(fn: () => void): void;
}

View File

@ -28,5 +28,4 @@ export * from './editor';
export * from './preference';
export * from './plugin-instance';
export * from './sensor';
export * from './resource-type';
export * from './resource';

View File

@ -1,7 +0,0 @@
import { ReactElement } from 'react';
export interface IPublicModelResourceType {
get description(): string | undefined;
get icon(): ReactElement | undefined;
}

View File

@ -2,11 +2,15 @@ import { ReactElement } from 'react';
import { IPublicModelResourceType } from './resource-type';
export interface IPublicModelResource {
get title(): string;
get title(): string | undefined;
get icon(): ReactElement | undefined;
get options(): Object;
get resourceType(): IPublicModelResourceType | undefined;
get name(): string | undefined;
get type(): string | undefined;
get category(): string | undefined;
}

View File

@ -1,18 +1,9 @@
import { ReactElement } from 'react';
import { IPublicTypeNodeSchema } from '../type';
import { IPublicModelResourceType } from './resource-type';
import { IPublicModelResource } from './resource';
export interface IPublicModelWindow {
/** 当前窗口导入 schema */
importSchema(schema: IPublicTypeNodeSchema): void;
/** 修改当前窗口视图类型 */
changeViewType(viewName: string): void;
/** 调用当前窗口视图保存钩子 */
save(): Promise<any>;
/** 窗口 id */
id: string;
@ -23,5 +14,14 @@ export interface IPublicModelWindow {
icon?: ReactElement;
/** 窗口资源类型 */
resourceType?: IPublicModelResourceType;
resource?: IPublicModelResource;
/** 当前窗口导入 schema */
importSchema(schema: IPublicTypeNodeSchema): void;
/** 修改当前窗口视图类型 */
changeViewType(viewName: string): void;
/** 调用当前窗口视图保存钩子 */
save(): Promise<any>;
}

View File

@ -0,0 +1,8 @@
export interface IPublicEditorViewConfig {
/** 视图初始化钩子 */
init?: () => Promise<void>;
/** 资源保存时,会调用视图的钩子 */
save?: () => Promise<void>;
}

View File

@ -0,0 +1,12 @@
import { IPublicEditorViewConfig } from './editor-view-config';
export interface IPublicTypeEditorView {
/** 资源名字 */
viewName: string;
/** 资源类型 */
viewType?: 'editor' | 'webview';
(ctx: any, options: any): IPublicEditorViewConfig;
}

View File

@ -73,7 +73,7 @@ export * from './tip-config';
export * from './widget-config-area';
export * from './hotkey-callback';
export * from './plugin-register-options';
export * from './resource-options';
export * from './resource-list';
export * from './engine-options';
export * from './on-change-options';
export * from './slot-schema';
@ -82,4 +82,8 @@ export * from './node-instance';
export * from './editor-value-key';
export * from './editor-get-options';
export * from './editor-get-result';
export * from './editor-register-options';
export * from './editor-register-options';
export * from './editor-view';
export * from './resource-type';
export * from './resource-type-config';
export * from './editor-view-config';

View File

@ -0,0 +1,10 @@
export interface IPublicResourceData {
resourceName: string;
title: string;
category: string;
options: {
[key: string]: any;
};
}
export type IPublicResourceList = IPublicResourceData[];

View File

@ -1,56 +0,0 @@
export interface IPublicViewFunctions {
/** 视图初始化钩子 */
init?: () => Promise<void>;
/** 资源保存时,会调用视图的钩子 */
save?: () => Promise<void>;
}
export interface IPublicEditorView {
/** 资源名字 */
viewName: string;
/** 资源类型 */
viewType?: 'editor' | 'webview';
(ctx: any, options: any): IPublicViewFunctions;
}
export interface IPublicResourceOptions {
/** 资源描述 */
description?: string;
/** 资源 icon 标识 */
icon?: React.ReactElement;
/** 默认视图类型 */
defaultViewType: string;
/** 资源视图 */
editorViews: IPublicEditorView[];
/** save 钩子 */
save?: (schema: {
[viewName: string]: any;
}) => Promise<void>;
/** import 钩子 */
import?: (schema: any) => Promise<{
[viewName: string]: any;
}>;
/** 默认标题 */
defaultTitle?: string;
}
export interface IPublicResourceData {
resourceName: string;
title: string;
options: {
[key: string]: any;
};
}
export type IPublicResourceList = IPublicResourceData[];

View File

@ -0,0 +1,31 @@
import { IPublicTypeEditorView } from './editor-view';
export interface IPublicResourceTypeConfig {
/** 资源描述 */
description?: string;
/** 资源 icon 标识 */
icon?: React.ReactElement;
/** 默认视图类型 */
defaultViewType: string;
/** 资源视图 */
editorViews: IPublicTypeEditorView[];
init?: () => void;
/** save 钩子 */
save?: (schema: {
[viewName: string]: any;
}) => Promise<void>;
/** import 钩子 */
import?: (schema: any) => Promise<{
[viewName: string]: any;
}>;
/** 默认标题 */
defaultTitle?: string;
}

View File

@ -0,0 +1,10 @@
import { IPublicModelPluginContext } from '../model';
import { IPublicResourceTypeConfig } from './resource-type-config';
export interface IPublicTypeResourceType {
resourceName: string;
resourceType: string;
(ctx: IPublicModelPluginContext): IPublicResourceTypeConfig;
}

View File

@ -1,5 +1,5 @@
import { makeObservable, obx } from '@alilc/lowcode-editor-core';
import { IPublicEditorView, IPublicViewFunctions } from '@alilc/lowcode-types';
import { IPublicEditorViewConfig, IPublicTypeEditorView } from '@alilc/lowcode-types';
import { flow } from 'mobx';
import { Workspace as InnerWorkspace } from '../workspace';
import { BasicContext } from '../base-context';
@ -9,31 +9,12 @@ import { getWebviewPlugin } from '../inner-plugins/webview';
export class Context extends BasicContext {
viewName = 'editor-view';
instance: IPublicViewFunctions;
instance: IPublicEditorViewConfig;
viewType: 'editor' | 'webview';
constructor(public workspace: InnerWorkspace, public editorWindow: EditorWindow, public editorView: IPublicEditorView, options: Object) {
super(workspace, editorView.viewName, editorWindow);
this.viewType = editorView.viewType || 'editor';
this.viewName = editorView.viewName;
this.instance = editorView(this.innerPlugins._getLowCodePluginContext({
pluginName: 'any',
}), options);
makeObservable(this);
}
@obx _activate = false;
setActivate = (_activate: boolean) => {
this._activate = _activate;
this.innerHotkey.activate(this._activate);
};
get active() {
return this._activate;
}
@obx isInit: boolean = false;
init = flow(function* (this: any) {
@ -48,6 +29,25 @@ export class Context extends BasicContext {
this.isInit = true;
});
constructor(public workspace: InnerWorkspace, public editorWindow: EditorWindow, public editorView: IPublicTypeEditorView, options: Object) {
super(workspace, editorView.viewName, editorWindow);
this.viewType = editorView.viewType || 'editor';
this.viewName = editorView.viewName;
this.instance = editorView(this.innerPlugins._getLowCodePluginContext({
pluginName: 'any',
}), options);
makeObservable(this);
}
setActivate = (_activate: boolean) => {
this._activate = _activate;
this.innerHotkey.activate(this._activate);
};
get active() {
return this._activate;
}
async save() {
return await this.instance?.save?.();
}

View File

@ -2,20 +2,24 @@ import { uniqueId } from '@alilc/lowcode-utils';
import { makeObservable, obx } from '@alilc/lowcode-editor-core';
import { Context } from '../editor-view/context';
import { Workspace } from '../workspace';
import { ResourceType } from '../resource-type';
import { Resource } from '../resource';
export class EditorWindow {
id: string = uniqueId('window');
icon: React.ReactElement | undefined;
constructor(readonly resourceType: ResourceType, readonly workspace: Workspace, public title: string | undefined = '', private options: Object = {}) {
@obx.ref editorView: Context;
@obx editorViews: Map<string, Context> = new Map<string, Context>();
constructor(readonly resource: Resource, readonly workspace: Workspace, public title: string | undefined = '', private options: Object = {}) {
makeObservable(this);
this.init();
this.icon = resourceType.icon;
this.icon = resource.icon;
}
async importSchema(schema: any) {
const newSchema = await this.resourceType.import(schema);
const newSchema = await this.resource.import(schema);
if (!newSchema) {
return;
@ -29,13 +33,13 @@ export class EditorWindow {
async save() {
const value: any = {};
const editorViews = this.resourceType.editorViews;
const editorViews = this.resource.editorViews;
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
const saveResult = await this.editorViews.get(name)?.save();
value[name] = saveResult;
}
return await this.resourceType.save(value);
return await this.resource.save(value);
}
async init() {
@ -45,7 +49,7 @@ export class EditorWindow {
}
initViewTypes = async () => {
const editorViews = this.resourceType.editorViews;
const editorViews = this.resource.editorViews;
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
await this.initViewType(name);
@ -56,7 +60,7 @@ export class EditorWindow {
};
execViewTypesInit = async () => {
const editorViews = this.resourceType.editorViews;
const editorViews = this.resource.editorViews;
for (let i = 0; i < editorViews.length; i++) {
const name = editorViews[i].viewName;
this.changeViewType(name);
@ -65,15 +69,11 @@ export class EditorWindow {
};
setDefaultViewType = () => {
this.changeViewType(this.resourceType.defaultViewType);
this.changeViewType(this.resource.defaultViewType);
};
@obx.ref editorView: Context;
@obx editorViews: Map<string, Context> = new Map<string, Context>();
initViewType = async (name: string) => {
const viewInfo = this.resourceType.getEditorView(name);
const viewInfo = this.resource.getEditorView(name);
if (this.editorViews.get(name)) {
return;
}

View File

@ -1,5 +1,4 @@
export { Workspace } from './workspace';
export { ResourceType } from './resource-type';
export * from './editor-window/context';
export * from './layouts/workbench';
export { Resource } from './resource';

View File

@ -48,9 +48,7 @@ class Contents extends Component<{ area: Area; itemClassName?: string }> {
right.push(content);
}
});
if (!center || !center.length) {
return null;
}
return (
<Fragment>
<div className="lc-top-area-left">{left}</div>

View File

@ -1,53 +1,14 @@
import { IPublicEditorView, IPublicModelResourceType, IPublicResourceOptions } from '@alilc/lowcode-types';
import { IPublicTypeResourceType } from '@alilc/lowcode-types';
export class ResourceType implements IPublicModelResourceType {
constructor(readonly name: string, readonly type: 'editor' | 'webview', options: IPublicResourceOptions) {
if (options.editorViews) {
options.editorViews.forEach((d: any) => {
this.editorViewMap.set(d.viewName, d);
});
}
this.options = options;
export class ResourceType {
constructor(readonly resourceTypeModel: IPublicTypeResourceType) {
}
get description() {
return this.options.description;
get name() {
return this.resourceTypeModel.resourceName;
}
options: IPublicResourceOptions;
editorViewMap: Map<string, IPublicEditorView> = new Map<string, IPublicEditorView>();
init(ctx: any) {
this.options.init(ctx);
}
get icon() {
return this.options.icon;
}
async import(schema: any) {
return await this.options.import?.(schema);
}
getEditorView(name: string) {
return this.editorViewMap.get(name);
}
get defaultViewType() {
return this.options.defaultViewType || this.editorViewMap.keys().next().value;
}
get editorViews() {
return Array.from(this.editorViewMap.values());
}
async save(value: any) {
return await this.options.save?.(value);
}
get title() {
return this.options.defaultTitle;
get type() {
return this.resourceTypeModel.resourceType;
}
}

View File

@ -1,29 +1,72 @@
import { IPublicModelResource, IPublicResourceData } from '@alilc/lowcode-types';
import { IPublicTypeEditorView, IPublicModelResource, IPublicResourceData, IPublicResourceTypeConfig } from '@alilc/lowcode-types';
import { Logger } from '@alilc/lowcode-utils';
import { BasicContext } from './base-context';
import { ResourceType } from './resource-type';
import { Workspace as InnerWorkSpace } from './workspace';
const logger = new Logger({ level: 'warn', bizName: 'workspace:resource' });
export class Resource implements IPublicModelResource {
constructor(readonly resourceData: IPublicResourceData, readonly resourceType: ResourceType) {
resourceTypeInstance: IPublicResourceTypeConfig;
editorViewMap: Map<string, IPublicTypeEditorView> = new Map<string, IPublicTypeEditorView>();
constructor(readonly resourceData: IPublicResourceData, readonly resourceType: ResourceType, workspace: InnerWorkSpace) {
this.resourceTypeInstance = resourceType.resourceTypeModel(new BasicContext(workspace, ''), {});
if (this.resourceTypeInstance.editorViews) {
this.resourceTypeInstance.editorViews.forEach((d: any) => {
this.editorViewMap.set(d.viewName, d);
});
}
if (!resourceType) {
logger.error(`resourceType[${resourceType}] is unValid.`);
}
}
get name() {
return this.resourceType.name;
}
get description() {
return this.resourceTypeInstance?.description;
}
get icon() {
return this.resourceType?.icon;
return this.resourceTypeInstance?.icon;
}
get type() {
return this.resourceData.resourceName;
return this.resourceType.type;
}
get title() {
return this.resourceData.title;
get title(): string | undefined {
return this.resourceData.title || this.resourceTypeInstance.defaultTitle;
}
get options() {
return this.resourceData.options;
}
get category() {
return this.resourceData?.category;
}
async import(schema: any) {
return await this.resourceTypeInstance.import?.(schema);
}
async save(value: any) {
return await this.resourceTypeInstance.save?.(value);
}
get editorViews() {
return this.resourceTypeInstance.editorViews;
}
get defaultViewType() {
return this.resourceTypeInstance.defaultViewType;
}
getEditorView(name: string) {
return this.editorViewMap.get(name);
}
}

View File

@ -1,7 +1,7 @@
import { Designer } from '@alilc/lowcode-designer';
import { createModuleEventBus, Editor, IEventBus, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { Plugins } from '@alilc/lowcode-shell';
import { IPublicApiWorkspace, IPublicResourceList, IPublicResourceOptions } from '@alilc/lowcode-types';
import { IPublicApiWorkspace, IPublicResourceList, IPublicTypeResourceType } from '@alilc/lowcode-types';
import { BasicContext } from './base-context';
import { EditorWindow } from './editor-window/context';
import { Resource } from './resource';
@ -16,7 +16,7 @@ enum event {
const CHANGE_EVENT = 'resource.list.change';
export class Workspace implements IPublicApiWorkspace {
private context: BasicContext;
context: BasicContext;
private emitter: IEventBus = createModuleEventBus('workspace');
@ -28,6 +28,18 @@ export class Workspace implements IPublicApiWorkspace {
return this.context.innerPlugins;
}
private _isActive = false;
windows: EditorWindow[] = [];
editorWindowMap: Map<string, EditorWindow> = new Map<string, EditorWindow>();
@obx.ref window: EditorWindow;
private resourceTypeMap: Map<string, ResourceType> = new Map();
private resourceList: Resource[] = [];
constructor(
readonly registryInnerPlugin: (designer: Designer, editor: Editor, plugins: Plugins) => Promise<void>,
readonly shellModelFactory: any,
@ -42,19 +54,18 @@ export class Workspace implements IPublicApiWorkspace {
}
initWindow() {
if (!this.defaultResource) {
if (!this.defaultResourceType) {
return;
}
const title = this.defaultResource.description;
this.window = new EditorWindow(this.defaultResource, this, title);
const title = this.defaultResourceType.name;
const resource = new Resource({}, this.defaultResourceType, this);
this.window = new EditorWindow(resource, this, title);
this.editorWindowMap.set(this.window.id, this.window);
this.windows.push(this.window);
this.emitChangeWindow();
this.emitChangeActiveWindow();
}
private _isActive = false;
get isActive() {
return this._isActive;
}
@ -63,22 +74,12 @@ export class Workspace implements IPublicApiWorkspace {
this._isActive = value;
}
windows: EditorWindow[] = [];
async registerResourceType(resourceTypeModel: IPublicTypeResourceType): Promise<void> {
if (resourceTypeModel.resourceType === 'editor') {
const resourceType = new ResourceType(resourceTypeModel);
this.resourceTypeMap.set(resourceTypeModel.resourceName, resourceType);
editorWindowMap: Map<string, EditorWindow> = new Map<string, EditorWindow>();
@obx.ref window: EditorWindow;
private resourceTypeMap: Map<string, ResourceType> = new Map();
private resourceList: Resource[] = [];
async registerResourceType(resourceName: string, type: 'editor' | 'webview', options: IPublicResourceOptions): Promise<void> {
if (type === 'editor') {
const resourceType = new ResourceType(resourceName, type, options);
this.resourceTypeMap.set(resourceName, resourceType);
if (!this.window && this.defaultResource) {
if (!this.window && this.defaultResourceType) {
this.initWindow();
}
}
@ -89,7 +90,7 @@ export class Workspace implements IPublicApiWorkspace {
}
setResourceList(resourceList: IPublicResourceList) {
this.resourceList = resourceList.map(d => new Resource(d, this.getResourceType(d.resourceName)));
this.resourceList = resourceList.map(d => new Resource(d, this.getResourceType(d.resourceName), this));
this.emitter.emit(CHANGE_EVENT, resourceList);
}
@ -104,9 +105,9 @@ export class Workspace implements IPublicApiWorkspace {
return this.resourceTypeMap.get(resourceName)!;
}
get defaultResource(): ResourceType | null {
if (this.resourceTypeMap.size > 1) {
return this.resourceTypeMap.values().next().value;
get defaultResourceType(): ResourceType | null {
if (this.resourceTypeMap.size >= 1) {
return Array.from(this.resourceTypeMap.values())[0];
}
return null;
@ -134,7 +135,7 @@ export class Workspace implements IPublicApiWorkspace {
}
removeEditorWindow(resourceName: string, title: string) {
const index = this.windows.findIndex(d => (d.resourceType.name === resourceName && d.title));
const index = this.windows.findIndex(d => (d.resource.name === resourceName && d.title));
this.remove(index);
}
@ -152,13 +153,14 @@ export class Workspace implements IPublicApiWorkspace {
console.error(`${name} is not available`);
return;
}
const filterWindows = this.windows.filter(d => (d.resourceType.name === name && d.title == title));
const filterWindows = this.windows.filter(d => (d.resource.name === name && d.title == title));
if (filterWindows && filterWindows.length) {
this.window = filterWindows[0];
this.emitChangeActiveWindow();
return;
}
this.window = new EditorWindow(resourceType, this, title, options);
const resource = new Resource({}, resourceType, this);
this.window = new EditorWindow(resource, this, title, options);
this.windows.push(this.window);
this.editorWindowMap.set(this.window.id, this.window);
this.emitChangeWindow();