|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | 3 | { |
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) { |
5 | 9 | if (el.closest('.empty-form') === null) { // Don't do empty inlines |
6 | 10 | var mce_conf = JSON.parse(el.dataset.mceConf); |
7 | 11 |
|
|
15 | 19 | 'images_upload_handler', |
16 | 20 | 'paste_postprocess', |
17 | 21 | 'paste_preprocess', |
| 22 | + 'init_instance_callback', |
18 | 23 | 'setup', |
19 | 24 | 'urlconverter_callback', |
20 | 25 | 'media_url_resolver', |
|
40 | 45 | if (el.dataset.mceGzConf) { |
41 | 46 | tinyMCE_GZ.init(JSON.parse(el.dataset.mceGzConf)); |
42 | 47 | } |
| 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 | + }; |
43 | 56 | if (!tinyMCE.get(el.id)) { |
44 | | - tinyMCE.init(mce_conf); |
| 57 | + return await tinyMCE.init(mce_conf); |
45 | 58 | } |
46 | 59 | } |
| 60 | + return []; |
47 | 61 | } |
48 | 62 |
|
49 | 63 | // Call function fn when the DOM is loaded and ready. If it is already |
|
56 | 70 | } |
57 | 71 | } |
58 | 72 |
|
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; |
61 | 84 | } |
62 | 85 |
|
63 | 86 | ready(function() { |
|
0 commit comments