Skip to content

Build your personal knowledge base with Trilium Notes

chore(skills): document external/async-rendered widgets and data-processor preprocessing

Elian Doran09d72ce99943
diff --git a/.claude/skills/ckeditor5-plugin-development/SKILL.md b/.claude/skills/ckeditor5-plugin-development/SKILL.md
index 298c8bb637..dce2a62630 100644
--- a/.claude/skills/ckeditor5-plugin-development/SKILL.md
+++ b/.claude/skills/ckeditor5-plugin-development/SKILL.md
@@ -196,7 +196,7 @@ Load the focused reference for the task at hand:
| `references/conversion.md` | Upcast/downcast pipelines, conversion helpers, custom (callback) converters, attribute/element/marker conversion, position mapping. |
| `references/commands.md` | `Command` patterns: `refresh()`/`execute()`, state (`value`/`isEnabled`), `forceDisabled()`, `affectsData`, command events. |
| `references/ui-and-localization.md` | Views & templates, component catalog (buttons, inputs, dropdowns, dialogs/modals, balloons, toolbars), icons, `componentFactory`, focus/keystroke management, and `t()` localization. |
-| `references/widgets.md` | Block & inline widgets: `toWidget`/`toWidgetEditable`, nested editables, `insertObject`, widget toolbars, view↔model position mapping, custom properties. |
+| `references/widgets.md` | Block & inline widgets: `toWidget`/`toWidgetEditable`, nested editables, `insertObject`, widget toolbars, view↔model position mapping, custom properties, and external/async-rendered widgets (UI-element render callbacks, re-render on change, stale-render guard, lazy-load). |
| `references/conventions.md` | Naming, file/CSS/BEM rules, imports, JSDoc/visibility, `CKEditorError`, TypeScript rules. For writing idiomatic code and reviewing. |
| `references/tooling-and-packaging.md` | Package generator, `ckeditor5-metadata.json`, build output/integration, custom editor creators, dev-repo setup, the Inspector. |
| `references/review-checklist.md` | A structured checklist for reviewing an existing plugin (architecture, schema, conversion, commands, UI, a11y, conventions). |
diff --git a/.claude/skills/ckeditor5-plugin-development/references/conversion.md b/.claude/skills/ckeditor5-plugin-development/references/conversion.md
index dd7fd5df3b..555bc7842e 100644
--- a/.claude/skills/ckeditor5-plugin-development/references/conversion.md
+++ b/.claude/skills/ckeditor5-plugin-development/references/conversion.md
@@ -174,6 +174,21 @@ editor.editing.mapper.on( 'viewToModelPosition',
This maps any position inside the view `<span>` to a position **outside** the model element.
+## Preprocessing input data (data processor)
+
+Upcast operates on the view the **data processor** produced from the input string. To massage the
+raw HTML *before* it becomes a view (e.g. protect content the HTML parser would mangle), wrap the
+processor's `toView`:
+
+```js
+const processor = editor.data.processor;
+const toView = processor.toView.bind( processor );
+processor.toView = data => toView( preprocess( data ) ); // and mirror toData for output if needed
+```
+
+Use sparingly — prefer real converters. It's for cases the converter layer can't reach, such as
+preserving newlines inside a formula or shielding a fragment from generic HTML normalization.
+
## Where to go deeper
The official CKEditor 5 "Conversion deep dive" docs (ckeditor.com/docs → Framework › Deep dive ›
diff --git a/.claude/skills/ckeditor5-plugin-development/references/widgets.md b/.claude/skills/ckeditor5-plugin-development/references/widgets.md
index f50942d8cd..5950afe139 100644
--- a/.claude/skills/ckeditor5-plugin-development/references/widgets.md
+++ b/.claude/skills/ckeditor5-plugin-development/references/widgets.md
@@ -183,6 +183,78 @@ class SimpleBoxToolbar extends Plugin {
This is the same mechanism images/tables use to show their contextual toolbars.
+## External / async-rendered widgets (math, diagrams, charts)
+
+Widgets that render the output of an external library (KaTeX, Mermaid, a charting lib, a code
+highlighter) need a few patterns beyond the static-widget basics. The model stores the **source**
+(e.g. a LaTeX string or diagram code) as an attribute; the **data downcast** emits that source as
+plain markup (so `getData()` is clean and re-parseable), while the **editing downcast** renders it.
+
+**Host the rendered output in a UI element.** A `UIElement`'s render callback runs in the real
+DOM and is the hook for injecting external output into the editing view (UI elements are filtered
+out of the data pipeline, so this never pollutes `getData()`):
+
+```js
+conversion.for( 'editingDowncast' ).elementToElement( {
+ model: { name: 'diagram', attributes: [ 'source' ] }, // attributes → reconvert on change (see below)
+ view: ( modelEl, { writer } ) => {
+ const wrapper = writer.createContainerElement( 'div', { class: 'diagram' } );
+ const rendered = writer.createUIElement( 'div', { class: 'diagram__preview' }, function( domDocument ) {
+ const domElement = this.toDomElement( domDocument ); // real DOM node
+ void renderInto( domElement, modelEl.getAttribute( 'source' ) ); // async render
+ return domElement;
+ } );
+ writer.insert( writer.createPositionAt( wrapper, 0 ), rendered );
+ return toWidget( wrapper, writer, { label: 'diagram' } );
+ }
+} );
+```
+
+**Re-render when the source changes.** Two options:
+- **Idiomatic — reconversion.** Listing the attribute in the model matcher
+ (`model: { name, attributes: [ 'source' ] }`, as above) makes the converter re-run when `source`
+ changes, rebuilding the element and its UI element → the render callback fires again. Simplest;
+ rebuilds the whole widget view.
+- **Imperative — downcast listener.** To update the existing DOM without rebuilding, listen to the
+ editing downcast dispatcher and re-render in place:
+
+ ```js
+ editor.editing.downcastDispatcher.on( 'attribute:source:diagram', ( evt, data, conversionApi ) => {
+ const viewEl = conversionApi.mapper.toViewElement( data.item );
+ // locate the rendered DOM node via the view→DOM converter and re-render it
+ } );
+ ```
+
+**Guard against stale async renders.** Overlapping renders can finish out of order and clobber a
+newer one. Gate writes with a generation counter:
+
+```js
+this._gen = 0;
+async renderInto( domEl, source ) {
+ const gen = ++this._gen;
+ const out = await lib.render( source );
+ if ( gen === this._gen ) domEl.innerHTML = out; // ignore if a newer render started
+}
+```
+
+**Lazy-load the library once.** Cache the load promise so a heavy dependency loads on first use
+and initializes a single time (drive it from config so the host app can supply the loader):
+
+```js
+this._libPromise ??= Promise.resolve( editor.config.get( 'diagram.lazyLoad' )() )
+ .then( lib => { lib.initialize( editor.config.get( 'diagram.config' ) ); return lib; } );
+const lib = await this._libPromise;
+```
+
+**Editable source inside the widget.** If users type the raw source into a textarea/field embedded
+in the widget, wrap that element so the editor's own keystroke/selection handlers don't hijack
+input — give the container the `data-cke-ignore-events` attribute. Debounce the input handler that
+writes back to the model (`editor.model.change(...)`).
+
+**Mode / state machine.** For source/preview/split toggles, store a `displayMode` attribute, expose
+commands that set it, render different DOM per mode in the editing downcast, and bind the
+widget-toolbar buttons' `isOn` to each command's value so they reflect the current mode.
+
## Useful widget utilities
`toWidget`, `toWidgetEditable`, `isWidget`, `viewToModelPositionOutsideModelElement`,
diff --git a/.claude/skills/ckeditor5-testing/references/running-and-config.md b/.claude/skills/ckeditor5-testing/references/running-and-config.md
index 23ccdff0a6..e0068698c0 100644
--- a/.claude/skills/ckeditor5-testing/references/running-and-config.md
+++ b/.claude/skills/ckeditor5-testing/references/running-and-config.md
@@ -79,7 +79,9 @@ overrides. The baseline sets:
- `setupFiles: [ test_setup.js ]` — sets `globalThis.CKEDITOR_GLOBAL_LICENSE_KEY = 'GPL'`.
- `testTimeout: 5000`.
- **Browser mode** via `@vitest/browser-playwright`, Chromium (`channel: 'chrome'`), viewport
- 1920×1080, `screenshotFailures: false`.
+ 1920×1080, `screenshotFailures: false`. (The browser provider is pluggable — `@vitest/browser-webdriverio`
+ is an equally valid choice; downstream projects pick one. `happy-dom`/`jsdom` also works for tests
+ that don't need real layout, though CKEditor UI/widget tests generally want a real browser.)
- **Coverage** (`v8`): `include: [ 'src/**' ]`, excludes `src/index.ts`, `src/augmentation.ts`,
`src/**/*config.ts`; `thresholds: { 100: true }`; reporters `text`, `html`.
- A `load-svg` plugin so `import icon from './x.svg'` yields the SVG string.