chore: extract debugger locale loader to injectable callback (#7051)

This commit is contained in:
白熱
2026-06-10 12:09:43 +08:00
committed by GitHub
parent cd2e980dee
commit fd9094bed5
16 changed files with 154 additions and 146 deletions
+6 -4
View File
@@ -14,25 +14,27 @@
* limitations under the License.
*/
import type { ILanguagePack, LocaleType, UniverInstanceType } from '@univerjs/core';
import type { MenuConfig } from '@univerjs/ui';
import { UniverInstanceType } from '@univerjs/core';
export const DEBUGGER_PLUGIN_CONFIG_KEY = 'debugger.config';
export const configSymbol = Symbol(DEBUGGER_PLUGIN_CONFIG_KEY);
export type UniverDebuggerLocaleLoader = (locale: LocaleType) => ILanguagePack | Promise<ILanguagePack>;
export interface IUniverDebuggerConfig {
menu?: MenuConfig;
fab?: boolean;
fabEntryUnitType?: UniverInstanceType;
fabEntryUnitType: UniverInstanceType;
localeLoader: UniverDebuggerLocaleLoader;
performanceMonitor?: {
enabled: boolean;
};
}
export const defaultPluginConfig: IUniverDebuggerConfig = {
export const defaultPluginConfig: Pick<IUniverDebuggerConfig, 'fab' | 'performanceMonitor'> = {
fab: true,
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
performanceMonitor: {
enabled: true,
},
+1 -1
View File
@@ -16,5 +16,5 @@
import './global.css';
export type { IUniverDebuggerConfig } from './config/config';
export type { IUniverDebuggerConfig, UniverDebuggerLocaleLoader } from './config/config';
export { UniverDebuggerPlugin } from './plugin';
+1 -1
View File
@@ -32,7 +32,7 @@ export class UniverDebuggerPlugin extends Plugin {
private _debuggerController!: DebuggerController;
constructor(
private readonly _config: Partial<IUniverDebuggerConfig> = defaultPluginConfig,
private readonly _config: IUniverDebuggerConfig,
@Inject(Injector) override readonly _injector: Injector,
@IConfigService private readonly _configService: IConfigService
) {
+33 -17
View File
@@ -16,7 +16,7 @@
import type { IDropdownMenuProps } from '@univerjs/design';
import type { IUniverDebuggerConfig } from '../config/config';
import { IConfigService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { IConfigService, UniverInstanceType } from '@univerjs/core';
import { borderClassName, clsx, DropdownMenu } from '@univerjs/design';
import { useDependency } from '@univerjs/ui';
import { DEBUGGER_PLUGIN_CONFIG_KEY } from '../config/config';
@@ -60,30 +60,46 @@ export function Fab() {
const user = useUser();
const dispose = useDispose();
const univerInstanceService = useDependency(IUniverInstanceService);
const unitType = univerInstanceService.getFocusedUnit()?.type;
if (!unitType) return null;
const items: IDropdownMenuProps['items'] = [
locale,
rtl,
darkMode,
theme,
const globalItems = [locale, rtl, darkMode, theme];
const lightweightItems = [
...globalItems,
watermark,
{ type: 'separator' },
];
const commonDebugItems = [
watermark,
{ type: 'separator' as const },
notification,
message,
dialog,
sidebar,
{ type: 'separator' },
(fabEntryUnitType === UniverInstanceType.UNIVER_SHEET || fabEntryUnitType === UniverInstanceType.UNIVER_DOC) && floatingDom,
fabEntryUnitType === UniverInstanceType.UNIVER_SHEET && cellContent,
fabEntryUnitType === UniverInstanceType.UNIVER_SHEET && units,
{ type: 'separator' as const },
snapshot,
editable,
fabEntryUnitType === UniverInstanceType.UNIVER_SHEET && user,
dispose,
].filter((item) => item !== null) as IDropdownMenuProps['items'];
];
const sheetItems = [
...globalItems,
watermark,
{ type: 'separator' as const },
notification,
message,
dialog,
sidebar,
{ type: 'separator' as const },
floatingDom,
cellContent,
units,
snapshot,
editable,
user,
dispose,
];
const items: IDropdownMenuProps['items'] = fabEntryUnitType === UniverInstanceType.UNIVER_BASE || fabEntryUnitType === UniverInstanceType.UNIVER_SLIDE
? lightweightItems
: fabEntryUnitType === UniverInstanceType.UNIVER_DOC
? [...globalItems, ...commonDebugItems, floatingDom].filter(Boolean) as IDropdownMenuProps['items']
: sheetItems.filter(Boolean) as IDropdownMenuProps['items'];
return (
<div
+15 -67
View File
@@ -14,9 +14,11 @@
* limitations under the License.
*/
import { LocaleService, LocaleType } from '@univerjs/core';
import type { IUniverDebuggerConfig } from '../config/config';
import { IConfigService, LocaleService, LocaleType } from '@univerjs/core';
import { useDependency } from '@univerjs/ui';
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import { DEBUGGER_PLUGIN_CONFIG_KEY } from '../config/config';
const locales = [
{
@@ -97,77 +99,23 @@ const locales = [
},
];
// eslint-disable-next-line max-lines-per-function
export function useLocale() {
const configService = useDependency(IConfigService);
const localeService = useDependency(LocaleService);
const configs = configService.getConfig<IUniverDebuggerConfig>(DEBUGGER_PLUGIN_CONFIG_KEY);
const localeLoader = configs?.localeLoader;
async function loadLocales(value: string) {
let locales;
switch (value) {
case LocaleType.ZH_CN:
locales = await import('@univerjs/mockdata/locales/zh-CN');
break;
case LocaleType.ZH_TW:
locales = await import('@univerjs/mockdata/locales/zh-TW');
break;
case LocaleType.ZH_HK:
locales = await import('@univerjs/mockdata/locales/zh-HK');
break;
case LocaleType.FR_FR:
locales = await import('@univerjs/mockdata/locales/fr-FR');
break;
case LocaleType.RU_RU:
locales = await import('@univerjs/mockdata/locales/ru-RU');
break;
case LocaleType.VI_VN:
locales = await import('@univerjs/mockdata/locales/vi-VN');
break;
case LocaleType.JA_JP:
locales = await import('@univerjs/mockdata/locales/ja-JP');
break;
case LocaleType.FA_IR:
locales = await import('@univerjs/mockdata/locales/fa-IR');
break;
case LocaleType.KO_KR:
locales = await import('@univerjs/mockdata/locales/ko-KR');
break;
case LocaleType.ES_ES:
locales = await import('@univerjs/mockdata/locales/es-ES');
break;
case LocaleType.CA_ES:
locales = await import('@univerjs/mockdata/locales/ca-ES');
break;
case LocaleType.SK_SK:
locales = await import('@univerjs/mockdata/locales/sk-SK');
break;
case LocaleType.PT_BR:
locales = await import('@univerjs/mockdata/locales/pt-BR');
break;
case LocaleType.DE_DE:
locales = await import('@univerjs/mockdata/locales/de-DE');
break;
case LocaleType.IT_IT:
locales = await import('@univerjs/mockdata/locales/it-IT');
break;
case LocaleType.ID_ID:
locales = await import('@univerjs/mockdata/locales/id-ID');
break;
case LocaleType.PL_PL:
locales = await import('@univerjs/mockdata/locales/pl-PL');
break;
case LocaleType.AR_SA:
locales = await import('@univerjs/mockdata/locales/ar-SA');
break;
case LocaleType.EN_US:
default:
locales = await import('@univerjs/mockdata/locales/en-US');
break;
const loadLocales = useCallback(async (value: string) => {
const locale = value as LocaleType;
if (!localeLoader) {
throw new Error('[UniverDebuggerPlugin]: localeLoader is required.');
}
const localePack = await localeLoader(locale);
localeService.load({
[value]: locales.default,
[locale]: localePack,
});
}
}, [localeLoader, localeService]);
useEffect(() => {
const locale = localStorage.getItem('local.locale');
@@ -177,7 +125,7 @@ export function useLocale() {
localeService.setLocale(locale as LocaleType);
});
}
}, []);
}, [loadLocales, localeService]);
const onSelect = async (value: string) => {
await loadLocales(value);
-11
View File
@@ -1,16 +1,5 @@
# @univerjs/mockdata
[![npm version](https://img.shields.io/npm/v/@univerjs/mockdata)](https://npmjs.org/packages/@univerjs/mockdata)
[![license](https://img.shields.io/npm/l/@univerjs/mockdata)](https://img.shields.io/npm/l/@univerjs/mockdata)
## Introduction
This package provides mock data for demo and e2e tests.
## Usage
### Installation
```shell
npm i @univerjs/mockdata
```
+47
View File
@@ -14,6 +14,53 @@
* limitations under the License.
*/
import type { ILanguagePack, LocaleType } from '@univerjs/core';
export * from './docs';
export * from './sheets';
export * from './slides';
export async function loadDebuggerLocale(locale: LocaleType): Promise<ILanguagePack> {
switch (locale) {
case 'arSA':
return (await import('./locales/ar-SA')).default;
case 'caES':
return (await import('./locales/ca-ES')).default;
case 'deDE':
return (await import('./locales/de-DE')).default;
case 'enUS':
return (await import('./locales/en-US')).default;
case 'esES':
return (await import('./locales/es-ES')).default;
case 'faIR':
return (await import('./locales/fa-IR')).default;
case 'frFR':
return (await import('./locales/fr-FR')).default;
case 'idID':
return (await import('./locales/id-ID')).default;
case 'itIT':
return (await import('./locales/it-IT')).default;
case 'jaJP':
return (await import('./locales/ja-JP')).default;
case 'koKR':
return (await import('./locales/ko-KR')).default;
case 'plPL':
return (await import('./locales/pl-PL')).default;
case 'ptBR':
return (await import('./locales/pt-BR')).default;
case 'ruRU':
return (await import('./locales/ru-RU')).default;
case 'skSK':
return (await import('./locales/sk-SK')).default;
case 'viVN':
return (await import('./locales/vi-VN')).default;
case 'zhCN':
return (await import('./locales/zh-CN')).default;
case 'zhHK':
return (await import('./locales/zh-HK')).default;
case 'zhTW':
return (await import('./locales/zh-TW')).default;
default:
return (await import('./locales/en-US')).default;
}
}
+7 -1
View File
@@ -26,12 +26,14 @@ import { UniverDocsThreadCommentUIPlugin } from '@univerjs/docs-thread-comment-u
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_DOCUMENT_DATA_SIMPLE } from '@univerjs/mockdata';
import { DEFAULT_DOCUMENT_DATA_SIMPLE, loadDebuggerLocale } from '@univerjs/mockdata';
import zhCN from '@univerjs/mockdata/locales/zh-CN';
import { UniverUIPlugin } from '@univerjs/ui';
import { UniverWatermarkPlugin } from '@univerjs/watermark';
import '@univerjs/docs/facade';
import '@univerjs/docs-ui/facade';
import '@univerjs/watermark/facade';
import '../global.css';
@@ -65,15 +67,19 @@ univer.registerPlugin(UniverDocsThreadCommentUIPlugin);
univer.registerPlugin(UniverDocsHyperLinkUIPlugin);
univer.registerPlugin(UniverDocsMentionUIPlugin);
univer.registerPlugin(UniverDocsQuickInsertUIPlugin);
univer.registerPlugin(UniverWatermarkPlugin);
if (!IS_E2E) {
univer.createUnit(UniverInstanceType.UNIVER_DOC, DEFAULT_DOCUMENT_DATA_SIMPLE);
univer.registerPlugin(UniverDebuggerPlugin, {
fabEntryUnitType: UniverInstanceType.UNIVER_DOC,
localeLoader: loadDebuggerLocale,
});
} else {
univer.registerPlugin(UniverDebuggerPlugin, {
fab: false,
fabEntryUnitType: UniverInstanceType.UNIVER_DOC,
localeLoader: loadDebuggerLocale,
performanceMonitor: {
enabled: false,
},
+6 -1
View File
@@ -16,7 +16,9 @@
import type { Plugin, PluginCtor } from '@univerjs/core';
import { UniverActionRecorderPlugin } from '@univerjs/action-recorder';
import { UniverInstanceType } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { loadDebuggerLocale } from '@univerjs/mockdata';
import { UniverSheetsCrosshairHighlightPlugin } from '@univerjs/sheets-crosshair-highlight';
import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace';
import { UniverSheetsHyperLinkUIPlugin } from '@univerjs/sheets-hyper-link-ui';
@@ -39,7 +41,10 @@ export default function getVeryLazyPlugins() {
];
if (!IS_E2E) {
plugins.push([UniverDebuggerPlugin]);
plugins.push([UniverDebuggerPlugin, {
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
}]);
plugins.push([UniverUniscriptPlugin, {
getWorkerUrl(_: string, label: string) {
if (label === 'json') {
+3 -1
View File
@@ -21,7 +21,7 @@ import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
import { DEFAULT_WORKBOOK_DATA_DEMO, loadDebuggerLocale } from '@univerjs/mockdata';
import zhCN from '@univerjs/mockdata/locales/zh-CN';
import { UniverNetworkPlugin } from '@univerjs/network';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
@@ -121,6 +121,8 @@ function createNewInstance() {
if (IS_E2E) {
univer.registerPlugin(UniverDebuggerPlugin, {
fab: false,
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
performanceMonitor: {
enabled: false,
},
+6 -1
View File
@@ -16,7 +16,9 @@
import type { Plugin, PluginCtor } from '@univerjs/core';
import { UniverActionRecorderPlugin } from '@univerjs/action-recorder';
import { UniverInstanceType } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { loadDebuggerLocale } from '@univerjs/mockdata';
import { UniverSheetsCrosshairHighlightPlugin } from '@univerjs/sheets-crosshair-highlight';
import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace';
import { UniverSheetsHyperLinkUIPlugin } from '@univerjs/sheets-hyper-link-ui';
@@ -38,7 +40,10 @@ export default function getVeryLazyPlugins() {
];
if (!IS_E2E) {
plugins.push([UniverDebuggerPlugin]);
plugins.push([UniverDebuggerPlugin, {
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
}]);
plugins.push([UniverUniscriptPlugin, {
getWorkerUrl(_: string, label: string) {
if (label === 'json') {
+3 -1
View File
@@ -20,7 +20,7 @@ import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { UNISCRIT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
import { loadDebuggerLocale, UNISCRIT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
import zhCN from '@univerjs/mockdata/locales/zh-CN';
import { UniverSheetsPlugin } from '@univerjs/sheets';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
@@ -76,6 +76,8 @@ univer.registerPlugin(UniverUniscriptPlugin, {
if (IS_E2E) {
univer.registerPlugin(UniverDebuggerPlugin, {
fab: false,
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
performanceMonitor: {
enabled: false,
},
+4 -38
View File
@@ -21,26 +21,9 @@ import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
import arSA from '@univerjs/mockdata/locales/ar-SA';
import caES from '@univerjs/mockdata/locales/ca-ES';
import deDE from '@univerjs/mockdata/locales/de-DE';
import { DEFAULT_WORKBOOK_DATA_DEMO, loadDebuggerLocale } from '@univerjs/mockdata';
import enUS from '@univerjs/mockdata/locales/en-US';
import esES from '@univerjs/mockdata/locales/es-ES';
import faIR from '@univerjs/mockdata/locales/fa-IR';
import frFR from '@univerjs/mockdata/locales/fr-FR';
import idID from '@univerjs/mockdata/locales/id-ID';
import itIT from '@univerjs/mockdata/locales/it-IT';
import jaJP from '@univerjs/mockdata/locales/ja-JP';
import koKR from '@univerjs/mockdata/locales/ko-KR';
import plPL from '@univerjs/mockdata/locales/pl-PL';
import ptBR from '@univerjs/mockdata/locales/pt-BR';
import ruRU from '@univerjs/mockdata/locales/ru-RU';
import skSK from '@univerjs/mockdata/locales/sk-SK';
import viVN from '@univerjs/mockdata/locales/vi-VN';
import zhCN from '@univerjs/mockdata/locales/zh-CN';
import zhHK from '@univerjs/mockdata/locales/zh-HK';
import zhTW from '@univerjs/mockdata/locales/zh-TW';
import { UniverNetworkPlugin } from '@univerjs/network';
import { UniverRPCMainThreadPlugin } from '@univerjs/rpc';
import { UniverSheetsPlugin } from '@univerjs/sheets';
@@ -106,29 +89,10 @@ export const mockUser = {
function createNewInstance() {
// univer
const univer = new Univer({
// theme: greenTheme,
darkMode: localStorage.getItem('local.darkMode') === 'dark',
locale: LocaleType.ZH_CN,
locale: LocaleType.EN_US,
locales: {
[LocaleType.AR_SA]: arSA,
[LocaleType.CA_ES]: caES,
[LocaleType.DE_DE]: deDE,
[LocaleType.EN_US]: enUS,
[LocaleType.ES_ES]: esES,
[LocaleType.FA_IR]: faIR,
[LocaleType.FR_FR]: frFR,
[LocaleType.ID_ID]: idID,
[LocaleType.IT_IT]: itIT,
[LocaleType.JA_JP]: jaJP,
[LocaleType.KO_KR]: koKR,
[LocaleType.PL_PL]: plPL,
[LocaleType.PT_BR]: ptBR,
[LocaleType.RU_RU]: ruRU,
[LocaleType.SK_SK]: skSK,
[LocaleType.VI_VN]: viVN,
[LocaleType.ZH_CN]: zhCN,
[LocaleType.ZH_HK]: zhHK,
[LocaleType.ZH_TW]: zhTW,
},
logLevel: LogLevel.VERBOSE,
});
@@ -177,6 +141,8 @@ function createNewInstance() {
if (IS_E2E) {
univer.registerPlugin(UniverDebuggerPlugin, {
fab: false,
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
performanceMonitor: {
enabled: false,
},
+6 -1
View File
@@ -16,7 +16,9 @@
import type { Plugin, PluginCtor } from '@univerjs/core';
import { UniverActionRecorderPlugin } from '@univerjs/action-recorder';
import { UniverInstanceType } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { loadDebuggerLocale } from '@univerjs/mockdata';
import { UniverSheetsCrosshairHighlightPlugin } from '@univerjs/sheets-crosshair-highlight';
import { UniverSheetsFindReplacePlugin } from '@univerjs/sheets-find-replace';
import { UniverSheetsHyperLinkUIPlugin } from '@univerjs/sheets-hyper-link-ui';
@@ -38,7 +40,10 @@ export default function getVeryLazyPlugins() {
];
if (!IS_E2E) {
plugins.push([UniverDebuggerPlugin]);
plugins.push([UniverDebuggerPlugin, {
fabEntryUnitType: UniverInstanceType.UNIVER_SHEET,
localeLoader: loadDebuggerLocale,
}]);
plugins.push([UniverUniscriptPlugin, {
getWorkerUrl(_: string, label: string) {
if (label === 'json') {
+14 -1
View File
@@ -15,18 +15,21 @@
*/
import { LocaleType, Univer, UniverInstanceType } from '@univerjs/core';
import { UniverDebuggerPlugin } from '@univerjs/debugger';
import { UniverDocsPlugin } from '@univerjs/docs';
import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
import { UniverDrawingPlugin } from '@univerjs/drawing';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
import { DEFAULT_SLIDE_DATA } from '@univerjs/mockdata';
import { DEFAULT_SLIDE_DATA, loadDebuggerLocale } from '@univerjs/mockdata';
import zhCN from '@univerjs/mockdata/locales/zh-CN';
import { UniverSlidesPlugin } from '@univerjs/slides';
import { UniverSlidesUIPlugin } from '@univerjs/slides-ui';
import { UniverUIPlugin } from '@univerjs/ui';
import { UniverWatermarkPlugin } from '@univerjs/watermark';
import '../global.css';
import '@univerjs/watermark/facade';
// univer
const univer = new Univer({
@@ -49,6 +52,16 @@ univer.registerPlugin(UniverFormulaEnginePlugin);
univer.registerPlugin(UniverDrawingPlugin);
univer.registerPlugin(UniverSlidesPlugin);
univer.registerPlugin(UniverSlidesUIPlugin);
univer.registerPlugin(UniverWatermarkPlugin);
univer.registerPlugin(UniverDebuggerPlugin, {
fab: false,
fabEntryUnitType: UniverInstanceType.UNIVER_SLIDE,
localeLoader: loadDebuggerLocale,
performanceMonitor: {
enabled: false,
},
});
univer.createUnit(UniverInstanceType.UNIVER_SLIDE, DEFAULT_SLIDE_DATA);
+2
View File
@@ -104,6 +104,8 @@ export class UniverWatermarkPlugin extends Plugin {
] as Dependency[]).forEach((d) => {
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_SHEET, d);
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_DOC, d);
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_SLIDE, d);
this._renderManagerSrv.registerRenderModule(UniverInstanceType.UNIVER_BASE, d);
});
}
}