Created
August 18, 2022 12:56
-
-
Save BananaAcid/16fba770db2055da6ed21bdcff562fee to your computer and use it in GitHub Desktop.
Load css & script with promises and wait till loaded
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
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