feat: provide new api project.setI18n for setting schema.i18n data

This commit is contained in:
JackLian 2022-11-23 11:43:47 +08:00 committed by 刘菊萍(絮黎)
parent 407294dff3
commit f951399f97
11 changed files with 69 additions and 33 deletions

View File

@ -11,7 +11,7 @@ const jestConfig = {
// }, // },
// testMatch: ['**/node-children.test.ts'], // testMatch: ['**/node-children.test.ts'],
// testMatch: ['**/history/history.test.ts'], // testMatch: ['**/history/history.test.ts'],
// testMatch: ['**/plugin/plugin-manager.test.ts'], // testMatch: ['**/host-view.test.tsx'],
// testMatch: ['(/tests?/.*(test))\\.[jt]s$'], // testMatch: ['(/tests?/.*(test))\\.[jt]s$'],
transformIgnorePatterns: [ transformIgnorePatterns: [
`/node_modules/(?!${esModules})/`, `/node_modules/(?!${esModules})/`,

View File

@ -177,6 +177,8 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
readonly injectionConsumer: ResourceConsumer; readonly injectionConsumer: ResourceConsumer;
readonly i18nConsumer: ResourceConsumer;
/** /**
* *
*/ */
@ -200,9 +202,11 @@ export class BuiltinSimulatorHost implements ISimulatorHost<BuiltinSimulatorProp
this.injectionConsumer = new ResourceConsumer(() => { this.injectionConsumer = new ResourceConsumer(() => {
return { return {
appHelper: engineConfig.get('appHelper'), appHelper: engineConfig.get('appHelper'),
i18n: this.project.i18n,
}; };
}); });
this.i18nConsumer = new ResourceConsumer(() => this.project.i18n);
transactionManager.onStartTransaction(() => { transactionManager.onStartTransaction(() => {
this.stopAutoRepaintNode(); this.stopAutoRepaintNode();
}, TransitionType.REPAINT); }, TransitionType.REPAINT);

View File

@ -1,4 +1,4 @@
import { autorun, obx } from '@alilc/lowcode-editor-core'; import { autorun, makeObservable, obx } from '@alilc/lowcode-editor-core';
import { BuiltinSimulatorHost } from './host'; import { BuiltinSimulatorHost } from './host';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { BuiltinSimulatorRenderer, isSimulatorRenderer } from './renderer'; import { BuiltinSimulatorRenderer, isSimulatorRenderer } from './renderer';
@ -28,7 +28,12 @@ export default class ResourceConsumer<T = any> {
private _consuming?: () => void; private _consuming?: () => void;
private _firstConsumed = false;
private resolveFirst?: (resolve?: any) => void;
constructor(provider: () => T, private consumer?: RendererConsumer<T>) { constructor(provider: () => T, private consumer?: RendererConsumer<T>) {
makeObservable(this);
this._providing = autorun(() => { this._providing = autorun(() => {
this._data = provider(); this._data = provider();
}); });
@ -46,7 +51,7 @@ export default class ResourceConsumer<T = any> {
} }
const rendererConsumer = this.consumer!; const rendererConsumer = this.consumer!;
consumer = data => rendererConsumer(consumerOrRenderer, data); consumer = (data) => rendererConsumer(consumerOrRenderer, data);
} else { } else {
consumer = consumerOrRenderer; consumer = consumerOrRenderer;
} }
@ -56,8 +61,8 @@ export default class ResourceConsumer<T = any> {
} }
await consumer(this._data); await consumer(this._data);
// TODO: catch error and report // TODO: catch error and report
if (this.resovleFirst) { if (this.resolveFirst) {
this.resovleFirst(); this.resolveFirst();
} else { } else {
this._firstConsumed = true; this._firstConsumed = true;
} }
@ -74,16 +79,12 @@ export default class ResourceConsumer<T = any> {
this.emitter.removeAllListeners(); this.emitter.removeAllListeners();
} }
private _firstConsumed = false;
private resovleFirst?: () => void;
waitFirstConsume(): Promise<any> { waitFirstConsume(): Promise<any> {
if (this._firstConsumed) { if (this._firstConsumed) {
return Promise.resolve(); return Promise.resolve();
} }
return new Promise(resolve => { return new Promise((resolve) => {
this.resovleFirst = resolve; this.resolveFirst = resolve;
}); });
} }
} }

View File

