Skip to content

Dispatch per-instance tinyMceEditorInit and batch tinyMceAllEditorsInit events#500

Merged
claudep merged 4 commits intojazzband:masterfrom
john-psina:master
Nov 8, 2025
Merged

Dispatch per-instance tinyMceEditorInit and batch tinyMceAllEditorsInit events#500
claudep merged 4 commits intojazzband:masterfrom
john-psina:master

Conversation

@john-psina
Copy link
Contributor

Description

  • Dispatches a per-instance CustomEvent named tinyMceEditorInit on document via TinyMCE's init_instance_callback. The event’s detail is a single editor instance.
  • Preserves any user-provided init_instance_callback by wrapping it (user callback is invoked first).
  • Makes initTinyMCE return a Promise and initializeTinyMCE aggregate with Promise.all, dispatching a batch tinyMceAllEditorsInit with all editors initialized in that batch as detail.

File: tinymce/static/django_tinymce/init_tinymce.js.

Rationale

Allows external scripts to reliably react to editor readiness without forking or patching the package. This enables clean integration points for plugins, hotkeys, and state sync both on initial load and when admin inlines are added.

Usage

Per-instance event:

document.addEventListener('tinyMceEditorInit', (e) => {
  const editor = e.detail;
  if (editor) {
    // Example:
    // editor.on('change', () => console.log('TinyMCE content changed'));
  }
});

Batch event (all editors initialized within a batch):

document.addEventListener('tinyMceAllEditorsInit', (e) => {
  const editors = e.detail || [];
  for (const editor of editors) {
    // Example per editor
    // editor.on('change', () => console.log('TinyMCE content changed'));
  }
});

Backwards Compatibility

  • Non-breaking: existing behavior remains unchanged; these are additive events.
  • Events are dispatched only for new initialization; if an instance already exists for the element, no duplicate per-instance event is fired, and the batch event will include only newly initialized editors.

Changelog

Added: Dispatch per-instance tinyMceEditorInit and batch tinyMceAllEditorsInit after TinyMCE initialization.

Introduced a new function to dispatch a custom event when the TinyMCE editor initializes, enhancing integration capabilities.
@john-psina
Copy link
Contributor Author

I think this can be considered related to #452

Copy link
Contributor

@claudep claudep left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch. It seems the JS world currently privileges using async with awaits instead of Promises. Would you be able to rewrite your code with async syntax instead?

Please add also an entry in CHANGELOG.rst (under an Unreleased chapter).

At some point, it would also be nice to have the various callbacks documented. But that could be a separate follow-up ticket.

@john-psina
Copy link
Contributor Author

Thanks for the feedback! I've updated the pull request according to your suggestions:

  1. The JavaScript initialization logic has been refactored to use async/await. I've kept Promise.all to ensure that multiple editors on a page initialize in parallel for better performance.
  2. An entry describing the new DOM events has been added to CHANGELOG.rst.

@john-psina john-psina requested a review from claudep November 3, 2025 07:40
@codecov
Copy link

codecov bot commented Nov 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.45%. Comparing base (685236d) to head (af036b9).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #500   +/-   ##
=======================================
  Coverage   86.45%   86.45%           
=======================================
  Files           6        6           
  Lines         251      251           
  Branches       40       40           
=======================================
  Hits          217      217           
  Misses         17       17           
  Partials       17       17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@claudep claudep merged commit e70b787 into jazzband:master Nov 8, 2025
13 checks passed
@claudep
Copy link
Contributor

claudep commented Nov 8, 2025

Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments