mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-01-22 01:14:23 +00:00
feat: 支持引擎 init 时传入参数, 逐渐取代 editor 参数的功能
This commit is contained in:
parent
389fee41fa
commit
a9c4b97135
@ -357,7 +357,6 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
|
|||||||
await renderer.loadAsyncLibrary(this.asyncLibraryMap);
|
await renderer.loadAsyncLibrary(this.asyncLibraryMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// step 5 ready & render
|
// step 5 ready & render
|
||||||
renderer.run();
|
renderer.run();
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { untracked, computed, obx } from '@ali/lowcode-editor-core';
|
import { untracked, computed, obx, engineConfig } from '@ali/lowcode-editor-core';
|
||||||
import { CompositeValue, isJSExpression, isJSSlot, JSSlot, SlotSchema } from '@ali/lowcode-types';
|
import { CompositeValue, isJSExpression, isJSSlot, JSSlot, SlotSchema } from '@ali/lowcode-types';
|
||||||
import { uniqueId, isPlainObject, hasOwnProperty } from '@ali/lowcode-utils';
|
import { uniqueId, isPlainObject, hasOwnProperty } from '@ali/lowcode-utils';
|
||||||
import { PropStash } from './prop-stash';
|
import { PropStash } from './prop-stash';
|
||||||
@ -99,11 +99,13 @@ export class Prop implements IPropParent {
|
|||||||
|
|
||||||
export(stage: TransformStage = TransformStage.Save): CompositeValue | UNSET {
|
export(stage: TransformStage = TransformStage.Save): CompositeValue | UNSET {
|
||||||
const type = this._type;
|
const type = this._type;
|
||||||
|
|
||||||
// 在设计器里,所有组件都需要展示
|
|
||||||
if (stage === TransformStage.Render && this.key === '___condition___') {
|
if (stage === TransformStage.Render && this.key === '___condition___') {
|
||||||
|
// 在设计器里,所有组件默认需要展示,除非开启了 enableCondition 配置
|
||||||
|
if (engineConfig.get('enableCondition') !== true) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return this._value;
|
||||||
|
}
|
||||||
|
|
||||||
if (type === 'unset') {
|
if (type === 'unset') {
|
||||||
// return UNSET; @康为 之后 review 下这块改造
|
// return UNSET; @康为 之后 review 下这块改造
|
||||||
|
|||||||
106
packages/editor-core/src/config.ts
Normal file
106
packages/editor-core/src/config.ts
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
export class EngineConfig {
|
||||||
|
private config: { [key: string]: any } = {};
|
||||||
|
|
||||||
|
private waits = new Map<
|
||||||
|
string,
|
||||||
|
Array<{
|
||||||
|
once?: boolean;
|
||||||
|
resolve:(data: any) => void;
|
||||||
|
}>
|
||||||
|
>();
|
||||||
|
|
||||||
|
constructor(config?: { [key: string]: any }) {
|
||||||
|
this.config = config || {};
|
||||||
|
}
|
||||||
|
|
||||||
|
has(key: string): boolean {
|
||||||
|
return this.config[key] !== undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
get(key: string): any {
|
||||||
|
return this.config[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
set(key: string, value: any) {
|
||||||
|
this.config[key] = value;
|
||||||
|
this.notifyGot(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
setConfig(config: { [key: string]: any }) {
|
||||||
|
if (config) {
|
||||||
|
Object.keys(config).forEach((key) => {
|
||||||
|
this.set(key, config[key]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onceGot(key: string): Promise<any> {
|
||||||
|
const val = this.config[key];
|
||||||
|
if (val !== undefined) {
|
||||||
|
return Promise.resolve(val);
|
||||||
|
}
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
this.setWait(key, resolve, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
onGot(key: string, fn: (data: any) => void): () => void {
|
||||||
|
const val = this.config?.[key];
|
||||||
|
if (val !== undefined) {
|
||||||
|
fn(val);
|
||||||
|
return () => {};
|
||||||
|
} else {
|
||||||
|
this.setWait(key, fn);
|
||||||
|
return () => {
|
||||||
|
this.delWait(key, fn);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private notifyGot(key: string) {
|
||||||
|
let waits = this.waits.get(key);
|
||||||
|
if (!waits) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
waits = waits.slice().reverse();
|
||||||
|
let i = waits.length;
|
||||||
|
while (i--) {
|
||||||
|
waits[i].resolve(this.get(key));
|
||||||
|
if (waits[i].once) {
|
||||||
|
waits.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (waits.length > 0) {
|
||||||
|
this.waits.set(key, waits);
|
||||||
|
} else {
|
||||||
|
this.waits.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private setWait(key: string, resolve: (data: any) => void, once?: boolean) {
|
||||||
|
const waits = this.waits.get(key);
|
||||||
|
if (waits) {
|
||||||
|
waits.push({ resolve, once });
|
||||||
|
} else {
|
||||||
|
this.waits.set(key, [{ resolve, once }]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private delWait(key: string, fn: any) {
|
||||||
|
const waits = this.waits.get(key);
|
||||||
|
if (!waits) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let i = waits.length;
|
||||||
|
while (i--) {
|
||||||
|
if (waits[i].resolve === fn) {
|
||||||
|
waits.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (waits.length < 1) {
|
||||||
|
this.waits.delete(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const engineConfig = new EngineConfig();
|
||||||
@ -4,3 +4,4 @@ export * from './utils';
|
|||||||
export * from './di';
|
export * from './di';
|
||||||
export * from './hotkey';
|
export * from './hotkey';
|
||||||
export * from './widgets';
|
export * from './widgets';
|
||||||
|
export * from './config';
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { createElement } from 'react';
|
import { createElement } from 'react';
|
||||||
import { render } from 'react-dom';
|
import { render } from 'react-dom';
|
||||||
import { globalContext, Editor } from '@ali/lowcode-editor-core';
|
import { globalContext, Editor, engineConfig } from '@ali/lowcode-editor-core';
|
||||||
import * as editorCabin from '@ali/lowcode-editor-core';
|
import * as editorCabin from '@ali/lowcode-editor-core';
|
||||||
import {
|
import {
|
||||||
Designer,
|
Designer,
|
||||||
@ -14,6 +14,7 @@ import * as skeletonCabin from '@ali/lowcode-editor-skeleton';
|
|||||||
import Outline, { OutlineBackupPane, getTreeMaster } from '@ali/lowcode-plugin-outline-pane';
|
import Outline, { OutlineBackupPane, getTreeMaster } from '@ali/lowcode-plugin-outline-pane';
|
||||||
import DesignerPlugin from '@ali/lowcode-plugin-designer';
|
import DesignerPlugin from '@ali/lowcode-plugin-designer';
|
||||||
import './modules/live-editing';
|
import './modules/live-editing';
|
||||||
|
import { isPlainObject } from '@ali/lowcode-utils';
|
||||||
|
|
||||||
export * from './modules/editor-types';
|
export * from './modules/editor-types';
|
||||||
export * from './modules/skeleton-types';
|
export * from './modules/skeleton-types';
|
||||||
@ -67,6 +68,7 @@ export {
|
|||||||
// store,
|
// store,
|
||||||
hotkey,
|
hotkey,
|
||||||
monitor,
|
monitor,
|
||||||
|
engineConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSelection = () => designer.currentDocument?.selection;
|
const getSelection = () => designer.currentDocument?.selection;
|
||||||
@ -95,6 +97,7 @@ const getSelection = () => designer.currentDocument?.selection;
|
|||||||
hotkey,
|
hotkey,
|
||||||
monitor,
|
monitor,
|
||||||
init,
|
init,
|
||||||
|
engineConfig,
|
||||||
};
|
};
|
||||||
|
|
||||||
// 注册默认的 setters
|
// 注册默认的 setters
|
||||||
@ -154,15 +157,55 @@ plugins.register((ctx: ILowCodePluginContext) => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
export async function init(container?: Element) {
|
interface EngineOptions {
|
||||||
let engineContainer = container;
|
/**
|
||||||
if (!engineContainer) {
|
* 是否开启 condition 的能力,默认在设计器中不管 condition 是啥都正常展示
|
||||||
|
*/
|
||||||
|
enableCondition?: boolean;
|
||||||
|
/**
|
||||||
|
* 设计模式,live 模式将会实时展示变量值
|
||||||
|
*/
|
||||||
|
designMode?: 'design' | 'live';
|
||||||
|
/**
|
||||||
|
* 设备类型
|
||||||
|
*/
|
||||||
|
device?: 'default' | 'mobile' | string;
|
||||||
|
/**
|
||||||
|
* 语言
|
||||||
|
*/
|
||||||
|
locale?: string;
|
||||||
|
/**
|
||||||
|
* 渲染器类型
|
||||||
|
*/
|
||||||
|
renderEnv?: 'react' | 'rax' | string;
|
||||||
|
/**
|
||||||
|
* 设备类型映射器,处理设计器与渲染器中 device 的映射
|
||||||
|
*/
|
||||||
|
deviceMapper?: {
|
||||||
|
transform: (originalDevice: string) => string;
|
||||||
|
};
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function init(container?: Element, options?: EngineOptions) {
|
||||||
|
let engineOptions = null;
|
||||||
|
let engineContainer = null;
|
||||||
|
if (isPlainObject(container)) {
|
||||||
|
engineOptions = container;
|
||||||
engineContainer = document.createElement('div');
|
engineContainer = document.createElement('div');
|
||||||
document.body.appendChild(engineContainer);
|
document.body.appendChild(engineContainer);
|
||||||
|
} else {
|
||||||
|
engineOptions = options;
|
||||||
|
engineContainer = container;
|
||||||
|
if (!container) {
|
||||||
|
engineContainer = document.createElement('div');
|
||||||
|
document.body.appendChild(engineContainer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
engineContainer.id = 'engine';
|
engineContainer.id = 'engine';
|
||||||
|
|
||||||
await plugins.init();
|
await plugins.init();
|
||||||
|
engineConfig.setConfig(engineOptions as any);
|
||||||
render(
|
render(
|
||||||
createElement(Workbench, {
|
createElement(Workbench, {
|
||||||
skeleton,
|
skeleton,
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
import React, { PureComponent } from 'react';
|
import React, { PureComponent } from 'react';
|
||||||
import { Editor } from '@ali/lowcode-editor-core';
|
import { Editor, engineConfig } from '@ali/lowcode-editor-core';
|
||||||
import { DesignerView, Designer } from '@ali/lowcode-designer';
|
import { DesignerView, Designer } from '@ali/lowcode-designer';
|
||||||
import { Asset } from '@ali/lowcode-utils';
|
import { Asset } from '@ali/lowcode-utils';
|
||||||
import './index.scss';
|
import './index.scss';
|
||||||
@ -49,14 +49,14 @@ export default class DesignerPlugin extends PureComponent<PluginProps, DesignerP
|
|||||||
const { editor } = this.props;
|
const { editor } = this.props;
|
||||||
try {
|
try {
|
||||||
const assets = await editor.onceGot('assets');
|
const assets = await editor.onceGot('assets');
|
||||||
const renderEnv = await editor.get('renderEnv');
|
const renderEnv = engineConfig.get('renderEnv') || editor.get('renderEnv');
|
||||||
const device = await editor.get('device');
|
const device = engineConfig.get('device') || editor.get('device');
|
||||||
const locale = await editor.get('locale');
|
const locale = engineConfig.get('locale') || editor.get('locale');
|
||||||
const designMode = await editor.get('designMode');
|
const designMode = engineConfig.get('designMode') || editor.get('designMode');
|
||||||
const deviceClassName = await editor.get('deviceClassName');
|
const deviceClassName = engineConfig.get('deviceClassName') || editor.get('deviceClassName');
|
||||||
const simulatorUrl = await editor.get('simulatorUrl');
|
const simulatorUrl = engineConfig.get('simulatorUrl') || editor.get('simulatorUrl');
|
||||||
// @TODO setupAssets 里设置 requestHandlersMap 不太合适
|
// @TODO setupAssets 里设置 requestHandlersMap 不太合适
|
||||||
const requestHandlersMap = await editor.get('requestHandlersMap');
|
const requestHandlersMap = engineConfig.get('requestHandlersMap') || editor.get('requestHandlersMap');
|
||||||
if (!this._mounted) {
|
if (!this._mounted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user