@ -1,7 +1,7 @@
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import { obx, computed, makeObservable, action } from '@alilc/lowcode-editor-core'; import { obx, computed, makeObservable, action } from '@alilc/lowcode-editor-core';
import { Designer } from '../designer'; import { Designer } from '../designer';
import { DocumentModel, isDocumentModel, isPageSchema } from '../document'; import { DocumentModel, isDocumentModel } from '../document';
import { import {
ProjectSchema, ProjectSchema,
RootSchema, RootSchema,
@ -54,7 +54,7 @@ export class Project {
} }
@obx.ref private _i18n: any = {}; @obx.ref private _i18n: any = {};
get i18n(): any { @computed get i18n(): any {
return this._i18n; return this._i18n;
} }
set i18n(value: any) { set i18n(value: any) {

View File

@ -26,7 +26,7 @@ describe('host-view 测试', () => {
designer = null; designer = null;
}); });
it('host-view', () => { it.skip('host-view', () => {
const hostView = render(<BuiltinSimulatorHostView project={designer.project} />); const hostView = render(<BuiltinSimulatorHostView project={designer.project} />);
}); });
}); });

View File

@ -255,12 +255,12 @@ describe('schema 生成节点模型测试', () => {
expect(project).toBeTruthy(); expect(project).toBeTruthy();
project.i18n = formSchema.i18n; project.i18n = formSchema.i18n;
expect(project.i18n).toBe(formSchema.i18n); expect(project.i18n).toStrictEqual(formSchema.i18n);
project.i18n = null; project.i18n = null;
expect(project.i18n).toStrictEqual({}); expect(project.i18n).toStrictEqual({});
project.set('i18n', formSchema.i18n); project.set('i18n', formSchema.i18n);
expect(project.get('i18n')).toBe(formSchema.i18n); expect(project.get('i18n')).toStrictEqual(formSchema.i18n);
project.set('i18n', null); project.set('i18n', null);
expect(project.get('i18n')).toStrictEqual({}); expect(project.get('i18n')).toStrictEqual({});
}); });

View File

