| 1 |
import { cloneDeep, merge } from 'lodash'; |
| 2 |
|
| 3 |
import camelCaseToDash from '../../../utils/camel-case-to-dash'; |
| 4 |
|
| 5 |
function fixStylesPropNames(styles) { |
| 6 |
if (typeof styles === 'object') { |
| 7 |
const clonedStyles = cloneDeep(styles); |
| 8 |
|
| 9 |
const result = {}; |
| 10 |
|
| 11 |
Object.keys(clonedStyles).forEach((k) => { |
| 12 |
result[camelCaseToDash(k)] = fixStylesPropNames(clonedStyles[k]); |
| 13 |
}); |
| 14 |
|
| 15 |
return result; |
| 16 |
} |
| 17 |
|
| 18 |
return styles; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Migrate Custom Styles. |
| 23 |
* |
| 24 |
* @param props |
| 25 |
*/ |
| 26 |
export default function migrate(props) { |
| 27 |
const { attributes } = props; |
| 28 |
const { |
| 29 |
ghostkitId, |
| 30 |
ghostkitClassname, |
| 31 |
ghostkitStyles, |
| 32 |
ghostkitCustomCSS, |
| 33 |
ghostkitPosition, |
| 34 |
ghostkitSpacings, |
| 35 |
ghostkitFrame, |
| 36 |
} = attributes; |
| 37 |
|
| 38 |
const result = {}; |
| 39 |
|
| 40 |
// Migration to new attribute. |
| 41 |
if ( |
| 42 |
ghostkitId || |
| 43 |
ghostkitClassname || |
| 44 |
ghostkitStyles || |
| 45 |
ghostkitCustomCSS || |
| 46 |
ghostkitPosition || |
| 47 |
ghostkitSpacings || |
| 48 |
ghostkitFrame |
| 49 |
) { |
| 50 |
// Clean old attributes. |
| 51 |
result.ghostkitId = undefined; |
| 52 |
result.ghostkitClassname = undefined; |
| 53 |
result.ghostkitStyles = undefined; |
| 54 |
result.ghostkitCustomCSS = undefined; |
| 55 |
result.ghostkitPosition = undefined; |
| 56 |
result.ghostkitSpacings = undefined; |
| 57 |
result.ghostkitFrame = undefined; |
| 58 |
|
| 59 |
const ghostkitData = cloneDeep(attributes?.ghostkit || {}); |
| 60 |
|
| 61 |
// ID. |
| 62 |
if (ghostkitId && !ghostkitData?.id) { |
| 63 |
ghostkitData.id = ghostkitId; |
| 64 |
} |
| 65 |
|
| 66 |
// Custom CSS. |
| 67 |
if (ghostkitCustomCSS && !ghostkitData?.styles?.custom) { |
| 68 |
if (typeof ghostkitData?.styles === 'undefined') { |
| 69 |
ghostkitData.styles = {}; |
| 70 |
} |
| 71 |
|
| 72 |
ghostkitData.styles.custom = ghostkitCustomCSS; |
| 73 |
} |
| 74 |
|
| 75 |
// Styles. |
| 76 |
if (ghostkitStyles && Object.keys(ghostkitStyles).length) { |
| 77 |
if (typeof ghostkitData?.styles === 'undefined') { |
| 78 |
ghostkitData.styles = {}; |
| 79 |
} |
| 80 |
|
| 81 |
Object.keys(ghostkitStyles).forEach((k) => { |
| 82 |
ghostkitData.styles = merge( |
| 83 |
ghostkitData.styles, |
| 84 |
ghostkitStyles[k] |
| 85 |
); |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
// Convert styles props to dash-case. |
| 90 |
if (ghostkitData.styles) { |
| 91 |
ghostkitData.styles = fixStylesPropNames(ghostkitData.styles); |
| 92 |
} |
| 93 |
|
| 94 |
result.ghostkit = ghostkitData; |
| 95 |
} |
| 96 |
|
| 97 |
return result; |
| 98 |
} |
| 99 |
|