Skip to content

Commit

Permalink
fix: no reuse border when insert row after
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Nov 23, 2024
1 parent 54c6aa4 commit d463ed7
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -123,13 +123,14 @@ export const InsertRangeMoveDownCommand: ICommand = {
// to keep style.
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -124,13 +124,14 @@ export const InsertRangeMoveRightCommand: ICommand = {
// to keep style.
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
57 changes: 43 additions & 14 deletions packages/sheets/src/commands/commands/utils/selection-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ export function createRangeIteratorWithSkipFilteredRows(sheet: Worksheet) {
* @param endColumn
* @param isRow
* @param styleRowOrColumn
* @returns
*/
export function copyRangeStyles(
worksheet: Worksheet,
Expand All @@ -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<ICellData> {
const cellDataMatrix: IObjectMatrixPrimitiveType<ICellData> = {};
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;
}

0 comments on commit d463ed7

Please sign in to comment.