| 1 |
//@ts-ignore |
| 2 |
import { omitBy, isUndefined, trim, isEmpty, isNumber } from "lodash"; |
| 3 |
import { getBorderVariablesCss, getSpacingCss } from "./utils/styling-helpers"; |
| 4 |
import { PaddingTypes, TablebergBlockAttrs } from "./types"; |
| 5 |
|
| 6 |
export function getStyles(attributes: TablebergBlockAttrs) { |
| 7 |
const { |
| 8 |
cellPadding, |
| 9 |
enableInnerBorder, |
| 10 |
evenRowBackgroundColor, |
| 11 |
innerBorder, |
| 12 |
headerBackgroundColor, |
| 13 |
oddRowBackgroundColor, |
| 14 |
tableBorder, |
| 15 |
headerBackgroundGradient, |
| 16 |
evenRowBackgroundGradient, |
| 17 |
oddRowBackgroundGradient, |
| 18 |
tableWidth, |
| 19 |
footerBackgroundColor, |
| 20 |
footerBackgroundGradient, |
| 21 |
enableTableFooter, |
| 22 |
tableAlignment, |
| 23 |
enableTableHeader, |
| 24 |
} = attributes; |
| 25 |
const cellPaddingCSS: PaddingTypes = getSpacingCss(cellPadding); |
| 26 |
const tableBorderVar = getBorderVariablesCss(tableBorder, "table"); |
| 27 |
const tableInnerBorder = enableInnerBorder |
| 28 |
? getBorderVariablesCss(innerBorder, "inner") |
| 29 |
: {}; |
| 30 |
|
| 31 |
let styles = { |
| 32 |
"--tableber-table-width": tableWidth, |
| 33 |
"--tableberg-footer-bg-color": !isEmpty(footerBackgroundColor) |
| 34 |
? footerBackgroundColor |
| 35 |
: footerBackgroundGradient, |
| 36 |
"--tableberg-header-bg-color": !isEmpty(headerBackgroundColor) |
| 37 |
? headerBackgroundColor |
| 38 |
: headerBackgroundGradient, |
| 39 |
"--tableberg-even-row-bg-color": !isEmpty(evenRowBackgroundColor) |
| 40 |
? evenRowBackgroundColor |
| 41 |
: evenRowBackgroundGradient, |
| 42 |
"--tableberg-odd-row-bg-color": !isEmpty(oddRowBackgroundColor) |
| 43 |
? oddRowBackgroundColor |
| 44 |
: oddRowBackgroundGradient, |
| 45 |
"--tableberg-cell-padding-top": cellPaddingCSS?.top, |
| 46 |
"--tableberg-cell-padding-right": cellPaddingCSS?.right, |
| 47 |
"--tableberg-cell-padding-bottom": cellPaddingCSS?.bottom, |
| 48 |
"--tableberg-cell-padding-left": cellPaddingCSS?.left, |
| 49 |
...tableBorderVar, |
| 50 |
...tableInnerBorder, |
| 51 |
}; |
| 52 |
|
| 53 |
return omitBy( |
| 54 |
styles, |
| 55 |
(value: any) => |
| 56 |
value === false || |
| 57 |
isEmpty(value) || |
| 58 |
isUndefined(value) || |
| 59 |
trim(value) === "" || |
| 60 |
trim(value) === "undefined undefined undefined" |
| 61 |
); |
| 62 |
} |
| 63 |
|