From fcc943fc263d4696c6a2970ec6d6cc016abc8f3b Mon Sep 17 00:00:00 2001 From: Kevin Ansfield Date: Fri, 10 Aug 2018 17:31:32 +0100 Subject: [PATCH] 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 --- .../addon/components/koenig-editor.js | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/koenig-editor/addon/components/koenig-editor.js b/lib/koenig-editor/addon/components/koenig-editor.js index c09720fc3..2c7b04bb0 100644 --- a/lib/koenig-editor/addon/components/koenig-editor.js +++ b/lib/koenig-editor/addon/components/koenig-editor.js @@ -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 --------------------------------------------- */