mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-07 22:10:21 +08:00
Improve paste behavior in styled blocks and multi-line content
- Preserve block styles (headings, blockquotes, list items) when pasting single-line text - Handle multi-line paste content by converting markdown to HTML - Maintain existing plain text insertion for mid-line pastes in content blocks
This commit is contained in:
parent
99de3eeff7
commit
d5dd8bc123
@ -422,18 +422,31 @@ export const useRichEditor = (options: UseRichEditorOptions = {}): UseRichEditor
|
|||||||
const text = event.clipboardData?.getData('text/plain') ?? ''
|
const text = event.clipboardData?.getData('text/plain') ?? ''
|
||||||
if (text) {
|
if (text) {
|
||||||
const { $from } = selection
|
const { $from } = selection
|
||||||
|
const parentNode = $from.parent
|
||||||
const atStartOfLine = $from.parentOffset === 0
|
const atStartOfLine = $from.parentOffset === 0
|
||||||
const inEmptyParagraph = $from.parent.type.name === 'paragraph' && $from.parent.textContent === ''
|
const isEmptyBlock = parentNode.textContent === ''
|
||||||
|
const hasMultipleLines = text.includes('\n')
|
||||||
|
|
||||||
// If pasting in the middle of a line, insert as plain text to avoid unwanted line breaks
|
// Check if we're in a styled block (heading, blockquote, etc.) that should preserve its style
|
||||||
if (!atStartOfLine && !inEmptyParagraph) {
|
const styledBlocks = ['heading', 'blockquote', 'listItem']
|
||||||
// Insert plain text without creating new paragraphs
|
const isInStyledBlock = styledBlocks.includes(parentNode.type.name)
|
||||||
|
|
||||||
|
// If in a styled block (like H1), always insert as plain text to preserve the style
|
||||||
|
// even if the block is empty or we're at the start
|
||||||
|
if (isInStyledBlock && !hasMultipleLines) {
|
||||||
const tr = view.state.tr.insertText(text, selection.from, selection.to)
|
const tr = view.state.tr.insertText(text, selection.from, selection.to)
|
||||||
view.dispatch(tr)
|
view.dispatch(tr)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// At start of line or in empty paragraph: convert markdown to HTML and insert
|
// If pasting in the middle of a line (not at start, block has content), insert plain text
|
||||||
|
if (!atStartOfLine && !isEmptyBlock && !hasMultipleLines) {
|
||||||
|
const tr = view.state.tr.insertText(text, selection.from, selection.to)
|
||||||
|
view.dispatch(tr)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise (at start of paragraph, or multi-line paste): convert markdown to HTML
|
||||||
const html = markdownToHtml(text)
|
const html = markdownToHtml(text)
|
||||||
editor.commands.insertContent(html)
|
editor.commands.insertContent(html)
|
||||||
onPaste?.(html)
|
onPaste?.(html)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user