Last active
September 27, 2017 07:53
-
-
Save junkycoder/d52c477c6157f67a1cdf2d61a57779f0 to your computer and use it in GitHub Desktop.
Convert CSS to JS in Marketch panel
This file contains hidden or 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
// Can be used with Custom JavaScript for Websites | |
// https://chrome.google.com/webstore/detail/custom-javascript-for-web/poakhlngfciodnhlhhgnaaelnpjljija | |
// but is necessary to allow 'local URLs' in chrome://extensions/ | |
if (document.title.indexOf('Marketch') === 0) { | |
$('.artboard').on('mouseup', () => setTimeout(init, 100)); | |
} | |
function init () { | |
const $code = $('textarea[name="code"]'); | |
let styles = $code.text().split('\n').map(line => ( | |
line.replace(';', '').split(':') | |
)); | |
styles = styles.map(([prop, value]) => ([ | |
snakeToCamel(prop), | |
valueToJsValue(value) | |
])); | |
styles = styles.reduce((text, style) => ( | |
`${text}${style[0]}: ${style[1]},\n` | |
), ''); | |
$code.text(styles); | |
} | |
function snakeToCamel (s) { | |
return s.replace(/(\-\w)/g, function(m){return m[1].toUpperCase();}); | |
} | |
function valueToJsValue (value) { | |
if (isNumericValue(value)) { | |
return parseFloat(value); | |
} | |
return `'${value}'`; | |
} | |
function isNumericValue (value) { | |
return (new RegExp(/^-?[\d]+[px]?/)).test(value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment