Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
feat(open-knowledge): live-preview linked mermaid labels while typing (#2535)
Andrew Mikofalvy431ecc03b9ac
diff --git a/.changeset/mermaid-live-linked-preview.md b/.changeset/mermaid-live-linked-preview.mdnew file mode 100644index 000000000..615614eec--- /dev/null+++ b/.changeset/mermaid-live-linked-preview.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++While you're editing a label in a Mermaid diagram, the other places the same text appears now update live as you type — for example, editing a sequence-diagram participant's top box updates its bottom box in real time, before you press Enter. The preview is local to your view until you commit, so it doesn't interfere with real-time collaboration; box sizes reflow once you commit.diff --git a/packages/app/src/editor/components/Mermaid.dom.test.tsx b/packages/app/src/editor/components/Mermaid.dom.test.tsxindex e54dc4623..9ff41df10 100644--- a/packages/app/src/editor/components/Mermaid.dom.test.tsx+++ b/packages/app/src/editor/components/Mermaid.dom.test.tsx@@ -79,7 +79,7 @@ mock.module('@panzoom/panzoom', () => ({default: createPanzoom,}));-const { MermaidView, flashLinkedLabels } = await import('./Mermaid');+const { MermaidView, flashLinkedLabels, collectLinkedLabelTargets } = await import('./Mermaid');const { TooltipProvider } = await import('@/components/ui/tooltip');function renderMermaidView(chart: string) {@@ -339,3 +339,33 @@ describe('flashLinkedLabels', () => {expect(flashLinkedLabels(blank, ' ')).toBe(0);});});++describe('collectLinkedLabelTargets (live-preview matching)', () => {+ test('returns the other occurrences of the token, excluding the edited one', () => {+ const c = document.createElement('div');+ c.innerHTML =+ '<text class="actor" id="top">Alice</text>' ++ '<text class="actor" id="bottom">Alice</text>' ++ '<text class="messageText">Alice waves</text>'; // different text: not a match+ const edited = c.querySelector('#top') as Element;+ const targets = collectLinkedLabelTargets(c, 'Alice', [edited]);+ expect(targets.length).toBe(1);+ expect((targets[0] as Element).id).toBe('bottom');+ });++ test('excludes nested/ancestor elements of an excluded node (either direction)', () => {+ const c = document.createElement('div');+ // The edited node's own text nests a matching span; excluding the outer+ // must also drop the inner (and vice versa) so we never preview onto the+ // element being typed into.+ c.innerHTML = '<span class="nodeLabel"><span class="nodeLabel">Foo</span></span>';+ const outer = c.querySelector('.nodeLabel') as Element;+ expect(collectLinkedLabelTargets(c, 'Foo', [outer])).toEqual([]);+ });++ test('empty needle matches nothing', () => {+ const c = document.createElement('div');+ c.innerHTML = '<text class="actor"> </text><text class="actor"> </text>';+ expect(collectLinkedLabelTargets(c, ' ')).toEqual([]);+ });+});diff --git a/packages/app/src/editor/components/Mermaid.tsx b/packages/app/src/editor/components/Mermaid.tsxindex baeb11912..149a23fbe 100644--- a/packages/app/src/editor/components/Mermaid.tsx+++ b/packages/app/src/editor/components/Mermaid.tsx@@ -767,6 +767,26 @@ const FLASHABLE_LABEL_SELECTOR =* edit, so the class lands on fresh nodes and the one-shot animation plays* without a restart dance. Returns the count flashed.*/+/**+ * Rendered label occurrences whose text matches `value`, minus `exclude` (and+ * anything nested within an excluded element, either direction). Used by the+ * live-typing preview to find the OTHER occurrences of the token being edited —+ * e.g. the second actor box of the sequence participant under the inline input.+ */+export function collectLinkedLabelTargets(+ container: HTMLElement,+ value: string,+ exclude: readonly Element[] = [],+): Element[] {+ const needle = value.trim();+ if (!needle) return [];+ return Array.from(container.querySelectorAll<Element>(FLASHABLE_LABEL_SELECTOR)).filter(+ (el) =>+ !exclude.some((ex) => ex === el || ex.contains(el) || el.contains(ex)) &&+ (el.textContent ?? '').trim() === needle,+ );+}+/*** Pulse one label's glyph paint to the agent accent and back, twice, by* toggling INLINE `!important` fill/color. Why inline `!important` and not a CSS@@ -1181,6 +1201,36 @@ export function MermaidView({ chart = '', className, editBinding }: MermaidPropsconst prevLabelVisibility = labelP.style.visibility;labelP.style.visibility = 'hidden';+ // Live preview: mirror each keystroke into the OTHER rendered occurrences+ // of the same token (e.g. a sequence participant's second actor box) so+ // the whole diagram tracks the edit before commit. DOM-only — `Y.Text` is+ // untouched until commit, so a concurrent editor sees nothing mid-type,+ // and a remote edit that re-renders tears this down via the edit-session+ // cleanup. Each target's `innerHTML` is captured so cleanup can restore it+ // (Escape reverts; commit re-renders over it — mermaid re-lays-out box+ // geometry, which stays fixed during the preview).+ const visibleLabel = (labelSpan.textContent ?? '').trim();+ const linkedPreview: Array<{ el: Element; html: string }> = containerRef.current+ ? collectLinkedLabelTargets(containerRef.current, visibleLabel, [labelSpan, labelP]).map(+ (el) => ({ el, html: el.innerHTML }),+ )+ : [];+ function syncLinkedPreview(): void {+ const next = input.value;+ for (const { el } of linkedPreview) {+ // Write into the innermost paint carrier so the occurrence keeps its+ // glyph fill. Mermaid paints the visible color on an inner `<tspan>`+ // (SVG text) or `<p>` (foreignObject label) while the outer element+ // carries only a background-matching fill (see the input-color note+ // above). Replacing the whole element's `textContent` drops that+ // carrier, so the preview text falls back to the near-invisible+ // background fill and reads as a faded "ghost". Targeting the carrier+ // keeps it the same weight/color as the committed label.+ const carrier = el.querySelector('tspan') ?? el.querySelector('p') ?? el;+ carrier.textContent = next;+ }+ }+document.body.appendChild(input);// Position the input *after* it's in the DOM so we can use its// own measured metrics — the browser hasn't laid it out until@@ -1286,6 +1336,7 @@ export function MermaidView({ chart = '', className, editBinding }: MermaidProps// Keep the overlay pinned if the page scrolls or the label moves.window.addEventListener('scroll', positionInput, true);window.addEventListener('resize', positionInput);+ input.addEventListener('input', syncLinkedPreview);let done = false;function cleanup(): void {@@ -1294,7 +1345,12 @@ export function MermaidView({ chart = '', className, editBinding }: MermaidPropswindow.removeEventListener('resize', positionInput);input.removeEventListener('keydown', onKeyDown);input.removeEventListener('blur', onBlur);+ input.removeEventListener('input', syncLinkedPreview);input.remove();+ // Restore the live-previewed occurrences to their original markup. On+ // Escape this reverts them; on commit the ensuing re-render replaces+ // them with the new value (and PR-1's flash fires on the linked ones).+ for (const { el, html } of linkedPreview) el.innerHTML = html;labelP.style.visibility = prevLabelVisibility;if (nodeShape) {if (nodeShapeStrokeBefore === null) {