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.

fix(sheets): apply bottom-left to top-right borders correctly (#8093)

白熱2157bedcbcfe
diff --git a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
index 69c58a2163..db00c423cf 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts
@@ -15,7 +15,7 @@
*/
import type { IBorderData, Injector, IRange, Univer, Workbook } from '@univerjs/core';
-import { BorderStyleTypes, BorderType, ICommandService, IConfirmService, IUniverInstanceService, RANGE_TYPE, TestConfirmService, UniverInstanceType } from '@univerjs/core';
+import { BorderStyleTypes, BorderType, ICommandService, IConfirmService, IUniverInstanceService, RANGE_TYPE, RedoCommand, TestConfirmService, UndoCommand, UniverInstanceType } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { SheetsSelectionsService } from '../../../services/selections/selection.service';
import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';
@@ -71,6 +71,88 @@ describe('Test style commands', () => {
describe('set border style', () => {
describe('correct situations', () => {
+ it.each([
+ { type: BorderType.TLBR, keys: ['tl_br'] },
+ { type: BorderType.TLBC_TLMR, keys: ['tl_bc', 'tl_mr'] },
+ { type: BorderType.TLBR_TLBC_TLMR, keys: ['tl_br', 'tl_bc', 'tl_mr'] },
+ { type: BorderType.BLTR, keys: ['bl_tr'] },
+ { type: BorderType.MLTR_BCTR, keys: ['ml_tr', 'bc_tr'] },
+ ] satisfies Array<{ type: BorderType; keys: Array<keyof IBorderData> }>)(
+ 'sets $type without removing existing borders',
+ async ({ type, keys }) => {
+ const service = get(ICommandService);
+ const range = { startRow: 0, startColumn: 0, endRow: 1, endColumn: 1 };
+ const diagonal = { s: BorderStyleTypes.DASHED, cl: { rgb: '#abcdef' } };
+ const expected = Object.fromEntries(keys.map((key) => [key, diagonal]));
+ get(SheetsSelectionsService).setSelections([{ range, primary: null, style: null }]);
+ for (const merged of [false, true]) {
+ if (merged) {
+ await service.executeCommand(AddWorksheetMergeAllCommand.id);
+ }
+ await service.executeCommand(SetBorderBasicCommand.id, {
+ value: { type: BorderType.TOP, color: '#123456', style: BorderStyleTypes.THIN },
+ });
+ const before = getBorder(range);
+ await service.executeCommand(SetBorderBasicCommand.id, {
+ value: { type, color: '#abcdef', style: BorderStyleTypes.DASHED },
+ });
+ expect(getBorder(range)).toEqual({ ...before, ...expected });
+ for (let row = 0; row <= 1; row++) {
+ for (let column = 0; column <= 1; column++) {
+ const border = getBorder({ startRow: row, endRow: row, startColumn: column, endColumn: column });
+ if (!merged || (row === 0 && column === 0)) {
+ expect(border).toMatchObject(expected);
+ } else {
+ for (const key of keys) expect(border?.[key]).toBeFalsy();
+ }
+ }
+ }
+ await service.executeCommand(UndoCommand.id);
+ expect(getBorder(range)).toEqual(before);
+ await service.executeCommand(RedoCommand.id);
+ expect(getBorder(range)).toEqual({ ...before, ...expected });
+ await service.executeCommand(SetBorderPositionCommand.id, { value: BorderType.NONE });
+ for (const key of keys) expect(getBorder(range)?.[key]).toBeFalsy();
+ expect(getBorder(range)?.t).toBeFalsy();
+ }
+ }
+ );
+
+ it.each([
+ { type: BorderType.TOP, edges: [['t'], ['t'], [], []] },
+ { type: BorderType.BOTTOM, edges: [[], [], ['b'], ['b']] },
+ { type: BorderType.LEFT, edges: [['l'], [], ['l'], []] },
+ { type: BorderType.RIGHT, edges: [[], ['r'], [], ['r']] },
+ { type: BorderType.NONE, edges: [[], [], [], []] },
+ { type: BorderType.ALL, edges: [['t', 'b', 'l', 'r'], ['t', 'b', 'l', 'r'], ['t', 'b', 'l', 'r'], ['t', 'b', 'l', 'r']] },
+ { type: BorderType.OUTSIDE, edges: [['t', 'l'], ['t', 'r'], ['b', 'l'], ['b', 'r']] },
+ { type: BorderType.INSIDE, edges: [['b', 'r'], ['b', 'l'], ['t', 'r'], ['t', 'l']] },
+ { type: BorderType.HORIZONTAL, edges: [['b'], ['b'], ['t'], ['t']] },
+ { type: BorderType.VERTICAL, edges: [['r'], ['l'], ['r'], ['l']] },
+ ])('applies $type to the requested cell edges', async ({ type, edges }) => {
+ const service = get(ICommandService);
+ const range = { startRow: 2, startColumn: 2, endRow: 3, endColumn: 3 };
+ const value = { type, color: '#123456', style: BorderStyleTypes.THIN };
+ if (type === BorderType.NONE) {
+ await service.executeCommand(SetBorderBasicCommand.id, {
+ ranges: [range],
+ value: { ...value, type: BorderType.ALL },
+ });
+ }
+ await service.executeCommand(SetBorderBasicCommand.id, { ranges: [range], value });
+ for (let row = 2; row <= 3; row++) {
+ for (let column = 2; column <= 3; column++) {
+ const border = getBorder({ startRow: row, endRow: row, startColumn: column, endColumn: column });
+ const actual = Object.fromEntries(Object.entries(border ?? {}).filter(([, style]) => style));
+ const expected = Object.fromEntries(edges[(row - 2) * 2 + column - 2].map((key) => [
+ key,
+ { s: BorderStyleTypes.THIN, cl: { rgb: '#123456' } },
+ ]));
+ expect(actual).toEqual(expected);
+ }
+ }
+ });
+
it('will set border style when there is a selected range', async () => {
const selectionManager = get(SheetsSelectionsService);
selectionManager.addSelections([
diff --git a/packages/sheets/src/commands/commands/set-border.command.ts b/packages/sheets/src/commands/commands/set-border.command.ts
index 757873cbba..d326469c75 100644
--- a/packages/sheets/src/commands/commands/set-border.command.ts
+++ b/packages/sheets/src/commands/commands/set-border.command.ts
@@ -24,9 +24,6 @@ import type {
IRange,
IStyleData,
} from '@univerjs/core';
-import type { IBorderInfo } from '../../services/border-style-manager.service';
-import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
-import type { IResult } from './utils/target-util';
import {
BorderType,
CommandType,
@@ -36,6 +33,9 @@ import {
ObjectMatrix,
Tools,
} from '@univerjs/core';
+import type { IBorderInfo } from '../../services/border-style-manager.service';
+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
+import type { IResult } from './utils/target-util';
import { BorderStyleManagerService } from '../../services/border-style-manager.service';
import { SheetsSelectionsService } from '../../services/selections/selection.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
@@ -191,7 +191,7 @@ function getBorderContext(borderStyleManagerService: BorderStyleManagerService,
const tl_mr = type.indexOf('tlmr') > -1;
- const bl_tr = type.indexOf('bltr') > -1;
+ const bl_tr = type === BorderType.BLTR;
const ml_tr = type.indexOf('mltr') > -1;
@@ -254,11 +254,6 @@ function getBorderContext(borderStyleManagerService: BorderStyleManagerService,
};
}
-// const setBorderStyle = (borderContext: ReturnType<typeof getBorderContext>, defaultStyle: IBorderData, reserve?: boolean) => {
-// setBorderStyleForRange(borderContext.mr, borderContext.worksheet, borderContext.range, defaultStyle, reserve);
-// };
-
-// eslint-disable-next-line max-lines-per-function
const innerBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
const { range, mr, borderStyle, vertical, horizontal, worksheet } = borderContext;
//#region inner vertical border
@@ -269,33 +264,21 @@ const innerBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
const topLeftStyle = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
if (mergedRange.startColumn !== range.startColumn) {
mr.setValue(row, column, {
- s: {
- bd: topLeftStyle?.bd
- ? Object.assign(topLeftStyle.bd, { l: Tools.deepClone(borderStyle) })
- : { l: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(topLeftStyle?.bd ?? {}, { l: Tools.deepClone(borderStyle) }) },
});
}
} else {
if (column !== range.endColumn) {
const style = mr.getValue(row, column)?.s as IStyleData;
mr.setValue(row, column, {
- s: {
- bd: style?.bd
- ? Object.assign(style.bd, { r: Tools.deepClone(borderStyle) })
- : { r: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(style?.bd ?? {}, { r: Tools.deepClone(borderStyle) }) },
});
}
if (column !== range.startColumn) {
const style = mr.getValue(row, column)?.s as IStyleData;
mr.setValue(row, column, {
- s: {
- bd: style?.bd
- ? Object.assign(style.bd, { l: Tools.deepClone(borderStyle) })
- : { l: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(style?.bd ?? {}, { l: Tools.deepClone(borderStyle) }) },
});
}
}
@@ -312,33 +295,21 @@ const innerBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
if (mergedRange.startRow !== range.startRow) {
mr.setValue(row, column, {
- s: {
- bd: topLeftStyle?.bd
- ? Object.assign(topLeftStyle.bd, { t: Tools.deepClone(borderStyle) })
- : { t: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(topLeftStyle?.bd ?? {}, { t: Tools.deepClone(borderStyle) }) },
});
}
} else {
if (row !== range.endRow) {
const style = mr.getValue(row, column)?.s as IStyleData;
mr.setValue(row, column, {
- s: {
- bd: style?.bd
- ? Object.assign(style.bd, { b: Tools.deepClone(borderStyle) })
- : { b: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(style?.bd ?? {}, { b: Tools.deepClone(borderStyle) }) },
});
}
if (row !== range.startRow) {
const style = mr.getValue(row, column)?.s as IStyleData;
mr.setValue(row, column, {
- s: {
- bd: style?.bd
- ? Object.assign(style.bd, { t: Tools.deepClone(borderStyle) })
- : { t: Tools.deepClone(borderStyle) },
- },
+ s: { bd: Object.assign(style?.bd ?? {}, { t: Tools.deepClone(borderStyle) }) },
});
}
}
@@ -415,7 +386,6 @@ const outlineBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
}
};
-// eslint-disable-next-line max-lines-per-function
const clearBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
const { range, worksheet, mr, top, bottom, left, right, vertical, horizontal, tl_br, tl_bc, tl_mr, bl_tr, ml_tr, bc_tr, topRange, bottomRange, leftRange, rightRange, topRangeOut, bottomRangeOut, leftRangeOut, rightRangeOut } = borderContext;
@@ -436,85 +406,27 @@ const clearBorder = (borderContext: ReturnType<typeof getBorderContext>) => {
!ml_tr &&
!bc_tr
) {
- // eslint-disable-next-line complexity
forEach(range, (row, column) => {
const mergedRange = worksheet.getMergedCell(row, column);
- // If this cell has border setting before merge, then we need clear settings of edge border.
- if (mergedRange) {
- // Remove the right border from all cells except for the right border of the rightmost cell.
- if (mergedRange.endColumn !== range.endColumn) {
- const style = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { r: null }) : { r: null },
- },
- });
- }
- // Remove the left border from all cells except for the left border of the leftmost cell.
- if (mergedRange.startColumn !== range.startColumn) {
- const style = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { l: null }) : { l: null },
- },
- });
- }
- // see https://github.com/dream-num/univer/pull/3506
- // Remove the top border from all cells except for the top border of the topmost cell.
- if (mergedRange.endRow !== range.endRow) {
- const style = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { b: null }) : { b: null },
- },
- });
- }
- // Remove the bottom border from all cells except for the bottom border of the bottommost cell.
- if (mergedRange.startRow !== range.startRow) {
- const style = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { t: null }) : { t: null },
- },
- });
- }
- } else {
- // Clear the right border of all columns except the last column
- if (column !== range.endColumn) {
- const style = mr.getValue(row, column)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { r: null }) : { r: null },
- },
- });
- }
- // Clear the left border of all columns except the first column
- if (column !== range.startColumn) {
- const style = mr.getValue(row, column)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { l: null }) : { l: null },
- },
- });
- }
- // Clear all the bottom border except the last line
- if (row !== range.endRow) {
- const style = mr.getValue(row, column)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { b: null }) : { b: null },
- },
- });
- }
- // Clear the top border of all lines except the first line
- if (row !== range.startRow) {
- const style = mr.getValue(row, column)?.s as IStyleData;
- mr.setValue(row, column, {
- s: {
- bd: style?.bd ? Object.assign(style.bd, { t: null }) : { t: null },
- },
- });
- }
+ const clearCellBorder = (key: keyof IBorderData) => {
+ const style = mr.getValue(mergedRange?.startRow ?? row, mergedRange?.startColumn ?? column)?.s as IStyleData;
+ mr.setValue(row, column, {
+ s: { bd: Object.assign(style?.bd ?? {}, { [key]: null }) },
+ });
+ };
+
+ // Clear borders left on individual cells before merging, using the merged bounds.
+ if ((mergedRange?.endColumn ?? column) !== range.endColumn) {
+ clearCellBorder('r');
+ }
+ if ((mergedRange?.startColumn ?? column) !== range.startColumn) {
+ clearCellBorder('l');
+ }
+ if ((mergedRange?.endRow ?? row) !== range.endRow) {
+ clearCellBorder('b');
+ }
+ if ((mergedRange?.startRow ?? row) !== range.startRow) {
+ clearCellBorder('t');
}
});