forked from exceljs/exceljs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deep copy inherited style (exceljs#1850)
* Deep copy style when a row is inserted with inherited style (exceljs#1813) * Add tests for row's style * Move setIfExists * Fix equal function
- Loading branch information
Showing
4 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const oneDepthCopy = (obj, nestKeys) => ({ | ||
...obj, | ||
...nestKeys.reduce((memo, key) => { | ||
if (obj[key]) memo[key] = {...obj[key]}; | ||
return memo; | ||
}, {}), | ||
}); | ||
|
||
const setIfExists = (src, dst, key, nestKeys = []) => { | ||
if (src[key]) dst[key] = oneDepthCopy(src[key], nestKeys); | ||
}; | ||
|
||
const isEmptyObj = obj => Object.keys(obj).length === 0; | ||
|
||
const copyStyle = style => { | ||
if (!style) return style; | ||
if (isEmptyObj(style)) return {}; | ||
|
||
const copied = {...style}; | ||
|
||
setIfExists(style, copied, 'font', ['color']); | ||
setIfExists(style, copied, 'alignment'); | ||
setIfExists(style, copied, 'protection'); | ||
if (style.border) { | ||
setIfExists(style, copied, 'border'); | ||
setIfExists(style.border, copied.border, 'top', ['color']); | ||
setIfExists(style.border, copied.border, 'left', ['color']); | ||
setIfExists(style.border, copied.border, 'bottom', ['color']); | ||
setIfExists(style.border, copied.border, 'right', ['color']); | ||
setIfExists(style.border, copied.border, 'diagonal', ['color']); | ||
} | ||
|
||
if (style.fill) { | ||
setIfExists(style, copied, 'fill', ['fgColor', 'bgColor', 'center']); | ||
if (style.fill.stops) { | ||
copied.fill.stops = style.fill.stops.map(s => oneDepthCopy(s, ['color'])); | ||
} | ||
} | ||
|
||
return copied; | ||
}; | ||
|
||
exports.copyStyle = copyStyle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const testUtils = require('../../utils/index'); | ||
|
||
const {copyStyle} = verquire('utils/copy-style'); | ||
|
||
const style1 = { | ||
numFmt: testUtils.styles.numFmts.numFmt1, | ||
font: testUtils.styles.fonts.broadwayRedOutline20, | ||
alignment: testUtils.styles.namedAlignments.topLeft, | ||
border: testUtils.styles.borders.thickRainbow, | ||
fill: testUtils.styles.fills.redGreenDarkTrellis, | ||
}; | ||
const style2 = { | ||
fill: testUtils.styles.fills.rgbPathGrad, | ||
}; | ||
|
||
describe('copyStyle', () => { | ||
it('should copy a style deeply', () => { | ||
const copied = copyStyle(style1); | ||
expect(copied).to.deep.equal(style1); | ||
expect(copied.font).to.not.equal(style1.font); | ||
expect(copied.alignment).to.not.equal(style1.alignment); | ||
expect(copied.border).to.not.equal(style1.border); | ||
expect(copied.fill).to.not.equal(style1.fill); | ||
|
||
expect(copyStyle({})).to.deep.equal({}); | ||
}); | ||
|
||
it('should copy fill.stops deeply', () => { | ||
const copied = copyStyle(style2); | ||
expect(copied.fill.stops).to.deep.equal(style2.fill.stops); | ||
expect(copied.fill.stops).to.not.equal(style2.fill.stops); | ||
expect(copied.fill.stops[0]).to.not.equal(style2.fill.stops[0]); | ||
}); | ||
|
||
it('should return the argument if a falsy value passed', () => { | ||
expect(copyStyle(null)).to.equal(null); | ||
expect(copyStyle(undefined)).to.equal(undefined); | ||
}); | ||
}); |