ice.fo.utils
Version:
65 lines (57 loc) • 1.59 kB
JavaScript
import { getValue } from 'ice.fo.utils/GetterUtils'
/**
* Convert text style options to inline style object
*
* Ex: { fontSize: 16, fontUnit: 'px', defaultColor: '#000', align: 'center', option: 'bold' }
* => { fontSize: '16px', color: '#000', textAlign: 'center', fontWeight: 'bold' }
*
* @param textStyle
* @returns {{fontFamily: *, color: null, textAlign: *, lineHeight: null}|{}}
*/
export function convertTextInlineStyle (textStyle) {
if (!textStyle) { return {} }
const {
fontSize,
fontUnit,
letterSpacing,
letterSpacingUnit,
lineSpacing,
lineSpacingUnit,
defaultColor,
option,
fontFamily,
align,
} = textStyle
const styles = {
fontFamily: getValue(fontFamily, null),
color: defaultColor || null,
textAlign: getValue(align, null),
}
if (fontSize && Number(fontSize) > 0) {
styles.fontSize = `${fontSize}${getValue(fontUnit, 'px')}`
}
if (lineSpacing && Number(lineSpacing) > 0) {
styles.lineHeight = `${lineSpacing}${getValue(lineSpacingUnit, 'px')}`
}
if (letterSpacing && Number(letterSpacing) > 0) {
styles.letterSpacing = `${letterSpacing}${getValue(letterSpacingUnit, 'px')}`
}
const fontOption = getValue(option)
switch (fontOption) {
case 'bold':
styles.fontWeight = fontOption
break
case 'italic':
styles.fontStyle = fontOption
break
case 'underline':
styles.textDecoration = fontOption
break
case 'strikethrough':
styles.textDecoration = 'line-through'
break
default:
break
}
return styles
}