feat(编辑器): 增强tiptap编辑器的mention和文本转换功能
修改mention插件的触发字符配置,并优化tiptapToString方法以正确处理mention、表情和换行符。现在能够保留表情的alt文本并正确格式化mention输出。
This commit is contained in:
parent
e5a5b36dcc
commit
516680d0cb
@ -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()
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user