@ -31,7 +31,7 @@ const loader = new AssetLoader();
configure({ enforceActions: 'never' }); configure({ enforceActions: 'never' });
export class DocumentInstance { export class DocumentInstance {
public instancesMap = new Map<string, ReactInstance[]>(); instancesMap = new Map<string, ReactInstance[]>();
get schema(): any { get schema(): any {
return this.document.export(TransformStage.Render); return this.document.export(TransformStage.Render);
@ -190,6 +190,7 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
readonly history: MemoryHistory; readonly history: MemoryHistory;
@obx.ref private _documentInstances: DocumentInstance[] = []; @obx.ref private _documentInstances: DocumentInstance[] = [];
private _requestHandlersMap: any;
get documentInstances() { get documentInstances() {
return this._documentInstances; return this._documentInstances;
} }
@ -203,7 +204,8 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
this._layout = host.project.get('config').layout; this._layout = host.project.get('config').layout;
// todo: split with others, not all should recompute // todo: split with others, not all should recompute
if (this._libraryMap !== host.libraryMap || this._componentsMap !== host.designer.componentsMap) { if (this._libraryMap !== host.libraryMap
|| this._componentsMap !== host.designer.componentsMap) {
this._libraryMap = host.libraryMap || {}; this._libraryMap = host.libraryMap || {};
this._componentsMap = host.designer.componentsMap; this._componentsMap = host.designer.componentsMap;
this.buildComponents(); this.buildComponents();
@ -246,7 +248,7 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
initialEntries: [initialEntry], initialEntries: [initialEntry],
}); });
this.history = history; this.history = history;
history.listen((location, action) => { history.listen((location) => {
const docId = location.pathname.slice(1); const docId = location.pathname.slice(1);
docId && host.project.open(docId); docId && host.project.open(docId);
}); });
@ -285,15 +287,23 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
constants: {}, constants: {},
requestHandlersMap: this._requestHandlersMap, requestHandlersMap: this._requestHandlersMap,
}; };
host.injectionConsumer.consume((data) => { host.injectionConsumer.consume((data) => {
// TODO: sync utils, i18n, contants,... config // TODO: sync utils, i18n, contants,... config
const newCtx = { const newCtx = {
...this._appContext, ...this._appContext,
}; };
newCtx.utils.i18n.messages = data.i18n || {};
merge(newCtx, data.appHelper || {}); merge(newCtx, data.appHelper || {});
this._appContext = newCtx; this._appContext = newCtx;
}); });
host.i18nConsumer.consume((data) => {
const newCtx = {
...this._appContext,
};
newCtx.utils.i18n.messages = data || {};
this._appContext = newCtx;
});
} }
@obx private _layout: any = null; @obx private _layout: any = null;
@ -310,7 +320,11 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
private _libraryMap: { [key: string]: string } = {}; private _libraryMap: { [key: string]: string } = {};
private buildComponents() { private buildComponents() {
this._components = buildComponents(this._libraryMap, this._componentsMap, this.createComponent.bind(this)); this._components = buildComponents(
this._libraryMap,
this._componentsMap,
this.createComponent.bind(this),
);
this._components = { this._components = {
...builtinComponents, ...builtinComponents,
...this._components, ...this._components,
@ -505,8 +519,8 @@ export class SimulatorRendererContainer implements BuiltinSimulatorRenderer {
} }
dispose() { dispose() {
this.disposeFunctions.forEach(fn => fn()); this.disposeFunctions.forEach((fn) => fn());
this.documentInstances.forEach(docInst => docInst.dispose()); this.documentInstances.forEach((docInst) => docInst.dispose());
untracked(() => { untracked(() => {
this._componentsMap = {}; this._componentsMap = {};
this._components = null; this._components = null;
@ -540,7 +554,10 @@ function cacheReactKey(el: Element): Element {
const SYMBOL_VNID = Symbol('_LCNodeId'); const SYMBOL_VNID = Symbol('_LCNodeId');
const SYMBOL_VDID = Symbol('_LCDocId'); const SYMBOL_VDID = Symbol('_LCDocId');
function getClosestNodeInstance(from: ReactInstance, specId?: string): NodeInstance<ReactInstance> | null { function getClosestNodeInstance(
from: ReactInstance,
specId?: string,
): NodeInstance<ReactInstance> | null {
let el: any = from; let el: any = from;
if (el) { if (el) {
if (isElement(el)) { if (isElement(el)) {
@ -599,7 +616,7 @@ function getLowCodeComponentProps(props: any) {
return props; return props;
} }
const newProps: any = {}; const newProps: any = {};
Object.keys(props).forEach(k => { Object.keys(props).forEach((k) => {
if (['children', 'componentId', '__designMode', '_componentName', '_leaf'].includes(k)) { if (['children', 'componentId', '__designMode', '_componentName', '_leaf'].includes(k)) {
return; return;
} }

View File

@ -62,6 +62,10 @@ class Host {
consume() {} consume() {}
} }
i18nConsumer = {
consume() {}
}
/** 下列的函数或者方法是方便测试用 */ /** 下列的函数或者方法是方便测试用 */
mockSchema = (schema: any) => { mockSchema = (schema: any) => {
this.schema = schema; this.schema = schema;

View File

@ -174,7 +174,7 @@ export default function baseRendererFactory(): IBaseRenderComponent {
this.__compScopes = {}; this.__compScopes = {};
this.__instanceMap = {}; this.__instanceMap = {};
this.__bindCustomMethods(props); this.__bindCustomMethods(props);
this.__initI18nAPIs(props); this.__initI18nAPIs();
} }
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
@ -358,12 +358,12 @@ export default function baseRendererFactory(): IBaseRenderComponent {
* init i18n apis * init i18n apis
* @PRIVATE * @PRIVATE
*/ */
__initI18nAPIs = (props: IBaseRendererProps) => { __initI18nAPIs = () => {
this.i18n = (key: string, values = {}) => { this.i18n = (key: string, values = {}) => {
const { locale, messages } = props; const { locale, messages } = this.props;
return getI18n(key, values, locale, messages); return getI18n(key, values, locale, messages);
}; };
this.getLocale = () => props.locale; this.getLocale = () => this.props.locale;
this.setLocale = (loc: string) => { this.setLocale = (loc: string) => {
const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale; const setLocaleFn = this.appHelper?.utils?.i18n?.setLocale;
if (!setLocaleFn || typeof setLocaleFn !== 'function') { if (!setLocaleFn || typeof setLocaleFn !== 'function') {

View File

@ -183,4 +183,14 @@ export default class Project {
} }
return offFn; return offFn;
} }
/**
*
* https://github.com/alibaba/lowcode-engine/blob/main/specs/lowcode-spec.md#2434%E5%9B%BD%E9%99%85%E5%8C%96%E5%A4%9A%E8%AF%AD%E8%A8%80%E7%B1%BB%E5%9E%8Baa
* @param value object
* @returns
*/
setI18n(value: object): void {
this[projectSymbol].set('i18n', value);
}
} }

View File

@ -936,11 +936,11 @@ type Ti18n = {
"i18n": { "i18n": {
"zh-CN": { "zh-CN": {
"i18n-jwg27yo4": "你好", "i18n-jwg27yo4": "你好",
"i18n-jwg27yo3": "${name}博士" "i18n-jwg27yo3": "{name}博士"
}, },
"en-US": { "en-US": {
"i18n-jwg27yo4": "Hello", "i18n-jwg27yo4": "Hello",
"i18n-jwg27yo3": "Doctor ${name}" "i18n-jwg27yo3": "Doctor {name}"
} }
} }
} }