Skip to content

Commit e70b787

Browse files
authored
Dispatch per-instance tinyMceEditorInit and batch tinyMceAllEditorsInit events
* Add event dispatch for TinyMCE editor initialization Introduced a new function to dispatch a custom event when the TinyMCE editor initializes, enhancing integration capabilities.
1 parent 6b413f4 commit e70b787

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ Changelog
33

44
This document describes changes between each past release.
55

6+
Unreleased
7+
==========
8+
9+
- Add `tinyMceEditorInit` and `tinyMceAllEditorsInit` events
10+
11+
612
5.0.0 (2025-10-21)
713
==================
814

tinymce/static/django_tinymce/init_tinymce.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict';
22

33
{
4-
function initTinyMCE(el) {
4+
function onEditorInit(editor) {
5+
const event = new CustomEvent('tinyMceEditorInit', { detail: editor });
6+
document.dispatchEvent(event);
7+
}
8+
async function initTinyMCE(el) {
59
if (el.closest('.empty-form') === null) { // Don't do empty inlines
610
var mce_conf = JSON.parse(el.dataset.mceConf);
711

@@ -15,6 +19,7 @@
1519
'images_upload_handler',
1620
'paste_postprocess',
1721
'paste_preprocess',
22+
'init_instance_callback',
1823
'setup',
1924
'urlconverter_callback',
2025
'media_url_resolver',
@@ -40,10 +45,19 @@
4045
if (el.dataset.mceGzConf) {
4146
tinyMCE_GZ.init(JSON.parse(el.dataset.mceGzConf));
4247
}
48+
// Dispatch per-instance event while preserving user callback
49+
const userInitInstanceCallback = mce_conf.init_instance_callback;
50+
mce_conf.init_instance_callback = function(editor) {
51+
if (typeof userInitInstanceCallback === 'function') {
52+
userInitInstanceCallback(editor);
53+
}
54+
onEditorInit(editor);
55+
};
4356
if (!tinyMCE.get(el.id)) {
44-
tinyMCE.init(mce_conf);
57+
return await tinyMCE.init(mce_conf);
4558
}
4659
}
60+
return [];
4761
}
4862

4963
// Call function fn when the DOM is loaded and ready. If it is already
@@ -56,8 +70,17 @@
5670
}
5771
}
5872

59-
function initializeTinyMCE(element, formsetName) {
60-
Array.from(element.querySelectorAll('.tinymce')).forEach(area => initTinyMCE(area));
73+
async function initializeTinyMCE(element, formsetName) {
74+
const areas = Array.from(element.querySelectorAll('.tinymce'));
75+
const initPromises = areas.map(area => initTinyMCE(area));
76+
77+
const results = await Promise.all(initPromises);
78+
const allEditors = results.flat();
79+
if (allEditors.length) {
80+
const event = new CustomEvent('tinyMceAllEditorsInit', { detail: allEditors });
81+
document.dispatchEvent(event);
82+
}
83+
return allEditors;
6184
}
6285

6386
ready(function() {

0 commit comments

Comments
 (0)