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: incorrect scrolling after selection moves in freeze situation (#278)

YuHongb1abadfff33d
diff --git a/packages/base-sheets/src/Controller/Selection.controller.ts b/packages/base-sheets/src/Controller/Selection.controller.ts
index 80260db663..1a7af30a1e 100644
--- a/packages/base-sheets/src/Controller/Selection.controller.ts
+++ b/packages/base-sheets/src/Controller/Selection.controller.ts
@@ -4,7 +4,6 @@ import {
IPointerEvent,
IRenderManagerService,
ISelectionTransformerShapeManager,
- ISelectionWithStyle,
} from '@univerjs/base-render';
import {
Disposable,
@@ -19,7 +18,6 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject, ISheetObjectParam } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
-import { ScrollCommand } from '../commands/commands/set-scroll.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';
@@ -144,86 +142,6 @@ export class SelectionController extends Disposable {
this._selectionTransformerShapeManager.convertSelectionRangeToData(selectionWithStyle);
this._selectionTransformerShapeManager.addControlToCurrentByRangeData(selectionData);
}
- param.length && this._scrollToSelection(param[param.length - 1]);
- });
- }
-
- private _scrollToSelection(selection: ISelectionWithStyle) {
- let startSheetViewRow;
- let startSheetViewColumn;
- const {
- startRow: selectionStartRow,
- startColumn: selectionStartColumn,
- endRow: selectionEndRow,
- endColumn: selectionEndColumn,
- } = selection.range;
- const { rowHeightAccumulation, columnWidthAccumulation } =
- this._sheetSkeletonManagerService.getCurrent()?.skeleton ?? {};
- if (rowHeightAccumulation == null || columnWidthAccumulation == null) {
- return;
- }
- const scene = this._getSheetObject()?.scene;
- if (scene == null) {
- return;
- }
- const viewport = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
- if (viewport == null) {
- return;
- }
- const bounds = viewport.getBounding();
- const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
- if (skeleton == null) {
- return;
- }
- const {
- startRow: viewportStartRow,
- startColumn: viewportStartColumn,
- endRow: viewportEndRow,
- endColumn: viewportEndColumn,
- } = skeleton.getRowColumnSegment(bounds);
- let isOverflow = false;
- // top overflow
- if (selectionStartRow <= viewportStartRow) {
- isOverflow = true;
- startSheetViewRow = selectionStartRow;
- }
- // left overflow
- if (selectionStartColumn <= viewportStartColumn) {
- isOverflow = true;
- startSheetViewColumn = selectionStartColumn;
- }
- // bottom overflow
- if (selectionEndRow >= viewportEndRow) {
- isOverflow = true;
- const minRowAccumulation = rowHeightAccumulation[selectionEndRow] - viewport.height!;
- for (let r = viewportStartRow; r <= selectionEndRow; r++) {
- if (rowHeightAccumulation[r] >= minRowAccumulation) {
- startSheetViewRow = r + 1;
- break;
- }
- }
- }
- // right overflow
- if (selectionEndColumn >= viewportEndColumn) {
- isOverflow = true;
- const minColumnAccumulation = columnWidthAccumulation[selectionEndColumn] - viewport.width!;
- for (let c = viewportStartColumn; c <= selectionEndColumn; c++) {
- if (columnWidthAccumulation[c] >= minColumnAccumulation) {
- startSheetViewColumn = c + 1;
- break;
- }
- }
- }
- if (!isOverflow) {
- return;
- }
- // sheetViewStartRow and sheetViewStartColumn maybe undefined
- const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
- this._commandService.executeCommand(ScrollCommand.id, {
- unitId: workbook.getUnitId(),
- sheetId: workbook.getActiveSheet().getSheetId(),
- sheetViewStartRow: startSheetViewRow,
- sheetViewStartColumn: startSheetViewColumn,
});
}
diff --git a/packages/base-sheets/src/Controller/scroll.controller.ts b/packages/base-sheets/src/Controller/scroll.controller.ts
index 3fcf5a0578..00b1ea5737 100644
--- a/packages/base-sheets/src/Controller/scroll.controller.ts
+++ b/packages/base-sheets/src/Controller/scroll.controller.ts
@@ -1,9 +1,19 @@
import { IRenderManagerService, ISelectionTransformerShapeManager } from '@univerjs/base-render';
-import { Disposable, ICommandService, ICurrentUniverService, LifecycleStages, OnLifecycle } from '@univerjs/core';
+import {
+ Direction,
+ Disposable,
+ ICommandInfo,
+ ICommandService,
+ ICurrentUniverService,
+ LifecycleStages,
+ OnLifecycle,
+} from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { getSheetObject } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
+import { ScrollCommand } from '../commands/commands/set-scroll.command';
+import { IMoveSelectionCommandParams, MoveSelectionCommand } from '../commands/commands/set-selections.command';
import { ScrollManagerService } from '../services/scroll-manager.service';
import { SelectionManagerService } from '../services/selection-manager.service';
import { ISheetSkeletonManagerParam, SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
@@ -32,6 +42,7 @@ export class ScrollController extends Disposable {
this._scrollSubscribeBinding();
this._skeletonListener();
+ this._commandExecutedListener();
}
private _scrollEventBinding() {
@@ -83,6 +94,119 @@ export class ScrollController extends Disposable {
});
}
+ private _commandExecutedListener() {
+ const updateCommandList = [MoveSelectionCommand.id];
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (updateCommandList.includes(command.id)) {
+ const params = command.params as IMoveSelectionCommandParams;
+ this.scrollToVisible(params.direction);
+ }
+ })
+ );
+ }
+
+ private scrollToVisible(direction: Direction) {
+ let startSheetViewRow;
+ let startSheetViewColumn;
+ const selection = this._selectionManagerService.getLast();
+ if (selection == null) {
+ return;
+ }
+ const { startRow: selectionStartRow, startColumn: selectionStartColumn } = selection.range;
+ const { rowHeightAccumulation, columnWidthAccumulation } =
+ this._sheetSkeletonManagerService.getCurrent()?.skeleton ?? {};
+ if (rowHeightAccumulation == null || columnWidthAccumulation == null) {
+ return;
+ }
+ const scene = this._getSheetObject()?.scene;
+ if (scene == null) {
+ return;
+ }
+ const viewport = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ if (viewport == null) {
+ return;
+ }
+
+ const worksheet = this._currentUniverService.getCurrentUniverSheetInstance().getActiveSheet();
+ const {
+ startColumn: freezeStartColumn,
+ startRow: freezeStartRow,
+ ySplit: freezeYSplit,
+ xSplit: freezeXSplit,
+ } = worksheet.getFreeze();
+
+ const bounds = viewport.getBounding();
+ const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
+ if (skeleton == null) {
+ return;
+ }
+ const {
+ startRow: viewportStartRow,
+ startColumn: viewportStartColumn,
+ endRow: viewportEndRow,
+ endColumn: viewportEndColumn,
+ } = skeleton.getRowColumnSegment(bounds);
+
+ // vertical overflow only happens when the selection's row is in not the freeze area
+ if (
+ (direction === Direction.UP || direction === Direction.DOWN) &&
+ selectionStartRow >= freezeStartRow &&
+ selectionStartColumn >= freezeStartColumn - freezeXSplit
+ ) {
+ // top overflow
+ if (selectionStartRow <= viewportStartRow) {
+ startSheetViewRow = selectionStartRow;
+ }
+
+ // bottom overflow
+ if (selectionStartRow >= viewportEndRow) {
+ const minRowAccumulation = rowHeightAccumulation[selectionStartRow] - viewport.height!;
+ for (let r = viewportStartRow; r <= selectionStartRow; r++) {
+ if (rowHeightAccumulation[r] >= minRowAccumulation) {
+ startSheetViewRow = r + 1;
+ break;
+ }
+ }
+ }
+ }
+ // horizontal overflow only happens when the selection's column is in not the freeze area
+ if (
+ (direction === Direction.LEFT || direction === Direction.RIGHT) &&
+ selectionStartColumn >= freezeStartColumn &&
+ selectionStartRow >= freezeStartRow - freezeYSplit
+ ) {
+ // left overflow
+ if (selectionStartColumn <= viewportStartColumn) {
+ startSheetViewColumn = selectionStartColumn;
+ }
+
+ // right overflow
+ if (selectionStartColumn >= viewportEndColumn) {
+ const minColumnAccumulation = columnWidthAccumulation[selectionStartColumn] - viewport.width!;
+ for (let c = viewportStartColumn; c <= selectionStartColumn; c++) {
+ if (columnWidthAccumulation[c] >= minColumnAccumulation) {
+ startSheetViewColumn = c + 1;
+ break;
+ }
+ }
+ }
+ }
+
+ if (startSheetViewRow === undefined && startSheetViewColumn === undefined) {
+ return;
+ }
+ // sheetViewStartRow and sheetViewStartColumn maybe undefined, which means should not make scroll at its direction
+ const workbook = this._currentUniverService.getCurrentUniverSheetInstance();
+ this._commandService.executeCommand(ScrollCommand.id, {
+ unitId: workbook.getUnitId(),
+ sheetId: workbook.getActiveSheet().getSheetId(),
+ sheetViewStartRow: startSheetViewRow,
+ sheetViewStartColumn: startSheetViewColumn,
+ });
+ }
+
private _scrollSubscribeBinding() {
this._scrollManagerService.scrollInfo$.subscribe((param) => {
const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;