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: zoom shortcut (#285)

YuHong979fc39c23c0
diff --git a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
index de8347793f..4626bcb209 100644
--- a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
+++ b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
@@ -60,7 +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 { ChangeZoomRatioCommand, 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';
@@ -197,6 +197,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetFrozenCancelCommand,
ScrollCommand,
SetZoomRatioCommand,
+ ChangeZoomRatioCommand,
].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 43cee398e5..1a7af30a1e 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 = [SetZoomRatioCommand.id];
+ const updateCommandList = [SetZoomRatioOperation.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 ISetZoomRatioCommandParams;
+ const params = command.params as ISetZoomRatioOperationParams;
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 e5c97d6f94..ce18d227fa 100644
--- a/packages/base-sheets/src/Controller/zoom.controller.ts
+++ b/packages/base-sheets/src/Controller/zoom.controller.ts
@@ -12,6 +12,7 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
import { SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
+import { SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
import { ScrollManagerService } from '../services/scroll-manager.service';
import { SelectionManagerService } from '../services/selection-manager.service';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -98,7 +99,7 @@ export class ZoomController extends Disposable {
}
private _commandExecutedListener() {
- const updateCommandList = [SetZoomRatioCommand.id];
+ const updateCommandList = [SetZoomRatioOperation.id];
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
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
index f8350468ee..04b754a86d 100644
--- a/packages/base-sheets/src/commands/commands/set-zoom-ratio.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-zoom-ratio.command.ts
@@ -1,4 +1,4 @@
-import { CommandType, ICommand, ICommandService } from '@univerjs/core';
+import { CommandType, ICommand, ICommandService, ICurrentUniverService } from '@univerjs/core';
import { SetZoomRatioOperation } from '../operations/set-zoom-ratio.operation';
@@ -7,10 +7,38 @@ export interface ISetZoomRatioCommandParams {
workbookId: string;
worksheetId: string;
}
+export interface IChangeZoomRatioCommandParams {
+ reset?: boolean;
+ delta: number;
+}
/**
* Zoom
*/
+
+export const ChangeZoomRatioCommand: ICommand<IChangeZoomRatioCommandParams> = {
+ id: 'sheet.command.change-zoom-ratio',
+ type: CommandType.COMMAND,
+ handler: async (accessor, params) => {
+ if (!params) {
+ return false;
+ }
+ const { delta, reset } = params;
+ const workbook = accessor.get(ICurrentUniverService).getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+ const workbookId = workbook.getUnitId();
+ const worksheetId = worksheet.getSheetId();
+
+ const zoomRatio = reset ? 1 : Math.round((worksheet.getConfig().zoomRatio + delta) * 100) / 100;
+
+ return accessor.get(ICommandService).executeCommand(SetZoomRatioOperation.id, {
+ workbookId,
+ worksheetId,
+ zoomRatio,
+ });
+ },
+};
+
export const SetZoomRatioCommand: ICommand<ISetZoomRatioCommandParams> = {
id: 'sheet.command.set-zoom-ratio',
type: CommandType.COMMAND,
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 81a09aefa7..a98bae7040 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -96,7 +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 { ChangeZoomRatioCommand, SetZoomRatioCommand } from './commands/commands/set-zoom-ratio.command';
export {
InsertColMutation,
InsertColMutationUndoFactory as InsertColMutationFactory,
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx b/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx
index 9a2fe12e5d..ea3a555566 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx
+++ b/packages/ui-plugin-sheets/src/View/CountBar/zoom-slider.tsx
@@ -1,4 +1,5 @@
import { SetWorksheetActivateCommand, SetZoomRatioCommand } from '@univerjs/base-sheets';
+import { SetZoomRatioOperation } from '@univerjs/base-sheets/commands/operations/set-zoom-ratio.operation.js';
import { Slider } from '@univerjs/base-ui';
import { ICommandService, ICurrentUniverService } from '@univerjs/core';
import { useDependency } from '@wendellhu/redi/react-bindings';
@@ -15,7 +16,7 @@ export function ZoomSlider() {
useEffect(() => {
const disposable = commandService.onCommandExecuted((commandInfo) => {
- if (commandInfo.id === SetZoomRatioCommand.id || commandInfo.id === SetWorksheetActivateCommand.id) {
+ if (commandInfo.id === SetZoomRatioOperation.id || commandInfo.id === SetWorksheetActivateCommand.id) {
const currentZoom = getCurrentZoom();
setZoom(currentZoom);
}
@@ -36,7 +37,7 @@ export function ZoomSlider() {
if (!workbook) return;
const worksheet = workbook.getActiveSheet();
if (!worksheet) return;
- const zoomRatio = (value / 100).toFixed(2);
+ const zoomRatio = value / 100;
commandService.executeCommand(SetZoomRatioCommand.id, {
workbookId: workbook.getUnitId(),
diff --git a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
index 3fde83d0a3..cbaa107b20 100644
--- a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
@@ -114,6 +114,7 @@ import {
SetUnderlineShortcutItem,
} from './shortcuts/style.shortcut';
import { ClearSelectionValueShortcutItem } from './shortcuts/value.shortcut';
+import { ResetZoomShortcutItem, ZoomInShortcutItem, ZoomOutShortcutItem } from './shortcuts/view.shortcut';
@OnLifecycle(LifecycleStages.Ready, SheetUIController)
export class SheetUIController extends Disposable {
@@ -247,6 +248,11 @@ export class SheetUIController extends Disposable {
ExpandSelectionEndRightShortcutItem,
SelectAllShortcutItem,
+ // view shortcuts
+ ZoomInShortcutItem,
+ ZoomOutShortcutItem,
+ ResetZoomShortcutItem,
+
// toggle cell style shortcuts
SetBoldShortcutItem,
SetItalicShortcutItem,
diff --git a/packages/ui-plugin-sheets/src/controller/shortcuts/view.shortcut.ts b/packages/ui-plugin-sheets/src/controller/shortcuts/view.shortcut.ts
new file mode 100644
index 0000000000..a9ca397704
--- /dev/null
+++ b/packages/ui-plugin-sheets/src/controller/shortcuts/view.shortcut.ts
@@ -0,0 +1,26 @@
+import { ChangeZoomRatioCommand } from '@univerjs/base-sheets';
+import { IShortcutItem, KeyCode, MetaKeys } from '@univerjs/base-ui';
+
+export const ZoomInShortcutItem: IShortcutItem = {
+ id: ChangeZoomRatioCommand.id,
+ binding: KeyCode.EQUAL | MetaKeys.CTRL_COMMAND,
+ staticParameters: {
+ delta: 0.2,
+ },
+};
+
+export const ZoomOutShortcutItem: IShortcutItem = {
+ id: ChangeZoomRatioCommand.id,
+ binding: KeyCode.MINUS | MetaKeys.CTRL_COMMAND,
+ staticParameters: {
+ delta: -0.2,
+ },
+};
+
+export const ResetZoomShortcutItem: IShortcutItem = {
+ id: ChangeZoomRatioCommand.id,
+ binding: KeyCode.Digit0 | MetaKeys.CTRL_COMMAND,
+ staticParameters: {
+ reset: true,
+ },
+};