Skip to content

Commit 2afcb91

Browse files
committed
[2.x] fix: TextEditor.onbuild race against async Mithril redraw
`oncreate` called `_load().then(() => setTimeout(this.onbuild, 50))`. The 50ms timer is a guess at how long it takes Mithril's async `m.redraw()` to flush — under slow first-load conditions (cold CDN, heavy DOM, additional `_loaders`) the redraw can land *after* the timer fires, leaving `this.$('.TextEditor-editorContainer')[0]` as `undefined` when `onbuild` runs. That `undefined` then propagates into the editor driver (notably breaking Tiptap-based drivers like fof/rich-text, where it produces a non-functional toolbar with no editor underneath). Use `m.redraw.sync()` in `_load` so the DOM is updated before the promise resolves, and drop the 50ms timer entirely. The `.then` runs on a microtask outside any Mithril event handler, so `redraw.sync()` is safe here. Closes #4612
1 parent c57b7a3 commit 2afcb91

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

framework/core/js/src/common/components/TextEditor.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ export default class TextEditor extends Component {
7272
oncreate(vnode) {
7373
super.oncreate(vnode);
7474

75-
this._load().then(() => {
76-
setTimeout(this.onbuild.bind(this), 50);
77-
});
75+
this._load().then(this.onbuild.bind(this));
7876
}
7977

8078
onbuild() {
@@ -96,7 +94,12 @@ export default class TextEditor extends Component {
9694
_load() {
9795
return Promise.all(this._loaders.map((loader) => loader())).then(() => {
9896
this.loading = false;
99-
m.redraw();
97+
// Synchronous redraw so the editor container is in the DOM before
98+
// `onbuild` runs. Mithril's regular `m.redraw()` queues for the next
99+
// frame, which previously raced against a 50ms setTimeout and could
100+
// leave `this.$('.TextEditor-editorContainer')[0]` undefined on slow
101+
// first loads. See flarum/framework#4612.
102+
m.redraw.sync();
100103
});
101104
}
102105

0 commit comments

Comments
 (0)