Improve paste behavior to preserve inline text formatting

- Insert plain text when pasting mid-line to avoid unwanted line breaks
- Only convert markdown to HTML when pasting at line start or in empty paragraphs
- Disable automatic line break conversion in markdown parser to prevent extra paragraphs
This commit is contained in:
suyao 2025-10-24 04:43:56 +08:00
parent 795fb715e3
commit 99de3eeff7
No known key found for this signature in database
2 changed files with 10 additions and 6 deletions

View File

@ -421,17 +421,21 @@ export const useRichEditor = (options: UseRichEditorOptions = {}): UseRichEditor
// Default behavior for non-code blocks // Default behavior for non-code blocks
const text = event.clipboardData?.getData('text/plain') ?? '' const text = event.clipboardData?.getData('text/plain') ?? ''
if (text) { if (text) {
const html = markdownToHtml(text)
const { $from } = selection const { $from } = selection
const atStartOfLine = $from.parentOffset === 0 const atStartOfLine = $from.parentOffset === 0
const inEmptyParagraph = $from.parent.type.name === 'paragraph' && $from.parent.textContent === '' const inEmptyParagraph = $from.parent.type.name === 'paragraph' && $from.parent.textContent === ''
// If pasting in the middle of a line, insert as plain text to avoid unwanted line breaks
if (!atStartOfLine && !inEmptyParagraph) { if (!atStartOfLine && !inEmptyParagraph) {
const cleanHtml = html.replace(/^<p>(.*?)<\/p>/s, '$1') // Insert plain text without creating new paragraphs
editor.commands.insertContent(cleanHtml) const tr = view.state.tr.insertText(text, selection.from, selection.to)
} else { view.dispatch(tr)
editor.commands.insertContent(html) return true
} }
// At start of line or in empty paragraph: convert markdown to HTML and insert
const html = markdownToHtml(text)
editor.commands.insertContent(html)
onPaste?.(html) onPaste?.(html)
return true return true
} }

View File

@ -81,7 +81,7 @@ export interface TaskListOptions {
const md = new MarkdownIt({ const md = new MarkdownIt({
html: true, // Enable HTML tags in source html: true, // Enable HTML tags in source
xhtmlOut: true, // Use '>' for single tags (<br> instead of <br />) xhtmlOut: true, // Use '>' for single tags (<br> instead of <br />)
breaks: true, // Preserve line breaks when pasting text breaks: false,
linkify: false, // Autoconvert URL-like text to links linkify: false, // Autoconvert URL-like text to links
typographer: false // Enable smartypants and other sweet transforms typographer: false // Enable smartypants and other sweet transforms
}) })