fix(editor): 修复Tiptap编辑器mention和emoji的文本转换问题
修改tiptapToString函数以正确处理mention节点和emoji节点的文本转换 添加mention配置参数char、allowSpaces和allowedPrefixes
This commit is contained in:
parent
b18a1b2604
commit
4863bc2220
@ -209,6 +209,9 @@ const editor = useEditor({
|
|||||||
},
|
},
|
||||||
suggestion: {
|
suggestion: {
|
||||||
...suggestion,
|
...suggestion,
|
||||||
|
char: '@',
|
||||||
|
allowSpaces: false,
|
||||||
|
allowedPrefixes: null,
|
||||||
items: ({ query }) => {
|
items: ({ query }) => {
|
||||||
return suggestion.items({
|
return suggestion.items({
|
||||||
query,
|
query,
|
||||||
@ -496,7 +499,36 @@ function tiptapToMessage() {
|
|||||||
function tiptapToString() {
|
function tiptapToString() {
|
||||||
if (!editor.value) return ''
|
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()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user