elk/composables/tiptap/emoji.ts

60 lines
976 B
TypeScript
Raw Normal View History

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 19:13:50 +00:00
inline: () => true,
group: () => 'inline',
draggable: false,
2022-12-27 19:13:50 +00:00
parseHTML() {
return [
{
tag: 'em-emoji[native]',
},
]
},
addAttributes() {
return {
2022-12-27 19:13:50 +00:00
native: {
default: null,
},
}
},
2022-12-27 19:13:50 +00:00
renderHTML(args) {
return ['em-emoji', mergeAttributes(this.options.HTMLAttributes, args.HTMLAttributes)]
},
addCommands() {
return {
2022-12-27 19:13:50 +00:00
insertEmoji: name => ({ commands }) => {
return commands.insertContent({
type: this.name,
2022-12-27 19:13:50 +00:00
attrs: {
native: name,
},
})
},
}
},
addInputRules() {
return [
nodeInputRule({
2022-12-27 19:13:50 +00:00
find: EMOJI_REGEX,
type: this.type,
getAttributes: (match) => {
2022-12-27 19:13:50 +00:00
const [native] = match
return { native }
},
}),
]
},
})