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): doc and editor support zoom (#348)

Univerc16dba2258a0
diff --git a/docs/object architecture design .tldr b/docs/object architecture design .tldr
index 8d272e2ed6..3d85c8d065 100644
--- a/docs/object architecture design .tldr
+++ b/docs/object architecture design .tldr
@@ -66,9 +66,9 @@
{
"id": "pointer:pointer",
"typeName": "pointer",
- "x": -410.59663851457776,
- "y": 372.5379325418338,
- "lastActivityTimestamp": 1698468348822,
+ "x": -485.4038593136576,
+ "y": 328.0656960830613,
+ "lastActivityTimestamp": 1698752050547,
"meta": {}
},
{
@@ -79,9 +79,9 @@
"typeName": "page"
},
{
- "x": 650.0906443061476,
- "y": 525.0931191337122,
- "z": 0.707046270098422,
+ "x": 767.9914624314993,
+ "y": 1113.6761096188661,
+ "z": 0.42818580385333427,
"meta": {},
"id": "camera:page:page",
"typeName": "camera"
@@ -104,7 +104,9 @@
"opacityForNextShape": 1,
"stylesForNextShape": {
"tldraw:arrowheadStart": "arrow",
- "tldraw:arrowheadEnd": "none"
+ "tldraw:arrowheadEnd": "none",
+ "tldraw:geo": "rectangle",
+ "tldraw:verticalAlign": "start"
},
"brush": null,
"scribble": null,
@@ -119,7 +121,7 @@
"screenBounds": {
"x": 0,
"y": 0,
- "w": 1566,
+ "w": 1049.3333740234375,
"h": 1278
},
"zoomBrush": null,
@@ -734,7 +736,7 @@
},
{
"x": -244.6117585671821,
- "y": 286.3290541574055,
+ "y": 552.5686141196694,
"rotation": 0,
"isLocked": false,
"opacity": 1,
@@ -742,7 +744,7 @@
"type": "geo",
"props": {
"w": 487.2859795869851,
- "h": 456.21253777103925,
+ "h": 189.97297780877534,
"geo": "rectangle",
"color": "black",
"labelColor": "black",
@@ -762,8 +764,8 @@
"typeName": "shape"
},
{
- "x": -185.00506487181798,
- "y": 561.4947001838739,
+ "x": -177.99876066228472,
+ "y": 615.9881298545911,
"rotation": 0,
"isLocked": false,
"opacity": 1,
@@ -915,7 +917,7 @@
"type": "geo",
"props": {
"w": 428.2377180569433,
- "h": 150.7916717529297,
+ "h": 122.76645491479667,
"geo": "rectangle",
"color": "black",
"labelColor": "black",
@@ -1091,6 +1093,35 @@
"index": "aR",
"id": "shape:Xeb45uyVU9_dUz_CHB6M8",
"typeName": "shape"
+ },
+ {
+ "x": -249.101257256061,
+ "y": 279.44526028630526,
+ "rotation": 0,
+ "isLocked": false,
+ "opacity": 1,
+ "meta": {},
+ "id": "shape:Z9wWd-UiMtQoXuLjrwqDx",
+ "type": "geo",
+ "props": {
+ "w": 495.0432431672655,
+ "h": 212.45560441351793,
+ "geo": "rectangle",
+ "color": "black",
+ "labelColor": "black",
+ "fill": "none",
+ "dash": "draw",
+ "size": "m",
+ "font": "draw",
+ "text": "Core",
+ "align": "middle",
+ "verticalAlign": "start",
+ "growY": 0,
+ "url": ""
+ },
+ "parentId": "page:page",
+ "index": "a0",
+ "typeName": "shape"
}
]
}
\ No newline at end of file
diff --git a/packages/base-docs/src/Controller/doc-render.controller.ts b/packages/base-docs/src/Controller/doc-render.controller.ts
index 5acb4307ec..ffef21611d 100644
--- a/packages/base-docs/src/Controller/doc-render.controller.ts
+++ b/packages/base-docs/src/Controller/doc-render.controller.ts
@@ -63,55 +63,55 @@ export class DocRenderController extends Disposable {
this.recalculateSizeBySkeleton(currentRender, documentSkeleton);
- this.calculatePagePosition(currentRender);
+ // this.calculatePagePosition(currentRender);
});
}
- calculatePagePosition(currentRender: IRender) {
- const { mainComponent, scene } = currentRender;
+ // calculatePagePosition(currentRender: IRender) {
+ // const { mainComponent, scene } = currentRender;
- const docsComponent = mainComponent as Documents;
+ // const docsComponent = mainComponent as Documents;
- const parent = scene?.getParent();
+ // const parent = scene?.getParent();
- const { width: docsWidth, height: docsHeight, pageMarginLeft, pageMarginTop } = docsComponent;
- if (parent == null || docsWidth === Infinity || docsHeight === Infinity) {
- return;
- }
- const { width: engineWidth, height: engineHeight } = parent;
- let docsLeft = 0;
- let docsTop = 0;
+ // const { width: docsWidth, height: docsHeight, pageMarginLeft, pageMarginTop } = docsComponent;
+ // if (parent == null || docsWidth === Infinity || docsHeight === Infinity) {
+ // return;
+ // }
+ // const { width: engineWidth, height: engineHeight } = parent;
+ // let docsLeft = 0;
+ // let docsTop = 0;
- let sceneWidth = 0;
+ // let sceneWidth = 0;
- let sceneHeight = 0;
+ // let sceneHeight = 0;
- if (engineWidth > docsWidth) {
- docsLeft = engineWidth / 2 - docsWidth / 2;
- sceneWidth = engineWidth - 34;
- } else {
- docsLeft = pageMarginLeft;
- sceneWidth = docsWidth + pageMarginLeft * 2;
- }
+ // if (engineWidth > docsWidth) {
+ // docsLeft = engineWidth / 2 - docsWidth / 2;
+ // sceneWidth = engineWidth - 34;
+ // } else {
+ // docsLeft = pageMarginLeft;
+ // sceneWidth = docsWidth + pageMarginLeft * 2;
+ // }
- if (engineHeight > docsHeight) {
- docsTop = engineHeight / 2 - docsHeight / 2;
- sceneHeight = engineHeight - 34;
- } else {
- docsTop = pageMarginTop;
- sceneHeight = docsHeight + pageMarginTop * 2;
- }
+ // if (engineHeight > docsHeight) {
+ // docsTop = engineHeight / 2 - docsHeight / 2;
+ // sceneHeight = engineHeight - 34;
+ // } else {
+ // docsTop = pageMarginTop;
+ // sceneHeight = docsHeight + pageMarginTop * 2;
+ // }
- // this.docsLeft = docsLeft;
+ // // this.docsLeft = docsLeft;
- // this.docsTop = docsTop;
+ // // this.docsTop = docsTop;
- scene.resize(sceneWidth, sceneHeight + 200);
+ // scene.resize(sceneWidth, sceneHeight + 200);
- docsComponent.translate(docsLeft, docsTop);
+ // docsComponent.translate(docsLeft, docsTop);
- return this;
- }
+ // return this;
+ // }
recalculateSizeBySkeleton(currentRender: IRender, skeleton: DocumentSkeleton) {
const { mainComponent, scene } = currentRender;
@@ -184,7 +184,7 @@ export class DocRenderController extends Disposable {
this.recalculateSizeBySkeleton(currentRender, skeleton);
- this.calculatePagePosition(currentRender);
+ // this.calculatePagePosition(currentRender);
}
})
);
diff --git a/packages/base-docs/src/Controller/text-selection.controller.ts b/packages/base-docs/src/Controller/text-selection.controller.ts
index fb521d7a63..626b05f86f 100644
--- a/packages/base-docs/src/Controller/text-selection.controller.ts
+++ b/packages/base-docs/src/Controller/text-selection.controller.ts
@@ -24,7 +24,10 @@ import { Inject } from '@wendellhu/redi';
import { getDocObjectById } from '../Basics/component-tools';
import { NORMAL_TEXT_SELECTION_PLUGIN_NAME, VIEWPORT_KEY } from '../Basics/docs-view-key';
-import { ISetZoomRatioMutationParams, SetZoomRatioMutation } from '../commands/mutations/set-zoom-ratio.mutation';
+import {
+ ISetDocZoomRatioOperationParams,
+ SetDocZoomRatioOperation,
+} from '../commands/operations/set-doc-zoom-ratio.operation';
import { SetTextSelectionsOperation } from '../commands/operations/text-selection.operation';
import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
import { TextSelectionManagerService } from '../services/text-selection-manager.service';
@@ -151,6 +154,8 @@ export class TextSelectionController extends Disposable {
}
this._textSelectionRenderManager.sync();
+
+ this._textSelectionRenderManager.scroll();
});
}
@@ -195,12 +200,12 @@ export class TextSelectionController extends Disposable {
}
private _commandExecutedListener() {
- const updateCommandList = [SetZoomRatioMutation.id];
+ const updateCommandList = [SetDocZoomRatioOperation.id];
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (updateCommandList.includes(command.id)) {
- const params = command.params as ISetZoomRatioMutationParams;
+ const params = command.params as ISetDocZoomRatioOperationParams;
const { documentId } = params;
const unitId = this._textSelectionManagerService.getCurrentSelection()?.unitId;
diff --git a/packages/base-docs/src/Controller/zoom.cotroller.ts b/packages/base-docs/src/Controller/zoom.cotroller.ts
new file mode 100644
index 0000000000..d2bde7da43
--- /dev/null
+++ b/packages/base-docs/src/Controller/zoom.cotroller.ts
@@ -0,0 +1,233 @@
+import {
+ DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
+ DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
+ IRenderManagerService,
+ IWheelEvent,
+} from '@univerjs/base-render';
+import {
+ Disposable,
+ ICommandInfo,
+ ICommandService,
+ IUniverInstanceService,
+ LifecycleStages,
+ OnLifecycle,
+ toDisposable,
+} from '@univerjs/core';
+import { Inject } from '@wendellhu/redi';
+
+import { getDocObject, IDocObjectParam } from '../Basics/component-tools';
+import { VIEWPORT_KEY } from '../Basics/docs-view-key';
+import { SetDocZoomRatioCommand } from '../commands/commands/set-doc-zoom-ratio.command';
+import { SetDocZoomRatioOperation } from '../commands/operations/set-doc-zoom-ratio.operation';
+import { DocSkeletonManagerService } from '../services/doc-skeleton-manager.service';
+
+interface ISetDocMutationParams {
+ documentId: string;
+}
+
+@OnLifecycle(LifecycleStages.Rendered, ZoomController)
+export class ZoomController extends Disposable {
+ private _initializedRender = new Set();
+
+ constructor(
+ @Inject(DocSkeletonManagerService) private readonly _docSkeletonManagerService: DocSkeletonManagerService,
+ @IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
+ @ICommandService private readonly _commandService: ICommandService,
+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService
+ ) {
+ super();
+
+ this._initialize();
+ }
+
+ override dispose(): void {
+ super.dispose();
+ }
+
+ private _initialize() {
+ this._skeletonListener();
+ this._commandExecutedListener();
+ this._initialRenderRefresh();
+ }
+
+ private _initialRenderRefresh() {
+ this._docSkeletonManagerService.currentSkeleton$.subscribe((param) => {
+ if (param == null) {
+ return;
+ }
+
+ const { unitId } = param;
+
+ const currentRender = this._renderManagerService.getRenderById(unitId);
+
+ if (currentRender == null) {
+ return;
+ }
+
+ if (
+ this._initializedRender.has(unitId) ||
+ [DOCS_NORMAL_EDITOR_UNIT_ID_KEY, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY].includes(unitId)
+ ) {
+ return;
+ }
+
+ this._initializedRender.add(unitId);
+
+ const { mainComponent, components, scene } = currentRender;
+
+ this.disposeWithMe(
+ toDisposable(
+ scene.onMouseWheelObserver.add((e: IWheelEvent, state) => {
+ if (!e.ctrlKey) {
+ return;
+ }
+
+ const deltaFactor = Math.abs(e.deltaX);
+ let ratioDelta = deltaFactor < 40 ? 0.2 : deltaFactor < 80 ? 0.4 : 0.2;
+ ratioDelta *= e.deltaY > 0 ? -1 : 1;
+ if (scene.scaleX < 1) {
+ ratioDelta /= 2;
+ }
+
+ const documentModel = this._currentUniverService.getCurrentUniverDocInstance();
+
+ const currentRatio = documentModel.zoomRatio;
+
+ let nextRatio = +parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);
+ nextRatio = nextRatio >= 4 ? 4 : nextRatio <= 0.1 ? 0.1 : nextRatio;
+
+ this._commandService.executeCommand(SetDocZoomRatioCommand.id, {
+ zoomRatio: nextRatio,
+ workbookId: documentModel.getUnitId(),
+ });
+
+ e.preventDefault();
+ })
+ )
+ );
+ });
+ }
+
+ // private _zoomEventBinding() {
+ // const scene = this._getDocObject()?.scene;
+ // if (scene == null) {
+ // return;
+ // }
+
+ // const viewportMain = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ // }
+
+ private _skeletonListener() {
+ this.disposeWithMe(
+ toDisposable(
+ this._docSkeletonManagerService.currentSkeletonBefore$.subscribe((param) => {
+ if (param == null) {
+ return;
+ }
+
+ const documentModel = this._currentUniverService.getCurrentUniverDocInstance();
+
+ const zoomRatio = documentModel.zoomRatio || 1;
+
+ this._updateViewZoom(zoomRatio);
+ })
+ )
+ );
+ }
+
+ private _commandExecutedListener() {
+ const updateCommandList = [SetDocZoomRatioOperation.id];
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (updateCommandList.includes(command.id)) {
+ const documentModel = this._currentUniverService.getCurrentUniverDocInstance();
+
+ const params = command.params;
+ const { documentId } = params as ISetDocMutationParams;
+ if (!(documentId === documentModel.getUnitId())) {
+ return;
+ }
+
+ const zoomRatio = documentModel.zoomRatio || 1;
+
+ this._updateViewZoom(zoomRatio);
+ }
+ })
+ );
+ }
+
+ private _updateViewZoom(zoomRatio: number) {
+ const docObject = this._getDocObject();
+ if (docObject == null) {
+ return;
+ }
+
+ docObject.scene.scale(zoomRatio, zoomRatio);
+
+ this._calculatePagePosition(docObject, zoomRatio);
+ }
+
+ private _calculatePagePosition(currentRender: IDocObjectParam, zoomRatio: number) {
+ const { document: docsComponent, scene } = currentRender;
+
+ const parent = scene?.getParent();
+
+ const { width: docsWidth, height: docsHeight, pageMarginLeft, pageMarginTop } = docsComponent;
+ if (parent == null || docsWidth === Infinity || docsHeight === Infinity) {
+ return;
+ }
+ const { width: engineWidth, height: engineHeight } = parent;
+ let docsLeft = 0;
+ let docsTop = 0;
+
+ let sceneWidth = 0;
+
+ let sceneHeight = 0;
+
+ let scrollToX = Infinity;
+
+ if (engineWidth > (docsWidth + pageMarginLeft * 2) * zoomRatio) {
+ docsLeft = engineWidth / 2 - (docsWidth * zoomRatio) / 2;
+ docsLeft /= zoomRatio;
+ sceneWidth = engineWidth - 34;
+
+ scrollToX = 0;
+ } else {
+ docsLeft = pageMarginLeft;
+ sceneWidth = docsWidth + pageMarginLeft * 2;
+
+ scrollToX = (sceneWidth - engineWidth / zoomRatio) / 2;
+ }
+
+ if (engineHeight > docsHeight) {
+ docsTop = engineHeight / 2 - docsHeight / 2;
+ sceneHeight = engineHeight - 34;
+ } else {
+ docsTop = pageMarginTop;
+ sceneHeight = docsHeight + pageMarginTop * 2;
+ }
+
+ // this.docsLeft = docsLeft;
+
+ // this.docsTop = docsTop;
+
+ scene.resize(sceneWidth, sceneHeight + 200);
+
+ docsComponent.translate(docsLeft, docsTop);
+
+ const viewport = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ if (scrollToX !== Infinity && viewport != null) {
+ const actualX = viewport.getBarScroll(scrollToX, 0).x;
+ viewport.scrollTo({
+ x: actualX,
+ });
+ }
+
+ return this;
+ }
+
+ private _getDocObject() {
+ return getDocObject(this._currentUniverService, this._renderManagerService);
+ }
+}
diff --git a/packages/base-docs/src/DocPlugin.ts b/packages/base-docs/src/DocPlugin.ts
index 6dd0afaf0f..1f9fee8a5a 100644
--- a/packages/base-docs/src/DocPlugin.ts
+++ b/packages/base-docs/src/DocPlugin.ts
@@ -21,10 +21,10 @@ import {
InsertCommand,
UpdateCommand,
} from './commands/commands/core-editing.command';
-import { SetZoomRatioCommand } from './commands/commands/set-zoom-ratio.command';
+import { SetDocZoomRatioCommand } from './commands/commands/set-doc-zoom-ratio.command';
import { RichTextEditingMutation } from './commands/mutations/core-editing.mutation';
-import { SetZoomRatioMutation } from './commands/mutations/set-zoom-ratio.mutation';
import { MoveCursorOperation } from './commands/operations/cursor.operation';
+import { SetDocZoomRatioOperation } from './commands/operations/set-doc-zoom-ratio.operation';
import { SetTextSelectionsOperation } from './commands/operations/text-selection.operation';
import { DeleteLeftInputController } from './Controller/delete-left-input.controller';
import { DocRenderController } from './Controller/doc-render.controller';
@@ -34,6 +34,7 @@ import { MoveCursorController } from './Controller/move-cursor.controller';
import { NormalInputController } from './Controller/normal-input.controller';
import { PageRenderController } from './Controller/page-render.controller';
import { TextSelectionController } from './Controller/text-selection.controller';
+import { ZoomController } from './Controller/zoom.cotroller';
import { enUS } from './locale';
import { DocSkeletonManagerService } from './services/doc-skeleton-manager.service';
import { DocsViewManagerService } from './services/docs-view-manager/docs-view-manager.service';
@@ -102,8 +103,8 @@ export class DocPlugin extends Plugin {
IMEInputCommand,
RichTextEditingMutation,
CoverCommand,
- SetZoomRatioCommand,
- SetZoomRatioMutation,
+ SetDocZoomRatioCommand,
+ SetDocZoomRatioOperation,
SetTextSelectionsOperation,
] as ICommand[]
).forEach((command) => {
@@ -175,6 +176,7 @@ export class DocPlugin extends Plugin {
[DeleteLeftInputController],
[LineBreakInputController],
[MoveCursorController],
+ [ZoomController],
] as Dependency[]
).forEach((d) => docInjector.add(d));
}
diff --git a/packages/base-docs/src/commands/commands/set-zoom-ratio.command.ts b/packages/base-docs/src/commands/commands/set-doc-zoom-ratio.command.ts
similarity index 64%
rename from packages/base-docs/src/commands/commands/set-zoom-ratio.command.ts
rename to packages/base-docs/src/commands/commands/set-doc-zoom-ratio.command.ts
index f9b9f941dc..0661df2a5f 100644
--- a/packages/base-docs/src/commands/commands/set-zoom-ratio.command.ts
+++ b/packages/base-docs/src/commands/commands/set-doc-zoom-ratio.command.ts
@@ -2,21 +2,21 @@ import { CommandType, ICommand, ICommandService, IUndoRedoService, IUniverInstan
import { IAccessor } from '@wendellhu/redi';
import {
- ISetZoomRatioMutationParams,
- SetZoomRatioMutation,
- SetZoomRatioUndoMutationFactory,
-} from '../mutations/set-zoom-ratio.mutation';
+ ISetDocZoomRatioOperationParams,
+ SetDocZoomRatioOperation,
+ SetDocZoomRatioUndoMutationFactory,
+} from '../operations/set-doc-zoom-ratio.operation';
-export interface ISetZoomRatioCommandParams {
+export interface ISetDocZoomRatioCommandParams {
zoomRatio?: number;
documentId?: string;
}
-export const SetZoomRatioCommand: ICommand = {
+export const SetDocZoomRatioCommand: ICommand = {
type: CommandType.COMMAND,
id: 'doc.command.set-zoom-ratio',
- handler: async (accessor: IAccessor, params?: ISetZoomRatioCommandParams) => {
+ handler: async (accessor: IAccessor, params?: ISetDocZoomRatioCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
@@ -32,22 +32,22 @@ export const SetZoomRatioCommand: ICommand = {
const workbook = univerInstanceService.getUniverDocInstance(documentId);
if (!workbook) return false;
- const setZoomRatioMutationParams: ISetZoomRatioMutationParams = {
+ const setZoomRatioMutationParams: ISetDocZoomRatioOperationParams = {
zoomRatio,
documentId,
};
- const undoMutationParams = SetZoomRatioUndoMutationFactory(accessor, setZoomRatioMutationParams);
- const result = commandService.syncExecuteCommand(SetZoomRatioMutation.id, setZoomRatioMutationParams);
+ const undoMutationParams = SetDocZoomRatioUndoMutationFactory(accessor, setZoomRatioMutationParams);
+ const result = commandService.syncExecuteCommand(SetDocZoomRatioOperation.id, setZoomRatioMutationParams);
if (result) {
undoRedoService.pushUndoRedo({
URI: documentId,
undo() {
- return commandService.syncExecuteCommand(SetZoomRatioMutation.id, undoMutationParams);
+ return commandService.syncExecuteCommand(SetDocZoomRatioOperation.id, undoMutationParams);
},
redo() {
- return commandService.syncExecuteCommand(SetZoomRatioMutation.id, setZoomRatioMutationParams);
+ return commandService.syncExecuteCommand(SetDocZoomRatioOperation.id, setZoomRatioMutationParams);
},
});
return true;
diff --git a/packages/base-docs/src/commands/mutations/set-zoom-ratio.mutation.ts b/packages/base-docs/src/commands/operations/set-doc-zoom-ratio.operation.ts
similarity index 54%
rename from packages/base-docs/src/commands/mutations/set-zoom-ratio.mutation.ts
rename to packages/base-docs/src/commands/operations/set-doc-zoom-ratio.operation.ts
index 4c8e54d67c..2160c34ef9 100644
--- a/packages/base-docs/src/commands/mutations/set-zoom-ratio.mutation.ts
+++ b/packages/base-docs/src/commands/operations/set-doc-zoom-ratio.operation.ts
@@ -1,29 +1,32 @@
-import { CommandType, IMutation, IUniverInstanceService, Tools } from '@univerjs/core';
+import { CommandType, IOperation, IUniverInstanceService, Tools } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
-export interface ISetZoomRatioMutationParams {
+export interface ISetDocZoomRatioOperationParams {
zoomRatio: number;
documentId: string;
}
-export const SetZoomRatioUndoMutationFactory = (
+export const SetDocZoomRatioUndoMutationFactory = (
accessor: IAccessor,
- params: ISetZoomRatioMutationParams
-): ISetZoomRatioMutationParams => {
+ params: ISetDocZoomRatioOperationParams
+): ISetDocZoomRatioOperationParams => {
const documentModel = accessor.get(IUniverInstanceService).getUniverDocInstance(params.documentId);
- const old = documentModel?.getSettings()?.zoomRatio || 1;
+ const old = documentModel?.zoomRatio || 1;
return {
...Tools.deepClone(params),
zoomRatio: old,
};
};
-export const SetZoomRatioMutation: IMutation<ISetZoomRatioMutationParams> = {
- id: 'doc.mutation.set-zoom-ratio',
- type: CommandType.MUTATION,
- handler: (accessor, params) => {
+export const SetDocZoomRatioOperation: IOperation<ISetDocZoomRatioOperationParams> = {
+ id: 'doc.operation.set-zoom-ratio',
+ type: CommandType.OPERATION,
+ handler: (accessor, params: ISetDocZoomRatioOperationParams) => {
const documentModel = accessor.get(IUniverInstanceService).getUniverDocInstance(params.documentId);
- if (!documentModel) return false;
+ if (!documentModel) {
+ return false;
+ }
+
const documentData = documentModel.getSnapshot();
if (documentData.settings == null) {
documentData.settings = {
diff --git a/packages/base-render/src/Component/Docs/Document.ts b/packages/base-render/src/Component/Docs/Document.ts
index 5d828019da..ff51d8c18f 100644
--- a/packages/base-render/src/Component/Docs/Document.ts
+++ b/packages/base-render/src/Component/Docs/Document.ts
@@ -216,18 +216,18 @@ export class Documents extends DocComponent {
} = renderConfig;
const horizontalOffsetNoAngle = this._horizontalHandler(
- actualWidth,
- pagePaddingLeft,
- pagePaddingRight,
+ actualWidth * scale,
+ pagePaddingLeft * scale,
+ pagePaddingRight * scale,
horizontalAlign
);
const verticalOffsetNoAngle = this._verticalHandler(
- actualHeight,
- pagePaddingTop,
- pagePaddingBottom,
+ actualHeight * scale,
+ pagePaddingTop * scale,
+ pagePaddingBottom * scale,
verticalAlign
);
- const alignOffsetNoAngle = Vector2.create(horizontalOffsetNoAngle, verticalOffsetNoAngle);
+ const alignOffsetNoAngle = Vector2.create(horizontalOffsetNoAngle / scale, verticalOffsetNoAngle / scale);
const centerAngle = degToRad(centerAngleDeg);
diff --git a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
index 7188760281..f075065da4 100644
--- a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
+++ b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
@@ -482,7 +482,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
private _initDOM() {
const container = document.createElement('div');
- container.style.position = 'absolute';
+ container.style.position = 'fixed';
container.style.left = `0px`;
container.style.top = `0px`;
@@ -696,22 +696,36 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return;
}
- const { left, top } = anchor;
+ const { scaleX, scaleY } = this._scene?.getAncestorScale() || { scaleX: 1, scaleY: 1 };
+
+ const { left, top, height, width } = anchor;
+
+ // left *= scaleX;
+
+ // top *= scaleY;
const { tl, tr, bl } = this._activeViewport.getBounding();
- const constantOffset = 100;
+ const constantOffsetWidth = (width * 2) / scaleX;
+ const constantOffsetHeight = (height * 2) / scaleY;
let offsetY = 0;
let offsetX = 0;
- if (top < tl.y + constantOffset) {
- offsetY = top - tl.y - constantOffset;
- } else if (top > bl.y - constantOffset) {
- offsetY = top - bl.y + constantOffset;
+
+ const boundTop = tl.y;
+ const boundBottom = bl.y;
+
+ if (top < boundTop + constantOffsetHeight) {
+ offsetY = top - boundTop - constantOffsetHeight * 2;
+ } else if (top > boundBottom - constantOffsetHeight) {
+ offsetY = top - boundBottom + constantOffsetHeight * 2;
}
- if (left < tl.x + constantOffset) {
- offsetX = left - tl.x - constantOffset;
- } else if (left > tr.x - constantOffset) {
- offsetX = left - tr.x + constantOffset;
+ const boundLeft = tl.x;
+ const boundRight = tr.x;
+
+ if (left < boundLeft + constantOffsetWidth) {
+ offsetX = left - boundLeft - constantOffsetWidth * 2;
+ } else if (left > boundRight - constantOffsetWidth) {
+ offsetX = left - boundRight + constantOffsetWidth * 2;
}
const config = this._activeViewport.getBarScroll(offsetX, offsetY);
@@ -730,9 +744,11 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
const absoluteCoord = this._activeViewport.getAbsoluteVector(Vector2.FromArray([left, top]));
+ const { scaleY } = this._scene?.getAncestorScale() || { scaleX: 1, scaleY: 1 };
+
const { x, y } = absoluteCoord;
- this._cursor.style.height = `${height}px`;
+ this._cursor.style.height = `${height * scaleY}px`;
// console.log('_syncDomToSelection', left, top, absoluteCoord?.x || 0, absoluteCoord?.y || 0);
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index 90bb563571..f9733904a9 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -950,6 +950,24 @@ export class SpreadsheetSkeleton extends Skeleton {
// };
// }
+ // convert canvas content position to physical position in screen
+ convertTransformToOffsetX(offsetX: number, scaleX: number, scrollXY: { x: number; y: number }) {
+ const { x: scrollX } = scrollXY;
+
+ offsetX = (offsetX - scrollX) * scaleX;
+
+ return offsetX;
+ }
+
+ // convert canvas content position to physical position in screen
+ convertTransformToOffsetY(offsetY: number, scaleY: number, scrollXY: { x: number; y: number }) {
+ const { y: scrollY } = scrollXY;
+
+ offsetY = (offsetY - scrollY) * scaleY;
+
+ return offsetY;
+ }
+
getSelectionBounding(startRow: number, startColumn: number, endRow: number, endColumn: number) {
return this.getMergeBounding(startRow, startColumn, endRow, endColumn);
}
@@ -989,24 +1007,6 @@ export class SpreadsheetSkeleton extends Skeleton {
};
}
- // convert canvas content position to physical position in screen
- convertTransformToOffsetX(offsetX: number, scaleX: number, scrollXY: { x: number; y: number }) {
- const { x: scrollX } = scrollXY;
-
- offsetX = (offsetX - scrollX) * scaleX;
-
- return offsetX;
- }
-
- // convert canvas content position to physical position in screen
- convertTransformToOffsetY(offsetY: number, scaleY: number, scrollXY: { x: number; y: number }) {
- const { x: scrollY } = scrollXY;
-
- offsetY = (offsetY - scrollY) * scaleY;
-
- return offsetY;
- }
-
getCellDocumentModel(
row: number,
column: number,
diff --git a/packages/base-render/src/Scene.ts b/packages/base-render/src/Scene.ts
index c2846f666b..de9a3264dc 100644
--- a/packages/base-render/src/Scene.ts
+++ b/packages/base-render/src/Scene.ts
@@ -568,6 +568,11 @@ export class Scene extends ThinScene {
};
}
+ /**
+ * In a nested scene scenario, it is necessary to obtain the relative offsets layer by layer.
+ * @param coord Coordinates to be converted.
+ * @returns
+ */
getRelativeCoord(coord: Vector2) {
let parent: any = this.getParent();
diff --git a/packages/base-render/src/Viewport.ts b/packages/base-render/src/Viewport.ts
index 0b02afba21..1f716fe1ec 100644
--- a/packages/base-render/src/Viewport.ts
+++ b/packages/base-render/src/Viewport.ts
@@ -555,7 +555,7 @@ export class Viewport {
const sceneTrans = this.scene.transform.clone();
const scroll = this.getTransformedScroll();
- const svCoord = sceneTrans.applyPoint(coord).subtract(Vector2.FromArray([scroll.x, scroll.y]));
+ const svCoord = sceneTrans.applyPoint(coord.subtract(Vector2.FromArray([scroll.x, scroll.y])));
return svCoord;
}
// eslint-disable-next-line max-lines-per-function
@@ -927,14 +927,14 @@ export class Viewport {
};
}
- const { scaleX, scaleY } = this._getBoundScale(this.scene.scaleX, this.scene.scaleY);
+ // const { scaleX, scaleY } = this._getBoundScale(this.scene.scaleX, this.scene.scaleY);
const ratioScrollX = this._scrollBar?.ratioScrollX ?? 1;
const ratioScrollY = this._scrollBar?.ratioScrollY ?? 1;
const xFrom: number = this.left;
- const xTo: number = (this.width || 0) * scaleX + this.left;
+ const xTo: number = (this.width || 0) + this.left;
const yFrom: number = this.top;
- const yTo: number = (this.height || 0) * scaleY + this.top;
+ const yTo: number = (this.height || 0) + this.top;
let differenceX = 0;
let differenceY = 0;
diff --git a/packages/base-sheets/src/services/selection/selection-shape.ts b/packages/base-sheets/src/services/selection/selection-shape.ts
index 02cd59baf7..0d9bef2d99 100644
--- a/packages/base-sheets/src/services/selection/selection-shape.ts
+++ b/packages/base-sheets/src/services/selection/selection-shape.ts
@@ -740,7 +740,7 @@ export class SelectionShape {
this._columnHeaderBorder.transformByState({
width: endX - startX,
height: columnHeaderStrokeWidth,
- top: columnHeaderHeight - columnHeaderStrokeWidth + 1,
+ top: columnHeaderHeight - columnHeaderStrokeWidth + 1 / scale,
});
this._columnHeaderGroup.show();
@@ -767,7 +767,7 @@ export class SelectionShape {
this._rowHeaderBorder.transformByState({
width: rowHeaderStrokeWidth,
height: endY - startY,
- left: rowHeaderWidth - rowHeaderStrokeWidth + 1,
+ left: rowHeaderWidth - rowHeaderStrokeWidth + 1 / scale,
});
this._rowHeaderGroup.show();
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index 413fcc93ba..2dc75d90f6 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -50,6 +50,10 @@ export class DocumentModelSimple {
return this.snapshot.body;
}
+ get zoomRatio() {
+ return this.snapshot.settings?.zoomRatio || 1;
+ }
+
dispose() {
this.bodyModel.dispose();
@@ -172,6 +176,16 @@ export class DocumentModelSimple {
objectProperties.positionH.posOffset = config.left;
objectProperties.positionV.posOffset = config.top;
}
+
+ setZoomRatio(zoomRatio: number = 1) {
+ if (this.snapshot.settings == null) {
+ this.snapshot.settings = {
+ zoomRatio,
+ };
+ } else {
+ this.snapshot.settings.zoomRatio = 1;
+ }
+ }
}
const UNIT_ID_LENGTH = 6;
diff --git a/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts b/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
index 6d64be7c33..85ba82d92f 100644
--- a/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor-bridge.controller.ts
@@ -97,13 +97,13 @@ export class EditorBridgeController extends Disposable {
const { scaleX, scaleY } = scene.getAncestorScale();
const scrollXY = scene.getScrollXY(this._selectionRenderService.getViewPort());
- startX = skeleton.convertTransformToOffsetX(startX, scaleX, scrollXY) - scrollXY.x * scaleX;
+ startX = skeleton.convertTransformToOffsetX(startX, scaleX, scrollXY);
- startY = skeleton.convertTransformToOffsetY(startY, scaleY, scrollXY) - scrollXY.y * scaleY;
+ startY = skeleton.convertTransformToOffsetY(startY, scaleY, scrollXY);
- endX = skeleton.convertTransformToOffsetX(endX, scaleX, scrollXY) - scrollXY.x * scaleX;
+ endX = skeleton.convertTransformToOffsetX(endX, scaleX, scrollXY);
- endY = skeleton.convertTransformToOffsetY(endY, scaleY, scrollXY) - scrollXY.y * scaleY;
+ endY = skeleton.convertTransformToOffsetY(endY, scaleY, scrollXY);
let documentLayoutObject = skeleton.getCellDocumentModel(startRow, startColumn, true, true);
@@ -111,6 +111,8 @@ export class EditorBridgeController extends Disposable {
documentLayoutObject = skeleton.getBlankCellDocumentModel(startRow, startColumn, true);
}
+ documentLayoutObject.documentModel?.setZoomRatio(Math.max(scaleX, scaleY));
+
this._commandService.executeCommand(SetActivateCellEditOperation.id, {
position: {
startX,
@@ -118,6 +120,8 @@ export class EditorBridgeController extends Disposable {
endX,
endY,
},
+ scaleX,
+ scaleY,
canvasOffset,
row: startRow,
column: startColumn,
diff --git a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
index 43ee2e36ac..37e1f9e139 100644
--- a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
@@ -116,7 +116,7 @@ export class StartEditController extends Disposable {
return;
}
- const { position, documentLayoutObject } = param;
+ const { position, documentLayoutObject, scaleX, scaleY } = param;
const editorObject = this._getEditorObject();
@@ -137,7 +137,7 @@ export class StartEditController extends Disposable {
// documentModel!.updateDocumentDataMargin(paddingData);
if (wrapStrategy === WrapStrategy.WRAP && angle === 0) {
- documentModel!.updateDocumentDataPageSize(endX - startX);
+ documentModel!.updateDocumentDataPageSize((endX - startX) / scaleX);
}
this._currentUniverService.changeDoc(DOCS_NORMAL_EDITOR_UNIT_ID_KEY, documentModel! as DocumentModel);
@@ -178,14 +178,18 @@ export class StartEditController extends Disposable {
actualRangeWithCoord: IPosition,
canvasOffset: ICanvasOffset,
documentSkeleton: DocumentSkeleton,
- documentLayoutObject: IDocumentLayoutObject
+ documentLayoutObject: IDocumentLayoutObject,
+ scaleX: number = 1,
+ scaleY: number = 1
) {
const { startX, startY, endX, endY } = actualRangeWithCoord;
const { actualWidth, actualHeight } = this._predictingSize(
actualRangeWithCoord,
canvasOffset,
documentSkeleton,
- documentLayoutObject
+ documentLayoutObject,
+ scaleX,
+ scaleY
);
const { verticalAlign, paddingData, fill } = documentLayoutObject;
@@ -210,6 +214,8 @@ export class StartEditController extends Disposable {
offsetTop = editorHeight - actualHeight;
}
+ offsetTop /= scaleY;
+
offsetTop = offsetTop < (paddingData.t || 0) ? paddingData.t || 0 : offsetTop;
documentSkeleton.getModel().updateDocumentDataMargin({
@@ -233,8 +239,11 @@ export class StartEditController extends Disposable {
actualRangeWithCoord: IPosition,
canvasOffset: ICanvasOffset,
documentSkeleton: DocumentSkeleton,
- documentLayoutObject: IDocumentLayoutObject
+ documentLayoutObject: IDocumentLayoutObject,
+ scaleX: number = 1,
+ scaleY: number = 1
) {
+ // startX and startY are the width and height after scaling.
const { startX, endX } = actualRangeWithCoord;
const { textRotation, wrapStrategy } = documentLayoutObject;
@@ -244,7 +253,12 @@ export class StartEditController extends Disposable {
const clientWidth = document.body.clientWidth;
if (wrapStrategy === WrapStrategy.WRAP && angle === 0) {
- return documentSkeleton.getActualSize();
+ const { actualWidth, actualHeight } = documentSkeleton.getActualSize();
+ // The skeleton obtains the original volume, which needs to be multiplied by the magnification factor.
+ return {
+ actualWidth: actualWidth * scaleX,
+ actualHeight: actualHeight * scaleY,
+ };
}
documentSkeleton.getModel().updateDocumentDataPageSize(clientWidth - startX - canvasOffset.left);
documentSkeleton.calculate();
@@ -253,11 +267,12 @@ export class StartEditController extends Disposable {
let editorWidth = endX - startX;
- if (editorWidth < size.actualWidth + EDITOR_INPUT_SELF_EXTEND_GAP) {
- editorWidth = size.actualWidth + EDITOR_INPUT_SELF_EXTEND_GAP;
+ if (editorWidth < size.actualWidth * scaleX + EDITOR_INPUT_SELF_EXTEND_GAP) {
+ editorWidth = size.actualWidth * scaleX + EDITOR_INPUT_SELF_EXTEND_GAP;
}
- documentSkeleton.getModel()!.updateDocumentDataPageSize(editorWidth);
+ // Scaling is handled by the renderer, so the skeleton only accepts the original width and height, which need to be divided by the magnification factor.
+ documentSkeleton.getModel()!.updateDocumentDataPageSize(editorWidth / scaleX);
documentSkeleton.getModel()!.updateDocumentRenderConfig({
horizontalAlign: HorizontalAlign.UNSPECIFIED,
@@ -265,7 +280,7 @@ export class StartEditController extends Disposable {
return {
actualWidth: editorWidth,
- actualHeight: size.actualHeight,
+ actualHeight: size.actualHeight * scaleY,
};
}
@@ -279,7 +294,9 @@ export class StartEditController extends Disposable {
editorHeight: number,
actualRangeWithCoord: IPosition,
canvasOffset: ICanvasOffset,
- fill: Nullable<string>
+ fill: Nullable<string>,
+ scaleX: number = 1,
+ scaleY: number = 1
) {
const editorObject = this._getEditorObject();
@@ -398,7 +415,7 @@ export class StartEditController extends Disposable {
return;
}
- const { position, documentLayoutObject, canvasOffset } = state;
+ const { position, documentLayoutObject, canvasOffset, scaleX, scaleY } = state;
const editorObject = this._getEditorObject();
@@ -420,7 +437,7 @@ export class StartEditController extends Disposable {
const documentModel = skeleton.getModel() as DocumentModel;
- this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject);
+ this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY);
// move selection
if (eventType === DeviceInputEventType.Keyboard) {
@@ -565,9 +582,9 @@ export class StartEditController extends Disposable {
return;
}
- const { position, documentLayoutObject, canvasOffset } = param;
+ const { position, documentLayoutObject, canvasOffset, scaleX, scaleY } = param;
- this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject);
+ this._fitTextSize(position, canvasOffset, skeleton, documentLayoutObject, scaleX, scaleY);
// const editorObject = this._getEditorObject();
diff --git a/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts b/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
index ff5f569e63..5505b4be02 100644
--- a/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
+++ b/packages/ui-plugin-sheets/src/services/editor-bridge.service.ts
@@ -18,6 +18,8 @@ export interface IEditorBridgeServiceParam {
position: IPosition;
canvasOffset: { left: number; top: number };
documentLayoutObject: IDocumentLayoutObject;
+ scaleX: number;
+ scaleY: number;
}
export interface IEditorBridgeService {