fix: emoji replacement when pasting (#777)
parent
d4e99566b8
commit
bd1b9fb5c8
|
@ -2,9 +2,39 @@ import {
|
||||||
Node,
|
Node,
|
||||||
mergeAttributes,
|
mergeAttributes,
|
||||||
nodeInputRule,
|
nodeInputRule,
|
||||||
|
nodePasteRule,
|
||||||
} from '@tiptap/core'
|
} from '@tiptap/core'
|
||||||
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
||||||
|
|
||||||
|
const createEmojiRule = <NR extends typeof nodeInputRule | typeof nodePasteRule>(
|
||||||
|
nodeRule: NR,
|
||||||
|
type: Parameters<NR>[0]['type'],
|
||||||
|
): ReturnType<NR>[] => {
|
||||||
|
const rule = nodeRule({
|
||||||
|
find: emojiRegEx as RegExp,
|
||||||
|
type,
|
||||||
|
getAttributes: (match) => {
|
||||||
|
const [native] = match
|
||||||
|
return getEmojiAttributes(native)
|
||||||
|
},
|
||||||
|
}) as ReturnType<NR>
|
||||||
|
|
||||||
|
// Error catch for unsupported emoji
|
||||||
|
const handler = rule.handler.bind(rule)
|
||||||
|
rule.handler = (...args) => {
|
||||||
|
try {
|
||||||
|
return handler(...args)
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
rule,
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
export const Emoji = Node.create({
|
export const Emoji = Node.create({
|
||||||
name: 'em-emoji',
|
name: 'em-emoji',
|
||||||
|
|
||||||
|
@ -50,26 +80,10 @@ export const Emoji = Node.create({
|
||||||
},
|
},
|
||||||
|
|
||||||
addInputRules() {
|
addInputRules() {
|
||||||
const inputRule = nodeInputRule({
|
return createEmojiRule(nodeInputRule, this.type)
|
||||||
find: emojiRegEx as RegExp,
|
|
||||||
type: this.type,
|
|
||||||
getAttributes: (match) => {
|
|
||||||
const [native] = match
|
|
||||||
return getEmojiAttributes(native)
|
|
||||||
},
|
},
|
||||||
})
|
|
||||||
// Error catch for unsupported emoji
|
addPasteRules() {
|
||||||
const handler = inputRule.handler.bind(inputRule)
|
return createEmojiRule(nodePasteRule, this.type)
|
||||||
inputRule.handler = (...args) => {
|
|
||||||
try {
|
|
||||||
return handler(...args)
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [
|
|
||||||
inputRule,
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue