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(sheets-ui): undo set cols visible should scroll to cols (#3355)

Wenzhao Hued6d7835ace6
diff --git a/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts b/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts
index ed0ad37982..74b4bde9f5 100644
--- a/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/render-controllers/scroll.render-controller.ts
@@ -24,10 +24,10 @@ import {
Injector,
RANGE_TYPE, toDisposable } from '@univerjs/core';
import { IRenderManagerService, PointerInput, RENDER_CLASS_TYPE, SHEET_VIEWPORT_KEY } from '@univerjs/engine-render';
-import { getSelectionsService, ScrollToCellOperation } from '@univerjs/sheets';
+import { getSelectionsService, ScrollToCellOperation, SetSelectionsOperation } from '@univerjs/sheets';
import type { IFreeze, IRange, IWorksheetData, Nullable, Workbook } from '@univerjs/core';
import type { IRenderContext, IRenderModule, IScrollObserverParam, IWheelEvent } from '@univerjs/engine-render';
-import type { SheetsSelectionsService } from '@univerjs/sheets';
+import type { ISetSelectionsOperationParams, SheetsSelectionsService } from '@univerjs/sheets';
import { ScrollCommand, SetScrollRelativeCommand } from '../../commands/commands/set-scroll.command';
import { ExpandSelectionCommand, MoveSelectionCommand, MoveSelectionEnterAndTabCommand } from '../../commands/commands/set-selection.command';
@@ -91,6 +91,8 @@ export class SheetsScrollRenderController extends Disposable implements IRenderM
} else if (command.id === ExpandSelectionCommand.id) {
const param = command.params as IExpandSelectionCommandParams;
this._scrollToSelectionForExpand(param);
+ } else if (command.id === SetSelectionsOperation.id && (command.params as ISetSelectionsOperationParams).reveal) {
+ this._scrollToSelection();
}
}));
}
@@ -444,14 +446,11 @@ export class SheetsScrollRenderController extends Disposable implements IRenderM
private _scrollToSelection(targetIsActualRowAndColumn = true) {
const selection = this._getSelectionsService().getCurrentLastSelection();
- if (selection == null) {
- return;
- }
+ if (!selection) return;
const { startRow, startColumn, actualRow, actualColumn } = selection.primary;
const selectionStartRow = targetIsActualRowAndColumn ? actualRow : startRow;
const selectionStartColumn = targetIsActualRowAndColumn ? actualColumn : startColumn;
-
this._scrollToCell(selectionStartRow, selectionStartColumn);
}
diff --git a/packages/sheets/src/commands/commands/set-col-visible.command.ts b/packages/sheets/src/commands/commands/set-col-visible.command.ts
index 53723647d5..e8bbe505fb 100644
--- a/packages/sheets/src/commands/commands/set-col-visible.command.ts
+++ b/packages/sheets/src/commands/commands/set-col-visible.command.ts
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, IRange, Nullable, Worksheet } from '@univerjs/core';
import {
CommandType,
ICommandService,
@@ -23,20 +22,21 @@ import {
RANGE_TYPE,
sequenceExecute,
} from '@univerjs/core';
+import type { IAccessor, ICommand, IRange, Nullable, Worksheet } from '@univerjs/core';
-import type { ISetColHiddenMutationParams, ISetColVisibleMutationParams } from '../mutations/set-col-visible.mutation';
+import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
+import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import {
SetColHiddenMutation,
SetColHiddenUndoMutationFactory,
SetColVisibleMutation,
SetColVisibleUndoMutationFactory,
} from '../mutations/set-col-visible.mutation';
-import type { ISetSelectionsOperationParams } from '../operations/selection.operation';
import { SetSelectionsOperation } from '../operations/selection.operation';
-import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
-import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
import { getPrimaryForRange } from './utils/selection-utils';
import { getSheetCommandTarget } from './utils/target-util';
+import type { ISetColHiddenMutationParams, ISetColVisibleMutationParams } from '../mutations/set-col-visible.mutation';
+import type { ISetSelectionsOperationParams } from '../operations/selection.operation';
export interface ISetSpecificColsVisibleCommandParams {
unitId: string;
@@ -66,7 +66,7 @@ export const SetSpecificColsVisibleCommand: ICommand<ISetSpecificColsVisibleComm
const setSelectionOperationParams: ISetSelectionsOperationParams = {
unitId,
subUnitId,
-
+ reveal: true,
selections: ranges.map((r) => ({ range: r, primary: getPrimaryForRange(r, worksheet), style: null })),
};
@@ -180,6 +180,7 @@ export const SetColHiddenCommand: ICommand = {
const undoMutationParams = SetColHiddenUndoMutationFactory(accessor, redoMutationParams);
const undoSetSelectionsOperationParams: ISetSelectionsOperationParams = {
unitId, subUnitId,
+ reveal: true,
selections: ranges.map((range) => ({
range,
primary: getPrimaryForRange(range, worksheet),
diff --git a/packages/sheets/src/commands/commands/set-row-visible.command.ts b/packages/sheets/src/commands/commands/set-row-visible.command.ts
index 7da6cde5e1..1daccf9f43 100644
--- a/packages/sheets/src/commands/commands/set-row-visible.command.ts
+++ b/packages/sheets/src/commands/commands/set-row-visible.command.ts
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, IRange, Nullable, Worksheet } from '@univerjs/core';
import {
CommandType,
ICommandService,
@@ -23,20 +22,21 @@ import {
RANGE_TYPE,
sequenceExecute,
} from '@univerjs/core';
+import type { IAccessor, ICommand, IRange, Nullable, Worksheet } from '@univerjs/core';
import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
-import type { ISetRowHiddenMutationParams, ISetRowVisibleMutationParams } from '../mutations/set-row-visible.mutation';
+import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import {
SetRowHiddenMutation,
SetRowHiddenUndoMutationFactory,
SetRowVisibleMutation,
SetRowVisibleUndoMutationFactory,
} from '../mutations/set-row-visible.mutation';
-import type { ISetSelectionsOperationParams } from '../operations/selection.operation';
import { SetSelectionsOperation } from '../operations/selection.operation';
-import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { getPrimaryForRange } from './utils/selection-utils';
import { getSheetCommandTarget } from './utils/target-util';
+import type { ISetRowHiddenMutationParams, ISetRowVisibleMutationParams } from '../mutations/set-row-visible.mutation';
+import type { ISetSelectionsOperationParams } from '../operations/selection.operation';
export interface ISetSpecificRowsVisibleCommandParams {
unitId: string;
@@ -61,7 +61,7 @@ export const SetSpecificRowsVisibleCommand: ICommand<ISetSpecificRowsVisibleComm
const setSelectionOperationParams: ISetSelectionsOperationParams = {
unitId,
subUnitId,
-
+ reveal: true,
selections: ranges.map((range) => ({
range,
primary: getPrimaryForRange(range, worksheet),
@@ -73,7 +73,6 @@ export const SetSpecificRowsVisibleCommand: ICommand<ISetSpecificRowsVisibleComm
const undoSetSelectionsOperationParams: ISetSelectionsOperationParams = {
unitId,
subUnitId,
-
selections: getSelectionsAfterHiding(ranges).map((range) => ({
range,
primary: getPrimaryForRange(range, worksheet),
@@ -180,7 +179,9 @@ export const SetRowHiddenCommand: ICommand<ISetRowHiddenCommandParams> = {
const undoMutationParams = SetRowHiddenUndoMutationFactory(accessor, redoMutationParams);
const undoSetSelectionsOperationParams: ISetSelectionsOperationParams = {
- unitId, subUnitId,
+ unitId,
+ subUnitId,
+ reveal: true,
selections: ranges.map((range) => ({
range,
primary: getPrimaryForRange(range, worksheet),
diff --git a/packages/sheets/src/commands/operations/selection.operation.ts b/packages/sheets/src/commands/operations/selection.operation.ts
index d6500aaecc..993b4bd735 100644
--- a/packages/sheets/src/commands/operations/selection.operation.ts
+++ b/packages/sheets/src/commands/operations/selection.operation.ts
@@ -26,6 +26,9 @@ export interface ISetSelectionsOperationParams {
subUnitId: string;
selections: ISelectionWithStyle[];
type?: SelectionMoveType;
+
+ /** If should scroll to the selected range. */
+ reveal?: boolean;
}
export const SetSelectionsOperation: IOperation<ISetSelectionsOperationParams> = {
@@ -36,11 +39,10 @@ export const SetSelectionsOperation: IOperation<ISetSelectionsOperationParams> =
const { selections, type, unitId, subUnitId } = params;
const selectionManagerService = getSelectionsService(accessor);
- // must be a new selectionData
- // this._ensureWorkbookSelection(unitIdOrSelections).setSelections would clear selection Data on selecitonManagerInstance.
- // see issues#2199
- selectionManagerService.setSelections(unitId, subUnitId, [...selections], type);
+ // Must update selections array ref.
+ // See https://github.com/dream-num/univer/issues/2199
+ selectionManagerService.setSelections(unitId, subUnitId, [...selections], type);
return true;
},
};