Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheet): border excel compatibility #1539

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions examples/src/data/sheets/demo/default-workbook-data-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13811,6 +13811,62 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
},
},
},
uJSelZ11: {
bd: {
r: {
s: 1,
cl: {
rgb: 'rgb(0,0,0)',
},
},
b: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 1,
},
t: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 1,
},
l: {
cl: {
rgb: 'rgb(0,0,0)',
},
s: 1,
},
},
},
uJSelZ22: {
bd: {
r: {
s: 1,
cl: {
rgb: 'rgb(255,255,255)',
},
},
b: {
cl: {
rgb: 'rgb(255,255,255)',
},
s: 1,
},
t: {
cl: {
rgb: 'rgb(255,255,255)',
},
s: 1,
},
l: {
cl: {
rgb: 'rgb(255,255,255)',
},
s: 1,
},
},
},
},
appVersion: '3.0.0-alpha',
sheets: {
Expand All @@ -13829,6 +13885,34 @@ export const DEFAULT_WORKBOOK_DATA_DEMO: IWorkbookData = {
v: 'A Schedule of Items',
},
},
5: {
5: {
s: 'uJSelZ11',
},
6: {
s: 'uJSelZ11',
},
7: {
s: 'uJSelZ11',
},
8: {
s: 'uJSelZ11',
},
},
6: {
5: {
s: 'uJSelZ22',
},
6: {
s: 'uJSelZ22',
},
7: {
s: 'uJSelZ22',
},
8: {
s: 'uJSelZ22',
},
},
},
freeze: {
xSplit: 0,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,6 @@ export { textEncoder } from './services/snapshot/snapshot-utils';
export { type ILogContext } from './services/log/context';
export { b64DecodeUnicode, b64EncodeUnicode } from './shared/coder';

export { isBlackColor, isWhiteColor } from './shared/color/color-kit';

installShims();
61 changes: 61 additions & 0 deletions packages/core/src/shared/color/color-kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,3 +854,64 @@ const rgb2Hsv: (color: IRgbColor) => IHsvColor = (color) => {
const isUndefinedOrNull = (value: unknown): value is null | undefined => value == null;

const isObject = (value: unknown): value is object => !isUndefinedOrNull(value) && typeof value === 'object';

export function isBlackColor(color: string): boolean {
// Regular expressions match different color formats.
const hexRegex = /^#(?:[0]{3}|[0]{6})\b/;
const rgbRegex = /^rgb\s*\(\s*0+\s*,\s*0+\s*,\s*0+\s*\)$/;
const rgbaRegex = /^rgba\s*\(\s*0+\s*,\s*0+\s*,\s*0+\s*,\s*(1|1\.0*|0?\.\d+)\)$/;
const hslRegex = /^hsl\s*\(\s*0*\s*,\s*0%*\s*,\s*0%*\s*\)$/;
const hslaRegex = /^hsla\s*\(\s*0*\s*,\s*0%*\s*,\s*0%*\s*,\s*(1|1\.0*|0?\.\d+)\)$/;

if (hexRegex.test(color)) {
return true;
}
if (rgbRegex.test(color)) {
return true;
}

if (rgbaRegex.test(color)) {
return true;
}

if (hslRegex.test(color)) {
return true;
}

if (hslaRegex.test(color)) {
return true;
}

return false;
}

export function isWhiteColor(color: string): boolean {
// Regular expressions match different color formats.
const hexRegex = /^#(?:[Ff]{3}|[Ff]{6})\b/;
const rgbRegex = /^rgb\s*\(\s*255\s*,\s*255\s*,\s*255\s*\)$/;
const rgbaRegex = /^rgba\s*\(\s*255\s*,\s*255\s*,\s*255\s*,\s*(1|1\.0*|0?\.\d+)\)$/;
const hslRegex = /^hsl\s*\(\s*0*\s*,\s*0%*\s*,\s*100%*\s*\)$/;
const hslaRegex = /^hsla\s*\(\s*0*\s*,\s*0%*\s*,\s*100%*\s*,\s*(1|1\.0*|0?\.\d+)\)$/;

if (hexRegex.test(color)) {
return true;
}

if (rgbRegex.test(color)) {
return true;
}

if (rgbaRegex.test(color)) {
return true;
}

if (hslRegex.test(color)) {
return true;
}

if (hslaRegex.test(color)) {
return true;
}

return false;
}
19 changes: 19 additions & 0 deletions packages/engine-render/src/components/sheets/sheet-skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
HorizontalAlign,
isEmptyCell,
isNullCell,
isWhiteColor,
ObjectMatrix,
searchArray,
Tools,
Expand Down Expand Up @@ -1802,6 +1803,24 @@ export class SpreadsheetSkeleton extends Skeleton {
borderCache.setValue(r, c, { [type]: {} });
}

/**
* https://github.com/dream-num/univer-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.
*/
if (type === BORDER_TYPE.TOP) {
const borderBottom = borderCache.getValue(r - 1, c)?.[BORDER_TYPE.BOTTOM];
if (borderBottom && isWhiteColor(rgb)) {
return true;
}
} else if (type === BORDER_TYPE.LEFT) {
const borderRight = borderCache.getValue(r, c - 1)?.[BORDER_TYPE.RIGHT];
if (borderRight && isWhiteColor(rgb)) {
return true;
}
}

borderCache.getValue(r, c)![type] = {
type,
style: props.s,
Expand Down
14 changes: 10 additions & 4 deletions packages/sheets/src/commands/commands/set-border-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,19 +290,25 @@ export const SetBorderCommand: ICommand = {
}

if (top) {
//setBorderStyle(topRangeOut, { b: null });
/**
* https://github.com/dream-num/univer-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(bottomRangeOut, { t: null });
setBorderStyle(bottomRange, { b: Tools.deepClone(border) }, true);
}
if (left) {
//setBorderStyle(leftRangeOut, { r: null });
setBorderStyle(leftRangeOut, { r: null });
setBorderStyle(leftRange, { l: Tools.deepClone(border) }, true);
}
if (right) {
//setBorderStyle(rightRangeOut, { l: null });
setBorderStyle(rightRangeOut, { l: null });
setBorderStyle(rightRange, { r: Tools.deepClone(border) }, true);
}

Expand Down
Loading