Skip to content

Commit d463ed7

Browse files
committed
fix: no reuse border when insert row after
1 parent 54c6aa4 commit d463ed7

File tree

4 files changed

+65
-34
lines changed

4 files changed

+65
-34
lines changed

packages/sheets/src/commands/commands/insert-range-move-down.command.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
*/
1616

1717
import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
18+
import type {
19+
IInsertRangeMutationParams,
20+
IInsertRowMutationParams,
21+
IRemoveRowsMutationParams,
22+
} from '../../basics/interfaces/mutation-interface';
23+
1824
import {
1925
BooleanNumber,
2026
CommandType,
@@ -27,18 +33,12 @@ import {
2733
Range,
2834
sequenceExecute,
2935
} from '@univerjs/core';
30-
31-
import type {
32-
IInsertRangeMutationParams,
33-
IInsertRowMutationParams,
34-
IRemoveRowsMutationParams,
35-
} from '../../basics/interfaces/mutation-interface';
3636
import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
3737
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
3838
import { InsertRowMutation, InsertRowMutationUndoFactory } from '../mutations/insert-row-col.mutation';
3939
import { RemoveRowMutation } from '../mutations/remove-row-col.mutation';
4040
import { getInsertRangeMutations } from '../utils/handle-range-mutation';
41-
import { followSelectionOperation } from './utils/selection-utils';
41+
import { copyStylesIgnoreBorder, followSelectionOperation } from './utils/selection-utils';
4242
import { getSheetCommandTarget } from './utils/target-util';
4343

4444
export interface InsertRangeMoveDownCommandParams {
@@ -123,13 +123,14 @@ export const InsertRangeMoveDownCommand: ICommand = {
123123
// to keep style.
124124
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
125125
Range.foreach(range, (row, col) => {
126-
const cell = worksheet.getCell(row, col);
126+
let cell = worksheet.getCell(row, col);
127127
if (!cell) {
128128
return;
129129
}
130130
if (!cellValue[row]) {
131131
cellValue[row] = {};
132132
}
133+
cell = copyStylesIgnoreBorder(cell, worksheet);
133134
cellValue[row][col] = { s: cell.s };
134135
});
135136
const insertRangeMutationParams: IInsertRangeMutationParams = {

packages/sheets/src/commands/commands/insert-range-move-right.command.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
18+
import type {
19+
IInsertColMutationParams,
20+
IInsertRangeMutationParams,
21+
IRemoveColMutationParams,
22+
} from '../../basics/interfaces/mutation-interface';
23+
1724
import {
1825
BooleanNumber,
1926
CommandType,
@@ -26,20 +33,13 @@ import {
2633
Range,
2734
sequenceExecute,
2835
} from '@univerjs/core';
29-
import type { IAccessor, ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';
30-
3136
import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';
3237
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
3338
import { InsertColMutation, InsertColMutationUndoFactory } from '../mutations/insert-row-col.mutation';
3439
import { RemoveColMutation } from '../mutations/remove-row-col.mutation';
3540
import { getInsertRangeMutations } from '../utils/handle-range-mutation';
36-
import { followSelectionOperation } from './utils/selection-utils';
41+
import { copyStylesIgnoreBorder, followSelectionOperation } from './utils/selection-utils';
3742
import { getSheetCommandTarget } from './utils/target-util';
38-
import type {
39-
IInsertColMutationParams,
40-
IInsertRangeMutationParams,
41-
IRemoveColMutationParams,
42-
} from '../../basics/interfaces/mutation-interface';
4343

4444
export interface InsertRangeMoveRightCommandParams {
4545
range: IRange;
@@ -124,13 +124,14 @@ export const InsertRangeMoveRightCommand: ICommand = {
124124
// to keep style.
125125
const cellValue: IObjectMatrixPrimitiveType<ICellData> = {};
126126
Range.foreach(range, (row, col) => {
127-
const cell = worksheet.getCell(row, col);
127+
let cell = worksheet.getCell(row, col);
128128
if (!cell || !cell.s) {
129129
return;
130130
}
131131
if (!cellValue[row]) {
132132
cellValue[row] = {};
133133
}
134+
cell = copyStylesIgnoreBorder(cell, worksheet);
134135
cellValue[row][col] = { s: cell.s };
135136
});
136137
const insertRangeMutationParams: IInsertRangeMutationParams = {

packages/sheets/src/commands/commands/insert-row-col.command.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
} from '../mutations/insert-row-col.mutation';
4343
import { RemoveColMutation, RemoveRowMutation } from '../mutations/remove-row-col.mutation';
4444
import { SetRangeValuesMutation } from '../mutations/set-range-values.mutation';
45-
import { copyRangeStyles, followSelectionOperation } from './utils/selection-utils';
45+
import { copyRangeStyles, copyRangeStylesWithoutBorder, followSelectionOperation } from './utils/selection-utils';
4646
import { getSheetCommandTarget } from './utils/target-util';
4747

4848
export interface IInsertRowCommandParams {
@@ -229,7 +229,7 @@ export const InsertRowAfterCommand: ICommand = {
229229
rangeType: RANGE_TYPE.ROW,
230230
},
231231
// copy styles from the row below
232-
cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, true, range.endRow),
232+
cellValue: copyRangeStylesWithoutBorder(worksheet, startRow, endRow, startColumn, endColumn, true, range.endRow),
233233
};
234234

235235
return accessor.get(ICommandService).executeCommand(InsertRowCommand.id, insertRowParams);
@@ -409,7 +409,7 @@ export const InsertColAfterCommand: ICommand = {
409409
endRow,
410410
},
411411
// copy styles from the column after
412-
cellValue: copyRangeStyles(worksheet, startRow, endRow, startColumn, endColumn, false, range.endColumn),
412+
cellValue: copyRangeStylesWithoutBorder(worksheet, startRow, endRow, startColumn, endColumn, false, range.endColumn),
413413
};
414414

415415
return accessor.get(ICommandService).executeCommand(InsertColCommand.id, insertColParams);

packages/sheets/src/commands/commands/utils/selection-utils.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ export function createRangeIteratorWithSkipFilteredRows(sheet: Worksheet) {
243243
* @param endColumn
244244
* @param isRow
245245
* @param styleRowOrColumn
246-
* @returns
247246
*/
248247
export function copyRangeStyles(
249248
worksheet: Worksheet,
@@ -264,21 +263,51 @@ export function copyRangeStyles(
264263
if (!cellValue[row]) {
265264
cellValue[row] = {};
266265
}
267-
268-
// univer-pro/issues/3016 insert row/column should not reuse border style
269-
if (typeof cell.s === 'string') {
270-
const styleData = worksheet.getStyleDataByHash(cell.s);
271-
if (styleData) {
272-
delete styleData.bd;
273-
cell.s = worksheet.setStyleData(styleData);
274-
}
275-
} else {
276-
const styleData = { ...cell.s };
277-
delete styleData.bd;
278-
cell.s = worksheet.setStyleData(styleData);
279-
}
280266
cellValue[row][column] = { s: cell.s };
281267
}
282268
}
283269
return cellValue;
284270
}
271+
272+
export function copyRangeStylesWithoutBorder(
273+
worksheet: Worksheet,
274+
startRow: number,
275+
endRow: number,
276+
startColumn: number,
277+
endColumn: number,
278+
isRow: boolean,
279+
styleRowOrColumn: number
280+
): IObjectMatrixPrimitiveType<ICellData> {
281+
const cellDataMatrix: IObjectMatrixPrimitiveType<ICellData> = {};
282+
for (let row = startRow; row <= endRow; row++) {
283+
for (let column = startColumn; column <= endColumn; column++) {
284+
let cell = isRow ? worksheet.getCell(styleRowOrColumn, column) : worksheet.getCell(row, styleRowOrColumn);
285+
if (!cell || !cell.s) {
286+
continue;
287+
}
288+
if (!cellDataMatrix[row]) {
289+
cellDataMatrix[row] = {};
290+
}
291+
292+
// univer-pro/issues/3016 insert row/column should not reuse border style
293+
cell = copyStylesIgnoreBorder(cell, worksheet);
294+
cellDataMatrix[row][column] = { s: cell.s };
295+
}
296+
}
297+
return cellDataMatrix;
298+
}
299+
300+
export function copyStylesIgnoreBorder(cell: ICellData, worksheet: Worksheet) {
301+
if (typeof cell.s === 'string') {
302+
const styleData = worksheet.getStyleDataByHash(cell.s);
303+
if (styleData) {
304+
delete styleData.bd;
305+
cell.s = worksheet.setStyleData(styleData);
306+
}
307+
} else {
308+
const styleData = { ...cell.s };
309+
delete styleData.bd;
310+
cell.s = worksheet.setStyleData(styleData);
311+
}
312+
return cell;
313+
}

0 commit comments

Comments
 (0)