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.

fix(docs-ui): calc range position error & scale by mouse (#4903)

ZhangWeif3c10660dca1
diff --git a/packages/docs-drawing-ui/src/controllers/doc-float-dom.controller.ts b/packages/docs-drawing-ui/src/controllers/doc-float-dom.controller.ts
index 0ddca0cb0f..e8e9d52422 100644
--- a/packages/docs-drawing-ui/src/controllers/doc-float-dom.controller.ts
+++ b/packages/docs-drawing-ui/src/controllers/doc-float-dom.controller.ts
@@ -47,8 +47,8 @@ function calcDocFloatDomPosition(
startY: (top - viewportScrollY) * scaleY,
endX: (left + width - viewportScrollX) * scaleX,
endY: (top + height - viewportScrollY) * scaleY,
- width,
- height,
+ width: width * scaleX,
+ height: height * scaleY,
rotate: angle,
absolute: {
left: false,
diff --git a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
index 3e07a783f9..254e5ce5f8 100644
--- a/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
+++ b/packages/docs-ui/src/controllers/render-controllers/zoom.render-controller.ts
@@ -25,6 +25,7 @@ import {
ICommandService,
IContextService,
Inject,
+ isInternalEditorID,
IUniverInstanceService,
} from '@univerjs/core';
import { DocSelectionManagerService, DocSkeletonManagerService } from '@univerjs/docs';
@@ -54,6 +55,10 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
// TODO: do not use setTimeout.
setTimeout(() => this.updateViewZoom(1, true), 20);
+
+ if (!isInternalEditorID(this._context.unitId)) {
+ this._initZoomEventListener();
+ }
}
private _initRenderRefresher() {
@@ -158,4 +163,35 @@ export class DocZoomRenderController extends Disposable implements IRenderModule
docObject.scene.getTransformer()?.clearSelectedObjects();
}
+
+ private _initZoomEventListener() {
+ const scene = this._context.scene;
+
+ this.disposeWithMe(
+ // hold ctrl & mousewheel ---> zoom
+ scene.onMouseWheel$.subscribeEvent((e: IWheelEvent) => {
+ 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 currentRatio = this._context.unit.zoomRatio;
+ let nextRatio = +Number.parseFloat(`${currentRatio + ratioDelta}`).toFixed(1);
+ nextRatio = nextRatio >= 4 ? 4 : nextRatio <= 0.1 ? 0.1 : nextRatio;
+
+ this._commandService.executeCommand(SetDocZoomRatioCommand.id, {
+ zoomRatio: Math.round(nextRatio * 10) / 10,
+ documentId: this._context.unitId,
+ });
+
+ e.preventDefault();
+ })
+ );
+ }
}
diff --git a/packages/docs-ui/src/services/doc-event-manager.service.ts b/packages/docs-ui/src/services/doc-event-manager.service.ts
index 0326a9006c..06bbd25438 100644
--- a/packages/docs-ui/src/services/doc-event-manager.service.ts
+++ b/packages/docs-ui/src/services/doc-event-manager.service.ts
@@ -79,41 +79,45 @@ const calcDocParagraphPositions = (startIndex: number, endIndex: number, documen
return null;
}
- const documentOffsetConfig = documents.getOffsetConfig();
- const convertor = new NodePositionConvertToCursor(documentOffsetConfig, skeleton);
- const { borderBoxPointGroup } = convertor.getRangePointData(startPosition, endPosition);
- const bounds = getLineBounding(borderBoxPointGroup);
- if (!bounds.length) {
- return null;
- }
- const rects = bounds.map((rect) => ({
- top: rect.top + documentOffsetConfig.docsTop - DOC_VERTICAL_PADDING,
- bottom: rect.bottom + documentOffsetConfig.docsTop + DOC_VERTICAL_PADDING,
- left: rect.left + documentOffsetConfig.docsLeft,
- right: rect.right + documentOffsetConfig.docsLeft,
- }));
+ try {
+ const documentOffsetConfig = documents.getOffsetConfig();
+ const convertor = new NodePositionConvertToCursor(documentOffsetConfig, skeleton);
+ const { borderBoxPointGroup } = convertor.getRangePointData(startPosition, endPosition);
+ const bounds = getLineBounding(borderBoxPointGroup);
+ if (!bounds.length) {
+ return null;
+ }
+ const rects = bounds.map((rect) => ({
+ top: rect.top + documentOffsetConfig.docsTop - DOC_VERTICAL_PADDING,
+ bottom: rect.bottom + documentOffsetConfig.docsTop + DOC_VERTICAL_PADDING,
+ left: rect.left + documentOffsetConfig.docsLeft,
+ right: rect.right + documentOffsetConfig.docsLeft,
+ }));
- let left;
- let right;
- let top;
- let bottom;
+ let left;
+ let right;
+ let top;
+ let bottom;
- for (const rect of rects) {
- if (left === undefined || rect.left < left) {
- left = rect.left;
- }
- if (right === undefined || rect.right > right) {
- right = rect.right;
- }
- if (top === undefined || rect.top < top) {
- top = rect.top;
- }
- if (bottom === undefined || rect.bottom > bottom) {
- bottom = rect.bottom;
+ for (const rect of rects) {
+ if (left === undefined || rect.left < left) {
+ left = rect.left;
+ }
+ if (right === undefined || rect.right > right) {
+ right = rect.right;
+ }
+ if (top === undefined || rect.top < top) {
+ top = rect.top;
+ }
+ if (bottom === undefined || rect.bottom > bottom) {
+ bottom = rect.bottom;
+ }
}
- }
- return { left: left!, right: right!, top: top!, bottom: bottom! };
+ return { left: left!, right: right!, top: top!, bottom: bottom! };
+ } catch (e) {
+ return null;
+ }
};
export const calcDocGlyphPosition = (glyph: IDocumentSkeletonGlyph, documents: Documents, skeleton: DocumentSkeleton, pageIndex = -1): IBoundRectNoAngle | undefined => {
@@ -453,7 +457,7 @@ export class DocEventManagerService extends Disposable implements IRenderModule
pageIndex,
rect: calcDocParagraphPositions(paragraph.paragraphStart, paragraph.paragraphEnd, this._documents, this._skeleton),
} as IParagraphBound;
- });
+ }).filter((layout) => layout.rect !== null);
}
private _buildParagraphBounds() {