Repository navigation
feat(preview): copy a Mermaid diagram as a PNG image - #1086
Merged
Merged
Conversation
A copy button in the diagram's top-right corner, like a code block's, and a Copy Diagram As Image item in its context menu. The diagram is drawn at twice its size on the preview's background and put on the clipboard through arboard, the same path the text copy uses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mermaid diagrams in the preview could only be saved as SVG, which WeChat, Feishu, Word and PowerPoint don't paste. This adds a copy button in the diagram's top-right corner, the same one code blocks have, and a Copy Diagram As Image item in its context menu. Both put a PNG on the clipboard.
Mechanism
diagramToPngBase64serializes the diagram's SVG, draws it onto a canvas at twice its viewBox size (longest side capped at 8192), and fills the preview's own background first. Mermaid leaves the background transparent, so a dark-theme diagram would otherwise paste as light text on white.clipboard_write_imagecommand, which writes it through arboard the wayclipboard_write_textdoes, including the X11 ownership hold from Copy doesn't work in the editor (paste works) #729. The webview's clipboard is not used.htmlLabels: false, so there is noforeignObjectto taint the canvas.onCopyDiagram, so exported HTML has none. It reuses.lang-label, so print already hides it. Its click stops propagation, so it doesn't also open the zoomed diagram.Tests
preview.richContent.test.tschecks that a diagram keeps exactly one button through a theme redraw, that clicking it calls back without propagating, and that a render without the callback has no button.diagramImage.test.tschecks the 2× size and the cap.Verification
npm run check,npm run lint,npm test(961 passed),npx vitest run(770 passed),vite buildandcheck-startup-chunk,cargo fmt --check,cargo clippy -D warnings,cargo test --locked(249 passed), bindings regenerated.Closes #1085