From 069ac34b8606f5e8b40c321f95d457ee000bbf86 Mon Sep 17 00:00:00 2001 From: codytseng Date: Fri, 11 Jul 2025 23:00:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/PostEditor/PostContent.tsx | 2 +- .../PostTextarea/ClipboardAndDropHandler.ts | 17 +++++++++++++---- .../PostEditor/PostTextarea/index.tsx | 11 +++++++---- src/lib/tiptap.ts | 4 ++++ 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/components/PostEditor/PostContent.tsx b/src/components/PostEditor/PostContent.tsx index ae871d1..d2031b7 100644 --- a/src/components/PostEditor/PostContent.tsx +++ b/src/components/PostEditor/PostContent.tsx @@ -104,7 +104,7 @@ export default function PostContent({
{ - textareaRef.current?.appendText(url + '\n') + textareaRef.current?.appendText(url, true) }} onUploadingChange={(uploading) => setUploadingFiles((prev) => (uploading ? prev + 1 : prev - 1)) diff --git a/src/components/PostEditor/PostTextarea/ClipboardAndDropHandler.ts b/src/components/PostEditor/PostTextarea/ClipboardAndDropHandler.ts index 3f2c290..4164dec 100644 --- a/src/components/PostEditor/PostTextarea/ClipboardAndDropHandler.ts +++ b/src/components/PostEditor/PostTextarea/ClipboardAndDropHandler.ts @@ -75,8 +75,15 @@ export const ClipboardAndDropHandler = Extension.create { - const textNode = view.state.schema.text(text) - const tr = view.state.tr.replaceSelectionWith(textNode) + const { schema } = view.state + const parts = text.split('\n') + const nodes = [] + for (let i = 0; i < parts.length; i++) { + if (i > 0) nodes.push(schema.nodes.hardBreak.create()) + if (parts[i]) nodes.push(schema.text(parts[i])) + } + const fragment = schema.nodes.paragraph.create(null, nodes) + const tr = view.state.tr.replaceSelectionWith(fragment) view.dispatch(tr) }) handled = true @@ -99,8 +106,10 @@ async function uploadFile(view: EditorView, file: File, options: ClipboardAndDro options.onUploadStart?.(file) const placeholder = `[Uploading "${name}"...]` - const uploadingNode = view.state.schema.text(placeholder + '\n') - const tr = view.state.tr.replaceSelectionWith(uploadingNode) + const uploadingNode = view.state.schema.text(placeholder) + const hardBreakNode = view.state.schema.nodes.hardBreak.create() + let tr = view.state.tr.replaceSelectionWith(uploadingNode) + tr = tr.insert(tr.selection.from, hardBreakNode) view.dispatch(tr) mediaUpload diff --git a/src/components/PostEditor/PostTextarea/index.tsx b/src/components/PostEditor/PostTextarea/index.tsx index 5836486..d1ec6b1 100644 --- a/src/components/PostEditor/PostTextarea/index.tsx +++ b/src/components/PostEditor/PostTextarea/index.tsx @@ -19,7 +19,7 @@ import Preview from './Preview' import suggestion from './suggestion' export type TPostTextareaHandle = { - appendText: (text: string) => void + appendText: (text: string, addNewline?: boolean) => void insertText: (text: string) => void } @@ -80,9 +80,9 @@ const PostTextarea = forwardRef< }) useImperativeHandle(ref, () => ({ - appendText: (text: string) => { + appendText: (text: string, addNewline = false) => { if (editor) { - editor + let chain = editor .chain() .focus() .command(({ tr, dispatch }) => { @@ -95,7 +95,10 @@ const PostTextarea = forwardRef< return true }) .insertContent(text) - .run() + if (addNewline) { + chain = chain.setHardBreak() + } + chain.run() } }, insertText: (text: string) => { diff --git a/src/lib/tiptap.ts b/src/lib/tiptap.ts index c993997..abac6b7 100644 --- a/src/lib/tiptap.ts +++ b/src/lib/tiptap.ts @@ -23,6 +23,10 @@ function _parseEditorJsonToText(node?: JSONContent): string { return node.text || '' } + if (node.type === 'hardBreak') { + return '\n' + } + if (Array.isArray(node.content)) { return ( node.content.map(_parseEditorJsonToText).join('') + (node.type === 'paragraph' ? '\n' : '')