It appears that the initialization code for the TinyMCE editor (specifically for version 5.10.2) includes the following:
var initEditor = function (editor) {
editor.bindPendingEventDelegates();
editor.initialized = true;
fireInit(editor);
editor.focus(true);
moveSelectionToFirstCaretPosition(editor);
editor.nodeChanged({ initial: true });
editor.execCallback('init_instance_callback', editor);
autoFocus(editor);
};
The fireInit(editor);
method call triggers the Init
event for the editor, causing all related event listeners to be immediately executed (including any plugins waiting for this event using editor.on("Init", ...)
).
If a plugin needs to run code after the init_instance_callback
has already been executed, it can do so by using:
setTimeout(function()
{
// code to run after init_instance_callback
}, 0);
In practice, this approach works because it adds the function reference to the end of the browser's native event queue, ensuring it runs once the current JS execution is complete.
It should be noted that the autoFocus()
implementation introduces a 100 ms delay before triggering the focus event, and will only work if editor.settings.auto_focus
is set to true.
This information is not documented officially. While it features in TinyMCE 5.10.2, it might change in future versions, so it should be considered more of a workaround than a fully supported feature.
A recommended approach would be to use both the setTimeout()
function call and listen for the focus
event. Whichever occurs first will likely happen almost immediately (or with the 100 ms delay) after the init_instance_callback
has been executed.
If TinyMCE updates its behavior to trigger the init_instance_callback
via the browser's native event loop in the future, this workaround may execute the code prematurely. In such a scenario, there is no guaranteed safe delay value (> 0) to wait for TinyMCE to complete its actions, as the JS execution could potentially stall indefinitely between the Init
event and setting a timeout for the init_instance_callback
trigger. If 0
is deemed unsafe (as per the current implementation), then any non-zero value could also pose risks.