2022-12-27 21:38:57 +03:00
|
|
|
import {
|
|
|
|
Node,
|
|
|
|
mergeAttributes,
|
|
|
|
nodeInputRule,
|
|
|
|
} from '@tiptap/core'
|
2023-01-02 07:53:53 +03:00
|
|
|
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
2022-12-27 21:38:57 +03:00
|
|
|
|
2022-12-27 22:13:50 +03:00
|
|
|
export const Emoji = Node.create({
|
|
|
|
name: 'em-emoji',
|
2022-12-27 21:38:57 +03:00
|
|
|
|
2022-12-27 22:13:50 +03:00
|
|
|
inline: () => true,
|
|
|
|
group: () => 'inline',
|
|
|
|
draggable: false,
|
2022-12-27 21:38:57 +03:00
|
|
|
|
2022-12-27 22:13:50 +03:00
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2023-01-02 07:53:53 +03:00
|
|
|
tag: 'img.iconify-emoji',
|
2022-12-27 22:13:50 +03:00
|
|
|
},
|
|
|
|
]
|
2022-12-27 21:38:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2023-01-02 07:53:53 +03:00
|
|
|
alt: {
|
2022-12-27 21:38:57 +03:00
|
|
|
default: null,
|
|
|
|
},
|
2023-01-02 07:53:53 +03:00
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
class: {
|
2022-12-27 23:42:58 +03:00
|
|
|
default: null,
|
|
|
|
},
|
2022-12-27 21:38:57 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-12-27 22:13:50 +03:00
|
|
|
renderHTML(args) {
|
2023-01-02 07:53:53 +03:00
|
|
|
return ['img', mergeAttributes(this.options.HTMLAttributes, args.HTMLAttributes)]
|
2022-12-27 21:38:57 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2023-01-02 07:53:53 +03:00
|
|
|
insertEmoji: code => ({ commands }) => {
|
2022-12-27 21:38:57 +03:00
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
2023-01-02 07:53:53 +03:00
|
|
|
attrs: getEmojiAttributes(code),
|
2022-12-27 21:38:57 +03:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
2022-12-27 23:48:01 +03:00
|
|
|
const inputRule = nodeInputRule({
|
2023-01-02 07:53:53 +03:00
|
|
|
find: emojiRegEx as RegExp,
|
2022-12-27 23:48:01 +03:00
|
|
|
type: this.type,
|
|
|
|
getAttributes: (match) => {
|
|
|
|
const [native] = match
|
2023-01-02 07:53:53 +03:00
|
|
|
return getEmojiAttributes(native)
|
2022-12-27 23:48:01 +03:00
|
|
|
},
|
|
|
|
})
|
|
|
|
// 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 21:38:57 +03:00
|
|
|
return [
|
2022-12-27 23:48:01 +03:00
|
|
|
inputRule,
|
2022-12-27 21:38:57 +03:00
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|