2022-12-27 18:38:57 +00:00
|
|
|
import {
|
|
|
|
Node,
|
|
|
|
mergeAttributes,
|
|
|
|
nodeInputRule,
|
|
|
|
} from '@tiptap/core'
|
|
|
|
|
2022-12-27 19:13:50 +00:00
|
|
|
export const Emoji = Node.create({
|
|
|
|
name: 'em-emoji',
|
2022-12-27 18:38:57 +00:00
|
|
|
|
2022-12-27 19:13:50 +00:00
|
|
|
inline: () => true,
|
|
|
|
group: () => 'inline',
|
|
|
|
draggable: false,
|
2022-12-27 18:38:57 +00:00
|
|
|
|
2022-12-27 19:13:50 +00:00
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: 'em-emoji[native]',
|
|
|
|
},
|
|
|
|
]
|
2022-12-27 18:38:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2022-12-27 19:13:50 +00:00
|
|
|
native: {
|
2022-12-27 18:38:57 +00:00
|
|
|
default: null,
|
|
|
|
},
|
2022-12-27 20:42:58 +00:00
|
|
|
fallback: {
|
|
|
|
default: null,
|
|
|
|
},
|
2022-12-27 18:38:57 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-12-27 19:13:50 +00:00
|
|
|
renderHTML(args) {
|
|
|
|
return ['em-emoji', mergeAttributes(this.options.HTMLAttributes, args.HTMLAttributes)]
|
2022-12-27 18:38:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2022-12-27 19:13:50 +00:00
|
|
|
insertEmoji: name => ({ commands }) => {
|
2022-12-27 18:38:57 +00:00
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
2022-12-27 19:13:50 +00:00
|
|
|
attrs: {
|
|
|
|
native: name,
|
2022-12-27 20:42:58 +00:00
|
|
|
fallback: name,
|
2022-12-27 19:13:50 +00:00
|
|
|
},
|
2022-12-27 18:38:57 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
2022-12-27 20:48:01 +00:00
|
|
|
const inputRule = nodeInputRule({
|
|
|
|
find: EMOJI_REGEX,
|
|
|
|
type: this.type,
|
|
|
|
getAttributes: (match) => {
|
|
|
|
const [native] = match
|
|
|
|
return {
|
|
|
|
native,
|
|
|
|
fallback: native,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// Error catch for unsupported emoji
|
|
|
|
const handler = inputRule.handler.bind(inputRule)
|
|
|
|
inputRule.handler = (...args) => {
|
|
|
|
try {
|
|
|
|
return handler(...args)
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 18:38:57 +00:00
|
|
|
return [
|
2022-12-27 20:48:01 +00:00
|
|
|
inputRule,
|
2022-12-27 18:38:57 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|