From 4863bc2220e7c0b681172fc08b8b92fe8a8a60a9 Mon Sep 17 00:00:00 2001 From: Phoenix <64720302+Concur-max@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:42:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E4=BF=AE=E5=A4=8DTiptap?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8mention=E5=92=8Cemoji=E7=9A=84?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E8=BD=AC=E6=8D=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改tiptapToString函数以正确处理mention节点和emoji节点的文本转换 添加mention配置参数char、allowSpaces和allowedPrefixes --- src/components/editor/TiptapEditor.vue | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/components/editor/TiptapEditor.vue b/src/components/editor/TiptapEditor.vue index 7969b2a..8194aca 100644 --- a/src/components/editor/TiptapEditor.vue +++ b/src/components/editor/TiptapEditor.vue @@ -209,6 +209,9 @@ const editor = useEditor({ }, suggestion: { ...suggestion, + char: '@', + allowSpaces: false, + allowedPrefixes: null, items: ({ query }) => { return suggestion.items({ query, @@ -496,7 +499,36 @@ function tiptapToMessage() { function tiptapToString() { if (!editor.value) return '' - return editor.value.getText() + const json = editor.value.getJSON() + let result = '' + + const processInlines = nodes => { + nodes.forEach(node => { + if (node.type === 'text') { + result += node.text + } else if (node.type === 'mention') { + result += `@${node.attrs.label} ` + } else if (node.type === 'emoji') { + // 关键修改:使用表情的alt文本而不是忽略 + result += node.attrs.alt || '' + } else if (node.type === 'hardBreak') { + result += '\n' + } + }) + } + + if (json.content) { + json.content.forEach(node => { + if (node.type === 'paragraph') { + if (node.content) { + processInlines(node.content) + } + result += '\n' + } + }) + } + + return result.trim() }