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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: border for merged cell (#3506)
lumix1c4c237ced3d
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.tsindex a4fdce093d..86b91bde95 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@@ -14,9 +14,9 @@* limitations under the License.*/-import type { IBorderData, Injector, IRange, Univer } from '@univerjs/core';import { BorderStyleTypes, BorderType, ICommandService, IUniverInstanceService, RANGE_TYPE } from '@univerjs/core';import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import type { IBorderData, Injector, IRange, Univer } from '@univerjs/core';import { SheetsSelectionsService } from '../../../services/selections/selection-manager.service';import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';@@ -268,6 +268,77 @@ describe('Test style commands', () => {cl: { rgb: '#123456' },});});++ it('set border for range has merged and unmerged cells', async () => {+ const selectionManager = get(SheetsSelectionsService);+ selectionManager.addSelections([+ {+ range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },+ primary: null,+ style: null,+ },+ ]);++ function getBorder({ startRow, startColumn }: IRange): IBorderData | undefined {+ return get(IUniverInstanceService)+ .getUniverSheetInstance('test')+ ?.getSheetBySheetId('sheet1')+ ?.getRange(startRow, startColumn)+ .getBorder();+ }++ expect(await commandService.executeCommand(AddWorksheetMergeAllCommand.id)).toBeTruthy();++ selectionManager.addSelections([+ {+ range: { startRow: 0, startColumn: 0, endRow: 5, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },+ primary: null,+ style: null,+ },+ ]);++ expect(+ await commandService.executeCommand(SetBorderPositionCommand.id, { value: BorderType.ALL })+ ).toBeTruthy();+ expect(+ await commandService.executeCommand(SetBorderColorCommand.id, { value: '#aaaaaa' })+ ).toBeTruthy();+ expect(+ await commandService.executeCommand(SetBorderStyleCommand.id, {+ value: BorderStyleTypes.SLANT_DASH_DOT,+ })+ ).toBeTruthy();+ expect(getBorder({ startRow: 0, endRow: 0, startColumn: 5, endColumn: 5 })?.r).toStrictEqual({+ s: BorderStyleTypes.SLANT_DASH_DOT,+ cl: { rgb: '#aaaaaa' },+ });++ selectionManager.clear();+ selectionManager.addSelections([+ {+ range: { startRow: 0, startColumn: 0, endRow: 5, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },+ primary: null,+ style: null,+ },+ ]);+ await commandService.executeCommand(SetBorderPositionCommand.id, { value: BorderType.NONE });+ expect(getBorder({ startRow: 0, endRow: 0, startColumn: 5, endColumn: 5 })?.r).toBeNull();++ selectionManager.addSelections([+ {+ range: { startRow: 0, startColumn: 0, endRow: 5, endColumn: 5, rangeType: RANGE_TYPE.NORMAL },+ primary: null,+ style: null,+ },+ ]);+ // executeCommand('sheet.command.set-border-position', { value: 'inside' })+ await commandService.executeCommand(SetBorderPositionCommand.id, { value: BorderType.INSIDE });+ expect(getBorder({ startRow: 0, endRow: 0, startColumn: 5, endColumn: 5 })?.r).toBeFalsy();+ expect(getBorder({ startRow: 1, endRow: 1, startColumn: 5, endColumn: 5 })?.r).toBeFalsy();+ expect(getBorder({ startRow: 1, endRow: 1, startColumn: 5, endColumn: 5 })?.b).not.toBeNull();+ expect(getBorder({ startRow: 1, endRow: 1, startColumn: 5, endColumn: 5 })?.t).not.toBeNull();+ expect(getBorder({ startRow: 1, endRow: 1, startColumn: 5, endColumn: 5 })?.l).not.toBeNull();+ });});describe('fault situations', () => {diff --git a/packages/sheets/src/commands/commands/set-border-command.ts b/packages/sheets/src/commands/commands/set-border-command.tsindex 524a33a8cb..93c80975c0 100644--- a/packages/sheets/src/commands/commands/set-border-command.ts+++ b/packages/sheets/src/commands/commands/set-border-command.ts@@ -23,6 +23,7 @@ import {ObjectMatrix,Tools,} from '@univerjs/core';+import type {BorderStyleTypes,IAccessor,@@ -33,12 +34,12 @@ import type {IRange,IStyleData,} from '@univerjs/core';-import { BorderStyleManagerService, type IBorderInfo } from '../../services/border-style-manager.service';import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';import { getSheetCommandTarget } from './utils/target-util';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';+import type { IResult } from './utils/target-util';function forEach(range: IRange, action: (row: number, column: number) => void): void {const { startRow, startColumn, endRow, endColumn } = range;@@ -55,481 +56,513 @@ export interface ISetBorderBasicCommandParams {value: IBorderInfo;}-export const SetBorderBasicCommand: ICommand<ISetBorderBasicCommandParams> = {- id: 'sheet.command.set-border-basic',- type: CommandType.COMMAND,- handler: async (accessor: IAccessor, params: ISetBorderBasicCommandParams) => {- const { unitId, subUnitId, value } = params;- const { type, color, style } = value;-- const commandService = accessor.get(ICommandService);- const borderManager = accessor.get(BorderStyleManagerService);-- borderManager.setType(type);- if (color) borderManager.setColor(color);- borderManager.setStyle(style);-- return commandService.executeCommand(SetBorderCommand.id, {- unitId,- subUnitId,- });- },-};export interface ISetBorderPositionCommandParams {value: BorderType;}-export const SetBorderPositionCommand: ICommand<ISetBorderPositionCommandParams> = {- id: 'sheet.command.set-border-position',- type: CommandType.COMMAND,- handler: async (accessor: IAccessor, params: ISetBorderPositionCommandParams) => {- if (!params.value) return false;- const commandService = accessor.get(ICommandService);- const borderManager = accessor.get(BorderStyleManagerService);- borderManager.setType(params.value);- return commandService.executeCommand(SetBorderCommand.id);- },-};-export interface ISetBorderStyleCommandParams {value: BorderStyleTypes;}-export const SetBorderStyleCommand: ICommand = {- id: 'sheet.command.set-border-style',- type: CommandType.COMMAND,- handler: async (accessor: IAccessor, params: ISetBorderStyleCommandParams) => {- const commandService = accessor.get(ICommandService);- const borderManager = accessor.get(BorderStyleManagerService);- borderManager.setStyle(params.value);- return commandService.executeCommand(SetBorderCommand.id);- },-};--export interface ISetBorderColorCommandParams {- value: string;-}--export const SetBorderColorCommand: ICommand<ISetBorderColorCommandParams> = {- id: 'sheet.command.set-border-color',- type: CommandType.COMMAND,- handler: async (accessor: IAccessor, params: ISetBorderColorCommandParams) => {- const commandService = accessor.get(ICommandService);- const borderManager = accessor.get(BorderStyleManagerService);- borderManager.setColor(params.value);- return commandService.executeCommand(SetBorderCommand.id);- },-};-export interface ISetBorderCommandParams {unitId?: string;subUnitId?: string;}-/**- * The command to clear content in current selected ranges.- */-export const SetBorderCommand: ICommand = {- id: 'sheet.command.set-border',- type: CommandType.COMMAND,-- handler: async (accessor: IAccessor, params?: ISetBorderCommandParams) => {- const commandService = accessor.get(ICommandService);- const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const selectionManagerService = accessor.get(SheetsSelectionsService);- const borderStyleManagerService = accessor.get(BorderStyleManagerService);-- const target = getSheetCommandTarget(univerInstanceService, params);- if (!target) return false;+export interface ISetBorderColorCommandParams {+ value: string;+}- const { worksheet, unitId, subUnitId } = target;- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);- if (!selections?.length) {- return false;+const setBorderStyleForRange = (borderContext: ReturnType<typeof getBorderContext>, range: IRange, defaultStyle: IBorderData, reserve?: boolean) => {+ const { mr, worksheet } = borderContext;+ if (range.startRow < 0 || range.startColumn < 0) return;+ forEach(range, (row, column) => {+ const rectangle = worksheet.getMergedCell(row, column);++ let bdStyle = defaultStyle;++ if (rectangle && (defaultStyle.bc_tr || defaultStyle.ml_tr || defaultStyle.bl_tr || defaultStyle.tl_mr || defaultStyle.tl_bc || defaultStyle.tl_br)) {+ if (reserve) {+ const style = Tools.deepClone(+ mr.getValue(rectangle.startRow, rectangle.startColumn)?.s+ ) as IStyleData;+ bdStyle = style?.bd ? Object.assign(style.bd, defaultStyle) : defaultStyle;+ }+ mr.setValue(rectangle.startRow, rectangle.startColumn, {+ s: {+ bd: bdStyle,+ },+ });+ } else {+ if (reserve) {+ const style = Tools.deepClone(mr.getValue(row, column)?.s) as IStyleData;+ bdStyle = style?.bd ? Object.assign(style.bd, defaultStyle) : defaultStyle;+ }+ mr.setValue(row, column, { s: { bd: bdStyle } });}+ });+};- const { style, color, type, activeBorderType } = borderStyleManagerService.getBorderInfo();- if (!activeBorderType) return false;- const top = type === BorderType.TOP || type === BorderType.ALL || type === BorderType.OUTSIDE;- const left = type === BorderType.LEFT || type === BorderType.ALL || type === BorderType.OUTSIDE;- const bottom = type === BorderType.BOTTOM || type === BorderType.ALL || type === BorderType.OUTSIDE;- const right = type === BorderType.RIGHT || type === BorderType.ALL || type === BorderType.OUTSIDE;- const vertical = type === BorderType.VERTICAL || type === BorderType.ALL || type === BorderType.INSIDE;- const horizontal = type === BorderType.HORIZONTAL || type === BorderType.ALL || type === BorderType.INSIDE;-- const tl_br = type.indexOf('tlbr') > -1;+const prepareEdgeRange = (range: IRange) => {+ const topRangeOut = {+ startRow: range.startRow - 1,+ startColumn: range.startColumn,+ endRow: range.startRow - 1,+ endColumn: range.endColumn,+ };++ const leftRangeOut = {+ startRow: range.startRow,+ startColumn: range.startColumn - 1,+ endRow: range.endRow,+ endColumn: range.startColumn - 1,+ };++ const bottomRangeOut = {+ startRow: range.endRow + 1,+ startColumn: range.startColumn,+ endRow: range.endRow + 1,+ endColumn: range.endColumn,+ };++ const rightRangeOut = {+ startRow: range.startRow,+ startColumn: range.endColumn + 1,+ endRow: range.endRow,+ endColumn: range.endColumn + 1,+ };++ // Cells in the upper, lower, left and right ranges+ const topRange = {+ startRow: range.startRow,+ startColumn: range.startColumn,+ endRow: range.startRow,+ endColumn: range.endColumn,+ };++ const leftRange = {+ startRow: range.startRow,+ startColumn: range.startColumn,+ endRow: range.endRow,+ endColumn: range.startColumn,+ };++ const bottomRange = {+ startRow: range.endRow,+ startColumn: range.startColumn,+ endRow: range.endRow,+ endColumn: range.endColumn,+ };++ const rightRange = {+ startRow: range.startRow,+ startColumn: range.endColumn,+ endRow: range.endRow,+ endColumn: range.endColumn,+ };+ return {+ topRangeOut,+ leftRangeOut,+ bottomRangeOut,+ rightRangeOut,+ topRange,+ leftRange,+ bottomRange,+ rightRange,+ };+};- const tl_bc = type.indexOf('tlbc') > -1;+function getBorderContext(borderStyleManagerService: BorderStyleManagerService, target: IResult, selections: IRange[]) {+ const { style, color, type } = borderStyleManagerService.getBorderInfo();++ const top = type === BorderType.TOP || type === BorderType.ALL || type === BorderType.OUTSIDE;+ const left = type === BorderType.LEFT || type === BorderType.ALL || type === BorderType.OUTSIDE;+ const bottom = type === BorderType.BOTTOM || type === BorderType.ALL || type === BorderType.OUTSIDE;+ const right = type === BorderType.RIGHT || type === BorderType.ALL || type === BorderType.OUTSIDE;+ const vertical = type === BorderType.VERTICAL || type === BorderType.ALL || type === BorderType.INSIDE;+ const horizontal = type === BorderType.HORIZONTAL || type === BorderType.ALL || type === BorderType.INSIDE;++ const tl_br = type.indexOf('tlbr') > -1;++ const tl_bc = type.indexOf('tlbc') > -1;++ const tl_mr = type.indexOf('tlmr') > -1;++ const bl_tr = type.indexOf('bltr') > -1;++ const ml_tr = type.indexOf('mltr') > -1;++ const bc_tr = type.indexOf('bctr') > -1;++ const range = selections[0]; // TODO @Dushusir support multiple ranges++ // Cells in the surrounding range may need to clear the border+ const {+ topRangeOut,+ leftRangeOut,+ bottomRangeOut,+ rightRangeOut,+ topRange,+ leftRange,+ bottomRange,+ rightRange,+ } = prepareEdgeRange(range);++ const mr = new ObjectMatrix<ICellData>();++ const { worksheet, unitId, subUnitId } = target;++ const borderStyle: IBorderStyleData = {+ s: style,+ cl: {+ rgb: color,+ },+ };+ return {+ worksheet, unitId, subUnitId,+ style, color, type,+ top,+ left,+ right,+ bottom,+ vertical,+ horizontal,+ tl_br,+ tl_bc,+ tl_mr,+ bl_tr,+ ml_tr,+ bc_tr,+ topRangeOut,+ leftRangeOut,+ bottomRangeOut,+ rightRangeOut,+ topRange,+ leftRange,+ bottomRange,+ rightRange,+ range,+ mr,+ borderStyle,+ };+}- const tl_mr = type.indexOf('tlmr') > -1;+// 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+ if (vertical) {+ forEach(range, (row, column) => {+ const mergedRange = worksheet.getMergedCell(row, column);+ if (mergedRange) {+ 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) },+ },+ });+ }+ } 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) },+ },+ });+ }- const bl_tr = type.indexOf('bltr') > -1;+ 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) },+ },+ });+ }+ }+ });+ }+ //#endregion- const ml_tr = type.indexOf('mltr') > -1;+ //#region inner horizontal border+ if (horizontal) {+ forEach(range, (row, column) => {+ const mergedRange = worksheet.getMergedCell(row, column);+ if (mergedRange) {+ const topLeftStyle = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;- const bc_tr = type.indexOf('bctr') > -1;+ 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) },+ },+ });+ }+ } 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) },+ },+ });+ }- const range = selections[0]; // TODO @Dushusir support multiple ranges+ 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) },+ },+ });+ }+ }+ });+ }+ //#endregion+};- // Cells in the surrounding range may need to clear the border- const topRangeOut = {- startRow: range.startRow - 1,- startColumn: range.startColumn,- endRow: range.startRow - 1,- endColumn: range.endColumn,- };+function otherBorders(borderContext: ReturnType<typeof getBorderContext>) {+ const { borderStyle, tl_br, tl_bc, tl_mr, bl_tr, ml_tr, bc_tr } = borderContext;- const leftRangeOut = {- startRow: range.startRow,- startColumn: range.startColumn - 1,- endRow: range.endRow,- endColumn: range.startColumn - 1,- };+ const setBorderStyle = (range: IRange, defaultStyle: IBorderData, reserve?: boolean) => {+ setBorderStyleForRange(borderContext, range, defaultStyle, reserve);+ };- const bottomRangeOut = {- startRow: range.endRow + 1,- startColumn: range.startColumn,- endRow: range.endRow + 1,- endColumn: range.endColumn,- };+ if (tl_br) {+ setBorderStyle(borderContext.range, { tl_br: Tools.deepClone(borderStyle) }, true);+ }- const rightRangeOut = {- startRow: range.startRow,- startColumn: range.endColumn + 1,- endRow: range.endRow,- endColumn: range.endColumn + 1,- };+ if (tl_bc) {+ setBorderStyle(borderContext.range, { tl_bc: Tools.deepClone(borderStyle) }, true);+ }- // Cells in the upper, lower, left and right ranges- const topRange = {- startRow: range.startRow,- startColumn: range.startColumn,- endRow: range.startRow,- endColumn: range.endColumn,- };+ if (tl_mr) {+ setBorderStyle(borderContext.range, { tl_mr: Tools.deepClone(borderStyle) }, true);+ }- const leftRange = {- startRow: range.startRow,- startColumn: range.startColumn,- endRow: range.endRow,- endColumn: range.startColumn,- };+ if (bl_tr) {+ setBorderStyle(borderContext.range, { bl_tr: Tools.deepClone(borderStyle) }, true);+ }- const bottomRange = {- startRow: range.endRow,- startColumn: range.startColumn,- endRow: range.endRow,- endColumn: range.endColumn,- };+ if (ml_tr) {+ setBorderStyle(borderContext.range, { ml_tr: Tools.deepClone(borderStyle) }, true);+ }- const rightRange = {- startRow: range.startRow,- startColumn: range.endColumn,- endRow: range.endRow,- endColumn: range.endColumn,- };+ if (bc_tr) {+ setBorderStyle(borderContext.range, { bc_tr: Tools.deepClone(borderStyle) }, true);+ }+}- const mr = new ObjectMatrix<ICellData>();+const outlineBorder = (borderContext: ReturnType<typeof getBorderContext>) => {+ const { top, left, right, bottom, borderStyle, bottomRange, topRange, leftRange, rightRange, bottomRangeOut, topRangeOut, leftRangeOut, rightRangeOut } = borderContext;+ const setBorderStyle = (range: IRange, defaultStyle: IBorderData, reserve?: boolean) => {+ setBorderStyleForRange(borderContext, range, defaultStyle, reserve);+ };+ if (top) {+ /**+ * pro/issues/344+ * Compatible with Excel's border rendering.+ * When the top border of a cell and the bottom border of the cell above it (r-1) overlap,+ * if the top border of cell r is white, then the rendering is ignored.+ */+ setBorderStyle(topRangeOut, { b: null });+ setBorderStyle(topRange, { t: Tools.deepClone(borderStyle) }, true);+ }- const border: IBorderStyleData = {- s: style,- cl: {- rgb: color,- },- };+ if (bottom) {+ //see univer-pro/issues/2561+ // add bottom line for merged cell+ // must put below inner horizontal border, horizontal border would clear right border when using topleftBorderStyle+ setBorderStyle(bottomRangeOut, { t: null });+ setBorderStyle(bottomRange, { b: Tools.deepClone(borderStyle) }, true);+ }+ if (left) {+ setBorderStyle(leftRangeOut, { r: null });+ setBorderStyle(leftRange, { l: Tools.deepClone(borderStyle) }, true);+ }+ if (right) {+ //see univer-pro/issues/2561+ // add bottom line for merged cell+ // must put below inner vertical border, vertical border would clear right border when using topleftBorderStyle+ setBorderStyle(rightRangeOut, { l: null });+ setBorderStyle(rightRange, { r: Tools.deepClone(borderStyle) }, true);+ }+};- function setBorderStyle(range: IRange, defaultStyle: IBorderData, reserve?: boolean) {- if (range.startRow < 0 || range.startColumn < 0) return;- forEach(range, (row, column) => {- const rectangle = worksheet.getMergedCell(row, column);-- let bdStyle = defaultStyle;-- if (rectangle && (defaultStyle.bc_tr || defaultStyle.ml_tr || defaultStyle.bl_tr || defaultStyle.tl_mr || defaultStyle.tl_bc || defaultStyle.tl_br)) {- if (reserve) {- const style = Tools.deepClone(- mr.getValue(rectangle.startRow, rectangle.startColumn)?.s- ) as IStyleData;- bdStyle = style?.bd ? Object.assign(style.bd, defaultStyle) : defaultStyle;- }- mr.setValue(rectangle.startRow, rectangle.startColumn, {+// 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;++ const setBorderStyle = (range: IRange, defaultStyle: IBorderData, reserve?: boolean) => {+ setBorderStyleForRange(borderContext, range, defaultStyle, reserve);+ };+ if (+ !top &&+ !bottom &&+ !left &&+ !right &&+ !vertical &&+ !horizontal &&+ !tl_br &&+ !tl_bc &&+ !tl_mr &&+ !bl_tr &&+ !ml_tr &&+ !bc_tr+ ) {+ // eslint-disable-next-line max-lines-per-function, complexity+ forEach(range, (row, column) => {+ const mergedRange = worksheet.getMergedCell(row, column);+ if (mergedRange) {+ // Clear the right border of all columns except the last column+ if (mergedRange.endColumn !== range.endColumn) {+ const style = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;+ mr.setValue(row, column, {s: {- bd: bdStyle,+ bd: style?.bd ? Object.assign(style.bd, { r: null }) : { r: null },},});- } else {- if (reserve) {- const style = Tools.deepClone(mr.getValue(row, column)?.s) as IStyleData;- bdStyle = style?.bd ? Object.assign(style.bd, defaultStyle) : defaultStyle;- }- mr.setValue(row, column, { s: { bd: bdStyle } });}- });- }-- if (top) {- /**- * pro/issues/344- * Compatible with Excel's border rendering.- * When the top border of a cell and the bottom border of the cell above it (r-1) overlap,- * if the top border of cell r is white, then the rendering is ignored.- */- setBorderStyle(topRangeOut, { b: null });- setBorderStyle(topRange, { t: Tools.deepClone(border) }, true);- }- if (bottom) {- setBorderStyle(bottomRangeOut, { t: null });- setBorderStyle(bottomRange, { b: Tools.deepClone(border) }, true);- }- if (left) {- setBorderStyle(leftRangeOut, { r: null });- setBorderStyle(leftRange, { l: Tools.deepClone(border) }, true);- }- if (right) {- setBorderStyle(rightRangeOut, { l: null });- setBorderStyle(rightRange, { r: Tools.deepClone(border) }, true);- }-- if (tl_br) {- setBorderStyle(range, { tl_br: Tools.deepClone(border) }, true);- }-- if (tl_bc) {- setBorderStyle(range, { tl_bc: Tools.deepClone(border) }, true);- }+ // Clear the left border of all columns except the first column+ 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 },+ },+ });+ }+ // Clear all the bottom border except the last line+ // see https://github.com/dream-num/univer/pull/3506+ // this is not right!! why set not last row to topleft cell border? why?+ // topleft border doesn't have { right: null }+ // after exec this, the endColumn lost { right: null } when clear border.+ 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 },+ },+ });+ }+ // Clear the top border of all lines except the first line+ 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 },+ },+ });+ }+ }+ });- if (tl_mr) {- setBorderStyle(range, { tl_mr: Tools.deepClone(border) }, true);- }+ setBorderStyle(topRangeOut, { b: null });+ setBorderStyle(topRange, { t: null }, true);+ setBorderStyle(bottomRangeOut, { t: null });+ setBorderStyle(bottomRange, { b: null }, true);+ setBorderStyle(leftRangeOut, { r: null });+ setBorderStyle(leftRange, { l: null }, true);+ setBorderStyle(rightRangeOut, { l: null });+ setBorderStyle(rightRange, { r: null }, true);++ setBorderStyle(range, { tl_br: null }, true);+ setBorderStyle(range, { tl_bc: null }, true);+ setBorderStyle(range, { tl_mr: null }, true);+ setBorderStyle(range, { bl_tr: null }, true);+ setBorderStyle(range, { ml_tr: null }, true);+ setBorderStyle(range, { bc_tr: null }, true);+ }+};+/**+ * Set border info for range, including clear border (type = NONE)+ */+export const SetBorderCommand: ICommand = {+ id: 'sheet.command.set-border',+ type: CommandType.COMMAND,- if (bl_tr) {- setBorderStyle(range, { bl_tr: Tools.deepClone(border) }, true);- }+ handler: async (accessor: IAccessor, params?: ISetBorderCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const selectionManagerService = accessor.get(SheetsSelectionsService);+ const borderStyleManagerService = accessor.get(BorderStyleManagerService);- if (ml_tr) {- setBorderStyle(range, { ml_tr: Tools.deepClone(border) }, true);- }+ const target = getSheetCommandTarget(univerInstanceService, params);+ if (!target) return false;- if (bc_tr) {- setBorderStyle(range, { bc_tr: Tools.deepClone(border) }, true);+ const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);+ if (!selections?.length) {+ return false;}- // inner vertical border- if (vertical) {- forEach(range, (row, column) => {- const rectangle = worksheet.getMergedCell(row, column);- if (rectangle) {- if (rectangle.endColumn !== range.endColumn) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;- mr.setValue(row, column, {- s: {- bd: style?.bd- ? Object.assign(style.bd, { r: Tools.deepClone(border) })- : { r: Tools.deepClone(border) },- },- });- }-- if (rectangle.startColumn !== range.startColumn) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;- mr.setValue(row, column, {- s: {- bd: style?.bd- ? Object.assign(style.bd, { l: Tools.deepClone(border) })- : { l: Tools.deepClone(border) },- },- });- }- } 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(border) })- : { r: Tools.deepClone(border) },- },- });- }-- 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(border) })- : { l: Tools.deepClone(border) },- },- });- }- }- });- }- // inner horizontal border- if (horizontal) {- forEach(range, (row, column) => {- const rectangle = worksheet.getMergedCell(row, column);- if (rectangle) {- if (rectangle.endRow !== range.endRow) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;- mr.setValue(row, column, {- s: {- bd: style?.bd- ? Object.assign(style.bd, { b: Tools.deepClone(border) })- : { b: Tools.deepClone(border) },- },- });- }-- if (rectangle.startRow !== range.startRow) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;- mr.setValue(row, column, {- s: {- bd: style?.bd- ? Object.assign(style.bd, { t: Tools.deepClone(border) })- : { t: Tools.deepClone(border) },- },- });- }- } 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(border) })- : { b: Tools.deepClone(border) },- },- });- }-- 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(border) })- : { t: Tools.deepClone(border) },- },- });- }- }- });- }+ const { activeBorderType } = borderStyleManagerService.getBorderInfo();+ if (!activeBorderType) return false;- // clear- if (- !top &&- !bottom &&- !left &&- !right &&- !vertical &&- !horizontal &&- !tl_br &&- !tl_bc &&- !tl_mr &&- !bl_tr &&- !ml_tr &&- !bc_tr- ) {- setBorderStyle(topRangeOut, { b: null });- setBorderStyle(topRange, { t: null }, true);- setBorderStyle(bottomRangeOut, { t: null });- setBorderStyle(bottomRange, { b: null }, true);- setBorderStyle(leftRangeOut, { r: null });- setBorderStyle(leftRange, { l: null }, true);- setBorderStyle(rightRangeOut, { l: null });- setBorderStyle(rightRange, { r: null }, true);-- setBorderStyle(range, { tl_br: null }, true);- setBorderStyle(range, { tl_bc: null }, true);- setBorderStyle(range, { tl_mr: null }, true);- setBorderStyle(range, { bl_tr: null }, true);- setBorderStyle(range, { ml_tr: null }, true);- setBorderStyle(range, { bc_tr: null }, true);-- forEach(range, (row, column) => {- const rectangle = worksheet.getMergedCell(row, column);- if (rectangle) {- // Clear the right border of all columns except the last column- if (rectangle.endColumn !== range.endColumn) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.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 (rectangle.startColumn !== range.startColumn) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.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 (rectangle.endRow !== range.endRow) {- const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.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 (rectangle.startRow !== range.startRow) {- const style = mr.getValue(rectangle.startRow, rectangle.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 borderContext = getBorderContext(borderStyleManagerService, target, selections);+ innerBorder(borderContext);+ outlineBorder(borderContext);+ otherBorders(borderContext);+ clearBorder(borderContext);+ const { unitId, subUnitId, mr } = borderContext;const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {unitId,subUnitId,@@ -556,3 +589,58 @@ export const SetBorderCommand: ICommand = {return false;},};++export const SetBorderPositionCommand: ICommand<ISetBorderPositionCommandParams> = {+ id: 'sheet.command.set-border-position',+ type: CommandType.COMMAND,+ handler: async (accessor: IAccessor, params: ISetBorderPositionCommandParams) => {+ if (!params.value) return false;+ const commandService = accessor.get(ICommandService);+ const borderManager = accessor.get(BorderStyleManagerService);+ borderManager.setType(params.value);+ return commandService.executeCommand(SetBorderCommand.id);+ },+};++export const SetBorderStyleCommand: ICommand = {+ id: 'sheet.command.set-border-style',+ type: CommandType.COMMAND,+ handler: async (accessor: IAccessor, params: ISetBorderStyleCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const borderManager = accessor.get(BorderStyleManagerService);+ borderManager.setStyle(params.value);+ return commandService.executeCommand(SetBorderCommand.id);+ },+};++export const SetBorderColorCommand: ICommand<ISetBorderColorCommandParams> = {+ id: 'sheet.command.set-border-color',+ type: CommandType.COMMAND,+ handler: async (accessor: IAccessor, params: ISetBorderColorCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const borderManager = accessor.get(BorderStyleManagerService);+ borderManager.setColor(params.value);+ return commandService.executeCommand(SetBorderCommand.id);+ },+};++export const SetBorderBasicCommand: ICommand<ISetBorderBasicCommandParams> = {+ id: 'sheet.command.set-border-basic',+ type: CommandType.COMMAND,+ handler: async (accessor: IAccessor, params: ISetBorderBasicCommandParams) => {+ const { unitId, subUnitId, value } = params;+ const { type, color, style } = value;++ const commandService = accessor.get(ICommandService);+ const borderManager = accessor.get(BorderStyleManagerService);++ borderManager.setType(type);+ if (color) borderManager.setColor(color);+ borderManager.setStyle(style);++ return commandService.executeCommand(SetBorderCommand.id, {+ unitId,+ subUnitId,+ });+ },+};diff --git a/packages/sheets/src/commands/commands/utils/target-util.ts b/packages/sheets/src/commands/commands/utils/target-util.tsindex 043be86efe..df6294d866 100644--- a/packages/sheets/src/commands/commands/utils/target-util.ts+++ b/packages/sheets/src/commands/commands/utils/target-util.ts@@ -33,7 +33,7 @@ export function getSheetCommandTargetWorkbook(univerInstanceService: IUniverInst};}-interface IResult {+export interface IResult {workbook: Workbook;worksheet: Worksheet;unitId: string;