Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): stop include note flicker on widget click

Elian Doran7085683a59b1
diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.ts
index 71e37f103f..07aa18085b 100644
--- a/apps/client/src/widgets/type_widgets/text/utils.ts
+++ b/apps/client/src/widgets/type_widgets/text/utils.ts
@@ -4,14 +4,14 @@ import froca from "../../../services/froca";
import link, { ViewScope } from "../../../services/link";
import utils from "../../../services/utils";
-export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>) {
+export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>, boxSize?: string) {
const note = await froca.getNote(noteId);
if (!note) return;
- // Get the box size from the parent section element
- const $section = $el.closest('section.include-note');
- const boxSize = $section.attr('data-box-size');
- const isExpandable = boxSize === 'expandable';
+ // The box size is supplied explicitly by the editing-view downcast; for the other
+ // callers (read-only rendering, script API refresh) fall back to reading it from the DOM.
+ const effectiveBoxSize = boxSize ?? $el.closest('section.include-note').attr('data-box-size');
+ const isExpandable = effectiveBoxSize === 'expandable';
const $wrapper = $('<div class="include-note-wrapper">');
const $link = await link.createLink(note.noteId, {
@@ -48,37 +48,6 @@ export async function loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>)
}
$el.empty().append($wrapper);
-
- // Watch for box-size attribute changes and re-render
- setupBoxSizeObserver($section[0], noteId, $el);
-}
-
-// Track observers to avoid duplicates
-const boxSizeObservers = new WeakMap<Element, MutationObserver>();
-
-function setupBoxSizeObserver(section: Element, noteId: string, $el: JQuery<HTMLElement>) {
- // Clean up existing observer if any
- const existingObserver = boxSizeObservers.get(section);
- if (existingObserver) {
- existingObserver.disconnect();
- }
-
- const observer = new MutationObserver((mutations) => {
- for (const mutation of mutations) {
- if (mutation.type === 'attributes' && mutation.attributeName === 'data-box-size') {
- // Re-render the included note with the new box size
- loadIncludedNote(noteId, $el);
- break;
- }
- }
- });
-
- observer.observe(section, {
- attributes: true,
- attributeFilter: ['data-box-size']
- });
-
- boxSizeObservers.set(section, observer);
}
export function refreshIncludedNote(container: HTMLDivElement, noteId: string) {
diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.ts
index a744470dac..7ebad27190 100644
--- a/packages/ckeditor5/src/augmentation.ts
+++ b/packages/ckeditor5/src/augmentation.ts
@@ -18,7 +18,7 @@ declare global {
interface EditorComponent extends Component {
loadReferenceLinkTitle($el: JQuery<HTMLElement>, href: string): Promise<void>;
createNoteForReferenceLink(title: string): Promise<string>;
- loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>): void;
+ loadIncludedNote(noteId: string, $el: JQuery<HTMLElement>, boxSize?: string): void;
fetchLinkMetadata(url: string): Promise<LinkEmbedMetadata>;
detectEmbedType(url: string): string;
renderLinkEmbed(container: HTMLElement, metadata: LinkEmbedMetadata, editable?: boolean): void;
diff --git a/packages/ckeditor5/src/plugins/includenote.spec.ts b/packages/ckeditor5/src/plugins/includenote.spec.ts
index e792d26311..3bb6a56d36 100644
--- a/packages/ckeditor5/src/plugins/includenote.spec.ts
+++ b/packages/ckeditor5/src/plugins/includenote.spec.ts
@@ -107,6 +107,9 @@ describe("IncludeNote", () => {
expect(loadIncludedNote).toHaveBeenCalledTimes(1);
expect(loadIncludedNote.mock.calls[0]?.[0]).toBe("noteY");
+ // The box size is passed explicitly so the client render does not have to read it back
+ // from the DOM (which may not be flushed yet at conversion time).
+ expect(loadIncludedNote.mock.calls[0]?.[2]).toBe("small");
});
it("updates the box-size class on the editing view when the boxSize attribute changes", () => {
@@ -123,6 +126,22 @@ describe("IncludeNote", () => {
expect(section?.getAttribute("data-box-size")).toBe("full");
});
+ it("re-renders the included note content with the new size on a genuine box-size change", () => {
+ insertIncludeNote(editor, "noteReload", "small");
+
+ // One call for the initial render.
+ expect(loadIncludedNote).toHaveBeenCalledTimes(1);
+ expect(loadIncludedNote.mock.calls[0]?.[2]).toBe("small");
+
+ editor.execute(BOX_SIZE_COMMAND_NAME, { value: "expandable" });
+
+ // The downcast handler drives a second render with the new size, without any DOM observer.
+ expect(loadIncludedNote).toHaveBeenCalledTimes(2);
+ const reloadCall = loadIncludedNote.mock.calls[1];
+ expect(reloadCall?.[0]).toBe("noteReload");
+ expect(reloadCall?.[2]).toBe("expandable");
+ });
+
it("handles a boxSize change from an empty old value (no class to remove)", () => {
// Insert an includeNote whose boxSize is empty so the attribute-change converter
// hits the falsy `oldBoxSize` branch when the value is set for the first time.
diff --git a/packages/ckeditor5/src/plugins/includenote.ts b/packages/ckeditor5/src/plugins/includenote.ts
index 763669083b..448dc24429 100644
--- a/packages/ckeditor5/src/plugins/includenote.ts
+++ b/packages/ckeditor5/src/plugins/includenote.ts
@@ -1,4 +1,4 @@
-import { ButtonView, Command, type Editor, Plugin, toWidget, Widget, type Observable } from 'ckeditor5';
+import { ButtonView, Command, type Editor, type ModelElement, Plugin, toWidget, type ViewElement, Widget, type Observable } from 'ckeditor5';
import noteIcon from '../icons/note.svg?raw';
export const COMMAND_NAME = 'insertIncludeNote';
@@ -115,7 +115,7 @@ class IncludeNoteEditing extends Plugin {
view: ( modelElement, { writer: viewWriter } ) => {
const noteId = modelElement.getAttribute( 'noteId' ) as string;
- const boxSize = modelElement.getAttribute( 'boxSize' );
+ const boxSize = modelElement.getAttribute( 'boxSize' ) as string | undefined;
const section = viewWriter.createContainerElement( 'section', {
class: 'include-note box-size-' + boxSize,
@@ -132,7 +132,7 @@ class IncludeNoteEditing extends Plugin {
const editorEl = editor.editing.view.getDomRoot();
const component = glob.getComponentByEl<EditorComponent>( editorEl );
- component.loadIncludedNote( noteId, $( domElement ) );
+ component.loadIncludedNote( noteId, $( domElement ), boxSize );
preventCKEditorHandling( domElement, editor );
@@ -165,6 +165,12 @@ class IncludeNoteEditing extends Plugin {
if ( newBoxSize ) {
viewWriter.addClass( 'box-size-' + newBoxSize, viewElement );
viewWriter.setAttribute( 'data-box-size', newBoxSize, viewElement );
+
+ // Re-render the included note content with the new box size. We drive this
+ // directly from the converter (rather than observing the DOM attribute) so the
+ // content is only rebuilt on a genuine box-size change — not whenever CKEditor
+ // re-applies unrelated attributes (e.g. `draggable` while selecting the widget).
+ reloadIncludedNote( editor, viewElement, data.item as ModelElement, newBoxSize );
}
} );
} );
@@ -224,6 +230,28 @@ class IncludeNoteBoxSizeCommand extends Command {
}
}
+/**
+ * Re-renders the included note content of an already-rendered widget after its box size changed.
+ *
+ * The wrapper is a `UIElement` whose DOM is opaque to CKEditor, so we reach into it directly to
+ * trigger the client-side render. The box size is passed explicitly because, at conversion time,
+ * the updated `data-box-size` attribute may not yet be flushed to the DOM.
+ *
+ * On the initial insert the attribute converter runs before the widget has been rendered to the
+ * DOM, so `mapViewToDom()` returns nothing and this is a no-op — the `UIElement` render callback
+ * performs that first paint instead. It only does work on a subsequent, genuine box-size change.
+ */
+function reloadIncludedNote( editor: Editor, viewElement: ViewElement, modelElement: ModelElement, boxSize: string ) {
+ const sectionDom = editor.editing.view.domConverter.mapViewToDom( viewElement );
+ const wrapperDom = sectionDom?.querySelector<HTMLElement>( '.include-note-wrapper' );
+ const noteId = modelElement.getAttribute( 'noteId' ) as string | undefined;
+
+ if ( wrapperDom && noteId ) {
+ const component = glob.getComponentByEl<EditorComponent>( editor.editing.view.getDomRoot() );
+ component.loadIncludedNote( noteId, $( wrapperDom ), boxSize );
+ }
+}
+
/**
* Hack coming from https://github.com/ckeditor/ckeditor5/issues/4465
* Source issue: https://github.com/zadam/trilium/issues/1117