From 955e0e5e8494c795f5bbfb47404b22becb8504c9 Mon Sep 17 00:00:00 2001 From: lumixraku Date: Wed, 23 Oct 2024 14:33:59 +0800 Subject: [PATCH] chore: better code --- packages/core/src/sheets/typedef.ts | 1 - .../src/components/sheets/sheet-skeleton.ts | 20 +++++-------- .../basics/const/command-listener-const.ts | 1 - .../set-worksheet-col-width.command.ts | 14 ++------- .../set-worksheet-col-width.mutation.ts | 30 ------------------- .../controllers/basic-worksheet.controller.ts | 1 - 6 files changed, 10 insertions(+), 57 deletions(-) diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.ts index d877487158a2..8366632f3633 100644 --- a/packages/core/src/sheets/typedef.ts +++ b/packages/core/src/sheets/typedef.ts @@ -208,7 +208,6 @@ export interface IColumnData { export interface IColAutoWidthInfo { col: number; - // autoWidth?: number; width?: number; } diff --git a/packages/engine-render/src/components/sheets/sheet-skeleton.ts b/packages/engine-render/src/components/sheets/sheet-skeleton.ts index 4c57dcce40b6..045365d3f8e4 100644 --- a/packages/engine-render/src/components/sheets/sheet-skeleton.ts +++ b/packages/engine-render/src/components/sheets/sheet-skeleton.ts @@ -705,8 +705,6 @@ export class SpreadsheetSkeleton extends Skeleton { } const results: IColAutoWidthInfo[] = []; - const { rowData } = this._worksheetData; - const rowObjectArray = rowData; const calculatedCols = new Set(); for (const range of ranges) { @@ -718,11 +716,6 @@ export class SpreadsheetSkeleton extends Skeleton { continue; } - // The row sets ia to false, and there is no need to calculate the automatic row height for the row. - if (rowObjectArray[colIndex]?.ia === BooleanNumber.FALSE) { - continue; - } - const hasUnMergedCell = this._hasUnMergedCellInColumn(colIndex, startRow, endRow); if (hasUnMergedCell) { @@ -740,7 +733,8 @@ export class SpreadsheetSkeleton extends Skeleton { } /** - * Iterate all rows and return column width of the specified column(by column index) + * Iterate rows in visible area(and rows around it) and return column width of the specified column(by column index) + * * @param colIndex * @returns {number} width */ @@ -755,9 +749,6 @@ export class SpreadsheetSkeleton extends Skeleton { return maxColWidth; } - // for performance: calc first and last row, - // then we can skip rows that are not in the range. - const getMeasuredWidthByCell = (cell: ICellDataForSheetInterceptor) => { let measuredWidth = 0; if (cell?.interceptorAutoWidth) { @@ -817,6 +808,11 @@ export class SpreadsheetSkeleton extends Skeleton { return measuredWidth; }; + // for cell with only v, auto size for content width in visible range and ± 10000 rows around. + // for cell with p, auto width for cotent in visible range and ± 1000 rows, 1/10 of situation above. + // first row and last row should be considerated. + // skip hidden row, merged cell + // also handle mutiple viewport situation (freeze row & freeze row&col) const visibleRangeViewMain = this.visibleRangeByViewportKey(SHEET_VIEWPORT_KEY.VIEW_MAIN); if (!visibleRangeViewMain) return maxColWidth; @@ -852,7 +848,7 @@ export class SpreadsheetSkeleton extends Skeleton { maxColWidth = Math.max(maxColWidth, measuredWidth); } - // check width of first row and last row + // check width of first row and last row, and rows in viewMainTop(viewMainTopLeft are included) const otherRowIndex: Set = new Set(); otherRowIndex.add(0); otherRowIndex.add(rowCount - 1); diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts index 55c4ce19b439..3984cfbd2a61 100644 --- a/packages/sheets/src/basics/const/command-listener-const.ts +++ b/packages/sheets/src/basics/const/command-listener-const.ts @@ -25,7 +25,6 @@ import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/muta import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation'; import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation'; import { SetWorksheetColAutoWidthMutation, SetWorksheetColWidthMutation } from '../../commands/mutations/set-worksheet-col-width.mutation'; -import { SetWorksheetColWidthMutation } from '../../commands/mutations/set-worksheet-col-width.mutation'; import { SetWorksheetDefaultStyleMutation } from '../../commands/mutations/set-worksheet-default-style.mutations'; import { SetWorksheetRowColumnStyleMutation } from '../../commands/mutations/set-worksheet-row-column-style.mutation'; import { diff --git a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts index 3b6001ce9ae0..ee096e67e8bb 100644 --- a/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts +++ b/packages/sheets/src/commands/commands/set-worksheet-col-width.command.ts @@ -234,17 +234,11 @@ export const SetWorksheetColIsAutoWidthCommand: ICommand = { unitId, subUnitId, ranges, - // autoWidthInfo: BooleanNumber.TRUE, // Hard code first, maybe it will change by the menu item in the future. }; // const undoMutationParams: ISetWorksheetColIsAutoWidthMutationParams = SetWorksheetColIsAutoWidthMutationFactory(redoMutationParams, worksheet); - // const setIsAutoWidthResult = commandService.syncExecuteCommand( - // SetWorksheetColIsAutoWidthMutation.id, - // redoMutationParams - // ); - // undos redos comes from auto-width.controller // for intercept 'sheet.command.set-col-is-auto-width' command. const { undos, redos } = accessor.get(SheetInterceptorService).onCommandExecute({ @@ -256,12 +250,8 @@ export const SetWorksheetColIsAutoWidthCommand: ICommand = { if (result.result) { undoRedoService.pushUndoRedo({ unitID: unitId, - undoMutations: [ - // { id: SetWorksheetColIsAutoWidthMutation.id, params: undoMutationParams }, - ...undos], - redoMutations: [ - // { id: SetWorksheetColIsAutoWidthMutation.id, params: redoMutationParams }, - ...redos], + undoMutations: [...undos], + redoMutations: [...redos], }); return true; diff --git a/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts index a3fa5d7cbc1d..3a2bd52a9991 100644 --- a/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts +++ b/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts @@ -30,7 +30,6 @@ export interface ISetWorksheetColIsAutoWidthMutationParams { unitId: string; subUnitId: string; ranges: IRange[]; - // autoWidthInfo: BooleanNumber | IObjectArrayPrimitiveType>; } export interface ISetWorksheetColAutoWidthMutationParams { @@ -147,35 +146,6 @@ export const SetWorksheetColWidthMutation: IMutation = { -// id: 'sheet.mutation.set-worksheet-col-is-auto-width', -// type: CommandType.MUTATION, -// handler: (accessor, params) => { -// const { ranges, autoWidthInfo } = params; -// const univerInstanceService = accessor.get(IUniverInstanceService); -// const target = getSheetCommandTarget(univerInstanceService, params); -// if (!target) return false; - -// const manager = target.worksheet.getColumnManager(); -// for (const { startColumn, endColumn } of ranges) { -// for (let index = startColumn; index <= endColumn; index++) { -// const col = manager.getColumnOrCreate(index); - -// if (typeof autoWidthInfo === 'number') { -// col.ia = autoWidthInfo; -// } else { -// col.ia = autoWidthInfo[index] ?? undefined; -// } -// } -// } - -// return true; -// }, -// }; - /** * Apply auto width value. */ diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts index 6daffa3de3f0..ddae8437b32e 100644 --- a/packages/sheets/src/controllers/basic-worksheet.controller.ts +++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts @@ -117,7 +117,6 @@ import { SetRowHiddenMutation, SetRowVisibleMutation } from '../commands/mutatio import { SetTabColorMutation } from '../commands/mutations/set-tab-color.mutation'; import { SetWorkbookNameMutation } from '../commands/mutations/set-workbook-name.mutation'; import { SetWorksheetColAutoWidthMutation, SetWorksheetColWidthMutation } from '../commands/mutations/set-worksheet-col-width.mutation'; -import { SetWorksheetColWidthMutation } from '../commands/mutations/set-worksheet-col-width.mutation'; import { SetWorksheetDefaultStyleMutation } from '../commands/mutations/set-worksheet-default-style.mutations'; import { SetWorksheetHideMutation } from '../commands/mutations/set-worksheet-hide.mutation'; import { SetWorksheetNameMutation } from '../commands/mutations/set-worksheet-name.mutation';