mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-12 00:49:14 +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
|
// 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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user