From 516680d0cbe5afc021e84074bd20ed93febbb8a8 Mon Sep 17 00:00:00 2001 From: Phoenix <64720302+Concur-max@users.noreply.github.com> Date: Mon, 7 Jul 2025 10:41:12 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=BC=96=E8=BE=91=E5=99=A8):=20=E5=A2=9E?= =?UTF-8?q?=E5=BC=BAtiptap=E7=BC=96=E8=BE=91=E5=99=A8=E7=9A=84mention?= =?UTF-8?q?=E5=92=8C=E6=96=87=E6=9C=AC=E8=BD=AC=E6=8D=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改mention插件的触发字符配置,并优化tiptapToString方法以正确处理mention、表情和换行符。现在能够保留表情的alt文本并正确格式化mention输出。 --- 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..ee349e5 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() }