Last active
November 23, 2017 14:07
-
-
Save long-lazuli/087c910c08b1fe9418d41e99b6eb8ced to your computer and use it in GitHub Desktop.
JSX to CKEditor5 Template
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
// Use `vdom` classic JSX syntax for CK templates. | |
import Template from '@ckeditor/ckeditor5-ui/src/template'; | |
const CKTemplateFromJSX = ({elementName, attributes, children}) => new Template({ | |
tag: elementName, | |
attributes: Object.keys(attributes).filter( key => { | |
return key.indexOf('on') !== 0 | |
}).reduce((acc, key) => { | |
let ckKey = key; | |
if( key == "className" ) ckKey = 'class' | |
if( key == "htmlFor" ) ckKey = 'for' | |
acc[ckKey] = attributes[key] | |
return acc | |
}, {}), | |
on: Object.keys(attributes).filter( key => { | |
return key.indexOf('on') === 0 | |
}).reduce( (acc, key) => { | |
const ckKey = key.slice(2).toLowerCase(); | |
acc[ckKey] = attributes[key] | |
return acc | |
}, {}), | |
children: children | |
}) | |
export default CKTemplateFromJSX |
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
// Using https://www.npmjs.com/package/babel-plugin-transform-jsx : | |
use: { | |
loader: 'babel-loader', | |
options: { | |
plugins: [ | |
["transform-jsx", { "function": "CKTemplateFromJSX" }] | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment