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.

feat(sheet): add zoom slider at footer (#281)

YuHonga745f6b5a6de
diff --git a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
index a8c8f7b0d7..b3960ee9d4 100644
--- a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
+++ b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
@@ -60,6 +60,7 @@ import { SetWorksheetNameCommand } from '../commands/commands/set-worksheet-name
import { SetWorksheetOrderCommand } from '../commands/commands/set-worksheet-order.command';
import { DeltaRowHeightCommand, SetRowHeightCommand } from '../commands/commands/set-worksheet-row-height.command';
import { SetWorksheetShowCommand } from '../commands/commands/set-worksheet-show.command';
+import { SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { TrimWhitespaceCommand } from '../commands/commands/trim-whitespace.command';
import { AddWorksheetMergeMutation } from '../commands/mutations/add-worksheet-merge.mutation';
import { DeleteRangeMutation } from '../commands/mutations/delete-range.mutation';
@@ -194,6 +195,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetFrozenMutation,
SetFrozenCancelCommand,
ScrollCommand,
+ SetZoomRatioCommand,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}
}
diff --git a/packages/base-sheets/src/Controller/Selection.controller.ts b/packages/base-sheets/src/Controller/Selection.controller.ts
index 1a7af30a1e..43cee398e5 100644
--- a/packages/base-sheets/src/Controller/Selection.controller.ts
+++ b/packages/base-sheets/src/Controller/Selection.controller.ts
@@ -18,8 +18,8 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject, ISheetObjectParam } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
+import { ISetZoomRatioCommandParams, SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { SetSelectionsOperation } from '../commands/operations/selection.operation';
-import { ISetZoomRatioOperationParams, SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../services/selection-manager.service';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -167,7 +167,7 @@ export class SelectionController extends Disposable {
}
private _commandExecutedListener() {
- const updateCommandList = [SetZoomRatioOperation.id];
+ const updateCommandList = [SetZoomRatioCommand.id];
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
@@ -175,7 +175,7 @@ export class SelectionController extends Disposable {
const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
- const params = command.params as ISetZoomRatioOperationParams;
+ const params = command.params as ISetZoomRatioCommandParams;
const { workbookId, worksheetId } = params;
if (!(workbookId === workbook.getUnitId() && worksheetId === worksheet.getSheetId())) {
return;
diff --git a/packages/base-sheets/src/Controller/zoom.controller.ts b/packages/base-sheets/src/Controller/zoom.controller.ts
index e647477bd6..e5c97d6f94 100644
--- a/packages/base-sheets/src/Controller/zoom.controller.ts
+++ b/packages/base-sheets/src/Controller/zoom.controller.ts
@@ -11,7 +11,7 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
-import { SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
+import { SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { ScrollManagerService } from '../services/scroll-manager.service';
import { SelectionManagerService } from '../services/selection-manager.service';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -71,7 +71,7 @@ export class ZoomController extends Disposable {
let nextRatio = +parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);
nextRatio = nextRatio >= 4 ? 4 : nextRatio <= 0.1 ? 0.1 : nextRatio;
- this._commandService.executeCommand(SetZoomRatioOperation.id, {
+ this._commandService.executeCommand(SetZoomRatioCommand.id, {
zoomRatio: nextRatio,
workbookId: workbook.getUnitId(),
worksheetId: sheet.getSheetId(),
@@ -98,7 +98,7 @@ export class ZoomController extends Disposable {
}
private _commandExecutedListener() {
- const updateCommandList = [SetZoomRatioOperation.id];
+ const updateCommandList = [SetZoomRatioCommand.id];
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
diff --git a/packages/base-sheets/src/SheetPlugin.ts b/packages/base-sheets/src/SheetPlugin.ts
index c45ee8dec2..0dc8677bce 100644
--- a/packages/base-sheets/src/SheetPlugin.ts
+++ b/packages/base-sheets/src/SheetPlugin.ts
@@ -4,6 +4,7 @@ import { Dependency, Inject, Injector } from '@wendellhu/redi';
import { SetScrollOperation } from './commands/operations/scroll.operation';
import { SetSelectionsOperation } from './commands/operations/selection.operation';
+import { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.operation';
import { BasicWorksheetController } from './Controller/BasicWorksheet.controller';
import { FormulaBarController } from './Controller/FormulaBarController';
import { FreezeController } from './Controller/freeze.controller';
@@ -114,5 +115,6 @@ export class SheetPlugin extends Plugin {
private _initializeCommands(): void {
this._commandService.registerCommand(SetSelectionsOperation);
this._commandService.registerCommand(SetScrollOperation);
+ this._commandService.registerCommand(SetZoomRatioOperation);
}
}
diff --git a/packages/base-sheets/src/commands/commands/set-zoom-ratio.command.ts b/packages/base-sheets/src/commands/commands/set-zoom-ratio.command.ts
new file mode 100644
index 0000000000..f8350468ee
--- /dev/null
+++ b/packages/base-sheets/src/commands/commands/set-zoom-ratio.command.ts
@@ -0,0 +1,29 @@
+import { CommandType, ICommand, ICommandService } from '@univerjs/core';
+
+import { SetZoomRatioOperation } from '../operations/set-zoom-ratio.operation';
+
+export interface ISetZoomRatioCommandParams {
+ zoomRatio: number;
+ workbookId: string;
+ worksheetId: string;
+}
+
+/**
+ * Zoom
+ */
+export const SetZoomRatioCommand: ICommand<ISetZoomRatioCommandParams> = {
+ id: 'sheet.command.set-zoom-ratio',
+ type: CommandType.COMMAND,
+ handler: async (accessor, params) => {
+ if (!params) {
+ return false;
+ }
+ const { workbookId, worksheetId, zoomRatio } = params;
+
+ return accessor.get(ICommandService).executeCommand(SetZoomRatioOperation.id, {
+ workbookId,
+ worksheetId,
+ zoomRatio,
+ });
+ },
+};
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 7244b5913f..b949ca55ba 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -96,6 +96,7 @@ export { SetWorksheetNameCommand } from './commands/commands/set-worksheet-name.
export { SetWorksheetOrderCommand } from './commands/commands/set-worksheet-order.command';
export { SetRowHeightCommand as SetWorksheetRowHeightCommand } from './commands/commands/set-worksheet-row-height.command';
export { SetWorksheetShowCommand } from './commands/commands/set-worksheet-show.command';
+export { SetZoomRatioCommand } from './commands/commands/set-zoom-ratio.command';
export {
InsertColMutation,
InsertColMutationUndoFactory as InsertColMutationFactory,
diff --git a/packages/base-ui/src/Common/AppContext.tsx b/packages/base-ui/src/Common/AppContext.tsx
index f10700c9bc..74d6eaaa93 100644
--- a/packages/base-ui/src/Common/AppContext.tsx
+++ b/packages/base-ui/src/Common/AppContext.tsx
@@ -16,5 +16,7 @@ export type AppContextValues = {
componentManager: ComponentManager;
zIndexManager: ZIndexManager;
};
-
+/**
+ * @deprecated
+ */
export const AppContext = createContext<AppContextValues>({} as unknown as AppContextValues);
diff --git a/packages/base-ui/src/views/app.tsx b/packages/base-ui/src/views/app.tsx
index 606952573f..6cf8fae967 100644
--- a/packages/base-ui/src/views/app.tsx
+++ b/packages/base-ui/src/views/app.tsx
@@ -123,7 +123,7 @@ export function App(props: IUniverAppProps) {
</Content>
</Layout>
{/* footer */}
- <Footer style={{ display: props.footer ? 'block' : 'none' }}>
+ <Footer style={{ display: props.footer ? 'flex' : 'none' }}>
{footerComponents &&
Array.from(footerComponents.values()).map((component, index) =>
React.createElement(component(), { key: `${index}` })
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
index d4ba89a824..e64d8b0884 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
@@ -1,30 +1,20 @@
-import { Button, Icon, Slider } from '@univerjs/base-ui';
-import { useState } from 'react';
+import { Button, Icon } from '@univerjs/base-ui';
import styles from './index.module.less';
+import { ZoomSlider } from './zoom-slider';
interface ICountBarProps {
changeRatio?: (ratio: string) => void;
onChange?: (value: string) => void;
}
-const ZOOM_MAP = [50, 75, 100, 125, 150, 175, 200, 400];
-
export function CountBar(props: ICountBarProps) {
- const [zoom, setZoom] = useState(100);
-
- function handleChange(value: number) {
- setZoom(value);
-
- // TODO: changeRatio, onChange
- }
-
return (
<section className={styles.countBar}>
<Button>
<Icon.Sheet.RegularIcon />
</Button>
- <Slider value={zoom} shortcuts={ZOOM_MAP} onChange={handleChange} />
+ <ZoomSlider />
</section>
);
}
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/index.module.less b/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
index 8276aba007..6dd1788e0c 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
+++ b/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
@@ -1,4 +1,5 @@
.count-bar {
+ flex: 0 0 auto;
display: flex;
justify-content: flex-end;
}
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx b/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx
new file mode 100644
index 0000000000..46627393ff
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx
@@ -0,0 +1,47 @@
+import { SetWorksheetActivateCommand, SetZoomRatioCommand } from '@univerjs/base-sheets';
+import { Slider } from '@univerjs/base-ui';
+import { ICommandService, ICurrentUniverService } from '@univerjs/core';
+import { useDependency } from '@wendellhu/redi/react-bindings';
+import { useEffect, useState } from 'react';
+
+const ZOOM_MAP = [50, 75, 100, 125, 150, 175, 200, 400];
+const ZOOM_RANGE = [10, 400];
+
+export function ZoomSlider() {
+ const commandService = useDependency(ICommandService);
+ const currentUniverService = useDependency(ICurrentUniverService);
+ const currentZoom = getCurrentZoom();
+ const [zoom, setZoom] = useState(currentZoom);
+
+ useEffect(() => {
+ const disposable = commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetZoomRatioCommand.id || commandInfo.id === SetWorksheetActivateCommand.id) {
+ const currentZoom = getCurrentZoom();
+ setZoom(currentZoom);
+ }
+ });
+ return disposable.dispose;
+ }, [commandService]);
+
+ function getCurrentZoom() {
+ return currentUniverService.getCurrentUniverSheetInstance()?.getActiveSheet().getZoomRatio() * 100 || 100;
+ }
+
+ function handleChange(value: number) {
+ if (value > ZOOM_RANGE[1] || value < ZOOM_RANGE[0]) return;
+ setZoom(value);
+ const workbook = currentUniverService.getCurrentUniverSheetInstance();
+ if (!workbook) return;
+ const worksheet = workbook.getActiveSheet();
+ if (!worksheet) return;
+ const zoomRatio = (value / 100).toFixed(2);
+
+ commandService.executeCommand(SetZoomRatioCommand.id, {
+ workbookId: workbook.getUnitId(),
+ worksheetId: worksheet.getSheetId(),
+ zoomRatio,
+ });
+ }
+
+ return <Slider value={zoom} shortcuts={ZOOM_MAP} onChange={handleChange} />;
+}
diff --git a/packages/ui-plugin-sheets/src/View/SheetBar/index.module.less b/packages/ui-plugin-sheets/src/View/SheetBar/index.module.less
index 22a4769726..59b5eb90fd 100644
--- a/packages/ui-plugin-sheets/src/View/SheetBar/index.module.less
+++ b/packages/ui-plugin-sheets/src/View/SheetBar/index.module.less
@@ -4,7 +4,8 @@
display: flex;
flex-direction: row;
background-color: var(--bg);
- width: 70%;
+ flex: 1;
+ width: calc(100% - 350px);
}
.sheet-bar-options {