Skip to content

Commit

Permalink
fix: border for merged cell
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Sep 20, 2024
1 parent a697a49 commit 9eb80a2
Showing 1 changed file with 73 additions and 68 deletions.
141 changes: 73 additions & 68 deletions packages/sheets/src/commands/commands/set-border-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

/* eslint-disable max-lines-per-function */
/* eslint-disable complexity */
import {
BorderType,
CommandType,
Expand All @@ -23,6 +25,7 @@ import {
ObjectMatrix,
Tools,
} from '@univerjs/core';

import type {
BorderStyleTypes,
IAccessor,
Expand All @@ -33,7 +36,6 @@ 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';
Expand Down Expand Up @@ -233,7 +235,7 @@ export const SetBorderCommand: ICommand = {

const mr = new ObjectMatrix<ICellData>();

const border: IBorderStyleData = {
const borderStyle: IBorderStyleData = {
s: style,
cl: {
rgb: color,
Expand Down Expand Up @@ -277,68 +279,70 @@ export const SetBorderCommand: ICommand = {
* 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);
setBorderStyle(topRange, { t: Tools.deepClone(borderStyle) }, true);
}

if (bottom) {
setBorderStyle(bottomRangeOut, { t: null });
setBorderStyle(bottomRange, { b: Tools.deepClone(border) }, true);
setBorderStyle(bottomRange, { b: Tools.deepClone(borderStyle) }, true);
}
if (left) {
setBorderStyle(leftRangeOut, { r: null });
setBorderStyle(leftRange, { l: Tools.deepClone(border) }, true);
setBorderStyle(leftRange, { l: Tools.deepClone(borderStyle) }, true);
}
if (right) {
setBorderStyle(rightRangeOut, { l: null });
setBorderStyle(rightRange, { r: Tools.deepClone(border) }, true);
setBorderStyle(rightRange, { r: Tools.deepClone(borderStyle) }, true);
}

if (tl_br) {
setBorderStyle(range, { tl_br: Tools.deepClone(border) }, true);
setBorderStyle(range, { tl_br: Tools.deepClone(borderStyle) }, true);
}

if (tl_bc) {
setBorderStyle(range, { tl_bc: Tools.deepClone(border) }, true);
setBorderStyle(range, { tl_bc: Tools.deepClone(borderStyle) }, true);
}

if (tl_mr) {
setBorderStyle(range, { tl_mr: Tools.deepClone(border) }, true);
setBorderStyle(range, { tl_mr: Tools.deepClone(borderStyle) }, true);
}

if (bl_tr) {
setBorderStyle(range, { bl_tr: Tools.deepClone(border) }, true);
setBorderStyle(range, { bl_tr: Tools.deepClone(borderStyle) }, true);
}

if (ml_tr) {
setBorderStyle(range, { ml_tr: Tools.deepClone(border) }, true);
setBorderStyle(range, { ml_tr: Tools.deepClone(borderStyle) }, true);
}

if (bc_tr) {
setBorderStyle(range, { bc_tr: Tools.deepClone(border) }, true);
setBorderStyle(range, { bc_tr: Tools.deepClone(borderStyle) }, true);
}

// 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;
const mergedRange = worksheet.getMergedCell(row, column);
if (mergedRange) {
// see univer-pro/issues/2561
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: Tools.deepClone(border) })
: { r: Tools.deepClone(border) },
? Object.assign(style.bd, { r: Tools.deepClone(borderStyle) })
: { r: Tools.deepClone(borderStyle) },
},
});
}

if (rectangle.startColumn !== range.startColumn) {
const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;
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: Tools.deepClone(border) })
: { l: Tools.deepClone(border) },
? Object.assign(style.bd, { l: Tools.deepClone(borderStyle) })
: { l: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -348,8 +352,8 @@ export const SetBorderCommand: ICommand = {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { r: Tools.deepClone(border) })
: { r: Tools.deepClone(border) },
? Object.assign(style.bd, { r: Tools.deepClone(borderStyle) })
: { r: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -359,8 +363,8 @@ export const SetBorderCommand: ICommand = {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { l: Tools.deepClone(border) })
: { l: Tools.deepClone(border) },
? Object.assign(style.bd, { l: Tools.deepClone(borderStyle) })
: { l: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -370,26 +374,27 @@ export const SetBorderCommand: ICommand = {
// 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;
const mergedRange = worksheet.getMergedCell(row, column);
if (mergedRange) {
const topLeftStyle = mr.getValue(mergedRange.startRow, mergedRange.startColumn)?.s as IStyleData;
//see univer-pro/issues/2561
// add bottom line for endrow
if (mergedRange.endRow === range.endRow) {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { b: Tools.deepClone(border) })
: { b: Tools.deepClone(border) },
bd: topLeftStyle?.bd
? Object.assign(topLeftStyle.bd, { b: Tools.deepClone(borderStyle) })
: { b: Tools.deepClone(borderStyle) },
},
});
}

if (rectangle.startRow !== range.startRow) {
const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;
if (mergedRange.startRow !== range.startRow) {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { t: Tools.deepClone(border) })
: { t: Tools.deepClone(border) },
bd: topLeftStyle?.bd
? Object.assign(topLeftStyle.bd, { t: Tools.deepClone(borderStyle) })
: { t: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -399,8 +404,8 @@ export const SetBorderCommand: ICommand = {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { b: Tools.deepClone(border) })
: { b: Tools.deepClone(border) },
? Object.assign(style.bd, { b: Tools.deepClone(borderStyle) })
: { b: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -410,8 +415,8 @@ export const SetBorderCommand: ICommand = {
mr.setValue(row, column, {
s: {
bd: style?.bd
? Object.assign(style.bd, { t: Tools.deepClone(border) })
: { t: Tools.deepClone(border) },
? Object.assign(style.bd, { t: Tools.deepClone(borderStyle) })
: { t: Tools.deepClone(borderStyle) },
},
});
}
Expand All @@ -434,55 +439,39 @@ export const SetBorderCommand: ICommand = {
!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) {
const mergedRange = worksheet.getMergedCell(row, column);
if (mergedRange) {
// 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;
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 },
},
});
}
// 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;
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
if (rectangle.endRow !== range.endRow) {
const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;
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 (rectangle.startRow !== range.startRow) {
const style = mr.getValue(rectangle.startRow, rectangle.startColumn)?.s as IStyleData;
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 },
Expand Down Expand Up @@ -528,6 +517,22 @@ export const SetBorderCommand: ICommand = {
}
}
});

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);
}

const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
Expand Down

0 comments on commit 9eb80a2

Please sign in to comment.