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.

flush

jerry881569823cb1
diff --git a/packages/base-sheets/src/Controller/InfoBarController.ts b/packages/base-sheets/src/Controller/InfoBarController.ts
index 8e6b711ebe..cbb3aa2a74 100644
--- a/packages/base-sheets/src/Controller/InfoBarController.ts
+++ b/packages/base-sheets/src/Controller/InfoBarController.ts
@@ -55,7 +55,7 @@ export class InfoBarController {
}
private _initialize() {
- const context = this._plugin.context;
+ const context = this._plugin.getContext();
const manager = context.getObserverManager();
manager.requiredObserver<InfoBar>('onInfoBarDidMountObservable', PLUGIN_NAMES.SPREADSHEET).add((component) => {
@@ -69,7 +69,7 @@ export class InfoBarController {
}
resetInfoList(list: BaseInfoBarProps) {
- const locale = this._plugin.context.getLocale();
+ const locale = this._plugin.getContext().getLocale();
for (let k in list) {
if (list[k].locale) {
list[k].label = locale.get(list[k].locale);
diff --git a/packages/ui-plugin-sheets/src/Controller/CountBarController.ts b/packages/ui-plugin-sheets/src/Controller/CountBarController.ts
index 0e4c0901e1..ef4653a95a 100644
--- a/packages/ui-plugin-sheets/src/Controller/CountBarController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/CountBarController.ts
@@ -1,12 +1,74 @@
-import { BaseComponentPlugin } from '..';
+import { RangeList, Plugin, Tools } from '@univerjs/core';
import { CountBar } from '../UI/CountBar';
export class CountBarController {
- private _plugin: BaseComponentPlugin;
+ protected _plugin: Plugin;
- private _countBar: CountBar;
+ protected _countBar: CountBar;
- constructor(plugin: BaseComponentPlugin) {
+ protected _initialize(): void {
+ const plugin = this._plugin;
+ const context = plugin.getContext();
+ const workbook = context.getWorkBook();
+
+ this._countBar.setState({
+ onChange: (v: any) => {
+ workbook.getActiveSheet().setZoomRatio(Tools.numberFixed(v / 100, 2));
+ },
+ });
+ }
+
+ protected _initializeSelectedObserver(): void {
+ const plugin = this._plugin;
+ const manager = plugin.getSelectionManager();
+ plugin.getObserver('onChangeSelectionObserver')?.add(() => {
+ const rangeList = manager.getActiveRangeList();
+ if (rangeList && this._countBar) {
+ this._totalRangeList(rangeList);
+ }
+ });
+ }
+
+ protected _totalRangeList(rangeList: RangeList): void {
+ let rectList = rangeList.getRangeList();
+ let recList: string[] = [];
+ let plugin = this._plugin;
+ let context = plugin.getContext();
+ let workbook = context.getWorkBook();
+ let worksheet = workbook.getActiveSheet();
+ let cellMatrix = worksheet.getCellMatrix();
+ let avg = 0;
+ let total = 0;
+ let count = 0;
+ for (let i = 0; i < rectList.length; i++) {
+ let rect = rectList[i];
+ for (let r = rect.startRow; r <= rect.endRow; r++) {
+ for (let c = rect.startColumn; c <= rect.endColumn; c++) {
+ if (recList.includes(`${r}${c}`)) {
+ continue;
+ }
+ const cell = cellMatrix.getValue(r, c);
+ if (cell) {
+ let value = parseFloat(cell.v as string);
+ // eslint-disable-next-line no-restricted-globals
+ if (!isNaN(value)) {
+ count += 1;
+ total += value;
+ }
+ }
+ recList.push(`${r}${c}`);
+ }
+ }
+ }
+ if (count > 0) {
+ avg = total / count;
+ }
+ this._countBar.setState({
+ content: `平均数:${Tools.numberFixed(avg, 2)} 计数:${Tools.numberFixed(total, 2)} 数量:${count}`,
+ });
+ }
+
+ constructor(plugin: Plugin) {
this._plugin = plugin;
}
diff --git a/packages/ui-plugin-sheets/src/Controller/InfoBarController.ts b/packages/ui-plugin-sheets/src/Controller/InfoBarController.ts
index 51bfb1ecd4..3241524ba7 100644
--- a/packages/ui-plugin-sheets/src/Controller/InfoBarController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/InfoBarController.ts
@@ -1,5 +1,6 @@
-import { BaseComponentPlugin } from '..';
+import { Plugin } from '@univerjs/core';
import { InfoBar } from '../UI/InfoBar';
+import { InfoBarModel } from '../Model/InfoBarModel';
type IProps = {
locale?: string;
@@ -15,13 +16,15 @@ export interface BaseInfoBarProps {
}
export class InfoBarController {
- private _infoBar: InfoBar;
+ private _infoList: BaseInfoBarProps;
- private _plugin: BaseComponentPlugin;
+ private _plugin: Plugin;
- private _infoList: BaseInfoBarProps;
+ private _infoBar: InfoBar;
+
+ private _infoBarModel: InfoBarModel;
- constructor(plugin: BaseComponentPlugin) {
+ constructor(plugin: Plugin) {
this._plugin = plugin;
this._infoList = {
@@ -50,6 +53,12 @@ export class InfoBarController {
this.resetInfoList(this._infoList);
};
+ setSheetName(e: FocusEvent) {
+ const target = e.target as HTMLInputElement;
+ const name = target.value;
+ this._infoBarModel.setName(name);
+ }
+
resetInfoList(list: BaseInfoBarProps) {
const locale = this._plugin.getLocale();
for (let k in list) {
@@ -60,5 +69,7 @@ export class InfoBarController {
this._infoBar.setInfoList(list);
}
- renameSheet() {}
+ renameSheet() {
+
+ }
}
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetBarUIControl.ts b/packages/ui-plugin-sheets/src/Controller/SheetBarUIControl.ts
index ee73a7e4c0..398e7776c8 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetBarUIControl.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetBarUIControl.ts
@@ -1,3 +1,24 @@
+import { BaseMenuItem, BaseUlProps } from '@univerjs/base-ui';
+import { Nullable, Plugin } from '@univerjs/core';
+import { SheetBar } from "../View/SheetBar";
+
+interface CustomComponent {
+ name: string;
+ props?: Record<string, any>;
+}
+
+interface SheetUl extends BaseMenuItem {
+ locale?: string;
+ customLabel?: CustomComponent;
+ children?: SheetUl[];
+}
+
+interface SheetUlProps extends BaseUlProps {
+ index: string;
+ color?: Nullable<string>;
+ sheetId: string;
+}
+
export class SheetBarUIController {
protected _sheetBar: SheetBar;
@@ -10,4 +31,324 @@ export class SheetBarUIController {
protected _sheetUl: SheetUl[];
protected _menuList: SheetUlProps[];
+
+ protected _plugin: Plugin;
+
+ protected _initializeObs() {
+ const plugin = this._barControl.getPlugin();
+ const context = plugin.getContext();
+ const manager = context.getObserverManager();
+ const workbook = context.getWorkBook();
+ const unitId = workbook.getUnitId();
+ // manager.requiredObserver<SheetBar>('onSheetBarDidMountObservable', PLUGIN_NAMES.SPREADSHEET);
+ manager.requiredObserver<SheetBar>('onSheetBarDidMountObservable', PLUGIN_NAMES.SPREADSHEET).add((component) => {
+ this._sheetBar = component;
+ this._sheetUl = this.resetSheetUl(this._sheetUl);
+ this._sheetBar.setValue({
+ sheetList: this._sheetList,
+ sheetUl: this._sheetUl,
+ menuList: this._menuList,
+ addSheet: () => {
+ this._barControl.addSheet();
+ },
+ selectSheet: (event: Event, data: { item: SheetUlProps }) => {
+ this._dataId = data.item.sheetId;
+ const sheet = plugin.getContext().getWorkBook().getSheetBySheetId(this._dataId);
+ if (sheet) {
+ sheet.activate();
+ }
+ },
+ contextMenu: (e: MouseEvent) => {
+ const target = e.currentTarget as HTMLDivElement;
+ this._dataId = target.dataset.id as string;
+ this._barControl.contextMenu(e);
+ },
+ changeSheetName: (e: Event) => {
+ this._barControl.changeSheetName(e);
+ },
+ dragEnd: (elements: HTMLDivElement[]) => {
+ this._barControl.dragEnd(elements);
+ },
+ });
+ });
+ context.getContextObserver('onAfterChangeUILocaleObservable').add(() => {
+ this._sheetUl = this.resetSheetUl(this._sheetUl);
+ this._sheetBar.setValue({
+ sheetUl: this._sheetUl,
+ });
+ });
+
+ CommandManager.getActionObservers().add((actionEvent: any) => {
+ const action = actionEvent.action as SheetActionBase<any>;
+ const workbook = action.getWorkBook();
+ const actionUnitId = workbook.getUnitId();
+ if (unitId !== actionUnitId) return;
+
+ const { data } = actionEvent;
+ switch (data.actionName) {
+ case ACTION_NAMES.SET_SHEET_ORDER_ACTION:
+ case ACTION_NAMES.INSERT_SHEET_ACTION:
+ case ACTION_NAMES.SET_TAB_COLOR_ACTION:
+ case ACTION_NAMES.REMOVE_SHEET_ACTION:
+ case ACTION_NAMES.SET_WORKSHEET_NAME_ACTION:
+ case ACTION_NAMES.SET_WORKSHEET_ACTIVATE_ACTION:
+ case ACTION_NAMES.SET_WORKSHEET_STATUS_ACTION: {
+ // update data;
+ this._initializeData();
+ // set ui bar sheetList;
+ this._sheetBar.setValue({
+ sheetList: this._sheetList,
+ menuList: this._menuList,
+ });
+ break;
+ }
+ }
+ });
+ }
+
+ protected _initializeData() {
+ const workbook = this._plugin.getWorkbook();
+ const sheets = workbook.getSheets();
+ this._menuList = sheets.map((sheet, index) => ({
+ label: sheet.getName(),
+ index: String(index),
+ sheetId: sheet.getSheetId(),
+ hidden: sheet.isSheetHidden(),
+ selected: sheet.getStatus() === 1,
+ onClick: (e: MouseEvent) => {
+ const target = e.currentTarget as HTMLDivElement;
+ this._dataId = target.dataset.id as string;
+ sheet.showSheet();
+ sheet.activate();
+ },
+ }));
+ this._sheetList = sheets
+ .filter((sheet) => !sheet.isSheetHidden())
+ .map((sheet, index) => ({
+ sheetId: sheet.getSheetId(),
+ label: sheet.getName(),
+ index: String(index),
+ selected: sheet.getStatus() === 1,
+ color: sheet.getTabColor() as string,
+ onDown: (e: MouseEvent) => {
+ const target = e.currentTarget as HTMLDivElement;
+ this._dataId = target.dataset.id as string;
+ },
+ onClick: (e: MouseEvent) => {
+ const target = e.currentTarget as HTMLDivElement;
+ this._dataId = target.dataset.id as string;
+
+ sheet.activate();
+ },
+ }));
+ this._sheetIndex = sheets.findIndex((sheet) => sheet.getStatus() === 1);
+ this._dataId = sheets[this._sheetIndex].getSheetId();
+ }
+
+ constructor(plugin: Plugin) {
+ this._plugin = plugin;
+ this._sheetUl = [
+ {
+ locale: 'sheetConfig.delete',
+ onClick: () => {
+ barControl.deleteSheet();
+ },
+ },
+ {
+ locale: 'sheetConfig.copy',
+ onClick: () => {
+ barControl.copySheet();
+ },
+ },
+ {
+ locale: 'sheetConfig.rename',
+ onClick: () => {
+ barControl.reNameSheet();
+ },
+ },
+ {
+ locale: 'sheetConfig.changeColor',
+ border: true,
+ className: styles.selectColorPickerParent,
+ children: [
+ {
+ customLabel: {
+ name: pluginName + ColorPicker.name,
+ props: {
+ onClick: (color: string) => {
+ barControl.setSheetColor(color);
+ },
+ lang: {
+ collapseLocale: 'colorPicker.collapse',
+ customColorLocale: 'colorPicker.customColor',
+ confirmColorLocale: 'colorPicker.confirmColor',
+ cancelColorLocale: 'colorPicker.cancelColor',
+ changeLocale: 'colorPicker.change',
+ },
+ },
+ },
+ className: styles.selectColorPicker,
+ },
+ ],
+ },
+ {
+ locale: 'sheetConfig.hide',
+ onClick: () => {
+ barControl.hideSheet();
+ },
+ },
+ {
+ locale: 'sheetConfig.unhide',
+ onClick: () => {
+ barControl.unHideSheet();
+ },
+ border: true,
+ },
+ {
+ locale: 'sheetConfig.moveLeft',
+ onClick: () => {
+ barControl.moveSheet('left');
+ },
+ },
+ {
+ locale: 'sheetConfig.moveRight',
+ onClick: () => {
+ barControl.moveSheet('right');
+ },
+ },
+ ];
+ this._initializeObs();
+ this._initializeData();
+ }
+
+ resetLabel(label: string[] | string) {
+ const locale = this._plugin.getContext().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]') {
+ obj[k] = this.resetSheetUl(obj[k]);
+ }
+ }
+ }
+
+ return obj;
+ }
+
+ getSheetBar(): SheetBar {
+ return this._sheetBar;
+ }
+
+ getDataId(): string {
+ return this._dataId;
+ }
+
+ getSheetList(): SheetUlProps[] {
+ return this._sheetList;
+ }
+
+ getMenuList(): SheetUlProps[] {
+ return this._menuList;
+ }
+
+ resetSheetUl(sheetUl: SheetUl[]) {
+ for (let i = 0; i < sheetUl.length; i++) {
+ let item = sheetUl[i];
+ item = this.findLocale(item);
+
+ if (item.children) {
+ item.children = this.resetSheetUl(item.children);
+ }
+ }
+ return sheetUl;
+ }
+
+ selectSheet() {
+
+ }
+
+ deleteSheet() {
+
+ }
+
+ sortMenu(index: number, hidden?: boolean, hideIndex?: number) {
+
+ }
+
+ copySheet() {
+
+ }
+
+ addSheet(position?: string, config?: SheetUlProps): void {
+ this._sheetBar.setValue({ sheetList: this._sheetList, menuList: this._menuList }, () => {
+ this._sheetBar.overGrid();
+ });
+ }
+
+ reNameSheet() {
+ this._sheetBar.reNameSheet(this._dataId);
+ }
+
+ hideSheet() {
+
+ }
+
+ unHideSheet() {
+ this._sheetBar.ref.current.showSelect();
+ }
+
+ moveSheet(direct: string) {
+
+ }
+
+ changeSheetName(e: Event) {
+
+ }
+
+ contextMenu(e: MouseEvent) {
+ this._sheetBar.contextMenu(e);
+ }
+
+ dragEnd(element: HTMLDivElement[]) {
+ let list: SheetUlProps[] = [];
+ Array.from(element).forEach((node: any) => {
+ const item = this._sheetList.find((ele) => ele.sheetId === node.dataset.id);
+ if (item) {
+ list.push(item);
+ }
+ });
+ this._sheetList = list;
+ this._sheetBar.setValue({
+ sheetList: list,
+ });
+ }
}
\ No newline at end of file
diff --git a/packages/ui-plugin-sheets/src/Model/InfoBarModel.ts b/packages/ui-plugin-sheets/src/Model/InfoBarModel.ts
new file mode 100644
index 0000000000..e9d969cf44
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/Model/InfoBarModel.ts
@@ -0,0 +1,11 @@
+export class InfoBarModel {
+ constructor(private _name: string) {}
+
+ get name(): string {
+ return this._name;
+ }
+
+ setName(name: string) {
+ this._name = name;
+ }
+}
diff --git a/packages/ui-plugin-sheets/src/Model/SheetBarModel.ts b/packages/ui-plugin-sheets/src/Model/SheetBarModel.ts
new file mode 100644
index 0000000000..aecba76a01
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/Model/SheetBarModel.ts
@@ -0,0 +1,11 @@
+export class SheetBarModel {
+ private _sheetList: any;
+
+ get sheetList(): any {
+ return this._sheetList;
+ }
+
+ setSheetName(list: any) {
+ this._sheetList = list;
+ }
+}