mirror of
https://github.com/elk-zone/elk.git
synced 2024-11-22 01:15:25 +03:00
3c43a1cdd1
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
87 lines
1.6 KiB
TypeScript
87 lines
1.6 KiB
TypeScript
import {
|
|
Node,
|
|
mergeAttributes,
|
|
nodeInputRule,
|
|
nodePasteRule,
|
|
} from '@tiptap/core'
|
|
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
|
|
|
function 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 TiptapPluginEmoji = Node.create({
|
|
name: 'em-emoji',
|
|
|
|
inline: () => true,
|
|
group: () => 'inline',
|
|
draggable: false,
|
|
|
|
parseHTML() {
|
|
return [
|
|
{
|
|
tag: 'img.iconify-emoji',
|
|
},
|
|
]
|
|
},
|
|
|
|
addAttributes() {
|
|
return {
|
|
alt: {
|
|
default: null,
|
|
},
|
|
src: {
|
|
default: null,
|
|
},
|
|
class: {
|
|
default: null,
|
|
},
|
|
}
|
|
},
|
|
|
|
renderHTML(args) {
|
|
return ['img', mergeAttributes(this.options.HTMLAttributes, args.HTMLAttributes)]
|
|
},
|
|
|
|
addCommands() {
|
|
return {
|
|
insertEmoji: code => ({ commands }) => {
|
|
return commands.insertContent({
|
|
type: this.name,
|
|
attrs: getEmojiAttributes(code),
|
|
})
|
|
},
|
|
}
|
|
},
|
|
|
|
addInputRules() {
|
|
return createEmojiRule(nodeInputRule, this.type)
|
|
},
|
|
|
|
addPasteRules() {
|
|
return createEmojiRule(nodePasteRule, this.type)
|
|
},
|
|
})
|