diff --git a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts index 7413bcd171b..eca3e8081a3 100644 --- a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts +++ b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts @@ -15,6 +15,12 @@ */ import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core'; +import type { + IInsertRangeMutationParams, + IInsertRowMutationParams, + IRemoveRowsMutationParams, +} from '../../basics/interfaces/mutation-interface'; + import { BooleanNumber, CommandType, @@ -27,18 +33,12 @@ import { Range, sequenceExecute, } from '@univerjs/core'; - -import type { - IInsertRangeMutationParams, - IInsertRowMutationParams, - IRemoveRowsMutationParams, -} from '../../basics/interfaces/mutation-interface'; import { SheetsSelectionsService } from '../../services/selections/selection-manager.service'; import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; import { InsertRowMutation, InsertRowMutationUndoFactory } from '../mutations/insert-row-col.mutation'; import { RemoveRowMutation } from '../mutations/remove-row-col.mutation'; import { getInsertRangeMutations } from '../utils/handle-range-mutation'; -import { followSelectionOperation } from './utils/selection-utils'; +import { copyStylesIgnoreBorder, followSelectionOperation } from './utils/selection-utils'; import { getSheetCommandTarget } from './utils/target-util'; export interface InsertRangeMoveDownCommandParams { @@ -123,13 +123,14 @@ export const InsertRangeMoveDownCommand: ICommand = { // to keep style. const cellValue: IObjectMatrixPrimitiveType = {}; Range.foreach(range, (row, col) => { - const cell = worksheet.getCell(row, col); + let cell = worksheet.getCell(row, col); if (!cell) { return; } if (!cellValue[row]) { cellValue[row] = {}; } + cell = copyStylesIgnoreBorder(cell, worksheet); cellValue[row][col] = { s: cell.s }; }); const insertRangeMutationParams: IInsertRangeMutationParams = { diff --git a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts index f966dba16b9..c75017ecb86 100644 --- a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts +++ b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts @@ -14,6 +14,13 @@ * limitations under the License. */ +import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core'; +import type { + IInsertColMutationParams, + IInsertRangeMutationParams, + IRemoveColMutationParams, +} from '../../basics/interfaces/mutation-interface'; + import { BooleanNumber, CommandType, @@ -26,20 +33,13 @@ import { Range, sequenceExecute, } from '@univerjs/core'; -import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core'; - import { SheetsSelectionsService } from '../../services/selections/selection-manager.service'; import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service'; import { InsertColMutation, InsertColMutationUndoFactory } from '../mutations/insert-row-col.mutation'; import { RemoveColMutation } from '../mutations/remove-row-col.mutation'; import { getInsertRangeMutations } from '../utils/handle-range-mutation'; -import { followSelectionOperation } from './utils/selection-utils'; +import { copyStylesIgnoreBorder, followSelectionOperation } from './utils/selection-utils'; import { getSheetCommandTarget } from './utils/target-util'; -import type { - IInsertColMutationParams, - IInsertRangeMutationParams, - IRemoveColMutationParams, -} from '../../basics/interfaces/mutation-interface'; export interface InsertRangeMoveRightCommandParams { range: IRange; @@ -124,13 +124,14 @@ export const InsertRangeMoveRightCommand: ICommand = { // to keep style. const cellValue: IObjectMatrixPrimitiveType = {}; Range.foreach(range, (row, col) => { - const cell = worksheet.getCell(row, col); + let cell = worksheet.getCell(row, col); if (!cell || !cell.s) { return; } if (!cellValue[row]) { cellValue[row] = {}; } + cell = copyStylesIgnoreBorder(cell, worksheet); cellValue[row][col] = { s: cell.s }; }); const insertRangeMutationParams: IInsertRangeMutationParams = { diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.ts index 8e515d14c78..d544afc2227 100644 --- a/packages/sheets/src/commands/commands/insert-row-col.command.ts +++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts @@ -42,7 +42,7 @@ import { } from '../mutations/insert-row-col.mutation'; import { RemoveColMutation, RemoveRowMutation } from '../mutations/remove-row-col.mutation'; import { SetRangeValuesMutation } from '../mutations/set-range-values.mutation'; -import { copyRangeStyles, followSelectionOperation } from './utils/selection-utils'; +import { copyRangeStyles, copyRangeStylesWithoutBorder, followSelectionOperation } from './utils/selection-utils'; import { getSheetCommandTarget } from './utils/target-util'; export interface IInsertRowCommandParams { @@ -229,7 +229,7 @@ export const InsertRowAfterCommand: ICommand = { rangeType: RANGE_TYPE.ROW, }, // copy styles from the row below - cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, true, range.endRow), + cellValue: copyRangeStylesWithoutBorder(worksheet, startRow, endRow, startColumn, endColumn, true, range.endRow), }; return accessor.get(ICommandService).executeCommand(InsertRowCommand.id, insertRowParams); @@ -409,7 +409,7 @@ export const InsertColAfterCommand: ICommand = { endRow, }, // copy styles from the column after - cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, false, range.endColumn), + cellValue: copyRangeStylesWithoutBorder(worksheet, startRow, endRow, startColumn, endColumn, false, range.endColumn), }; return accessor.get(ICommandService).executeCommand(InsertColCommand.id, insertColParams); diff --git a/packages/sheets/src/commands/commands/utils/selection-utils.ts b/packages/sheets/src/commands/commands/utils/selection-utils.ts index 073a7e60c2a..28e154f55c5 100644 --- a/packages/sheets/src/commands/commands/utils/selection-utils.ts +++ b/packages/sheets/src/commands/commands/utils/selection-utils.ts @@ -243,7 +243,6 @@ export function createRangeIteratorWithSkipFilteredRows(sheet: Worksheet) { * @param endColumn * @param isRow * @param styleRowOrColumn - * @returns */ export function copyRangeStyles( worksheet: Worksheet, @@ -264,21 +263,51 @@ export function copyRangeStyles( if (!cellValue[row]) { cellValue[row] = {}; } - - // univer-pro/issues/3016 insert row/column should not reuse border style - if (typeof cell.s === 'string') { - const styleData = worksheet.getStyleDataByHash(cell.s); - if (styleData) { - delete styleData.bd; - cell.s = worksheet.setStyleData(styleData); - } - } else { - const styleData = { ...cell.s }; - delete styleData.bd; - cell.s = worksheet.setStyleData(styleData); - } cellValue[row][column] = { s: cell.s }; } } return cellValue; } + +export function copyRangeStylesWithoutBorder( + worksheet: Worksheet, + startRow: number, + endRow: number, + startColumn: number, + endColumn: number, + isRow: boolean, + styleRowOrColumn: number +): IObjectMatrixPrimitiveType { + const cellDataMatrix: IObjectMatrixPrimitiveType = {}; + for (let row = startRow; row <= endRow; row++) { + for (let column = startColumn; column <= endColumn; column++) { + let cell = isRow ? worksheet.getCell(styleRowOrColumn, column) : worksheet.getCell(row, styleRowOrColumn); + if (!cell || !cell.s) { + continue; + } + if (!cellDataMatrix[row]) { + cellDataMatrix[row] = {}; + } + + // univer-pro/issues/3016 insert row/column should not reuse border style + cell = copyStylesIgnoreBorder(cell, worksheet); + cellDataMatrix[row][column] = { s: cell.s }; + } + } + return cellDataMatrix; +} + +export function copyStylesIgnoreBorder(cell: ICellData, worksheet: Worksheet) { + if (typeof cell.s === 'string') { + const styleData = worksheet.getStyleDataByHash(cell.s); + if (styleData) { + delete styleData.bd; + cell.s = worksheet.setStyleData(styleData); + } + } else { + const styleData = { ...cell.s }; + delete styleData.bd; + cell.s = worksheet.setStyleData(styleData); + } + return cell; +}