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(sheet): auto height in row context menu (#323)

Ran Luob63701c8403b
diff --git a/packages/base-render/src/Basics/Const.ts b/packages/base-render/src/Basics/Const.ts
index 242d032ee7..725df510c1 100644
--- a/packages/base-render/src/Basics/Const.ts
+++ b/packages/base-render/src/Basics/Const.ts
@@ -296,3 +296,5 @@ export const DOCS_NORMAL_EDITOR_UNIT_ID_KEY = '__defaultDocumentNormalEditorSpec
export const DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY = '__defaultDocumentFormulaBarEditorSpecialUnitId_20231012__';
export const DEFAULT_SELECTION_LAYER_INDEX = 1000;
+
+export const MAXIMUM_ROW_HEIGHT = 2000;
diff --git a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
index 7276ebe90b..4e6b88e787 100644
--- a/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
+++ b/packages/base-render/src/Component/Sheets/SheetSkeleton.ts
@@ -34,7 +34,7 @@ import {
WrapStrategy,
} from '@univerjs/core';
-import { BORDER_TYPE, COLOR_BLACK_RGB, ORIENTATION_TYPE } from '../../Basics/Const';
+import { BORDER_TYPE, COLOR_BLACK_RGB, MAXIMUM_ROW_HEIGHT, ORIENTATION_TYPE } from '../../Basics/Const';
import { getRotateOffsetAndFarthestHypotenuse, getRotateOrientation } from '../../Basics/Draw';
import { IDocumentSkeletonColumn } from '../../Basics/IDocumentSkeletonCached';
import {
@@ -369,7 +369,8 @@ export class SpreadsheetSkeleton extends Skeleton {
}
const results: IRowAutoHeightInfo[] = [];
- const { mergeData } = this._config;
+ const { mergeData, rowData } = this._config;
+ const rowObjectArray = Tools.createObjectArray(rowData);
for (const range of ranges) {
const { startRow, endRow, startColumn, endColumn } = range;
@@ -380,6 +381,11 @@ export class SpreadsheetSkeleton extends Skeleton {
continue;
}
+ // The row sets isAutoHeight to false, and there is no need to calculate the automatic row height for the row.
+ if (rowObjectArray.get(rowIndex)?.isAutoHeight === false) {
+ continue;
+ }
+
const hasUnMergedCell = hasUnMergedCellInRow(rowIndex, startColumn, endColumn, mergeData);
if (hasUnMergedCell) {
@@ -439,7 +445,7 @@ export class SpreadsheetSkeleton extends Skeleton {
height = Math.max(height, h);
}
- return height;
+ return Math.min(height, MAXIMUM_ROW_HEIGHT);
}
updateLayout() {
diff --git a/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts b/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts
index 1120a7a607..54ddbb9633 100644
--- a/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts
+++ b/packages/base-sheets/src/Basics/Const/COMMAND_LISTENER_CONST.ts
@@ -12,10 +12,12 @@ import { SetWorksheetColWidthMutation } from '../../commands/mutations/set-works
import {
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightMutation,
} from '../../commands/mutations/set-worksheet-row-height.mutation';
export const COMMAND_LISTENER_SKELETON_CHANGE = [
SetWorksheetRowHeightMutation.id,
+ SetWorksheetRowIsAutoHeightMutation.id,
SetWorksheetRowAutoHeightMutation.id,
SetWorksheetColWidthMutation.id,
SetWorksheetActivateMutation.id,
diff --git a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
index 2da11b0509..7c29713329 100644
--- a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
+++ b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
@@ -68,8 +68,8 @@ import { SetWorksheetNameCommand } from '../commands/commands/set-worksheet-name
import { SetWorksheetOrderCommand } from '../commands/commands/set-worksheet-order.command';
import {
DeltaRowHeightCommand,
- // SetRowAutoHeightCommand,
SetRowHeightCommand,
+ SetWorksheetRowIsAutoHeightCommand,
} from '../commands/commands/set-worksheet-row-height.command';
import { SetWorksheetShowCommand } from '../commands/commands/set-worksheet-show.command';
import { ChangeZoomRatioCommand, SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
@@ -99,6 +99,7 @@ import { SetWorksheetOrderMutation } from '../commands/mutations/set-worksheet-o
import {
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightMutation,
} from '../commands/mutations/set-worksheet-row-height.mutation';
export interface IStyleTypeValue<T> {
@@ -156,9 +157,10 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetColWidthCommand,
SetWorksheetColWidthMutation,
- // SetRowAutoHeightCommand,
+ SetWorksheetRowIsAutoHeightCommand,
SetRowHeightCommand,
SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightMutation,
SetWorksheetRowAutoHeightMutation,
SetRowHiddenCommand,
SetRowHiddenMutation,
diff --git a/packages/base-sheets/src/Controller/auto-height.controller.ts b/packages/base-sheets/src/Controller/auto-height.controller.ts
index 5cec14a92c..4a1e92799c 100644
--- a/packages/base-sheets/src/Controller/auto-height.controller.ts
+++ b/packages/base-sheets/src/Controller/auto-height.controller.ts
@@ -3,9 +3,11 @@ import { Inject, Injector } from '@wendellhu/redi';
import { ISetStyleParams, SetStyleCommand } from '../commands/commands/set-style.command';
import { DeltaColumnWidthCommand, SetColWidthCommand } from '../commands/commands/set-worksheet-col-width.command';
+import { SetWorksheetRowIsAutoHeightCommand } from '../commands/commands/set-worksheet-row-height.command';
import { ISetWorksheetColWidthMutationParams } from '../commands/mutations/set-worksheet-col-width.mutation';
import {
ISetWorksheetRowAutoHeightMutationParams,
+ ISetWorksheetRowIsAutoHeightMutationParams,
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowAutoHeightMutationFactory,
} from '../commands/mutations/set-worksheet-row-height.mutation';
@@ -68,6 +70,19 @@ export class AutoHeightController {
private _initialize() {
const { _sheetInterceptorService: sheetInterceptorService, _selectionManagerService: selectionManagerService } =
this;
+ // for intercept 'sheet.command.set-row-is-auto-height' command.
+ sheetInterceptorService.interceptCommand({
+ getMutations: (command: { id: string; params: ISetWorksheetRowIsAutoHeightMutationParams }) => {
+ if (command.id !== SetWorksheetRowIsAutoHeightCommand.id) {
+ return {
+ redos: [],
+ undos: [],
+ };
+ }
+
+ return this._getUndoRedoParamsOfAutoHeight(command.params.ranges);
+ },
+ });
// for intercept set-worksheet-col-width command.
sheetInterceptorService.interceptCommand({
diff --git a/packages/base-sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts b/packages/base-sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
index eec54e1289..1f06e52584 100644
--- a/packages/base-sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
+++ b/packages/base-sheets/src/commands/commands/__tests__/set-row-height.command.spec.ts
@@ -1,18 +1,30 @@
/* eslint-disable no-magic-numbers */
-import { ICommandService, IUniverInstanceService, RANGE_TYPE, RedoCommand, UndoCommand, Univer } from '@univerjs/core';
+import {
+ ICommandService,
+ IUniverInstanceService,
+ Nullable,
+ RANGE_TYPE,
+ RedoCommand,
+ UndoCommand,
+ Univer,
+} from '@univerjs/core';
import { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import { SetWorksheetRowHeightMutation } from '../../..';
import {
NORMAL_SELECTION_PLUGIN_NAME,
SelectionManagerService,
} from '../../../services/selection/selection-manager.service';
+import {
+ SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightMutation,
+} from '../../mutations/set-worksheet-row-height.mutation';
import {
DeltaRowHeightCommand,
IDeltaRowHeightCommand,
ISetRowHeightCommandParams,
SetRowHeightCommand,
+ SetWorksheetRowIsAutoHeightCommand,
} from '../set-worksheet-row-height.command';
import { createCommandTestBed } from './create-command-test-bed';
@@ -28,6 +40,14 @@ describe('Test set row height commands', () => {
return worksheet.getRowHeight(row);
}
+ function getRowIsAutoHeight(row: number): Nullable<boolean> {
+ const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();
+ const rowManager = worksheet.getRowManager();
+ const rowInfo = rowManager.getRow(row);
+
+ return rowInfo?.isAutoHeight;
+ }
+
beforeEach(() => {
const testBed = createCommandTestBed();
univer = testBed.univer;
@@ -36,16 +56,20 @@ describe('Test set row height commands', () => {
commandService = get(ICommandService);
commandService.registerCommand(DeltaRowHeightCommand);
commandService.registerCommand(SetRowHeightCommand);
+ commandService.registerCommand(SetWorksheetRowIsAutoHeightCommand);
commandService.registerCommand(SetWorksheetRowHeightMutation);
+ commandService.registerCommand(SetWorksheetRowIsAutoHeightMutation);
const worksheet = get(IUniverInstanceService).getCurrentUniverSheetInstance().getActiveSheet();
const maxColumn = worksheet.getMaxColumns() - 1;
const selectionManager = get(SelectionManagerService);
+
selectionManager.setCurrentSelection({
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
unitId: 'test',
sheetId: 'sheet1',
});
+
selectionManager.add([
{
range: { startRow: 1, startColumn: 0, endRow: 2, endColumn: maxColumn, rangeType: RANGE_TYPE.ROW },
@@ -62,6 +86,7 @@ describe('Test set row height commands', () => {
style: null,
},
]);
+
selectionManager.add([
{
range: { startRow: 5, startColumn: 0, endColumn: maxColumn, endRow: 5, rangeType: RANGE_TYPE.ROW },
@@ -82,8 +107,11 @@ describe('Test set row height commands', () => {
anchorRow: 5,
});
expect(getRowHeight(1)).toBe(14);
+ expect(getRowIsAutoHeight(1)).toBe(false);
expect(getRowHeight(2)).toBe(14);
+ expect(getRowIsAutoHeight(2)).toBe(false);
expect(getRowHeight(5)).toBe(14);
+ expect(getRowIsAutoHeight(5)).toBe(false);
await commandService.executeCommand(UndoCommand.id);
expect(getRowHeight(1)).toBe(19);
@@ -118,8 +146,11 @@ describe('Test set row height commands', () => {
value: 77,
});
expect(getRowHeight(1)).toBe(77);
+ expect(getRowIsAutoHeight(1)).toBe(false);
expect(getRowHeight(2)).toBe(77);
+ expect(getRowIsAutoHeight(2)).toBe(false);
expect(getRowHeight(5)).toBe(77);
+ expect(getRowIsAutoHeight(5)).toBe(false);
await commandService.executeCommand(UndoCommand.id);
expect(getRowHeight(1)).toBe(19);
@@ -131,4 +162,23 @@ describe('Test set row height commands', () => {
expect(getRowHeight(2)).toBe(77);
expect(getRowHeight(5)).toBe(77);
});
+
+ describe('Set fit content in ranges', () => {
+ it('Should change all rows isAutoHeight to true in selections', async () => {
+ await commandService.executeCommand(SetWorksheetRowIsAutoHeightCommand.id);
+ expect(getRowIsAutoHeight(1)).toBe(true);
+ expect(getRowIsAutoHeight(2)).toBe(true);
+ expect(getRowIsAutoHeight(5)).toBe(true);
+
+ await commandService.executeCommand(UndoCommand.id);
+ expect(getRowIsAutoHeight(1)).toBe(undefined);
+ expect(getRowIsAutoHeight(2)).toBe(undefined);
+ expect(getRowIsAutoHeight(5)).toBe(undefined);
+
+ await commandService.executeCommand(RedoCommand.id);
+ expect(getRowIsAutoHeight(1)).toBe(true);
+ expect(getRowIsAutoHeight(2)).toBe(true);
+ expect(getRowIsAutoHeight(5)).toBe(true);
+ });
+ });
});
diff --git a/packages/base-sheets/src/commands/commands/set-worksheet-row-height.command.ts b/packages/base-sheets/src/commands/commands/set-worksheet-row-height.command.ts
index cd44fb9d31..9b26732ed0 100644
--- a/packages/base-sheets/src/commands/commands/set-worksheet-row-height.command.ts
+++ b/packages/base-sheets/src/commands/commands/set-worksheet-row-height.command.ts
@@ -6,14 +6,19 @@ import {
IUniverInstanceService,
RANGE_TYPE,
Rectangle,
+ sequenceExecute,
+ SheetInterceptorService,
} from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
import { SelectionManagerService } from '../../services/selection/selection-manager.service';
import {
ISetWorksheetRowHeightMutationParams,
+ ISetWorksheetRowIsAutoHeightMutationParams,
SetWorksheetRowHeightMutation,
SetWorksheetRowHeightMutationFactory,
+ SetWorksheetRowIsAutoHeightMutation,
+ SetWorksheetRowIsAutoHeightMutationFactory,
} from '../mutations/set-worksheet-row-height.mutation';
export interface IDeltaRowHeightCommand {
@@ -42,13 +47,10 @@ export const DeltaRowHeightCommand: ICommand = {
const destRowHeight = anchorRowHeight + deltaY;
const rowSelections = selections.filter((s) => s.range.rangeType === RANGE_TYPE.ROW);
- const rangeType = rowSelections.some((s) => {
- const r = s.range;
- if (r.startRow <= anchorRow && anchorRow <= r.endRow) {
- return true;
- }
+ const rangeType = rowSelections.some(({ range }) => {
+ const { startRow, endRow } = range;
- return false;
+ return startRow <= anchorRow && anchorRow <= endRow;
})
? RANGE_TYPE.ROW
: RANGE_TYPE.NORMAL;
@@ -81,63 +83,76 @@ export const DeltaRowHeightCommand: ICommand = {
redoMutationParams
);
+ const redoSetIsAutoHeightParams: ISetWorksheetRowIsAutoHeightMutationParams = {
+ workbookId,
+ worksheetId,
+ ranges: redoMutationParams.ranges,
+ autoHeightInfo: false,
+ };
+
+ const undoSetIsAutoHeightParams: ISetWorksheetRowIsAutoHeightMutationParams =
+ SetWorksheetRowIsAutoHeightMutationFactory(accessor, redoSetIsAutoHeightParams);
+
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
- const result = commandService.executeCommand(SetWorksheetRowHeightMutation.id, redoMutationParams);
- if (result) {
- undoRedoService.pushUndoRedo({
- URI: workbookId,
- undo() {
- return commandService.executeCommand(SetWorksheetRowHeightMutation.id, undoMutationParams);
+
+ const result = await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: redoMutationParams,
},
- redo() {
- return commandService.executeCommand(SetWorksheetRowHeightMutation.id, redoMutationParams);
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: redoSetIsAutoHeightParams,
},
- });
+ ],
+ commandService
+ );
+ if (result.result) {
+ undoRedoService.pushUndoRedo({
+ URI: workbookId,
+ undo: async () =>
+ (
+ await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: undoMutationParams,
+ },
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: undoSetIsAutoHeightParams,
+ },
+ ],
+ commandService
+ )
+ ).result,
+ redo: async () =>
+ (
+ await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: redoMutationParams,
+ },
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: redoSetIsAutoHeightParams,
+ },
+ ],
+ commandService
+ )
+ ).result,
+ });
return true;
}
- return true;
+ return false;
},
};
-export interface ISetRowAutoHeightCommandParams {}
-
-// export const SetRowAutoHeightCommand: ICommand = {
-// type: CommandType.COMMAND,
-// id: 'sheet.command.set-row-auto-height',
-// handler: async (accessor: IAccessor) => {
-// const commandService = accessor.get(ICommandService);
-// const undoRedoService = accessor.get(IUndoRedoService);
-// const univerInstanceService = accessor.get(IUniverInstanceService);
-// const workbook = univerInstanceService.getCurrentUniverSheetInstance();
-// const workbookId = workbook.getUnitId();
-// const { redoMutationParams, undoMutationParams } = getAutoHeightUndoRedoParams(accessor);
-
-// if (redoMutationParams == null || undoMutationParams == null) {
-// return false;
-// }
-
-// const result = commandService.executeCommand(SetWorksheetRowAutoHeightMutation.id, redoMutationParams);
-
-// if (result) {
-// undoRedoService.pushUndoRedo({
-// URI: workbookId,
-// undo() {
-// return commandService.executeCommand(SetWorksheetRowAutoHeightMutation.id, undoMutationParams);
-// },
-// redo() {
-// return commandService.executeCommand(SetWorksheetRowAutoHeightMutation.id, redoMutationParams);
-// },
-// });
-
-// return true;
-// }
-// return true;
-// },
-// };
-
export interface ISetRowHeightCommandParams {
value: number;
}
@@ -171,20 +186,136 @@ export const SetRowHeightCommand: ICommand = {
redoMutationParams
);
- const result = commandService.executeCommand(SetWorksheetRowHeightMutation.id, redoMutationParams);
- if (result) {
- undoRedoService.pushUndoRedo({
- URI: workbookId,
- undo() {
- return commandService.executeCommand(SetWorksheetRowHeightMutation.id, undoMutationParams);
+ const redoSetIsAutoHeightParams: ISetWorksheetRowIsAutoHeightMutationParams = {
+ workbookId,
+ worksheetId,
+ ranges: redoMutationParams.ranges,
+ autoHeightInfo: false,
+ };
+
+ const undoSetIsAutoHeightParams: ISetWorksheetRowIsAutoHeightMutationParams =
+ SetWorksheetRowIsAutoHeightMutationFactory(accessor, redoSetIsAutoHeightParams);
+
+ const result = await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: redoMutationParams,
},
- redo() {
- return commandService.executeCommand(SetWorksheetRowHeightMutation.id, redoMutationParams);
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: redoSetIsAutoHeightParams,
},
+ ],
+ commandService
+ );
+
+ if (result.result) {
+ undoRedoService.pushUndoRedo({
+ URI: workbookId,
+ undo: async () =>
+ (
+ await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: undoMutationParams,
+ },
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: undoSetIsAutoHeightParams,
+ },
+ ],
+ commandService
+ )
+ ).result,
+ redo: async () =>
+ (
+ await sequenceExecute(
+ [
+ {
+ id: SetWorksheetRowHeightMutation.id,
+ params: redoMutationParams,
+ },
+ {
+ id: SetWorksheetRowIsAutoHeightMutation.id,
+ params: redoSetIsAutoHeightParams,
+ },
+ ],
+ commandService
+ )
+ ).result,
+ });
+ return true;
+ }
+
+ return false;
+ },
+};
+
+export interface ISetRowAutoHeightCommandParams {}
+
+export const SetWorksheetRowIsAutoHeightCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-row-is-auto-height',
+ handler: async (accessor: IAccessor) => {
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const selectionManagerService = accessor.get(SelectionManagerService);
+
+ const ranges = selectionManagerService.getSelectionRanges();
+ if (!ranges?.length) {
+ return false;
+ }
+
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const workbookId = univerInstanceService.getCurrentUniverSheetInstance().getUnitId();
+ const worksheetId = univerInstanceService.getCurrentUniverSheetInstance().getActiveSheet().getSheetId();
+
+ const redoMutationParams: ISetWorksheetRowIsAutoHeightMutationParams = {
+ workbookId,
+ worksheetId,
+ ranges,
+ autoHeightInfo: true, // Hard code first, maybe it will change by the menu item in the future.
+ };
+
+ const undoMutationParams: ISetWorksheetRowIsAutoHeightMutationParams =
+ SetWorksheetRowIsAutoHeightMutationFactory(accessor, redoMutationParams);
+
+ const setIsAutoHeightResult = commandService.executeCommand(
+ SetWorksheetRowIsAutoHeightMutation.id,
+ redoMutationParams
+ );
+
+ const { undos, redos } = accessor.get(SheetInterceptorService).onCommandExecute({
+ id: SetWorksheetRowIsAutoHeightCommand.id,
+ params: redoMutationParams,
+ });
+
+ const result = await sequenceExecute([...redos], commandService);
+
+ if (setIsAutoHeightResult && result.result) {
+ undoRedoService.pushUndoRedo({
+ URI: workbookId,
+ undo: async () =>
+ (
+ await sequenceExecute(
+ [{ id: SetWorksheetRowIsAutoHeightMutation.id, params: undoMutationParams }, ...undos],
+ commandService
+ )
+ ).result,
+ redo: async () =>
+ (
+ await sequenceExecute(
+ [{ id: SetWorksheetRowIsAutoHeightMutation.id, params: redoMutationParams }, ...redos],
+ commandService
+ )
+ ).result,
});
return true;
}
- return true;
+
+ return false;
},
};
diff --git a/packages/base-sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts b/packages/base-sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
index e0fe99906e..7c0a8b6444 100644
--- a/packages/base-sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
+++ b/packages/base-sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
@@ -1,7 +1,9 @@
import { IRowAutoHeightInfo } from '@univerjs/base-render';
-import { CommandType, IMutation, IRange, IUniverInstanceService, ObjectArray } from '@univerjs/core';
+import { CommandType, IMutation, IRange, IUniverInstanceService, Nullable, ObjectArray } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
+const MAXIMUM_ROW_HEIGHT = 2000;
+
export interface ISetWorksheetRowHeightMutationParams {
workbookId: string;
worksheetId: string;
@@ -9,6 +11,13 @@ export interface ISetWorksheetRowHeightMutationParams {
rowHeight: number | ObjectArray<number>;
}
+export interface ISetWorksheetRowIsAutoHeightMutationParams {
+ workbookId: string;
+ worksheetId: string;
+ ranges: IRange[];
+ autoHeightInfo: boolean | ObjectArray<Nullable<boolean>>;
+}
+
export interface ISetWorksheetRowAutoHeightMutationParams {
workbookId: string;
worksheetId: string;
@@ -48,23 +57,51 @@ export const SetWorksheetRowHeightMutationFactory = (
};
};
+export const SetWorksheetRowIsAutoHeightMutationFactory = (
+ accessor: IAccessor,
+ params: ISetWorksheetRowIsAutoHeightMutationParams
+): ISetWorksheetRowIsAutoHeightMutationParams => {
+ const { workbookId, worksheetId, ranges } = params;
+
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const workbook = univerInstanceService.getUniverSheetInstance(workbookId);
+ const worksheet = workbook!.getSheetBySheetId(worksheetId);
+
+ const autoHeightInfo = new ObjectArray<Nullable<boolean>>();
+ const manager = worksheet!.getRowManager();
+
+ for (const { startRow, endRow } of ranges) {
+ for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
+ const row = manager.getRowOrCreate(rowIndex);
+
+ autoHeightInfo.set(rowIndex, row.isAutoHeight);
+ }
+ }
+
+ return {
+ workbookId,
+ worksheetId,
+ ranges,
+ autoHeightInfo,
+ };
+};
+
export const SetWorksheetRowAutoHeightMutationFactory = (
accessor: IAccessor,
params: ISetWorksheetRowAutoHeightMutationParams
): ISetWorksheetRowAutoHeightMutationParams => {
+ const { workbookId, worksheetId, rowsAutoHeightInfo } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
- const workbook = univerInstanceService.getUniverSheetInstance(params.workbookId);
- const worksheet = workbook?.getSheetBySheetId(params.worksheetId);
- if (worksheet == null) {
- throw new Error('workbook or worksheet is null error!');
- }
- const { rowsAutoHeightInfo } = params;
+ const workbook = univerInstanceService.getUniverSheetInstance(workbookId);
+ const worksheet = workbook?.getSheetBySheetId(worksheetId);
+
const results: IRowAutoHeightInfo[] = [];
- const manager = worksheet.getRowManager();
+ const manager = worksheet!.getRowManager();
for (const rowInfo of rowsAutoHeightInfo) {
const { row } = rowInfo;
const { ah } = manager.getRowOrCreate(row);
+
results.push({
row,
autoHeight: ah,
@@ -72,8 +109,8 @@ export const SetWorksheetRowAutoHeightMutationFactory = (
}
return {
- workbookId: params.workbookId,
- worksheetId: params.worksheetId,
+ workbookId,
+ worksheetId,
rowsAutoHeightInfo: results,
};
};
@@ -82,29 +119,60 @@ export const SetWorksheetRowHeightMutation: IMutation<ISetWorksheetRowHeightMuta
id: 'sheet.mutation.set-worksheet-row-height',
type: CommandType.MUTATION,
handler: async (accessor, params) => {
+ const { workbookId, worksheetId, ranges, rowHeight } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
- const universheet = univerInstanceService.getUniverSheetInstance(params.workbookId);
+ const workbook = univerInstanceService.getUniverSheetInstance(workbookId);
- if (universheet == null) {
- throw new Error('universheet is null error!');
+ const worksheet = workbook?.getSheetBySheetId(worksheetId);
+ if (!worksheet) {
+ return false;
}
- const worksheet = universheet.getSheetBySheetId(params.worksheetId);
- if (!worksheet) return false;
-
const defaultRowHeight = worksheet.getConfig().defaultRowHeight;
const manager = worksheet.getRowManager();
- const ranges = params.ranges;
- for (let i = 0; i < ranges.length; i++) {
- const range = ranges[i];
- for (let j = range.startRow; j < range.endRow + 1; j++) {
- const row = manager.getRowOrCreate(j);
+ for (const { startRow, endRow } of ranges) {
+ for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
+ const row = manager.getRowOrCreate(rowIndex);
+
+ if (typeof rowHeight === 'number') {
+ row.h = rowHeight;
+ } else {
+ row.h = rowHeight.get(rowIndex - startRow) ?? defaultRowHeight;
+ }
+
+ row.h = Math.min(MAXIMUM_ROW_HEIGHT, row.h);
+ }
+ }
+
+ return true;
+ },
+};
+
+export const SetWorksheetRowIsAutoHeightMutation: IMutation<ISetWorksheetRowIsAutoHeightMutationParams> = {
+ id: 'sheet.mutation.set-worksheet-row-is-auto-height',
+ type: CommandType.MUTATION,
+ handler: async (accessor, params) => {
+ const { workbookId, worksheetId, ranges, autoHeightInfo } = params;
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const workbook = univerInstanceService.getUniverSheetInstance(workbookId);
+
+ const worksheet = workbook?.getSheetBySheetId(worksheetId);
+ if (!worksheet) {
+ return false;
+ }
+
+ const defaultRowIsAutoHeight = undefined;
+ const manager = worksheet.getRowManager();
+
+ for (const { startRow, endRow } of ranges) {
+ for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
+ const row = manager.getRowOrCreate(rowIndex);
- if (typeof params.rowHeight === 'number') {
- row.h = params.rowHeight;
+ if (typeof autoHeightInfo === 'boolean') {
+ row.isAutoHeight = autoHeightInfo;
} else {
- row.h = params.rowHeight.get(j - range.startRow) ?? defaultRowHeight;
+ row.isAutoHeight = autoHeightInfo.get(rowIndex - startRow) ?? defaultRowIsAutoHeight;
}
}
}
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 631a75a8c5..b6c612cd79 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -107,7 +107,10 @@ export { SetColWidthCommand as SetWorksheetColWidthCommand } from './commands/co
export { SetWorksheetHideCommand } from './commands/commands/set-worksheet-hide.command';
export { SetWorksheetNameCommand } from './commands/commands/set-worksheet-name.command';
export { SetWorksheetOrderCommand } from './commands/commands/set-worksheet-order.command';
-export { SetRowHeightCommand as SetWorksheetRowHeightCommand } from './commands/commands/set-worksheet-row-height.command';
+export {
+ SetRowHeightCommand as SetWorksheetRowHeightCommand,
+ SetWorksheetRowIsAutoHeightCommand,
+} from './commands/commands/set-worksheet-row-height.command';
export { SetWorksheetShowCommand } from './commands/commands/set-worksheet-show.command';
export { ChangeZoomRatioCommand, SetZoomRatioCommand } from './commands/commands/set-zoom-ratio.command';
export { getPrimaryForRange } from './commands/commands/utils/selection-util';
@@ -150,6 +153,7 @@ export {
type ISetWorksheetRowHeightMutationParams,
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowHeightMutation,
+ SetWorksheetRowIsAutoHeightMutation,
} from './commands/mutations/set-worksheet-row-height.mutation';
export { SetScrollOperation } from './commands/operations/scroll.operation';
export { type ISetSelectionsOperationParams } from './commands/operations/selection.operation';
diff --git a/packages/core/src/sheets/row-manager.ts b/packages/core/src/sheets/row-manager.ts
index 3817411b6f..a8d8130d55 100644
--- a/packages/core/src/sheets/row-manager.ts
+++ b/packages/core/src/sheets/row-manager.ts
@@ -46,7 +46,9 @@ export class RowManager {
hd: BooleanNumber.FALSE,
h: config.defaultRowHeight,
});
- height += row.h;
+ const { isAutoHeight, ah, h } = row;
+
+ height += (isAutoHeight == null || !!isAutoHeight) && typeof ah === 'number' ? ah : h;
}
return height;
diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.ts
index 22f3fcd293..73b4539dbf 100644
--- a/packages/core/src/sheets/worksheet.ts
+++ b/packages/core/src/sheets/worksheet.ts
@@ -155,6 +155,7 @@ export class Worksheet {
return range;
}
}
+
return null;
}
@@ -248,6 +249,7 @@ export class Worksheet {
*/
getStatus() {
const { _snapshot: _config } = this;
+
return _config.status;
}
@@ -282,6 +284,7 @@ export class Worksheet {
getMaxColumns(): number {
const { _snapshot: _config } = this;
const { columnCount } = _config;
+
return columnCount;
}
@@ -292,6 +295,7 @@ export class Worksheet {
getMaxRows(): number {
const { _snapshot: _config } = this;
const { rowCount } = _config;
+
return rowCount;
}
@@ -339,6 +343,7 @@ export class Worksheet {
if (showGridlines === 0) {
return true;
}
+
return false;
}
@@ -349,6 +354,7 @@ export class Worksheet {
getTabColor(): Nullable<string> {
const { _snapshot: _config } = this;
const { tabColor } = _config;
+
return tabColor;
}
@@ -378,6 +384,7 @@ export class Worksheet {
const lastColumn = this.getMaxColumns() - 1;
const ranges = this._rowManager.getHiddenRows(start, end);
ranges.forEach((range) => (range.endColumn = lastColumn));
+
return ranges;
}
@@ -389,6 +396,7 @@ export class Worksheet {
const lastRow = this.getMaxRows() - 1;
const ranges = this._columnManager.getHiddenCols(start, end);
ranges.forEach((range) => (range.endRow = lastRow));
+
return ranges;
}
@@ -399,6 +407,7 @@ export class Worksheet {
isRightToLeft(): BooleanNumber {
const { _snapshot: _config } = this;
const { rightToLeft } = _config;
+
return rightToLeft;
}
diff --git a/packages/ui-plugin-sheets/src/Locale/en.ts b/packages/ui-plugin-sheets/src/Locale/en.ts
index ac1543ac88..de0b7e528a 100644
--- a/packages/ui-plugin-sheets/src/Locale/en.ts
+++ b/packages/ui-plugin-sheets/src/Locale/en.ts
@@ -502,6 +502,7 @@ export default {
byRow: 'By row',
byCol: 'By column',
generateNewMatrix: 'Generate new matrix',
+ fitContent: 'Fit for data',
freeze: 'Freeze',
},
info: {
diff --git a/packages/ui-plugin-sheets/src/Locale/zh.ts b/packages/ui-plugin-sheets/src/Locale/zh.ts
index 68d3e5397d..5e121bc188 100644
--- a/packages/ui-plugin-sheets/src/Locale/zh.ts
+++ b/packages/ui-plugin-sheets/src/Locale/zh.ts
@@ -365,6 +365,7 @@ export default {
byRow: '按行',
byCol: '按列',
generateNewMatrix: '生成新矩阵',
+ fitContent: '适合数据',
freeze: '冻结',
},
info: {
diff --git a/packages/ui-plugin-sheets/src/controller/menu/menu.ts b/packages/ui-plugin-sheets/src/controller/menu/menu.ts
index 981738107a..0d3004c22f 100644
--- a/packages/ui-plugin-sheets/src/controller/menu/menu.ts
+++ b/packages/ui-plugin-sheets/src/controller/menu/menu.ts
@@ -45,6 +45,7 @@ import {
SetWorksheetColWidthCommand,
SetWorksheetHideCommand,
SetWorksheetRowHeightCommand,
+ SetWorksheetRowIsAutoHeightCommand,
SetWorksheetShowCommand,
} from '@univerjs/base-sheets';
import {
@@ -1110,6 +1111,15 @@ export function InsertColAfterMenuItemFactory(accessor: IAccessor): IMenuButtonI
};
}
+export function FitContentMenuItemFactory(): IMenuButtonItem {
+ return {
+ id: SetWorksheetRowIsAutoHeightCommand.id,
+ type: MenuItemType.BUTTON,
+ positions: [SheetMenuPosition.ROW_HEADER_CONTEXT_MENU],
+ title: 'rightClick.fitContent',
+ };
+}
+
export function FrozenMenuItemFactory(): IMenuButtonItem {
return {
id: SetSelectionFrozenCommand.id,
diff --git a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
index d57a20569c..15398bf856 100644
--- a/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/sheet-ui.controller.ts
@@ -51,6 +51,7 @@ import {
DeleteRangeMoveLeftMenuItemFactory,
DeleteRangeMoveUpMenuItemFactory,
DeleteSheetMenuItemFactory,
+ FitContentMenuItemFactory,
FontFamilySelectorMenuItemFactory,
FontSizeSelectorMenuItemFactory,
FormatPainterMenuItemFactory,
@@ -198,6 +199,7 @@ export class SheetUIController extends Disposable {
ShowColMenuItemFactory,
RemoveColMenuItemFactory,
SetRowHeightMenuItemFactory,
+ FitContentMenuItemFactory,
SetColWidthMenuItemFactory,
DeleteRangeMenuItemFactory,
DeleteRangeMoveLeftMenuItemFactory,