Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Created August 18, 2022 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BananaAcid/16fba770db2055da6ed21bdcff562fee to your computer and use it in GitHub Desktop.
Save BananaAcid/16fba770db2055da6ed21bdcff562fee to your computer and use it in GitHub Desktop.
Load css & script with promises and wait till loaded
let add = (tag, url) => new Promise( (resolve) => {
let el = document.createElement(tag);
if (tag === 'link') {
el.rel = 'stylesheet';
el.href = url;
}
else if (tag === 'script') {
el.src = url;
}
el.addEventListener('load', resolve);
document.head.appendChild(el);
});
Promise.all([
add('script', '//cdn.ckeditor.com/ckeditor5/35.0.1/classic/ckeditor.js'),
])
.then(async _=> {
// init ckeditor
let instance = await (window as any).ClassicEditor
.create(document.querySelector( '#editor' ) );
// update orig textarea on change, for form submit
instance.model.document.on('change', function(){ instance.updateSourceElement(); });
});
/*
<body>
<textarea id="editor"></textarea>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment