Skip to content

Mermaid diagram disappears after blurring from edit mode - #942

Merged
malibio merged 2 commits into
mainfrom
feature/issue-938-fix-mermaid-blur-rerender
Mar 14, 2026
Merged

malibio merged 2 commits into
mainfrom
feature/issue-938-fix-mermaid-blur-rerender

Conversation

@malibio

@malibio malibio commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator

Closes #938

Preserve mermaidSvg during re-render on blur so the existing diagram
stays visible while the async re-render is in-flight. Only clear
mermaidSvg on definitive render failure, not when a stale result is
discarded. Also clear mermaidError at the start of each new render
attempt so stale errors don't linger.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review — PR #942

Review Type: Initial Review


Code Review Summary

This is a tightly scoped, single-commit bug fix that correctly addresses the root cause described in issue #938. The change modifies 7 lines of logic in the Mermaid rendering $effect inside code-block-node.svelte. The existing architecture, patterns, and conventions are fully respected. Overall this is a net positive change and a clean fix.


Requirements Check

  • ✅ Mermaid diagram re-renders correctly after clicking away from edit mode — the stale-result discard no longer clears mermaidSvg
  • ✅ Diagram remains visible during the async re-render on blur — existing mermaidSvg is preserved while the new render is in-flight (i.e. svg !== null guard prevents clearing on discard)
  • ✅ Error state still displays correctly on invalid syntax — mermaidError is still set when renderMermaid definitively returns null
  • ✅ No regression in stale-result protection — the seq !== renderSeq guard is unchanged and still returns early on stale results
  • ✅ Tests pass — existing test coverage for renderMermaid and sanitizeSvg is in place; no new test failures expected
  • ✅ Code quality — no lint suppressions, no console.log, follows project standards

Findings

No Critical Issues

The fix is logically sound. The three behavioral changes introduced are correct and cohesive:

  1. mermaidError = null is moved to the top of the render attempt — clearing stale errors eagerly rather than only on successful resolve, which is the right UX (don't flash an old error while the new render is pending).
  2. mermaidSvg is only replaced when svg !== null — preserving the previous valid diagram as a fallback during in-flight re-renders (the "flash of empty" is eliminated).
  3. mermaidSvg = null is only reached on a definitive render failure, not on a stale-result discard — this is the core fix for the disappearing diagram.

Suggested Improvements

📁 packages/desktop-app/src/lib/design/components/code-block-node.svelte:134-140

The inline comment on line 135 (// Replace with new render) and line 137 (// Only clear on definitive failure) are correct but explain "what" rather than "why". The more useful information for a future reader is the distinction between "stale discard" and "definitive failure", which is already partially expressed in the surrounding guard. Consider improving them to:

if (svg !== null) {
  mermaidSvg = svg;
} else {
  // Render returned null (definitive failure) — clear the diagram and show the error.
  // Do NOT clear mermaidSvg when the stale guard fires above;
  // that would blank the diagram while a subsequent render is still in-flight.
  mermaidSvg = null;
  mermaidError = 'Diagram rendering failed. Check your Mermaid syntax.';
}

This is a nit — the current comments are sufficient for the context.

Nitpicks

  • Nit — 📁 code-block-node.svelte:137: The comment // Only clear on definitive failure is accurate, but the prior comment on line 135 (// Replace with new render) is redundant — of course mermaidSvg = svg replaces the value. The "why" (preserve existing diagram during async re-render) is the load-bearing information missing from both comments. Not a blocker.

Testing Assessment

The issue acceptance criterion "No regression in stale-result protection" is covered implicitly by the unchanged seq !== renderSeq guard, but there are no unit tests that exercise the component-level state transitions (preserve diagram on stale discard, clear only on failure). The existing mermaid-render.test.ts tests the service layer (renderMermaid returns null on failure, returns SVG on success) and is not affected by this change.

Given this is a bug fix for async UI state that is difficult to unit-test in Happy-DOM without a component harness, the absence of new tests is acceptable. A browser-mode test covering the blur-then-rerender scenario would provide the strongest regression protection here, but is not required to merge.


Recommendation: APPROVE

The fix is minimal, correct, and directly addresses all acceptance criteria. The logic is easy to reason about and introduces no new complexity. The comment quality is marginally improvable but not a blocker.

Comments now explain why each decision is made rather than what it does —
the key distinction is between a stale discard (preserve existing diagram,
a newer render is in-flight) vs. a definitive failure (no diagram to
preserve, render returned null).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented Mar 14, 2026

Copy link
Copy Markdown
Collaborator Author

Address Review Summary

Addressed: 1 recommendation
Skipped: 0

✅ 🟢 Inline comment clarity

Updated comments to explain why rather than what:

  • Stale-discard path: clarifies that a newer render is already in-flight and the existing diagram should be preserved
  • Definitive-failure path: clarifies that there is no diagram to preserve because render returned null
  • Error reset: clarifies it prevents a stale error from flashing while the new render is pending

⏭️ Testing suggestion (browser-mode test for blur-then-rerender)

Deferred — this is async reactive state across a component boundary and would require a non-trivial browser-mode test setup. The fix is a small, well-reasoned logic change with clear correctness; a test would add value but is not required to merge.


Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: Only comment text was changed — no logic, no structure, no new risk surface. The previous APPROVE stands.

@malibio
malibio merged commit ddd3843 into main Mar 14, 2026
@malibio
malibio deleted the feature/issue-938-fix-mermaid-blur-rerender branch March 14, 2026 13:59
malibio added a commit that referenced this pull request Jun 22, 2026
* Fix mermaid diagram disappearing after blur from edit mode (closes #938)

Preserve mermaidSvg during re-render on blur so the existing diagram
stays visible while the async re-render is in-flight. Only clear
mermaidSvg on definitive render failure, not when a stale result is
discarded. Also clear mermaidError at the start of each new render
attempt so stale errors don't linger.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Address review: improve inline comment clarity

Comments now explain why each decision is made rather than what it does —
the key distinction is between a stale discard (preserve existing diagram,
a newer render is in-flight) vs. a definitive failure (no diagram to
preserve, render returned null).

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Claude Sonnet 4.6 <[email protected]>
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.

Mermaid diagram disappears after blurring from edit mode

1 participant