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

jerryebddc9ec52e8
diff --git a/packages/base-sheets/src/Controller/CountBarController.ts b/packages/base-sheets/src/Controller/CountBarController.ts
index 4098ada121..bdddcc0b06 100644
--- a/packages/base-sheets/src/Controller/CountBarController.ts
+++ b/packages/base-sheets/src/Controller/CountBarController.ts
@@ -1,125 +1,32 @@
-import { RangeList, Tools } from '@univerjs/core';
+import { UIObserver } from '@univerjs/core';
import { SheetPlugin } from '../SheetPlugin';
-import { CountBar } from '../View/UI/CountBar';
-
-class APIController {
- private _master: CountBarController;
-
- constructor(master: CountBarController) {
- this._master = master;
- }
-}
-
-class UIController {
- private _master: CountBarController;
-
- private _countBar: CountBar;
-
- private _initialize(): void {
- const plugin = this._master.getPlugin();
- const context = plugin.getContext();
- const workbook = context.getWorkBook();
-
- this._countBar.setState({
- onChange: (v: any) => {
- workbook.getActiveSheet().setZoomRatio(Tools.numberFixed(v / 100, 2));
- },
- });
- }
-
- private _totalRangeList(rangeList: RangeList): void {
- let rectList = rangeList.getRangeList();
- let recList: string[] = [];
- let plugin = this._master.getPlugin();
- 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}`,
- });
- }
-
- private _initializeSelectedObserver(): void {
- const plugin = this._master.getPlugin();
- const manager = plugin.getSelectionManager();
- plugin.getObserver('onChangeSelectionObserver')?.add(() => {
- const rangeList = manager.getActiveRangeList();
- if (rangeList && this._countBar) {
- this._totalRangeList(rangeList);
- }
- });
- }
-
- constructor(master: CountBarController) {
- this._master = master;
- const context = master.getPlugin().getContext();
- const manager = context.getObserverManager();
- manager.requiredObserver<CountBar>('onCountBarDidMountObservable').add((countBar: CountBar) => {
- this._countBar = countBar;
- this._initialize();
- });
- context.getContextObserver('onAfterChangeActiveSheetObservable').add(() => {
- const zoomRatio = context.getWorkBook().getActiveSheet().getZoomRatio();
- this._countBar.setZoom(Math.trunc(zoomRatio * 100));
- });
- context.getContextObserver('onSheetRenderDidMountObservable').add(() => {
- this._initializeSelectedObserver();
- });
- context.getContextObserver('onZoomRatioSheetObservable').add((value) => {
- this._countBar.setZoom(Math.trunc(value.zoomRatio * 100));
- });
- }
-}
export class CountBarController {
private _plugin: SheetPlugin;
- private _uiController: UIController;
-
- private _apiController: APIController;
+ protected _getCoreObserver<T>(type: string) {
+ return this._plugin.getContext().getObserverManager().requiredObserver<UIObserver<T>>(type, 'core');
+ }
constructor(plugin: SheetPlugin) {
this._plugin = plugin;
- this._uiController = new UIController(this);
- this._apiController = new APIController(this);
}
getPlugin(): SheetPlugin {
return this._plugin;
}
- getUIController(): UIController {
- return this._uiController;
- }
-
- getAPIController(): APIController {
- return this._apiController;
+ listenEventManager(): void {
+ this._getCoreObserver<number>('onUIChangeObservable').add(({ name, value }) => {
+ switch (name) {
+ case 'changeZoom': {
+ const workbook = this._plugin.getContext().getWorkBook();
+ if (workbook) {
+ workbook.getActiveSheet().setZoomRatio(value!);
+ }
+ break;
+ }
+ }
+ });
}
}
diff --git a/packages/base-sheets/src/Controller/SheetBarController.ts b/packages/base-sheets/src/Controller/SheetBarController.ts
index 605a6b11a9..57bd8f16d5 100644
--- a/packages/base-sheets/src/Controller/SheetBarController.ts
+++ b/packages/base-sheets/src/Controller/SheetBarController.ts
@@ -1,13 +1,14 @@
-import { NameGen, Plugin, UIObserver } from '@univerjs/core';
+import { NameGen, UIObserver } from '@univerjs/core';
+import { SheetPlugin } from '../SheetPlugin';
export class SheetBarControl {
- protected _plugin: Plugin;
+ protected _plugin: SheetPlugin;
protected _getCoreObserver<T>(type: string) {
- return this._plugin.getContext().getUniver().getGlobalContext().getObserverManager().requiredObserver<UIObserver<T>>(type, 'core');
+ return this._plugin.getContext().getObserverManager().requiredObserver<UIObserver<T>>(type, 'core');
}
- constructor(plugin: Plugin) {
+ constructor(plugin: SheetPlugin) {
this._plugin = plugin;
}
@@ -15,14 +16,14 @@ export class SheetBarControl {
this._getCoreObserver('onUIChangeObservable').add(({ name, value }) => {
switch (name) {
case 'deleteSheet': {
- const workbook = this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
if (workbook) {
workbook.removeSheetBySheetId(value as string);
}
break;
}
case 'copySheet': {
- const workbook = this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
const activeSheet = workbook.getActiveSheet();
const copySheet = activeSheet.copy(NameGen.getSheetName());
if (workbook) {
@@ -40,8 +41,7 @@ export class SheetBarControl {
break;
}
case 'addSheet': {
- const context = this._plugin.getContext();
- const workbook = context.getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
workbook.insertSheet();
const size = workbook.getSheetSize();
@@ -53,7 +53,7 @@ export class SheetBarControl {
break;
}
case 'unHideSheet': {
- const workbook = this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
const worksheet = workbook.getSheetBySheetId(value as string);
if (worksheet) {
worksheet.showSheet();
@@ -61,7 +61,7 @@ export class SheetBarControl {
break;
}
case 'hideSheet': {
- const workbook = this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
const worksheet = workbook.getSheetBySheetId(value as string);
if (worksheet) {
worksheet.hideSheet();
@@ -70,7 +70,7 @@ export class SheetBarControl {
}
case 'changeSheetColor': {
const { color, sheetId } = value as { color: string; sheetId: string };
- const workbook = this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook();
+ const workbook = this._plugin.getContext().getWorkBook();
const worksheet = workbook.getSheetBySheetId(sheetId);
if (worksheet) {
worksheet.setTabColor(color);
diff --git a/packages/ui-plugin-sheets/src/Controller/CountBarUIController.ts b/packages/ui-plugin-sheets/src/Controller/CountBarUIController.ts
index 15d5ae0183..39df04b136 100644
--- a/packages/ui-plugin-sheets/src/Controller/CountBarUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/CountBarUIController.ts
@@ -1,5 +1,5 @@
import { SheetPlugin } from '@univerjs/base-sheets';
-import { RangeList, Plugin, Tools, PLUGIN_NAMES, CommandManager, SheetActionBase, SetZoomRatioAction } from '@univerjs/core';
+import { RangeList, Plugin, Tools, PLUGIN_NAMES, CommandManager, SheetActionBase, SetZoomRatioAction, UIObserver } from '@univerjs/core';
import { CountBar } from '../View/CountBar';
export class CountBarUIController {
@@ -52,6 +52,10 @@ export class CountBarUIController {
this._refreshCountBarUI();
}
+ protected _setUIObserve<T>(type: string, msg: UIObserver<T>) {
+ this._plugin.getContext().getObserverManager().requiredObserver<UIObserver<T>>(type, 'core').notifyObservers(msg);
+ }
+
constructor(plugin: Plugin) {
this._plugin = plugin;
CommandManager.getActionObservers().add((event) => {
@@ -87,8 +91,7 @@ export class CountBarUIController {
// changeRatio
onChange = (v: string) => {
- console.log(v);
- this._plugin.getContext().getUniver().getCurrentUniverSheetInstance().getWorkBook().getActiveSheet().setZoomRatio(Tools.numberFixed(v / 100, 2));
+ this._setUIObserve('onUIChangeObservable', { name: 'changeZoom', value: v });
}
// 刷新组件