feat(muya): add invalidateImageCache by Jocs · Pull Request #4396 · marktext/marktext · GitHub
Skip to content

feat(muya): add invalidateImageCache - #4396

Merged
Jocs merged 1 commit into
developfrom
feat/muya-invalidate-image-cache
Jun 8, 2026
Merged

Jocs merged 1 commit into
developfrom
feat/muya-invalidate-image-cache

Conversation

@Jocs

@Jocs Jocs commented Jun 8, 2026

Copy link
Copy Markdown
Member

Why

The desktop shell calls editor.invalidateImageCache() to force inline images to reload — e.g. after a watched image file changes on disk, or on the mt::invalidate-image-cache IPC. @muyajs/core memoises loaded images but had no way to flush and re-render them, so an image whose file changed on disk would keep showing the stale bitmap. This is part of the desktop migration off legacy packages/muyajs (which already exposes StateRender.invalidateImageCache).

What

  • InlineRenderer.invalidateImageCache() (src/inlineRenderer/index.ts): clears the renderer's loadImageMap and urlMap image caches, then re-renders every content block via scrollPage.breadthFirstTraverse(...).update(). Re-rendering re-runs loadImageAsync, which — with the caches now empty — loads each image source afresh. This follows the existing ScrollPage.updateRefLinkAndImage traversal pattern.
  • Muya.invalidateImageCache() (src/muya.ts): public delegating wrapper, inserted immediately after hideAllFloatTools().
  • happy-dom vitest spec at src/__tests__/invalidateImageCache.spec.ts: seeds the cache maps, calls muya.invalidateImageCache(), and asserts both maps are cleared, the API is present, content blocks are re-patched (via vi.waitFor), and it does not throw on image-free / empty documents.

Behavior

invalidateImageCache() is a no-op-safe flush: the image caches are cleared synchronously and every content block is re-rendered so inline images reload from their source. Documents with no images (or empty documents) are handled without error.

Verification

All green in packages/muya:

  • lint — 0 errors (pre-existing complexity warnings only, none in touched files)
  • lint:types — clean
  • check-circular — no circular dependency
  • test — 446 passed
  • test:spec — 1347 passed (CommonMark + GFM conformance)

🤖 Generated with Claude Code

The renderer memoises loaded inline images in `loadImageMap` (skipped on
the next render once `isSuccess` is true) and resolved URLs in `urlMap`,
so an image whose file changed on disk would keep showing the stale
bitmap with no way to flush it.

Add `InlineRenderer.invalidateImageCache()` which clears both caches and
re-renders every content block (via `scrollPage.breadthFirstTraverse`),
so `loadImageAsync` runs afresh for inline images. Expose it publicly as
`Muya.invalidateImageCache()`, mirroring legacy muyajs
`StateRender.invalidateImageCache` so the desktop migration can call
`editor.invalidateImageCache()` after a watched image file changes or on
the `mt::invalidate-image-cache` IPC.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 8, 2026 07:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a public invalidateImageCache() API to @muyajs/core so the desktop shell can force inline images to reload after their underlying files change, by flushing the inline renderer’s image caches and re-rendering all content blocks.

Changes:

  • Add Muya.invalidateImageCache() as a public wrapper delegating to the inline renderer.
  • Add InlineRenderer.invalidateImageCache() to clear loadImageMap/urlMap and re-render all content blocks via scrollPage.breadthFirstTraverse(...).update().
  • Add a happy-dom Vitest spec validating cache flush + re-render behavior and no-op safety.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/muya/src/muya.ts Exposes the new public invalidateImageCache() API on Muya.
packages/muya/src/inlineRenderer/index.ts Implements cache flush and document-wide content re-render to trigger image reloads.
packages/muya/src/tests/invalidateImageCache.spec.ts Adds regression coverage for API presence, cache clearing, re-rendering, and no-op safety.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Jocs
Jocs merged commit 8421aec into develop Jun 8, 2026
8 checks passed
@Jocs
Jocs deleted the feat/muya-invalidate-image-cache branch June 8, 2026 07:59
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.

2 participants