feat: 新增 globalLocale API

This commit is contained in:
lihao.ylh 2021-12-29 20:04:21 +08:00
parent 2eeafb1db1
commit 70a81d2a77
6 changed files with 37 additions and 32 deletions

View File

@ -29,7 +29,7 @@ const languageMap: { [key: string]: string } = {
const LowcodeConfigKey = 'ali-lowcode-config'; const LowcodeConfigKey = 'ali-lowcode-config';
class AliGlobalLocale { class GlobalLocale {
private emitter = new EventEmitter(); private emitter = new EventEmitter();
@obx.ref private _locale?: string; @obx.ref private _locale?: string;
@ -114,7 +114,7 @@ class AliGlobalLocale {
return this.locale; return this.locale;
} }
onLocaleChange(fn: (locale: string) => void): () => void { onChangeLocale(fn: (locale: string) => void): () => void {
this.emitter.on('localechange', fn); this.emitter.on('localechange', fn);
return () => { return () => {
this.emitter.removeListener('localechange', fn); this.emitter.removeListener('localechange', fn);
@ -135,12 +135,13 @@ function hasLocalStorage(obj: any): obj is WindowLocalStorage {
return obj.localStorage; return obj.localStorage;
} }
let globalLocale: AliGlobalLocale; let globalLocale = new GlobalLocale();
if ((window as any).__aliGlobalLocale) { // let globalLocale: GlobalLocale;
globalLocale = (window as any).__aliGlobalLocale as any; // if ((window as any).__GlobalLocale) {
} else { // globalLocale = (window as any).__GlobalLocale as any;
globalLocale = new AliGlobalLocale(); // } else {
(window as any).__aliGlobalLocale = globalLocale; // globalLocale = new GlobalLocale();
} // (window as any).__GlobalLocale = globalLocale;
// }
export { globalLocale }; export { globalLocale };

View File

@ -1,6 +1,6 @@
import { ReactNode, Component, createElement } from 'react'; import { ReactNode, Component, createElement } from 'react';
import { IntlMessageFormat } from 'intl-messageformat'; import { IntlMessageFormat } from 'intl-messageformat';
import { globalLocale } from './ali-global-locale'; import { globalLocale } from './global-locale';
import { isI18nData } from '@ali/lowcode-types'; import { isI18nData } from '@ali/lowcode-types';
import { observer, computed } from '../utils'; import { observer, computed } from '../utils';

View File

@ -21,4 +21,4 @@ export {
IReactionOptions, IReactionOptions,
} from 'mobx'; } from 'mobx';
export * as mobx from 'mobx'; export * as mobx from 'mobx';
export { observer }; export { observer };

View File

@ -3,5 +3,5 @@
"compilerOptions": { "compilerOptions": {
"outDir": "lib" "outDir": "lib"
}, },
"include": ["./src/"], "include": ["./src/"]
} }

View File

@ -37,9 +37,9 @@ const editor = new Editor();
globalContext.register(editor, Editor); globalContext.register(editor, Editor);
globalContext.register(editor, 'editor'); globalContext.register(editor, 'editor');
const skeleton = new InnerSkeleton(editor); const innerSkeleton = new InnerSkeleton(editor);
editor.set(Skeleton, skeleton); editor.set(Skeleton, innerSkeleton);
editor.set('skeleton' as any, skeleton); editor.set('skeleton' as any, innerSkeleton);
const designer = new Designer({ editor }); const designer = new Designer({ editor });
editor.set(Designer, designer); editor.set(Designer, designer);
@ -48,7 +48,7 @@ editor.set('designer' as any, designer);
const plugins = new LowCodePluginManager(editor).toProxy(); const plugins = new LowCodePluginManager(editor).toProxy();
editor.set('plugins' as any, plugins); editor.set('plugins' as any, plugins);
const { project, currentSelection: selection } = designer; const { project: innerProject, currentSelection: selection } = designer;
const { Workbench } = skeletonCabin; const { Workbench } = skeletonCabin;
// const setters: Setters = { // const setters: Setters = {
// getSetter, // getSetter,
@ -57,9 +57,9 @@ const { Workbench } = skeletonCabin;
// }; // };
const hotkey = new Hotkey(); const hotkey = new Hotkey();
const project2 = new Project(project); const project = new Project(innerProject);
const skeleton2 = new Skeleton(skeleton); const skeleton = new Skeleton(innerSkeleton);
const setters2 = new Setters(); const setters = new Setters();
const material = new Material(editor); const material = new Material(editor);
const config = engineConfig; const config = engineConfig;
const event = new Event(editor, { prefix: 'common' }); const event = new Event(editor, { prefix: 'common' });
@ -67,13 +67,13 @@ const logger = getLogger({ level: 'warn', bizName: 'common' });
export { export {
// editor, // editor,
// editorCabin, editorCabin,
// skeleton, // skeleton,
// skeletonCabin, skeletonCabin,
// designer, // designer,
// designerCabin, designerCabin,
plugins, plugins,
// setters, setters,
project, project,
// selection, // selection,
/** /**
@ -84,8 +84,11 @@ export {
* *
*/ */
// store, // store,
// hotkey, hotkey,
utils, utils,
config,
event,
logger,
// engineConfig, // engineConfig,
}; };
@ -95,18 +98,18 @@ const getSelection = () => designer.currentDocument?.selection;
/** /**
* start * start
*/ */
editor, editor: event,
editorCabin,
skeletonCabin,
designer, designer,
designerCabin,
/** /**
* end * end
*/ */
editorCabin,
skeletonCabin,
designerCabin,
plugins, plugins,
skeleton: skeleton2, skeleton,
project: project2, project,
setters: setters2, setters,
material, material,
// get selection() { // get selection() {
// return getSelection(); // return getSelection();
@ -235,7 +238,7 @@ export async function init(container?: HTMLElement, options?: EngineOptions) {
await plugins.init(); await plugins.init();
render( render(
createElement(Workbench, { createElement(Workbench, {
skeleton, skeleton: innerSkeleton,
className: 'engine-main', className: 'engine-main',
topAreaItemClassName: 'engine-actionitem', topAreaItemClassName: 'engine-actionitem',
}), }),

View File

@ -9,4 +9,5 @@ export {
observer, observer,
getSetter, getSetter,
getSettersMap, getSettersMap,
globalLocale,
} from '@ali/lowcode-editor-core'; } from '@ali/lowcode-editor-core';