Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

xiugai

tonytonychopper123f23d3d881d38
diff --git a/packages/base-sheets/src/Controller/SheetContainerController.ts b/packages/base-sheets/src/Controller/SheetContainerController.ts
index e0f42400d6..4f3fb95bb5 100644
--- a/packages/base-sheets/src/Controller/SheetContainerController.ts
+++ b/packages/base-sheets/src/Controller/SheetContainerController.ts
@@ -1,65 +1,7 @@
-import { CommandManager, ContextBase, ISheetActionData, SheetActionBase } from '@univerjs/core';
-import { BaseComponentProps } from '@univerjs/base-ui';
-
+import { CommandManager, ISheetActionData, SheetActionBase } from '@univerjs/core';
import { SheetPlugin } from '../SheetPlugin';
-import { IShowToolbarConfig } from '../Model';
-import { IHideRightMenuConfig } from './RightMenuController';
// All skins' less file
-export interface IShowContainerConfig {
- outerLeft?: boolean;
-
- outerRight?: boolean;
-
- header?: boolean;
-
- footer?: boolean;
-
- innerLeft?: boolean;
-
- innerRight?: boolean;
-
- frozenHeaderLT?: boolean;
-
- frozenHeaderRT?: boolean;
-
- frozenHeaderLM?: boolean;
-
- frozenContent?: boolean;
-
- infoBar?: boolean;
-
- formulaBar?: boolean;
-
- countBar?: boolean;
-
- sheetBar?: boolean;
-
- // Whether to show the toolbar
- toolbar?: boolean;
-
- rightMenu?: boolean;
-
- contentSplit?: boolean | string;
-}
-
-export interface ILayout {
- sheetContainerConfig?: IShowContainerConfig;
- toolbarConfig?: IShowToolbarConfig;
- rightMenuConfig?: IHideRightMenuConfig;
-}
-
-export interface ISheetPluginConfigBase {
- layout: ILayout;
-}
-
-export interface BaseSheetContainerConfig extends BaseComponentProps, ISheetPluginConfigBase {
- container: HTMLElement;
- skin: string;
- onDidMount: () => void;
- context: ContextBase;
-}
-
export class SheetContainerController {
private _plugin: SheetPlugin;
diff --git a/packages/base-sheets/src/Controller/ToolBarController.ts b/packages/base-sheets/src/Controller/ToolBarController.ts
index 8e15061799..483adb692f 100644
--- a/packages/base-sheets/src/Controller/ToolBarController.ts
+++ b/packages/base-sheets/src/Controller/ToolBarController.ts
@@ -1,31 +1,14 @@
-import { Range, HorizontalAlign, VerticalAlign, WrapStrategy, DEFAULT_STYLES, BorderType } from '@univerjs/core';
+import { Range, HorizontalAlign, VerticalAlign, WrapStrategy, BorderType } from '@univerjs/core';
import { SheetPlugin } from '../SheetPlugin';
import { SelectionControl } from './Selection/SelectionController';
-import { LineColor } from '../View/UI/Common/Line/LineColor';
-import { SelectionModel } from '../Model';
-import { IShowToolbarConfig, IToolbarItemProps, ToolbarModel } from '../Model/ToolbarModel';
-import { Toolbar } from '../View/UI/Toolbar';
-import styles from '../View/UI/Toolbar/index.module.less';
-import {
- BORDER_LINE_CHILDREN,
- BORDER_SIZE_CHILDREN,
- FONT_FAMILY_CHILDREN,
- FONT_SIZE_CHILDREN,
- HORIZONTAL_ALIGN_CHILDREN,
- MERGE_CHILDREN,
- TEXT_ROTATE_CHILDREN,
- TEXT_WRAP_CHILDREN,
- VERTICAL_ALIGN_CHILDREN,
-} from '../View/UI/Toolbar/Const';
-import { DefaultToolbarConfig } from '../Basics';
-import { LineBold } from '../View/UI/Common/Line/LineBold';
-import { ColorSelect } from '../View/UI/Common/ColorSelect/ColorSelect';
+import { SelectionModel, ToolbarModel } from '../Model';
interface BorderInfo {
+ type: BorderType;
color: string;
- type: number;
+ style: number;
}
/**
@@ -36,20 +19,6 @@ export class ToolbarController {
private _plugin: SheetPlugin;
- private _toolbarComponent: Toolbar;
-
- private _toolList: IToolbarItemProps[];
-
- private _moreText: Record<string, string>;
-
- private _lineColor: LineColor;
-
- private _lineBold: LineBold;
-
- private _colorSelect: ColorSelect[] = [];
-
- private _borderInfo: BorderInfo; //存储边框信息
-
private _changeToolbarState(range: Range): void {
const workbook = this._plugin.getContext().getWorkBook();
const worksheet = workbook.getActiveSheet();
@@ -126,360 +95,13 @@ export class ToolbarController {
}
}
- constructor(plugin: SheetPlugin, config?: IShowToolbarConfig) {
+ constructor(plugin: SheetPlugin) {
this._plugin = plugin;
- const pluginName = this._plugin.getPluginName();
-
- this._initRegisterComponent();
-
- const toolbarConfig = config || DefaultToolbarConfig;
-
- this._borderInfo = {
- color: 'rgb(217, 217, 217)',
- type: 1,
- };
-
- this._toolList = [
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.undo',
- name: 'undo',
- customLabel: {
- name: 'ForwardIcon',
- },
- show: toolbarConfig.undoRedo,
- onClick: () => {
- this.setUndo();
- },
- },
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.redo',
- customLabel: {
- name: 'BackIcon',
- },
- name: 'redo',
- show: toolbarConfig.undoRedo,
- onClick: () => {
- this.setRedo();
- },
- },
- // {
- // toolbarType: 1,
- // tooltipLocale: 'paintFormat',
- // label: 'FormatIcon',
- // show: toolbarConfig.paintFormat,
- // },
- // {
- // toolbarType: 1,
- // tooltipLocale: 'currencyFormat',
- // label: 'MoneyIcon',
- // show: toolbarConfig.currencyFormat,
- // },
- // {
- // toolbarType: 1,
- // tooltipLocale: 'percentageFormat',
- // label: 'PercentIcon',
- // show: toolbarConfig.percentageFormat,
- // },
- // {
- // toolbarType: 1,
- // tooltipLocale: 'numberDecrease',
- // label: 'ReduceNumIcon',
- // show: toolbarConfig.numberDecrease,
- // },
- // {
- // toolbarType: 1,
- // tooltipLocale: 'numberIncrease',
- // label: 'AddNumIcon',
- // show: toolbarConfig.numberIncrease,
- // },
- {
- type: 0,
- tooltipLocale: 'toolbar.font',
- className: styles.selectLabelString,
- name: 'font',
- show: toolbarConfig.font,
- border: true,
- onClick: (fontFamily: string) => {
- this._plugin.getObserver('onAfterChangeFontFamilyObservable')?.notifyObservers(fontFamily);
- },
- children: FONT_FAMILY_CHILDREN,
- },
- {
- type: 1,
- tooltipLocale: 'toolbar.fontSize',
- label: String(DEFAULT_STYLES.fs),
- name: 'fontSize',
- show: toolbarConfig.fontSize,
- onClick: (fontSize: number) => {
- this._plugin.getObserver('onAfterChangeFontSizeObservable')?.notifyObservers(fontSize);
- },
- onKeyUp: (fontSize: number) => {
- this._plugin.getObserver('onAfterChangeFontSizeObservable')?.notifyObservers(fontSize);
- },
- children: FONT_SIZE_CHILDREN,
- },
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.bold',
- customLabel: {
- name: 'BoldIcon',
- },
- active: false,
- name: 'bold',
- show: toolbarConfig.bold,
- onClick: (isBold: boolean) => {
- this._plugin.getObserver('onAfterChangeFontWeightObservable')?.notifyObservers(isBold);
- },
- },
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.italic',
- customLabel: {
- name: 'ItalicIcon',
- },
- name: 'italic',
- show: toolbarConfig.italic,
- onClick: (isItalic: boolean) => {
- this._plugin.getObserver('onAfterChangeFontItalicObservable')?.notifyObservers(isItalic);
- },
- },
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.strikethrough',
- customLabel: {
- name: 'DeleteLineIcon',
- },
- name: 'strikethrough',
- show: toolbarConfig.strikethrough,
- onClick: (isStrikethrough: boolean) => {
- this._plugin.getObserver('onAfterChangeFontStrikethroughObservable')?.notifyObservers(isStrikethrough);
- },
- },
- {
- toolbarType: 1,
- tooltipLocale: 'toolbar.underline',
- customLabel: {
- name: 'UnderLineIcon',
- },
- name: 'underline',
- show: toolbarConfig.underline,
- onClick: (isItalic: boolean) => {
- this._plugin.getObserver('onAfterChangeFontUnderlineObservable')?.notifyObservers(isItalic);
- },
- },
- {
- type: 5,
- tooltipLocale: 'toolbar.textColor.main',
- customLabel: {
- name: pluginName + ColorSelect.name,
- props: {
- customLabel: {
- name: 'TextColorIcon',
- },
- id: 'textColor',
- },
- },
- hideSelectedIcon: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- customLabel: {
- name: pluginName + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => {
- const colorSelect = this._colorSelect.find((item) => item.getId() === 'textColor');
- colorSelect?.setColor(color);
- this._plugin.getObserver('onAfterChangeFontColorObservable')?.notifyObservers(color);
- },
- lang: {
- collapseLocale: 'colorPicker.collapse',
- customColorLocale: 'colorPicker.customColor',
- confirmColorLocale: 'colorPicker.confirmColor',
- cancelColorLocale: 'colorPicker.cancelColor',
- changeLocale: 'colorPicker.change',
- },
- },
- },
- className: styles.selectColorPicker,
- },
- ],
- name: 'textColor',
- show: toolbarConfig.textColor,
- },
- {
- type: 5,
- tooltipLocale: 'toolbar.fillColor.main',
- customLabel: {
- name: pluginName + ColorSelect.name,
- props: {
- customLabel: {
- name: 'FillColorIcon',
- },
- id: 'fillColor',
- },
- },
- hideSelectedIcon: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- customLabel: {
- name: pluginName + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => {
- const colorSelect = this._colorSelect.find((item) => item.getId() === 'fillColor');
- colorSelect?.setColor(color);
- this._plugin.getObserver('onAfterChangeFontBackgroundObservable')?.notifyObservers(color);
- },
- lang: {
- collapseLocale: 'colorPicker.collapse',
- customColorLocale: 'colorPicker.customColor',
- confirmColorLocale: 'colorPicker.confirmColor',
- cancelColorLocale: 'colorPicker.cancelColor',
- changeLocale: 'colorPicker.change',
- },
- },
- },
- className: styles.selectColorPicker,
- },
- ],
- name: 'fillColor',
- show: toolbarConfig.fillColor,
- },
- {
- type: 3,
- display: 1,
- show: toolbarConfig.border,
- tooltipLocale: 'toolbar.border.main',
- className: styles.selectDoubleString,
- onClick: (value) => {
- if (value) {
- this.setBorder(value);
- }
- },
- name: 'border',
- children: [
- ...BORDER_LINE_CHILDREN,
- {
- customLabel: {
- name: pluginName + LineColor.name,
- props: {
- locale: 'borderLine.borderColor',
- },
- },
- unSelectable: true,
- className: styles.selectColorPickerParent,
- children: [
- {
- customLabel: {
- name: pluginName + ColorPicker.name,
- props: {
- onClick: (color: string, e: MouseEvent) => this.handleLineColor(color, e),
- lang: {
- collapseLocale: 'colorPicker.collapse',
- customColorLocale: 'colorPicker.customColor',
- confirmColorLocale: 'colorPicker.confirmColor',
- cancelColorLocale: 'colorPicker.cancelColor',
- changeLocale: 'colorPicker.change',
- },
- },
- },
- className: styles.selectColorPicker,
- onClick: this.handleLineColor,
- },
- ],
- },
- {
- customLabel: {
- name: pluginName + LineBold.name,
- props: {
- locale: 'borderLine.borderSize',
- },
- },
- onClick: (...arg) => this.handleLineBold(arg[1], arg[2], arg[0]),
- className: styles.selectLineBoldParent,
- unSelectable: true,
- children: BORDER_SIZE_CHILDREN,
- },
- ],
- },
- {
- type: 5,
- tooltipLocale: 'toolbar.mergeCell.main',
- customLabel: {
- name: 'MergeIcon',
- },
- show: toolbarConfig.mergeCell,
- onClick: (value: string) => {
- this.setMerge(value);
- },
- name: 'mergeCell',
- children: MERGE_CHILDREN,
- },
- {
- type: 3,
- tooltipLocale: 'toolbar.horizontalAlignMode.main',
- className: styles.selectDoubleString,
- display: 1,
- name: 'horizontalAlignMode',
- show: toolbarConfig.horizontalAlignMode,
- onClick: (value: HorizontalAlign) => {
- this.setHorizontalAlignment(value);
- },
- children: HORIZONTAL_ALIGN_CHILDREN,
- },
- {
- type: 3,
- tooltipLocale: 'toolbar.verticalAlignMode.main',
- className: styles.selectDoubleString,
- display: 1,
- name: 'verticalAlignMode',
- show: toolbarConfig.verticalAlignMode,
- onClick: (value: VerticalAlign) => {
- this.setVerticalAlignment(value);
- },
- children: VERTICAL_ALIGN_CHILDREN,
- },
- {
- type: 3,
- className: styles.selectDoubleString,
- tooltipLocale: 'toolbar.textWrapMode.main',
- display: 1,
- name: 'textWrapMode',
- show: toolbarConfig.textWrapMode,
- onClick: (value: WrapStrategy) => {
- this.setWrapStrategy(value);
- },
- children: TEXT_WRAP_CHILDREN,
- },
- {
- type: 3,
- className: styles.selectDoubleString,
- name: 'textRotateMode',
- tooltipLocale: 'toolbar.textRotateMode.main',
- display: 1,
- show: toolbarConfig.textRotateMode,
- onClick: (value: number | string) => {
- this.setTextRotation(value);
- },
- children: TEXT_ROTATE_CHILDREN,
- },
- ];
-
- this._moreText = { more: 'toolbar.toolMore', tip: 'toolbar.toolMoreTip' };
-
- this._toolbarModel = new ToolbarModel();
- this._toolbarModel.config = toolbarConfig;
- this._toolbarModel.toolList = this._toolList;
-
this._initialize();
}
_initialize() {
- console.log('base-sheet toolbarController');
-
this._plugin.getObserver('onAfterChangeFontFamilyObservable')?.add((value: string) => {
if (!this._plugin.getCellEditorController().isEditMode) {
this.setFontFamily(value);
@@ -536,32 +158,6 @@ export class ToolbarController {
this._plugin.getCellEditorController().richText.cellTextStyle.updateFormat('bg', value);
}
});
-
- this._plugin.getObserver('onToolbarDidMountObservable')?.add((component) => {
- //初始化视图
- this._toolbarComponent = component;
- this.resetToolbarList();
- });
- this._plugin.getObserver('onLineColorDidMountObservable')?.add((component) => {
- //初始化视图
- this._lineColor = component;
- });
- this._plugin.getObserver('onLineBoldDidMountObservable')?.add((component) => {
- //初始化视图
- this._lineBold = component;
- });
- this._plugin.getObserver('onColorSelectDidMountObservable')?.add((component) => {
- //初始化视图
- this._colorSelect.push(component);
- });
-
- this._plugin.context
- .getObserverManager()
- .getObserver('onAfterChangeUILocaleObservable', 'core')
- ?.add(() => {
- this.resetToolbarList();
- });
-
// Monitor selection changes, update toolbar button status and values TODO: 根据不同的焦点对象,接受
this._plugin.getObserver('onChangeSelectionObserver')?.add((selectionControl: SelectionControl) => {
// const currentCell = selectionControl.model.currentCell;
@@ -598,83 +194,6 @@ export class ToolbarController {
});
}
- private _initRegisterComponent() {
- const pluginName = this._plugin.getPluginName();
-
- // 注册自定义组件
- this._plugin.registerComponent(pluginName + LineColor.name, LineColor);
- this._plugin.registerComponent(pluginName + LineBold.name, LineBold);
- this._plugin.registerComponent(pluginName + ColorSelect.name, ColorSelect);
- this._plugin.registerComponent(pluginName + ColorPicker.name, ColorPicker);
- }
-
- resetLabel(label: string[] | string) {
- const locale = this._plugin.context.getLocale();
-
- let str = '';
-
- if (label instanceof Array) {
- label.forEach((item) => {
- if (item.includes('.')) {
- str += locale.get(item);
- } else {
- str += item;
- }
- });
- } else {
- if (label.includes('.')) {
- str = locale.get(label);
- } else {
- str += label;
- }
- }
-
- return str;
- }
-
- findLocale(obj: any) {
- for (let k in obj) {
- if (k === 'locale') {
- obj.label = this.resetLabel(obj[k]);
- } else if (k.endsWith('Locale')) {
- const index = k.indexOf('Locale');
- obj[k.slice(0, index)] = this.resetLabel(obj[k]);
- } else if (!obj[k].$$typeof) {
- if (Object.prototype.toString.call(obj[k]) === '[object Object]') {
- this.findLocale(obj[k]);
- } else if (Object.prototype.toString.call(obj[k]) === '[object Array]') {
- this.resetToolListLabel(obj[k]);
- }
- }
- }
-
- return obj;
- }
-
- resetToolListLabel(toolList: IToolbarItemProps[]) {
- for (let i = 0; i < toolList.length; i++) {
- let item = toolList[i];
-
- item = this.findLocale(item);
-
- if (item.children) {
- item.children = this.resetToolListLabel(item.children);
- }
- }
-
- return toolList;
- }
-
- resetToolbarList() {
- const locale = this._plugin.context.getLocale();
-
- const toolList = this.resetToolListLabel(this._toolList);
- this._toolbarComponent.setToolbar(toolList, {
- more: locale.get(this._moreText.more),
- tip: locale.get(this._moreText.tip),
- });
- }
-
setRedo() {
this._plugin.getContext().getCommandManager().redo();
}
@@ -763,7 +282,7 @@ export class ToolbarController {
}
}
- setBorder(type: BorderType) {
+ setBorder(info: BorderInfo) {
const controls = this._plugin.getSelectionManager().getCurrentControls();
if (controls && controls.length > 0) {
@@ -776,53 +295,8 @@ export class ToolbarController {
endColumn: model.endColumn,
};
- this._plugin.getContext().getWorkBook().getActiveSheet().getRange(range).setBorderByType(type, this._borderInfo.color, this._borderInfo.type);
+ this._plugin.getContext().getWorkBook().getActiveSheet().getRange(range).setBorderByType(info.type, info.color, info.style);
});
}
}
-
- // 改变边框线颜色
- handleLineColor(color: string, e: MouseEvent) {
- e.stopPropagation();
- this._lineColor.setColor(color);
- this._borderInfo.color = color;
- }
-
- // 改变边框线粗细
- /**
- * 由于
- * @param value
- * @param index
- * @param e
- * @returns
- */
- handleLineBold(value: string, index: number, e: MouseEvent) {
- console.log('handleLineBold===', value, index, e);
-
- if (index === 7) {
- index = 8;
- } else if (index === 8) {
- index = 9;
- } else if (index === 9) {
- index = 10;
- } else if (index === 10) {
- index = 11;
- } else if (index === 11) {
- index = 13;
- }
-
- e.stopPropagation();
- if (!value) return;
- this._lineBold.setLineType(value);
- this._borderInfo.type = index;
- }
-
- addToolButton(config: IToolbarItemProps) {
- const index = this._toolList.findIndex((item) => item.name === config.name);
- if (index > -1) return;
- this._toolList.push(config);
- if (this._toolbarComponent) {
- this.resetToolbarList();
- }
- }
}
diff --git a/packages/base-ui/src/Common/AppContext.tsx b/packages/base-ui/src/Common/AppContext.tsx
index 658a2b1f5d..6683a328e9 100644
--- a/packages/base-ui/src/Common/AppContext.tsx
+++ b/packages/base-ui/src/Common/AppContext.tsx
@@ -3,9 +3,11 @@
*/
import { ContextBase } from '@univerjs/core';
import { createContext } from 'preact';
+import { LocaleType } from '../Enum';
export type AppContextValues = {
context: ContextBase;
+ locale: LocaleType;
};
const AppContext = createContext<Partial<AppContextValues>>({});
diff --git a/packages/base-ui/src/Framework/Preact.ts b/packages/base-ui/src/Framework/Preact.ts
index 0a40e20439..f2ad8ca081 100644
--- a/packages/base-ui/src/Framework/Preact.ts
+++ b/packages/base-ui/src/Framework/Preact.ts
@@ -20,6 +20,10 @@ abstract class Component<P = {}, S = {}> extends PreactComponent<P, S> {
getContext(): Context {
return this.context.context;
}
+
+ getLocale(name: string) {
+ return this.getContext().getLocale().get(name);
+ }
}
abstract class PureComponent<P = {}, S = {}> extends PreactPureComponent<P, S> {
@@ -27,16 +31,19 @@ abstract class PureComponent<P = {}, S = {}> extends PreactPureComponent<P, S> {
protected _context: Context;
- constructor(props?: P, context?: any) {
- super(props, context);
- this._context = context.context;
+ constructor(props?: P) {
+ super(props);
this.initialize(props);
}
protected initialize(props?: P): void {}
getContext(): Context {
- return this._context;
+ return this.context.context;
+ }
+
+ getLocale(name: string) {
+ return this.getContext().getLocale().get(name);
}
}
diff --git a/packages/core/src/Shared/Locale.ts b/packages/core/src/Shared/Locale.ts
index 81e03fb596..0464434556 100644
--- a/packages/core/src/Shared/Locale.ts
+++ b/packages/core/src/Shared/Locale.ts
@@ -63,6 +63,7 @@ export class Locale {
: LocaleType.EN,
locales: { zh, en },
};
+ console.dir(this.options);
}
/**
@@ -78,6 +79,7 @@ export class Locale {
*/
get(key: string | undefined): string {
if (key) {
+ // const { locales, currentLocale } = this.options;
const { locales, currentLocale } = this.options;
return (Locale.getValue(locales[currentLocale], key) as string) || '';
}
diff --git a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
index 1f220f0e55..d3c22c53c0 100644
--- a/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
+++ b/packages/ui-plugin-sheets/src/Basics/Interfaces/ComponentConfig/ISheetUIPluginConfig.ts
@@ -1,7 +1,7 @@
import { SheetRightMenuConfig } from './RightMenuConfig';
import { SheetContainerConfig } from './SheetContainerConfig';
import { SheetToolbarConfig } from './ToolbarConfig';
-import { Context } from '@univerjs/core/src/Basics/Context';
+import { LocaleType } from '@univerjs/core';
export interface ILayout {
sheetContainerConfig?: SheetContainerConfig;
@@ -12,4 +12,5 @@ export interface ILayout {
export interface ISheetUIPluginConfig {
container?: HTMLElement | string;
layout?: ILayout;
+ locale?: LocaleType;
}
diff --git a/packages/ui-plugin-sheets/src/Controller/AppUIController.ts b/packages/ui-plugin-sheets/src/Controller/AppUIController.ts
new file mode 100644
index 0000000000..fb016a7b9b
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/Controller/AppUIController.ts
@@ -0,0 +1,47 @@
+import { LocaleType } from '@univerjs/core';
+import { SheetUIPlugin } from '../SheetUIPlugin';
+import { UI } from '../View';
+import { SheetContainerUIController } from './SheetContainerUIController';
+
+export class AppUIController {
+ protected _plugin: SheetUIPlugin;
+
+ private _sheetContainerController: SheetContainerUIController;
+
+ constructor(plugin: SheetUIPlugin) {
+ this._plugin = plugin;
+
+ this._sheetContainerController = new SheetContainerUIController(this._plugin);
+
+ const UIConfig = this._sheetContainerController.getUIConfig();
+
+ UI.create({
+ context: this._plugin.getContext(),
+ locale: this._plugin.getContext().getLocale().getCurrentLocale(),
+ changeLocale: this.changeLocale,
+ UIConfig,
+ container: this._plugin.getConfig().container,
+ });
+ }
+
+ /**
+ * Change language
+ * @param {String} lang new language
+ *
+ * e: {target: HTMLSelectElement } reference from https://stackoverflow.com/a/48443771
+ *
+ */
+ changeLocale = (locale: string) => {
+ this._plugin
+ .getContext()
+ .getLocale()
+ .change(locale as LocaleType);
+
+ // publish
+ this._plugin.getContext().getObserverManager().requiredObserver('onAfterChangeUILocaleObservable', 'core')!.notifyObservers();
+ };
+
+ getSheetContainerController() {
+ return this._sheetContainerController;
+ }
+}
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
index 9bcb34ad9f..89d3ff50fc 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetContainerUIController.ts
@@ -54,6 +54,56 @@ export class SheetContainerUIController {
this._sheetBarController = new SheetBarUIController(this._plugin);
// 初始化UI
+ // const config = {
+ // context: this._plugin.getContext(),
+ // config: this._config,
+ // changeLocale: this.changeLocale,
+ // getComponent: this.getComponent,
+ // // 其余组件的props
+ // methods: {
+ // toolbar: {
+ // getComponent: this._toolbarController.getComponent,
+ // },
+ // cellEditor: {
+ // getComponent: this._cellEditorUIController.getComponent,
+ // },
+ // formulaBar: {
+ // getComponent: this._formulaBarUIController.getComponent,
+ // },
+ // infoBar: {
+ // getComponent: this._infoBarController.getComponent,
+ // // renameSheet: this._infoBarController.renameSheet,
+ // },
+ // rightMenu: {
+ // // getComponent: this._rightMenuController.getComponent,
+ // },
+ // countBar: {
+ // // getComponent: this._countBarController.getComponent,
+ // },
+ // sheetBar: {
+ // // getComponent: this._sheetBarController.getComponent,
+ // // addSheet: this._sheetBarController.addSheet,
+ // // selectSheet: this._sheetBarController.selectSheet,
+ // // changeSheetName: this._sheetBarController.changeSheetName,
+ // },
+ // },
+ // };
+ // UI.create(config);
+ }
+
+ private _initialize() {
+ this._dragManager = new DragManager(this._plugin);
+ this._eventManager = new EventManager(this._plugin);
+
+ this.setEventManager();
+ }
+
+ private _initSheetContainer() {
+ // handle drag event
+ this._dragManager.handleDragAction(getRefElement(this._sheetContainer));
+ }
+
+ getUIConfig() {
const config = {
context: this._plugin.getContext(),
config: this._config,
@@ -88,19 +138,7 @@ export class SheetContainerUIController {
},
},
};
- UI.create(config);
- }
-
- private _initialize() {
- this._dragManager = new DragManager(this._plugin);
- this._eventManager = new EventManager(this._plugin);
-
- this.setEventManager();
- }
-
- private _initSheetContainer() {
- // handle drag event
- this._dragManager.handleDragAction(getRefElement(this._sheetContainer));
+ return config;
}
// 获取SheetContainer组件
diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
index 87901abf48..a8f6e21524 100644
--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsx
@@ -2,12 +2,12 @@ import { Plugin, UniverSheet, Tools, PLUGIN_NAMES } from '@univerjs/core';
import { zh, en } from './Locale';
import { DefaultSheetUiConfig, installObserver, ISheetUIPluginConfig, SheetUIPluginObserve, SHEET_UI_PLUGIN_NAME } from './Basics';
import { Context } from '@univerjs/core/src/Basics/Context';
-import { SheetContainerUIController } from './Controller';
import { ComponentManager, getRefElement, RegisterManager } from '@univerjs/base-ui';
import { Engine, RenderEngine } from '@univerjs/base-render';
+import { AppUIController } from './Controller/AppUIController';
export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
- private _sheetsController: SheetContainerUIController;
+ private _appUIController: AppUIController;
private _componentManager: ComponentManager;
@@ -30,7 +30,6 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
initialize(ctx: Context): void {
installObserver(this);
- this._config.context = ctx;
const context = this.getContext();
/**
* load more Locale object
@@ -42,7 +41,7 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
this._componentManager = new ComponentManager();
this._registerManager = new RegisterManager(this);
- this._sheetsController = new SheetContainerUIController(this);
+ this._appUIController = new AppUIController(this);
this.initRender();
}
@@ -52,7 +51,7 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
initRender() {
const engine = this.getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;
- let container = getRefElement(this.getSheetContainerController().getContentRef());
+ let container = getRefElement(this._appUIController.getSheetContainerController().getContentRef());
// mount canvas to DOM container
engine.setContainer(container);
@@ -75,8 +74,8 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {
onDestroy(): void {}
- getSheetContainerController() {
- return this._sheetsController;
+ getAppUIController() {
+ return this._appUIController;
}
getComponentManager() {
diff --git a/packages/ui-plugin-sheets/src/View/App.tsx b/packages/ui-plugin-sheets/src/View/App.tsx
new file mode 100644
index 0000000000..06f4acabff
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/View/App.tsx
@@ -0,0 +1,69 @@
+import { AppContext, BaseComponentProps, Component } from '@univerjs/base-ui';
+import { LocaleType } from '@univerjs/core';
+import { BaseSheetContainerProps, SheetContainer } from './SheetContainer';
+
+export interface BaseUIProps extends BaseComponentProps {
+ locale: LocaleType;
+ UIConfig: BaseSheetContainerProps;
+ changeLocale: (locale: string) => void;
+}
+
+interface IState {
+ locale: LocaleType;
+}
+
+export class App extends Component<BaseUIProps, IState> {
+ constructor(props: BaseUIProps) {
+ super(props);
+ this.state = {
+ locale: this.props.locale,
+ };
+ }
+
+ setLocale(e: Event) {
+ const value = (e.target as HTMLSelectElement).value as LocaleType;
+ this.props.changeLocale(value);
+ this.setState({
+ locale: value,
+ });
+ }
+
+ render() {
+ const { context, UIConfig } = this.props;
+ const { locale } = this.state;
+
+ return (
+ <AppContext.Provider
+ value={{
+ context,
+ locale,
+ }}
+ >
+ <div
+ style={{
+ position: 'fixed',
+ right: '200px',
+ top: '10px',
+ fontSize: '14px',
+ zIndex: 100,
+ }}
+ >
+ <span
+ style={{
+ display: 'inline-block',
+ width: 50,
+ margin: '5px 0 0 5px',
+ }}
+ >
+ 语言
+ </span>
+ <select value={locale} onChange={this.setLocale.bind(this)} style={{ width: 55 }}>
+ <option value="en">English</option>
+ <option value="zh">中文</option>
+ </select>
+ </div>
+ <SheetContainer {...UIConfig} />
+ </AppContext.Provider>
+ );
+ }
+}
diff --git a/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx b/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
index 45f6e2d02a..0a8dd1c7bc 100644
--- a/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
+++ b/packages/ui-plugin-sheets/src/View/SheetContainer/SheetContainer.tsx
@@ -6,25 +6,19 @@ import { CountBar } from '../CountBar';
import { SheetBar } from '../SheetBar';
import { FormulaBar } from '../FormulaBar';
import { RichText } from '../RichText';
-import { AppContext, BaseComponentProps, Component, Container, Content, createRef, Footer, Header, Layout, Sider } from '@univerjs/base-ui';
+import { BaseComponentProps, Component, Container, Content, createRef, Footer, Header, Layout, Sider } from '@univerjs/base-ui';
import { ISheetUIPluginConfig } from '../../Basics';
export interface BaseSheetContainerProps extends BaseComponentProps {
config: ISheetUIPluginConfig;
- container?: HTMLElement;
changeLocale: (locale: string) => void;
methods?: any;
}
-// Types for state
-interface IState {
- currentLocale: string;
-}
-
/**
* One universheet instance DOM container
*/
-export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
+export class SheetContainer extends Component<BaseSheetContainerProps> {
leftContentLeft: number;
leftContentTop: number;
@@ -37,14 +31,6 @@ export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
contentRef = createRef<HTMLDivElement>();
- constructor(props: BaseSheetContainerProps) {
- super(props);
- // init state
- this.state = {
- currentLocale: 'zh',
- };
- }
-
componentDidMount() {
this.props.getComponent?.(this);
}
@@ -102,21 +88,6 @@ export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
return this.splitLeftRef;
}
- setLocale(e: Event) {
- const { changeLocale } = this.props;
- const target = e.target as HTMLSelectElement;
- const locale = target.value;
- // You must use setState to trigger the re-rendering of the child component
- this.setState(
- {
- currentLocale: locale,
- },
- () => {
- changeLocale(locale);
- }
- );
- }
-
/**
* Render the component's HTML
*
@@ -125,11 +96,11 @@ export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
render() {
const { methods } = this.props;
const { layout } = this.props.config;
- const { currentLocale } = this.state;
const config = layout?.sheetContainerConfig!;
// Set Provider for entire Container
return (
<Container className={style.layoutContainer}>
+ {this.getLocale('toolbar.undo')}
<Layout>
<Sider style={{ display: config.outerLeft ? 'block' : 'none' }}></Sider>
<Layout className={style.mainContent} style={{ position: 'relative' }}>
@@ -159,28 +130,6 @@ export class SheetContainer extends Component<BaseSheetContainerProps, IState> {
{config.cellEditor && <RichText {...methods.cellEditor}></RichText>} */}
{<RichText {...methods.cellEditor}></RichText>}
- <div
- style={{
- position: 'fixed',
- right: '200px',
- top: '10px',
- fontSize: '14px',
- }}
- >
- <span
- style={{
- display: 'inline-block',
- width: 50,
- margin: '5px 0 0 5px',
- }}
- >
- 语言
- </span>
- <select value={currentLocale} onChange={this.setLocale.bind(this)} style={{ width: 55 }}>
- <option value="en">English</option>
- <option value="zh">中文</option>
- </select>
- </div>
</Container>
</Content>
<Sider
diff --git a/packages/ui-plugin-sheets/src/View/index.tsx b/packages/ui-plugin-sheets/src/View/index.tsx
index 826c971f6b..10dce0a373 100644
--- a/packages/ui-plugin-sheets/src/View/index.tsx
+++ b/packages/ui-plugin-sheets/src/View/index.tsx
@@ -1,46 +1,39 @@
-import { AppContext, isElement, render } from '@univerjs/base-ui';
-import { BaseSheetContainerProps, SheetContainer } from './SheetContainer';
+import { isElement, render } from '@univerjs/base-ui';
+import { App, BaseUIProps } from './App';
+
+interface BaseSheetUIConfig extends BaseUIProps {
+ container?: HTMLElement | string;
+}
export class UI {
- constructor(props: BaseSheetContainerProps) {
+ constructor(props: BaseSheetUIConfig) {
this._initialize(props);
}
- static create(props: BaseSheetContainerProps) {
+ static create(props: BaseSheetUIConfig) {
return new UI(props);
}
- private _initialize(props: BaseSheetContainerProps) {
- let sheetContainer: HTMLElement;
- const container = props.config.container;
+ private _initialize(props: BaseSheetUIConfig) {
+ let renderContainer: HTMLElement;
+ const container = props.container;
if (typeof container === 'string') {
const containerDOM = document.getElementById(container);
if (containerDOM == null) {
- sheetContainer = document.createElement('div');
- sheetContainer.id = container;
+ renderContainer = document.createElement('div');
+ renderContainer.id = container;
} else {
- sheetContainer = containerDOM;
+ renderContainer = containerDOM;
}
} else if (isElement(container)) {
- sheetContainer = container!;
+ renderContainer = container!;
} else {
- sheetContainer = document.createElement('div');
- sheetContainer.id = 'univer';
+ renderContainer = document.createElement('div');
+ renderContainer.id = 'univer';
}
- props.container = sheetContainer;
-
- render(
- <AppContext.Provider
- value={{
- context: props.context,
- }}
- >
- <SheetContainer {...props} />
- </AppContext.Provider>,
- sheetContainer
- );
+ render(<App {...props} />, renderContainer);
}
}