Skip to content

Build your personal knowledge base with Trilium Notes

docs(skill): record the elementToStructure child-view reuse hazard

Elian Doran6c5c427e4325
diff --git a/.claude/skills/ckeditor5-plugin-development/SKILL.md b/.claude/skills/ckeditor5-plugin-development/SKILL.md
index 1c0b8b5820..9cc43c87bc 100644
--- a/.claude/skills/ckeditor5-plugin-development/SKILL.md
+++ b/.claude/skills/ckeditor5-plugin-development/SKILL.md
@@ -260,11 +260,11 @@ Load the focused reference for the task at hand:
| `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, and external/async-rendered widgets (UI-element render callbacks, re-render on change, stale-render guard, lazy-load). |
| `references/conventions.md` | Trilium conventions: imports from `ckeditor5`/`@triliumnext` + required file extensions, per-package license/headers (not uniform), `@triliumnext` scope + `workspace:*`, per-package tsconfig, `?raw` icons, localization via `editor.t()` message ids, `declare module 'ckeditor5'` augmentation, plus the upstream naming/CSS/BEM/JSDoc/TypeScript rules inherited via `eslint-config-ckeditor5`. For writing idiomatic code and reviewing. |
-| `references/tooling-and-packaging.md` | Trilium packaging & wiring: the `@triliumnext/ckeditor5-<feature>` package layout, `workspace:*` deps, `main: src/index.ts` (no per-package dist), tsconfig/eslint/stylelint setup, the full registration flow (`plugins.ts` arrays → editor classes `builtinPlugins` → `toolbar.ts`), the three editor classes, the Vite build, how `apps/client` creates the editor (config, watchdog, lazy premium), and the Inspector. |
+| `references/tooling-and-packaging.md` | Trilium packaging & wiring: the `@triliumnext/ckeditor5-<feature>` package layout, `workspace:*` deps, `main: src/index.ts` (no per-package dist), tsconfig/eslint/stylelint setup, the full registration flow (`plugins.ts` arrays → editor classes `builtinPlugins` → `toolbar.ts`), the three editor classes, the Vite build, how `apps/client` creates the editor (config, watchdog, lazy premium), the Inspector, and how to read CKEditor's own source (via bundle sourcemaps) and trace its runtime decisions. |
| `references/persisted-attributes.md` | Persisting a `data-trilium-*` attribute end to end: schema → both conversion directions → the **deliberate** markdown export/import decision (collapsed is DROPPED, task-state is KEPT) → editing-view-only CSS so read-only and share rendering stay correct. Read it before storing plugin state in the saved note content. |
| `references/review-checklist.md` | A structured checklist for reviewing an existing plugin (architecture, schema, conversion, commands, UI, a11y, conventions). |
| `references/recipes.md` | Task-oriented how-tos: insert content, find/iterate nodes, custom observers, place caret, extend other plugins' UI, etc. |
-| `references/core-plugin-patterns.md` | Canonical idioms mined from the actual `packages/*/src` source: toolbar+menu-bar button factory, plugin flags & `augmentation.ts`, `AttributeCommand`/`setAttributeProperties`, inline-attribute boundary helpers, `elementToStructure`+slots, reconversion, `BalloonToolbar`, raw-HTML widgets, sanitizing untrusted HTML (CKEditor ships no sanitizer — the host supplies one per feature namespace), clipboard pipeline, markers, post-fixers, async/upload. Each cites its source file. |
+| `references/core-plugin-patterns.md` | Canonical idioms mined from the actual `packages/*/src` source: toolbar+menu-bar button factory, plugin flags & `augmentation.ts`, `AttributeCommand`/`setAttributeProperties`, inline-attribute boundary helpers, `elementToStructure`+slots, reconversion, `BalloonToolbar`, raw-HTML widgets, sanitizing untrusted HTML (CKEditor ships no sanitizer — the host supplies one per feature namespace), clipboard pipeline, markers, post-fixers, async/upload. Each cites its source file. Includes the reconversion hazard that drops sibling UIElements (to-do checkboxes) from reused child views, and the listener-priority rule a forced `reconvertItem()` depends on. |
For **testing** a plugin (Vitest setup, test editors, model/view assertions, command/UI test
patterns), use the separate **`ckeditor5-testing`** skill.
diff --git a/.claude/skills/ckeditor5-plugin-development/references/conversion.md b/.claude/skills/ckeditor5-plugin-development/references/conversion.md
index 1d98d3b24e..2cef34d538 100644
--- a/.claude/skills/ckeditor5-plugin-development/references/conversion.md
+++ b/.claude/skills/ckeditor5-plugin-development/references/conversion.md
@@ -204,4 +204,6 @@ callbacks above) renders the external/async output into the editing view.
The official CKEditor 5 "Conversion deep dive" docs (ckeditor.com/docs → Framework › Deep dive ›
Conversion) cover
`elementToStructure`, reconversion/triggers, marker conversion, and data processors. For
+`elementToStructure` + slots as used here, reconversion triggers, and the hazard where a
+reconversion drops sibling UIElements from reused child views, see `core-plugin-patterns.md`. For
widget-specific conversion patterns see `widgets.md`.
diff --git a/.claude/skills/ckeditor5-plugin-development/references/core-plugin-patterns.md b/.claude/skills/ckeditor5-plugin-development/references/core-plugin-patterns.md
index 81b7b92cfd..3e6ea93ac2 100644
--- a/.claude/skills/ckeditor5-plugin-development/references/core-plugin-patterns.md
+++ b/.claude/skills/ckeditor5-plugin-development/references/core-plugin-patterns.md
@@ -161,6 +161,48 @@ editor.editing.reconvertItem( item ); // manual full re-downcast (editingcontr
Use when the *tag/structure* depends on an attribute (e.g. `td`↔`th`). Reconvert the parent when a
child-count change affects the parent's rendering.
+### Reconversion silently drops sibling UIElements from reused child views
+
+`elementToStructure` reconverts on **every** direct-child insert/remove, and the reconversion
+reuses the children it can: `fillSlots()` → `reinsertNode()` moves each child by
+`writer.move(writer.createRangeOn(mappedViewElement), …)`. That range covers only the element the
+mapper is bound to — anything a *sibling* converter put next to it inside the same attribute
+wrapper is left behind in the discarded subtree.
+
+A to-do list item is the case that bites: its mapped element is
+`<span class="todo-list__label__description">`, while the checkbox is a UIElement sibling inside
+`<span class="todo-list__label">`. Put a to-do list in a structure slot and its checkboxes vanish
+as the body is edited. Plain lists survive only by accident — CKEditor's opt-out
+`isItemBlockInsideStructureSlot()` walks up through attribute elements named `ol`/`ul`/`li` only,
+so it reaches the slot parent from an `<li>` but stops at the to-do label. (Trilium hits this in
+`collapsible_editing.ts`; issue #11544.)
+
+The fix is to refresh those blocks so they are rebuilt rather than moved:
+
+```ts
+this.listenTo( model.document, 'change:data', () => {
+ // …collect the structure elements whose child list changed, then:
+ editor.editing.reconvertItem( block );
+} ); // NOTE: default priority — see below
+```
+
+Two things that look right and are not:
+
+- **Don't force non-reuse by adding to `refreshedItems`** from a `reduceChanges` listener. That
+ set only feeds `canReuseView()`; without the matching remove/insert differ entries the old view
+ stays bound in the mapper, and the next position lookup into it throws
+ `mapping-model-offset-not-found`. `editing.reconvertItem()` emits both — use it.
+- **Don't hook `ListEditing`'s `checkElement`.** It only fires for lists a change already
+ mentions, so a heading typed *below* the list reconverts the structure with nothing pointing at
+ the list, and every marker goes at once.
+
+**Listener priority is load-bearing here.** `change` and `change:data` listeners on
+`model.document` share one priority-ordered list, so `EditingController`'s `change` handler at
+`low` outranks anything at `lowest`. A forced reconversion must run at **default** priority: after
+`ListEditing`'s `high` handler, whose own refreshes add the insert/remove entries that trigger the
+reconversion, and before the controller's `low` handler that runs the conversion. At `high` it
+misses those entries; at `lowest` it runs after conversion and does nothing at all.
+
## Floating selection toolbar (BalloonToolbar)
The `BalloonToolbar` plugin shows a toolbar over a non-collapsed selection (Medium-style). Items
diff --git a/.claude/skills/ckeditor5-plugin-development/references/tooling-and-packaging.md b/.claude/skills/ckeditor5-plugin-development/references/tooling-and-packaging.md
index a2e0bb977a..9e9d66f7c3 100644
--- a/.claude/skills/ckeditor5-plugin-development/references/tooling-and-packaging.md
+++ b/.claude/skills/ckeditor5-plugin-development/references/tooling-and-packaging.md
@@ -177,6 +177,44 @@ CKEditorInspector.attach( editor );
imports allowed in Trilium code; everything else imports from the `ckeditor5` aggregate. There is
also a bookmarklet that injects the inspector without code changes (blocked under a strict CSP).
+## Reading CKEditor's own source
+
+`node_modules/ckeditor5` ships only a re-export shim, and each `@ckeditor/ckeditor5-*` package
+ships a bundle — there is no `src/` to grep. The original TypeScript is in the bundles'
+sourcemaps, under `sourcesContent`. Unpack the package you need into a scratch directory and grep
+that:
+
+```js
+// node -e, from the repo root
+const fs = require( 'fs' );
+const map = JSON.parse( fs.readFileSync( 'node_modules/@ckeditor/ckeditor5-list/dist/index.js.map', 'utf8' ) );
+const dir = `${ process.env.TEMP }/cksrc/`;
+fs.mkdirSync( dir, { recursive: true } );
+map.sources.forEach( ( s, i ) => fs.writeFileSync(
+ dir + s.replace( /^\.\.\/src\//, '' ).replace( /\//g, '__' ), map.sourcesContent[ i ] || '' ) );
+```
+
+(`node_modules/ckeditor5/dist/ckeditor5.js.map` has no original sources — always go to the
+individual `@ckeditor/ckeditor5-<feature>` package.) This is how you answer "when exactly does
+CKEditor decide X", which the Inspector cannot show.
+
+For the *dynamic* half of the same question, monkey-patch inside a spec. Both of these read
+cleanly against a real `ClassicEditor` and are how the reconversion hazard above was pinned down:
+
+```ts
+// Which view operations a change actually performs.
+const proto = ViewDowncastWriter.prototype as any;
+const move = proto.move;
+proto.move = function( range: any, target: any ) { console.log( "MOVE", … ); return move.call( this, range, target ); };
+
+// Which items a feature decides to re-downcast, and what its checks return.
+const editing = editor.editing as any;
+const orig = editing.reconvertItem.bind( editing );
+editing.reconvertItem = ( item: any ) => { console.log( "refresh", item ); return orig( item ); };
+```
+
+Restore the prototype in a `finally` — it is shared across the editors a spec file creates.
+
## Per-package commands
Run scripts through the pnpm workspace filter: