fix: 🐛

This commit is contained in:
codytseng
2025-07-11 23:00:45 +08:00
parent 8db655cc37
commit 069ac34b86
4 changed files with 25 additions and 9 deletions

View File

@@ -104,7 +104,7 @@ export default function PostContent({
<div className="flex gap-2 items-center"> <div className="flex gap-2 items-center">
<Uploader <Uploader
onUploadSuccess={({ url }) => { onUploadSuccess={({ url }) => {
textareaRef.current?.appendText(url + '\n') textareaRef.current?.appendText(url, true)
}} }}
onUploadingChange={(uploading) => onUploadingChange={(uploading) =>
setUploadingFiles((prev) => (uploading ? prev + 1 : prev - 1)) setUploadingFiles((prev) => (uploading ? prev + 1 : prev - 1))

View File

@@ -75,8 +75,15 @@ export const ClipboardAndDropHandler = Extension.create<ClipboardAndDropHandlerO
} }
} else if (item.kind === 'string' && item.type === 'text/plain') { } else if (item.kind === 'string' && item.type === 'text/plain') {
item.getAsString((text) => { item.getAsString((text) => {
const textNode = view.state.schema.text(text) const { schema } = view.state
const tr = view.state.tr.replaceSelectionWith(textNode) 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) view.dispatch(tr)
}) })
handled = true handled = true
@@ -99,8 +106,10 @@ async function uploadFile(view: EditorView, file: File, options: ClipboardAndDro
options.onUploadStart?.(file) options.onUploadStart?.(file)
const placeholder = `[Uploading "${name}"...]` const placeholder = `[Uploading "${name}"...]`
const uploadingNode = view.state.schema.text(placeholder + '\n') const uploadingNode = view.state.schema.text(placeholder)
const tr = view.state.tr.replaceSelectionWith(uploadingNode) 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) view.dispatch(tr)
mediaUpload mediaUpload

View File

@@ -19,7 +19,7 @@ import Preview from './Preview'
import suggestion from './suggestion' import suggestion from './suggestion'
export type TPostTextareaHandle = { export type TPostTextareaHandle = {
appendText: (text: string) => void appendText: (text: string, addNewline?: boolean) => void
insertText: (text: string) => void insertText: (text: string) => void
} }
@@ -80,9 +80,9 @@ const PostTextarea = forwardRef<
}) })
useImperativeHandle(ref, () => ({ useImperativeHandle(ref, () => ({
appendText: (text: string) => { appendText: (text: string, addNewline = false) => {
if (editor) { if (editor) {
editor let chain = editor
.chain() .chain()
.focus() .focus()
.command(({ tr, dispatch }) => { .command(({ tr, dispatch }) => {
@@ -95,7 +95,10 @@ const PostTextarea = forwardRef<
return true return true
}) })
.insertContent(text) .insertContent(text)
.run() if (addNewline) {
chain = chain.setHardBreak()
}
chain.run()
} }
}, },
insertText: (text: string) => { insertText: (text: string) => {

View File

@@ -23,6 +23,10 @@ function _parseEditorJsonToText(node?: JSONContent): string {
return node.text || '' return node.text || ''
} }
if (node.type === 'hardBreak') {
return '\n'
}
if (Array.isArray(node.content)) { if (Array.isArray(node.content)) {
return ( return (
node.content.map(_parseEditorJsonToText).join('') + (node.type === 'paragraph' ? '\n' : '') node.content.map(_parseEditorJsonToText).join('') + (node.type === 'paragraph' ? '\n' : '')