Koenig - Fixed `.cleanup` when called during editor initialisation

refs https://github.com/TryGhost/Ghost/issues/9724
- we call `koenig.cleanup` when setting a post in the editor controller but the call will happen before `componentCards` has been populated so none of our "delete if empty" routines were being run
- calls to `.cleanup` now schedule the cleanup after the next editor render which should mean cards are populated before we try to remove them
This commit is contained in:
Kevin Ansfield 2018-08-10 17:31:32 +01:00
parent 72c600486c
commit fcc943fc26
1 changed files with 21 additions and 4 deletions

View File

@ -394,6 +394,10 @@ export default Component.extend({
this._startedRunLoop = false;
run.end();
}
if (this._cleanupScheduled) {
run.schedule('afterRender', this, this._cleanup);
}
});
editor.postDidChange(() => {
@ -624,16 +628,28 @@ export default Component.extend({
},
/* public interface ----------------------------------------------------- */
// TODO: find a better way to expose this?
// TODO: find a better way to expose the public interface?
// HACK: this scheduled cleanup is a bit hacky. We call .cleanup when
// initializing Koenig in our editor controller but we have to wait for
// rendering to finish so that componentCards is populated, even then
// it's unlikely the card.component registration has finished.
//
// TODO: see if there's a way we can perform cleanup directly on the
// mobiledoc, maybe with a "cleanupOnInit" option so that we modify the
// mobiledoc before we start rendering
cleanup() {
this._cleanupScheduled = true;
},
_cleanup() {
this.componentCards.forEach((card) => {
if (!card.koenigOptions.deleteIfEmpty) {
let shouldDelete = card.koenigOptions.deleteIfEmpty;
if (!shouldDelete) {
return;
}
let shouldDelete = card.koenigOptions.deleteIfEmpty;
if (typeof shouldDelete === 'string') {
let payloadKey = shouldDelete;
shouldDelete = card => isBlank(get(card, payloadKey));
@ -643,6 +659,7 @@ export default Component.extend({
this.deleteCard(card, NO_CURSOR_MOVEMENT);
}
});
this._cleanupScheduled = false;
},
/* mobiledoc event handlers --------------------------------------------- */