mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-10 07:19:02 +08:00
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:
parent
795fb715e3
commit
99de3eeff7
@ -421,17 +421,21 @@ export const useRichEditor = (options: UseRichEditorOptions = {}): UseRichEditor
|
||||
// Default behavior for non-code blocks
|
||||
const text = event.clipboardData?.getData('text/plain') ?? ''
|
||||
if (text) {
|
||||
const html = markdownToHtml(text)
|
||||
const { $from } = selection
|
||||
const atStartOfLine = $from.parentOffset === 0
|
||||
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) {
|
||||
const cleanHtml = html.replace(/^<p>(.*?)<\/p>/s, '$1')
|
||||
editor.commands.insertContent(cleanHtml)
|
||||
} else {
|
||||
editor.commands.insertContent(html)
|
||||
// Insert plain text without creating new paragraphs
|
||||
const tr = view.state.tr.insertText(text, selection.from, selection.to)
|
||||
view.dispatch(tr)
|
||||
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)
|
||||
return true
|
||||
}
|
||||
|
||||
@ -81,7 +81,7 @@ export interface TaskListOptions {
|
||||
const md = new MarkdownIt({
|
||||
html: true, // Enable HTML tags in source
|
||||
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
|
||||
typographer: false // Enable smartypants and other sweet transforms
|
||||
})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user