feat(muya): emit format-click and preview-image interaction events by Jocs · Pull Request #4390 · marktext/marktext · GitHub
Skip to content

feat(muya): emit format-click and preview-image interaction events - #4390

Merged
Jocs merged 2 commits into
developfrom
feat/muya-format-click-events
Jun 8, 2026
Merged

Jocs merged 2 commits into
developfrom
feat/muya-format-click-events

Conversation

@Jocs

@Jocs Jocs commented Jun 8, 2026

Copy link
Copy Markdown
Member

Motivation

The desktop app is migrating off the legacy packages/muyajs engine to packages/muya (@muyajs/core). The legacy engine emitted custom interaction events that the desktop renderer (packages/desktop/src/renderer/src/components/editorWithTabs/editor.vue) subscribes to so a Cmd/Ctrl-click can open a link or preview an image. packages/muya rendered links/images but had no emitter for these events, so the migration would silently lose link-open and image-preview-on-click.

This PR ports the two click events the desktop consumes. (The related selection-change cursorCoords/formats work is already merged; this PR is only the click events.)

Events + payloads

Ported faithfully from the legacy engine (packages/muyajs/lib/contentState/clickCtrl.js link path + packages/muyajs/lib/eventHandler/clickEvent.js image path):

  • format-click (link) — editor/linkMouseEvents.ts. On a Cmd/Ctrl-click of a rendered link, emits { event, formatType: 'link', data } where data is the getLinkInfo payload ({ href, raw, text, range }, a superset of the legacy { text, href }). Plain (no-modifier) clicks keep their existing cursor-placement-only behavior — the preventDefault() on anchors is unchanged.
  • format-click (image) — selection/index.ts (_handleClickInlineImage). On a Cmd/Ctrl-click of an <img>, emits { event, formatType: 'image', data: <src> }. The src is resolved via getImageSrc(token.src || token.attrs.src) (the same path-resolution the renderer uses), falling back to the rendered <img>'s own src attribute. The plain-click image-toolbar / transformer / selector / resize path is untouched.

The desktop's format-click handler re-checks the OS modifier and opens the link (FORMAT_LINK_CLICK) or a SimpleImageViewer, so muya stays platform-agnostic and only emits.

Both checks gate on the modifier in muya so plain clicks are unaffected. packages/muya/src/muya.ts was intentionally not touched.

Deferred

  • heading-copy-link is not implemented here. It requires new heading-hover UI that does not yet exist in packages/muya (legacy fired it from a .ag-copy-header-link button that the new renderer has no equivalent for). Out of scope for this PR.
  • preview-image is not emitted from the click path: in the legacy engine it fired only from the Space-key handler (keyboard.js), not on click, and on the desktop side it does the same thing as the image format-click (opens the viewer). Emitting it on a modifier-click too would open the viewer twice, so the click path uses the image format-click only — matching legacy.

Tests

Adds packages/muya/src/__tests__/formatClickEvents.spec.ts (happy-dom, follows the getTOC.spec.ts boot pattern). It subscribes via muya.on('format-click', ...) and simulates modifier-clicks on a rendered link and image, asserting the payload shape, the macOS (metaKey) and non-macOS (ctrlKey) variants, and that plain clicks do not emit.

Verify

All green in packages/muya:

  • pnpm -C packages/muya lint — 0 errors (pre-existing complexity warnings only, none in changed files)
  • pnpm -C packages/muya lint:types — passes
  • pnpm -C packages/muya check-circular — no circular dependency
  • pnpm -C packages/muya test — 417 passed (51 files; +5 new)
  • pnpm -C packages/muya test:spec — 1347 passed

🤖 Generated with Claude Code

Desktop is migrating off legacy packages/muyajs to packages/muya
(@muyajs/core). The legacy engine emitted custom interaction events that
the desktop renderer's editor.vue subscribes to in order to open links and
preview images on a Cmd/Ctrl-click. packages/muya had no such emitter, so
port the link + image format-click events:

- editor/linkMouseEvents.ts: on a Cmd/Ctrl-click of a rendered link, emit
  `format-click` with { event, formatType: 'link', data } where data is the
  getLinkInfo payload (superset of the legacy { text, href }). Plain clicks
  keep their cursor-placement-only behavior (preventDefault unchanged).
- selection/index.ts (_handleClickInlineImage): on a Cmd/Ctrl-click of an
  <img>, emit `format-click` with { event, formatType: 'image', data: <src> }
  (src resolved via getImageSrc, falling back to the <img> src attribute).
  The plain-click image-toolbar / transformer / selector path is untouched.

The desktop's format-click handler re-checks the OS modifier and opens the
link / SimpleImageViewer, so muya stays platform-agnostic and only emits.

heading-copy-link is deferred: it needs new heading-hover UI that does not
exist in packages/muya yet.

Adds a happy-dom vitest spec that subscribes via muya.on('format-click')
and simulates modifier-clicks on a rendered link and image, asserting the
payload shape and that plain clicks do not emit.

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

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

This PR restores legacy interaction behavior during the ongoing migration from packages/muyajs to packages/muya by emitting desktop-consumed click events for Cmd/Ctrl-clicks on rendered links and inline images.

Changes:

  • Emit format-click for Cmd/Ctrl-clicked links from editor/linkMouseEvents.ts with a getLinkInfo()-based payload.
  • Emit format-click for Cmd/Ctrl-clicked inline images from selection/index.ts, resolving the image src via getImageSrc() with an attribute fallback.
  • Add a Vitest (happy-dom) spec to ensure modifier-clicks emit and plain clicks do not.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
packages/muya/src/selection/index.ts Emits format-click for modifier-click inline images, providing a resolved src string.
packages/muya/src/editor/linkMouseEvents.ts Emits format-click for modifier-click links while keeping existing click-suppression behavior.
packages/muya/src/tests/formatClickEvents.spec.ts Adds regression tests for link/image format-click emissions and plain-click non-emission.

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

Comment on lines +118 to +132

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added in dbbcff5 — there's now an explicit image Ctrl-click test (dispatchClick(img, { ctrlKey: true })) asserting the same payload, mirroring the existing link Ctrl-click case. Both modifier paths are now covered.

Address Copilot review: the image format-click tests only exercised the
macOS metaKey modifier, but the handler emits on (metaKey || ctrlKey). Add a
Ctrl-click case for the image path, mirroring the existing link Ctrl-click test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Jocs
Jocs merged commit c2c4a1e into develop Jun 8, 2026
9 checks passed
@Jocs
Jocs deleted the feat/muya-format-click-events branch June 8, 2026 05:31
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