Skip to content

[Markdown Plugin] Support auto-conversion on native paste and add undoConversion option#3381

Open
juliaroldi wants to merge 5 commits into
masterfrom
u/juliaroldi/markdown-plugin-dedection
Open

[Markdown Plugin] Support auto-conversion on native paste and add undoConversion option#3381
juliaroldi wants to merge 5 commits into
masterfrom
u/juliaroldi/markdown-plugin-dedection

Conversation

@juliaroldi

Copy link
Copy Markdown
Contributor

Summary

Improves the Markdown paste plugin so that auto-conversion works for native (Ctrl+V) paste, not just the explicit "Paste as Markdown" path. MarkdownPastePlugin now reacts to the contentChanged event with source Paste: when autoConversion is enabled and the pasted content is detected as Markdown, it merges the converted Content Model into modelBeforePaste and re-applies it via formatContentModel. A new undoConversion option (default false) takes an extra undo snapshot before the conversion so the user can undo the Markdown conversion separately from the paste.

To distinguish genuine native paste events from programmatic clipboard reads, extractClipboardItems now accepts an isPasteNative flag that drives ClipboardData.pasteNativeEvent (previously hard-coded to true). CopyPastePlugin passes true for real paste events, while demo/scripts/utils/readClipboardData.ts leaves it undefined. The demo's editor options gain an "Undo auto-converted markdown" checkbox wired to the new option.

How to test

  1. Run the unit tests:
    yarn test:fast --testPathPattern=MarkdownPastePlugin
  2. Manual: In the demo (yarn builddemo), enable the Markdown plugin with "Auto convert on paste" on. Copy Markdown text (e.g. # Title or **bold**) and paste with Ctrl+V.
    • Before this change: native paste did not auto-convert Markdown (only "Paste as Markdown" did).
    • After this change: the pasted Markdown is converted in place.
  3. With "Undo auto-converted markdown" enabled, paste Markdown and press Ctrl+Z — the conversion is undone as its own step.

@github-actions

github-actions Bot commented Jun 23, 2026

Copy link
Copy Markdown
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://microsoft.github.io/roosterjs/pr-preview/pr-3381/

Built to branch gh-pages at 2026-06-24 18:12 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@juliaroldi juliaroldi marked this pull request as ready for review June 23, 2026 23:00
this.options.autoConversion
) {
const clipboardData = event.data as ClipboardData;
const shouldConvert = this.options.autoConversion && clipboardData.pasteNativeEvent;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Need null check for clipboardData

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

clipboardData itself needs null check. It comes from event.data. But event.data is not guaranteed to have valid value.

isPastedContentMarkdown(this.editor, clipboardData) &&
clipboardData.modelBeforePaste
) {
mergeModel(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggest clone a model from clipboardData.modelBeforePaste, but do not modify the original one.

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