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(selection): moving event should be effected by scale

Wenzhao Hu7df79a120ed8
diff --git a/packages/base-render/src/Basics/IEvents.ts b/packages/base-render/src/Basics/IEvents.ts
index 872dd7e60a..6033afc8b1 100644
--- a/packages/base-render/src/Basics/IEvents.ts
+++ b/packages/base-render/src/Basics/IEvents.ts
@@ -86,35 +86,8 @@ export enum DeviceInputEventType {
/**
* Native friendly interface for Event Obj ect
*/
-export interface IEvent {
- // Properties
- /**
- * Current target for an event
- */
- currentTarget?: any;
-
- /**
- * Alias for target
- * @deprecated
- */
- srcElement?: any;
-
- /**
- * Type of event
- */
- type: string;
-
- /**
- * Reference to object where object was dispatched
- */
- target: any;
-
+export interface IEvent extends Event {
// Methods
- /**
- * Tells user agent what to do when not explicitly handled
- */
- preventDefault: () => void;
-
/**
* Device type
*/
@@ -133,26 +106,11 @@ export interface IEvent {
currentState: Nullable<number>;
}
+// TODO@wzhudev: this interface maybe not necessary
/**
* Native friendly interface for UIEvent Object
*/
-export interface IUIEvent extends IEvent {
- // Properties
- /**
- * Provides current click count
- */
- detail: number;
-
- /**
- * Horizontal coordinate of event
- */
- pageX: number;
-
- /**
- * Vertical coordinate of event
- */
- pageY: number;
-}
+export interface IUIEvent extends IEvent, UIEvent {}
/**
* Native friendly interface for KeyboardEvent Object
@@ -204,7 +162,7 @@ export interface IKeyboardEvent extends IUIEvent {
/**
* Native friendly interface for MouseEvent Object
*/
-export interface IMouseEvent extends IUIEvent {
+export interface IMouseEvent extends IUIEvent, MouseEvent {
// Properties
/**
* Status of Alt key being pressed
diff --git a/packages/base-render/src/Component/Sheets/Spreadsheet.ts b/packages/base-render/src/Component/Sheets/Spreadsheet.ts
index 4ef8ad5122..63ad52dc2e 100644
--- a/packages/base-render/src/Component/Sheets/Spreadsheet.ts
+++ b/packages/base-render/src/Component/Sheets/Spreadsheet.ts
@@ -158,12 +158,15 @@ export class Spreadsheet extends SheetComponent {
if (!spreadsheetSkeleton) {
return;
}
- const { x: scrollX, y: scrollY } = scrollXY;
+ const { scaleX = 1, scaleY = 1 } = this.getParentScale();
+
+ // these values are not affected by zooming (ideal positions)
const { rowHeightAccumulation, columnWidthAccumulation, rowTitleWidth, columnTitleHeight, dataMergeCacheAll } = spreadsheetSkeleton;
- offsetX += scrollX - rowTitleWidth;
- offsetY += scrollY - columnTitleHeight;
+ // so we should map physical positions to ideal positions
+ offsetX = (offsetX + scrollX) / scaleX - rowTitleWidth;
+ offsetY = (offsetY + scrollY) / scaleY - columnTitleHeight;
let row = searchArray(rowHeightAccumulation, offsetY);
let column = searchArray(columnWidthAccumulation, offsetX);
@@ -188,7 +191,6 @@ export class Spreadsheet extends SheetComponent {
}
}
- const { scaleX = 1, scaleY = 1 } = this.getParentScale();
let { isMerged, startY, endY, startX, endX, mergeInfo, isMergedMainCell } = getCellByIndex(row, column, rowHeightAccumulation, columnWidthAccumulation, dataMergeCacheAll);
diff --git a/packages/base-render/src/Engine.ts b/packages/base-render/src/Engine.ts
index 42ef6856c2..0a35278972 100644
--- a/packages/base-render/src/Engine.ts
+++ b/packages/base-render/src/Engine.ts
@@ -62,13 +62,14 @@ export class Engine {
private _pointerDownEvent: (evt: any) => void;
- private _pointerUpEvent: (evt: any) => void;
+ private _pointerUpEvent: (evt: Event) => void;
private _pointerBlurEvent: (evt: any) => void;
private _pointerWheelEvent: (evt: any) => void;
- private __pointer: { [deviceSlot: number]: number } = {};
+ /** previous pointer position */
+ private pointer: { [deviceSlot: number]: number } = {};
private __mouseId = -1;
@@ -327,15 +328,15 @@ export class Engine {
this._pointerMoveEvent = (evt: any) => {
const deviceType = this.__getPointerType(evt);
// Store previous values for event
- const previousHorizontal = this.__pointer[PointerInput.Horizontal];
- const previousVertical = this.__pointer[PointerInput.Vertical];
- const previousDeltaHorizontal = this.__pointer[PointerInput.DeltaHorizontal];
- const previousDeltaVertical = this.__pointer[PointerInput.DeltaVertical];
-
- this.__pointer[PointerInput.Horizontal] = evt.clientX;
- this.__pointer[PointerInput.Vertical] = evt.clientY;
- this.__pointer[PointerInput.DeltaHorizontal] = evt.movementX;
- this.__pointer[PointerInput.DeltaVertical] = evt.movementY;
+ const previousHorizontal = this.pointer[PointerInput.Horizontal];
+ const previousVertical = this.pointer[PointerInput.Vertical];
+ const previousDeltaHorizontal = this.pointer[PointerInput.DeltaHorizontal];
+ const previousDeltaVertical = this.pointer[PointerInput.DeltaVertical];
+
+ this.pointer[PointerInput.Horizontal] = evt.clientX;
+ this.pointer[PointerInput.Vertical] = evt.clientY;
+ this.pointer[PointerInput.DeltaHorizontal] = evt.movementX;
+ this.pointer[PointerInput.DeltaVertical] = evt.movementY;
// console.log('pointerMoveEvent_1', previousHorizontal, evt.clientX, previousVertical, evt.clientY, this.__pointer);
let deviceEvent = evt as IPointerEvent;
deviceEvent.deviceType = deviceType;
@@ -343,28 +344,28 @@ export class Engine {
if (previousHorizontal !== evt.clientX) {
deviceEvent.inputIndex = PointerInput.Horizontal;
deviceEvent.previousState = previousHorizontal;
- deviceEvent.currentState = this.__pointer[PointerInput.Horizontal];
+ deviceEvent.currentState = this.pointer[PointerInput.Horizontal];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
if (previousVertical !== evt.clientY) {
deviceEvent.inputIndex = PointerInput.Vertical;
deviceEvent.previousState = previousVertical;
- deviceEvent.currentState = this.__pointer[PointerInput.Vertical];
+ deviceEvent.currentState = this.pointer[PointerInput.Vertical];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
- if (this.__pointer[PointerInput.DeltaHorizontal] !== 0) {
+ if (this.pointer[PointerInput.DeltaHorizontal] !== 0) {
deviceEvent.inputIndex = PointerInput.DeltaHorizontal;
deviceEvent.previousState = previousDeltaHorizontal;
- deviceEvent.currentState = this.__pointer[PointerInput.DeltaHorizontal];
+ deviceEvent.currentState = this.pointer[PointerInput.DeltaHorizontal];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
- if (this.__pointer[PointerInput.DeltaVertical] !== 0) {
+ if (this.pointer[PointerInput.DeltaVertical] !== 0) {
deviceEvent.inputIndex = PointerInput.DeltaVertical;
deviceEvent.previousState = previousDeltaVertical;
- deviceEvent.currentState = this.__pointer[PointerInput.DeltaVertical];
+ deviceEvent.currentState = this.pointer[PointerInput.DeltaVertical];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
@@ -372,18 +373,19 @@ export class Engine {
// Lets Propagate the event for move with same position.
if (!this._usingSafari && evt.button !== -1) {
deviceEvent.inputIndex = evt.button + 2;
- deviceEvent.previousState = this.__pointer[evt.button + 2];
- this.__pointer[evt.button + 2] = this.__pointer[evt.button + 2] ? 0 : 1; // Reverse state of button if evt.button has value
- deviceEvent.currentState = this.__pointer[evt.button + 2];
+ deviceEvent.previousState = this.pointer[evt.button + 2];
+ this.pointer[evt.button + 2] = this.pointer[evt.button + 2] ? 0 : 1; // Reverse state of button if evt.button has value
+ deviceEvent.currentState = this.pointer[evt.button + 2];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
};
- this._pointerDownEvent = (evt: any) => {
+ this._pointerDownEvent = (_evt: Event) => {
+ const evt = _evt as IPointerEvent;
const deviceType = this.__getPointerType(evt);
- const previousHorizontal = this.__pointer[PointerInput.Horizontal];
- const previousVertical = this.__pointer[PointerInput.Vertical];
- const previousButton = this.__pointer[evt.button + 2];
+ const previousHorizontal = this.pointer[PointerInput.Horizontal];
+ const previousVertical = this.pointer[PointerInput.Vertical];
+ const previousButton = this.pointer[evt.button + 2];
// console.log('pointerDownEvent_1', previousHorizontal, evt.clientX, previousVertical, evt.clientY, this.__pointer);
if (deviceType === DeviceType.Mouse) {
@@ -406,9 +408,9 @@ export class Engine {
}
}
- this.__pointer[PointerInput.Horizontal] = evt.clientX;
- this.__pointer[PointerInput.Vertical] = evt.clientY;
- this.__pointer[evt.button + 2] = 1;
+ this.pointer[PointerInput.Horizontal] = evt.clientX;
+ this.pointer[PointerInput.Vertical] = evt.clientY;
+ this.pointer[evt.button + 2] = 1;
let deviceEvent = evt as IPointerEvent;
deviceEvent.deviceType = deviceType;
@@ -416,7 +418,7 @@ export class Engine {
if (previousHorizontal !== evt.clientX) {
deviceEvent.inputIndex = PointerInput.Horizontal;
deviceEvent.previousState = previousHorizontal;
- deviceEvent.currentState = this.__pointer[PointerInput.Horizontal];
+ deviceEvent.currentState = this.pointer[PointerInput.Horizontal];
this.onInputChangedObservable.notifyObservers(deviceEvent);
// console.log('pointerDownEvent_clientX');
@@ -424,7 +426,7 @@ export class Engine {
if (previousVertical !== evt.clientY) {
deviceEvent.inputIndex = PointerInput.Vertical;
deviceEvent.previousState = previousVertical;
- deviceEvent.currentState = this.__pointer[PointerInput.Vertical];
+ deviceEvent.currentState = this.pointer[PointerInput.Vertical];
this.onInputChangedObservable.notifyObservers(deviceEvent);
// console.log('pointerDownEvent_clientY');
@@ -432,20 +434,22 @@ export class Engine {
deviceEvent.inputIndex = evt.button + 2;
deviceEvent.previousState = previousButton;
- deviceEvent.currentState = this.__pointer[evt.button + 2];
+ deviceEvent.currentState = this.pointer[evt.button + 2];
this.onInputChangedObservable.notifyObservers(deviceEvent);
// console.log('pointerDownEvent_2', previousHorizontal, evt.clientX, previousVertical, evt.clientY, this.__pointer);
};
- this._pointerUpEvent = (evt: any) => {
+ this._pointerUpEvent = (_evt: Event) => {
+ const evt = _evt as PointerEvent | MouseEvent;
+
const deviceType = this.__getPointerType(evt);
- const previousHorizontal = this.__pointer[PointerInput.Horizontal];
- const previousVertical = this.__pointer[PointerInput.Vertical];
- const previousButton = this.__pointer[evt.button + 2];
+ const previousHorizontal = this.pointer[PointerInput.Horizontal];
+ const previousVertical = this.pointer[PointerInput.Vertical];
+ const previousButton = this.pointer[evt.button + 2];
- this.__pointer[PointerInput.Horizontal] = evt.clientX;
- this.__pointer[PointerInput.Vertical] = evt.clientY;
- this.__pointer[evt.button + 2] = 0;
+ this.pointer[PointerInput.Horizontal] = evt.clientX;
+ this.pointer[PointerInput.Vertical] = evt.clientY;
+ this.pointer[evt.button + 2] = 0;
let deviceEvent = evt as IPointerEvent;
deviceEvent.deviceType = deviceType;
@@ -453,33 +457,33 @@ export class Engine {
if (previousHorizontal !== evt.clientX) {
deviceEvent.inputIndex = PointerInput.Horizontal;
deviceEvent.previousState = previousHorizontal;
- deviceEvent.currentState = this.__pointer[PointerInput.Horizontal];
+ deviceEvent.currentState = this.pointer[PointerInput.Horizontal];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
if (previousVertical !== evt.clientY) {
deviceEvent.inputIndex = PointerInput.Vertical;
deviceEvent.previousState = previousVertical;
- deviceEvent.currentState = this.__pointer[PointerInput.Vertical];
+ deviceEvent.currentState = this.pointer[PointerInput.Vertical];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
deviceEvent.inputIndex = evt.button + 2;
deviceEvent.previousState = previousButton;
- deviceEvent.currentState = this.__pointer[evt.button + 2];
+ deviceEvent.currentState = this.pointer[evt.button + 2];
if (deviceType === DeviceType.Mouse && this.__mouseId >= 0 && this._canvasEle.hasPointerCapture(this.__mouseId)) {
this._canvasEle.releasePointerCapture(this.__mouseId);
- } else if (evt.pointerId && this._canvasEle.hasPointerCapture(evt.pointerId)) {
- this._canvasEle.releasePointerCapture(evt.pointerId);
+ } else if (deviceEvent.pointerId && this._canvasEle.hasPointerCapture(deviceEvent.pointerId)) {
+ this._canvasEle.releasePointerCapture(deviceEvent.pointerId);
}
this.onInputChangedObservable.notifyObservers(deviceEvent);
// We don't want to unregister the mouse because we may miss input data when a mouse is moving after a click
if (deviceType !== DeviceType.Mouse) {
- this.__pointer = {};
+ this.pointer = {};
}
};
@@ -489,39 +493,39 @@ export class Engine {
this.__mouseId = -1;
}
- this.__pointer = {};
+ this.pointer = {};
};
this._pointerWheelEvent = (evt: any) => {
const deviceType = DeviceType.Mouse;
// Store previous values for event
- let previousWheelScrollX = this.__pointer[PointerInput.MouseWheelX];
- let previousWheelScrollY = this.__pointer[PointerInput.MouseWheelY];
- let previousWheelScrollZ = this.__pointer[PointerInput.MouseWheelZ];
+ let previousWheelScrollX = this.pointer[PointerInput.MouseWheelX];
+ let previousWheelScrollY = this.pointer[PointerInput.MouseWheelY];
+ let previousWheelScrollZ = this.pointer[PointerInput.MouseWheelZ];
- this.__pointer[PointerInput.MouseWheelX] = evt.deltaX || 0;
- this.__pointer[PointerInput.MouseWheelY] = evt.deltaY || evt.wheelDelta || 0;
- this.__pointer[PointerInput.MouseWheelZ] = evt.deltaZ || 0;
+ this.pointer[PointerInput.MouseWheelX] = evt.deltaX || 0;
+ this.pointer[PointerInput.MouseWheelY] = evt.deltaY || evt.wheelDelta || 0;
+ this.pointer[PointerInput.MouseWheelZ] = evt.deltaZ || 0;
let deviceEvent = evt as IPointerEvent;
deviceEvent.deviceType = deviceType;
- if (this.__pointer[PointerInput.MouseWheelX] !== 0) {
+ if (this.pointer[PointerInput.MouseWheelX] !== 0) {
deviceEvent.inputIndex = PointerInput.MouseWheelX;
deviceEvent.previousState = previousWheelScrollX;
- deviceEvent.currentState = this.__pointer[PointerInput.MouseWheelX];
+ deviceEvent.currentState = this.pointer[PointerInput.MouseWheelX];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
- if (this.__pointer[PointerInput.MouseWheelY] !== 0) {
+ if (this.pointer[PointerInput.MouseWheelY] !== 0) {
deviceEvent.inputIndex = PointerInput.MouseWheelY;
deviceEvent.previousState = previousWheelScrollY;
- deviceEvent.currentState = this.__pointer[PointerInput.MouseWheelY];
+ deviceEvent.currentState = this.pointer[PointerInput.MouseWheelY];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
- if (this.__pointer[PointerInput.MouseWheelZ] !== 0) {
+ if (this.pointer[PointerInput.MouseWheelZ] !== 0) {
deviceEvent.inputIndex = PointerInput.MouseWheelZ;
deviceEvent.previousState = previousWheelScrollZ;
- deviceEvent.currentState = this.__pointer[PointerInput.MouseWheelZ];
+ deviceEvent.currentState = this.pointer[PointerInput.MouseWheelZ];
this.onInputChangedObservable.notifyObservers(deviceEvent);
}
};
diff --git a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
index 3e70a353bf..81b4a4157a 100644
--- a/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
+++ b/packages/base-sheets/src/Controller/Selection/SelectionManager.ts
@@ -534,8 +534,9 @@ export class SelectionManager {
const { offsetX: moveOffsetX, offsetY: moveOffsetY, clientX, clientY } = moveEvt;
const { startRow, startColumn, endRow, endColumn } = this._startSelectionRange;
+ // moveOffsetX and moveOffsetY is relative to the top left corner of the canvas
const scrollXY = main.getAncestorScrollXY(this._startOffsetX, this._startOffsetY);
- const moveCellInfo = main.calculateCellIndexByPosition(moveOffsetX, moveOffsetY, scrollXY);
+ const moveCellInfo = main.calculateCellIndexByPosition(moveOffsetX, moveOffsetY, scrollXY); // scrollXY is canvas content scrolling
const moveActualSelection = makeCellToSelection(moveCellInfo);
if (!moveActualSelection) {