Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit fcc943f

Browse files
committed
Koenig - Fixed .cleanup when called during editor initialisation
refs TryGhost/Ghost#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
1 parent 72c6004 commit fcc943f

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

lib/koenig-editor/addon/components/koenig-editor.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,10 @@ export default Component.extend({
394394
this._startedRunLoop = false;
395395
run.end();
396396
}
397+
398+
if (this._cleanupScheduled) {
399+
run.schedule('afterRender', this, this._cleanup);
400+
}
397401
});
398402

399403
editor.postDidChange(() => {
@@ -624,16 +628,28 @@ export default Component.extend({
624628
},
625629

626630
/* public interface ----------------------------------------------------- */
627-
// TODO: find a better way to expose this?
628-
631+
// TODO: find a better way to expose the public interface?
632+
633+
// HACK: this scheduled cleanup is a bit hacky. We call .cleanup when
634+
// initializing Koenig in our editor controller but we have to wait for
635+
// rendering to finish so that componentCards is populated, even then
636+
// it's unlikely the card.component registration has finished.
637+
//
638+
// TODO: see if there's a way we can perform cleanup directly on the
639+
// mobiledoc, maybe with a "cleanupOnInit" option so that we modify the
640+
// mobiledoc before we start rendering
629641
cleanup() {
642+
this._cleanupScheduled = true;
643+
},
644+
645+
_cleanup() {
630646
this.componentCards.forEach((card) => {
631-
if (!card.koenigOptions.deleteIfEmpty) {
647+
let shouldDelete = card.koenigOptions.deleteIfEmpty;
648+
649+
if (!shouldDelete) {
632650
return;
633651
}
634652

635-
let shouldDelete = card.koenigOptions.deleteIfEmpty;
636-
637653
if (typeof shouldDelete === 'string') {
638654
let payloadKey = shouldDelete;
639655
shouldDelete = card => isBlank(get(card, payloadKey));
@@ -643,6 +659,7 @@ export default Component.extend({
643659
this.deleteCard(card, NO_CURSOR_MOVEMENT);
644660
}
645661
});
662+
this._cleanupScheduled = false;
646663
},
647664

648665
/* mobiledoc event handlers --------------------------------------------- */

0 commit comments

Comments
 (0)