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: should scroll to changed position on set range values

Wenzhao Hubb4f0352f0fd
diff --git a/packages/sheets-ui/src/controllers/active-worksheet/active-worksheet.controller.ts b/packages/sheets-ui/src/controllers/active-worksheet/active-worksheet.controller.ts
index d04887c087..192a67ca72 100644
--- a/packages/sheets-ui/src/controllers/active-worksheet/active-worksheet.controller.ts
+++ b/packages/sheets-ui/src/controllers/active-worksheet/active-worksheet.controller.ts
@@ -14,20 +14,22 @@
* limitations under the License.
*/
-import type { IMutationInfo, Workbook } from '@univerjs/core';
import { Disposable, ICommandService, IUniverInstanceService, LifecycleStages, OnLifecycle } from '@univerjs/core';
-import type {
- IInsertSheetMutationParams,
- IRemoveSheetMutationParams,
- ISetWorksheetActiveOperationParams,
- ISetWorksheetHideMutationParams,
-} from '@univerjs/sheets';
import {
InsertSheetMutation,
RemoveSheetMutation,
+ SetSelectionsOperation,
SetWorksheetActiveOperation,
SetWorksheetHideMutation,
} from '@univerjs/sheets';
+import type { IMutationInfo, IOperationInfo, Workbook } from '@univerjs/core';
+import type {
+ IInsertSheetMutationParams,
+ IRemoveSheetMutationParams,
+ ISetSelectionsOperationParams,
+ ISetWorksheetActiveOperationParams,
+ ISetWorksheetHideMutationParams,
+} from '@univerjs/sheets';
/**
* This controller is responsible for changing the active worksheet when
@@ -44,49 +46,41 @@ export class ActiveWorksheetController extends Disposable {
) {
super();
- this.disposeWithMe(
- this._commandService.beforeCommandExecuted((command) => {
- if (command.id === RemoveSheetMutation.id) {
- return this._beforeAdjustActiveSheetOnRemoveSheet(
- command as IMutationInfo<IRemoveSheetMutationParams>
- );
- }
- })
- );
-
- this.disposeWithMe(
- this._commandService.onCommandExecuted((command, options) => {
- if (command.id === RemoveSheetMutation.id) {
- return this._adjustActiveSheetOnRemoveSheet(command as IMutationInfo<IRemoveSheetMutationParams>);
- }
-
- if (
- command.id === SetWorksheetHideMutation.id &&
- (command.params as ISetWorksheetHideMutationParams).hidden
- ) {
- return this._adjustActiveSheetOnHideSheet(command as IMutationInfo<ISetWorksheetHideMutationParams>);
- }
-
- // It is a mutation that we comes from the collaboration peer so we should not handle them.
- if (options?.fromCollab) {
- return;
- }
-
- // We only handle the local mutations, for
- // * add / copy sheet
- // * set sheet visible
- if (command.id === InsertSheetMutation.id) {
- return this._adjustActiveSheetOnInsertSheet(command as IMutationInfo<IInsertSheetMutationParams>);
- }
-
- if (
- command.id === SetWorksheetHideMutation.id &&
- !(command.params as ISetWorksheetHideMutationParams).hidden
- ) {
- return this._adjustActiveSheetOnShowSheet(command as IMutationInfo<ISetWorksheetHideMutationParams>);
- }
- })
- );
+ this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
+ if (command.id === RemoveSheetMutation.id) {
+ return this._beforeAdjustActiveSheetOnRemoveSheet(
+ command as IMutationInfo<IRemoveSheetMutationParams>
+ );
+ }
+ }));
+
+ this.disposeWithMe(this._commandService.onCommandExecuted((command, options) => {
+ if (command.id === RemoveSheetMutation.id) {
+ return this._adjustActiveSheetOnRemoveSheet(command as IMutationInfo<IRemoveSheetMutationParams>);
+ }
+
+ if (command.id === SetWorksheetHideMutation.id && (command.params as ISetWorksheetHideMutationParams).hidden) {
+ return this._adjustActiveSheetOnHideSheet(command as IMutationInfo<ISetWorksheetHideMutationParams>);
+ }
+
+ // It is a mutation that we comes from the collaboration peer so we should not handle them.
+ if (options?.fromCollab) return false;
+
+ // We only handle the local mutations, for
+ // * add / copy sheet
+ // * set sheet visible
+ if (command.id === InsertSheetMutation.id) {
+ return this._adjustActiveSheetOnInsertSheet(command as IMutationInfo<IInsertSheetMutationParams>);
+ }
+
+ if (command.id === SetWorksheetHideMutation.id && !(command.params as ISetWorksheetHideMutationParams).hidden) {
+ return this._adjustActiveSheetOnShowSheet(command as IMutationInfo<ISetWorksheetHideMutationParams>);
+ }
+
+ if (command.id === SetSelectionsOperation.id) {
+ return this._adjustActiveSheetOnSelection(command as IOperationInfo<ISetSelectionsOperationParams>);
+ }
+ }));
}
private _adjustActiveSheetOnHideSheet(mutation: IMutationInfo<ISetWorksheetHideMutationParams>) {
@@ -162,6 +156,13 @@ export class ActiveWorksheetController extends Disposable {
this._switchToNextSheet(unitId, subUnitId);
}
+ private _adjustActiveSheetOnSelection(operation: IOperationInfo<ISetSelectionsOperationParams>) {
+ const { unitId, subUnitId } = operation.params;
+ if (subUnitId !== this._univerInstanceService.getUnit<Workbook>(unitId)?.getActiveSheet().getSheetId()) {
+ this._switchToNextSheet(unitId, subUnitId);
+ }
+ }
+
private _switchToNextSheet(unitId: string, subUnitId: string): void {
this._commandService.executeCommand(SetWorksheetActiveOperation.id, {
unitId,
diff --git a/packages/sheets/src/commands/commands/set-range-values.command.ts b/packages/sheets/src/commands/commands/set-range-values.command.ts
index 339cc4d52a..7bace61bb3 100644
--- a/packages/sheets/src/commands/commands/set-range-values.command.ts
+++ b/packages/sheets/src/commands/commands/set-range-values.command.ts
@@ -31,6 +31,7 @@ import { WorksheetEditPermission } from '../../services/permission/permission-po
import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
+import { followSelectionOperation } from './utils/selection-utils';
import { getSheetCommandTarget } from './utils/target-util';
import type { ISheetCommandSharedParams } from '../utils/interface';
@@ -51,7 +52,7 @@ export interface ISetRangeValuesCommandParams extends Partial<ISheetCommandShare
export const SetRangeValuesCommand: ICommand = {
id: 'sheet.command.set-range-values',
type: CommandType.COMMAND,
- // eslint-disable-next-line max-lines-per-function
+
handler: (accessor: IAccessor, params: ISetRangeValuesCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
@@ -60,26 +61,15 @@ export const SetRangeValuesCommand: ICommand = {
const sheetInterceptorService = accessor.get(SheetInterceptorService);
const permissionService = accessor.get(IPermissionService);
- // use subUnitId and unitId from params first
- const target = getSheetCommandTarget(univerInstanceService, {
- subUnitId: params.subUnitId,
- unitId: params.unitId,
- });
- if (!target) {
- return false;
- }
- const { subUnitId, unitId } = target;
+ const target = getSheetCommandTarget(univerInstanceService, params);
+ if (!target) return false;
+ const { subUnitId, unitId, workbook, worksheet } = target;
const { value, range } = params;
const currentSelections = range ? [range] : selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!currentSelections || !currentSelections.length) {
- return false;
- }
-
- if (!permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)) {
- return false;
- }
+ if (!currentSelections || !currentSelections.length) return false;
+ if (!permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)) return false;
const cellValue = new ObjectMatrix<ICellData>();
let realCellValue: IObjectMatrixPrimitiveType<ICellData> | undefined;
@@ -108,40 +98,33 @@ export const SetRangeValuesCommand: ICommand = {
realCellValue = value as IObjectMatrixPrimitiveType<ICellData>;
}
- const setRangeValuesMutationParams = {
- subUnitId,
- unitId,
- cellValue: realCellValue ?? cellValue.getMatrix(),
- };
- const undoSetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(accessor, setRangeValuesMutationParams);
-
- // if (
- // !sheetInterceptorService.fetchThroughInterceptors(INTERCEPTOR_POINT.PERMISSION)(null, {
- // id: SetRangeValuesCommand.id,
- // params: setRangeValuesMutationParams,
- // })
- // ) {
- // return false;
- // }
-
- const setValueMutationResult = commandService.syncExecuteCommand(
- SetRangeValuesMutation.id,
- setRangeValuesMutationParams
- );
-
- // may cause performance issues
+ const setRangeValuesMutationParams = { subUnitId, unitId, cellValue: realCellValue ?? cellValue.getMatrix() };
+ const redoParams = SetRangeValuesUndoMutationFactory(accessor, setRangeValuesMutationParams);
+
+ const setValueMutationResult = commandService.syncExecuteCommand(SetRangeValuesMutation.id, setRangeValuesMutationParams);
+ if (!setValueMutationResult) return false;
+
const { undos, redos } = sheetInterceptorService.onCommandExecute({
id: SetRangeValuesCommand.id,
params: { ...setRangeValuesMutationParams, range: currentSelections },
});
const result = sequenceExecute([...redos], commandService);
+ if (result.result) {
+ const selectionOperation = followSelectionOperation(range ?? cellValue.getRange(), workbook, worksheet);
- if (setValueMutationResult && result.result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
- undoMutations: [{ id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams }, ...undos],
- redoMutations: [{ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams }, ...redos],
+ undoMutations: [
+ { id: SetRangeValuesMutation.id, params: redoParams },
+ ...undos,
+ selectionOperation,
+ ],
+ redoMutations: [
+ { id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams },
+ ...redos,
+ Tools.deepClone(selectionOperation),
+ ],
});
return true;
diff --git a/packages/sheets/src/commands/commands/utils/selection-utils.ts b/packages/sheets/src/commands/commands/utils/selection-utils.ts
index acc9dcdade..21f842247e 100644
--- a/packages/sheets/src/commands/commands/utils/selection-utils.ts
+++ b/packages/sheets/src/commands/commands/utils/selection-utils.ts
@@ -14,12 +14,12 @@
* limitations under the License.
*/
-import type { ICellData, IObjectMatrixPrimitiveType, IRange, ISelectionCell, Nullable, Workbook, Worksheet } from '@univerjs/core';
import { RANGE_TYPE, Rectangle, selectionToArray } from '@univerjs/core';
+import type { ICellData, IObjectMatrixPrimitiveType, IRange, ISelectionCell, Nullable, Workbook, Worksheet } from '@univerjs/core';
-import type { ISetSelectionsOperationParams } from '../../operations/selection.operation';
import { SetSelectionsOperation } from '../../operations/selection.operation';
import type { ISelectionWithStyle } from '../../../basics/selection';
+import type { ISetSelectionsOperationParams } from '../../operations/selection.operation';
export interface IExpandParams {
left?: boolean;
@@ -191,7 +191,7 @@ export const followSelectionOperation = (range: IRange, workbook: Workbook, work
params: {
unitId: workbook.getUnitId(),
subUnitId: worksheet.getSheetId(),
-
+ reveal: true,
selections: [{ range, primary: getPrimaryForRange(range, worksheet) }],
} as ISetSelectionsOperationParams,
});