2022-12-27 22:13:50 +03:00
|
|
|
import {
|
|
|
|
mergeAttributes,
|
2024-09-30 11:11:56 +03:00
|
|
|
Node,
|
2022-12-27 22:13:50 +03:00
|
|
|
nodeInputRule,
|
|
|
|
} from '@tiptap/core'
|
|
|
|
|
|
|
|
export interface EmojiOptions {
|
|
|
|
inline: boolean
|
|
|
|
allowBase64: boolean
|
|
|
|
HTMLAttributes: Record<string, any>
|
|
|
|
}
|
|
|
|
|
|
|
|
declare module '@tiptap/core' {
|
|
|
|
interface Commands<ReturnType> {
|
|
|
|
emoji: {
|
|
|
|
/**
|
|
|
|
* Insert a custom emoji.
|
|
|
|
*/
|
2024-03-05 17:48:58 +03:00
|
|
|
insertCustomEmoji: (options: {
|
|
|
|
src: string
|
|
|
|
alt?: string
|
|
|
|
title?: string
|
|
|
|
}) => ReturnType
|
2022-12-27 22:13:50 +03:00
|
|
|
/**
|
|
|
|
* Insert a emoji.
|
|
|
|
*/
|
|
|
|
insertEmoji: (native: string) => ReturnType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-16 17:52:08 +03:00
|
|
|
const inputRegex = /(?:^|\s)(!\[(.+|:?)\]\((\S+)(?:\s+["'](\S+)["'])?\))$/
|
2022-12-27 22:13:50 +03:00
|
|
|
|
2023-01-16 14:40:47 +03:00
|
|
|
export const TiptapPluginCustomEmoji = Node.create<EmojiOptions>({
|
2022-12-27 22:13:50 +03:00
|
|
|
name: 'custom-emoji',
|
|
|
|
|
|
|
|
addOptions() {
|
|
|
|
return {
|
|
|
|
inline: false,
|
|
|
|
allowBase64: false,
|
|
|
|
HTMLAttributes: {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
inline() {
|
|
|
|
return this.options.inline
|
|
|
|
},
|
|
|
|
|
|
|
|
group() {
|
|
|
|
return this.options.inline ? 'inline' : 'block'
|
|
|
|
},
|
|
|
|
|
|
|
|
draggable: false,
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
|
|
|
'src': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
'alt': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
'title': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
'width': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
'height': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
'data-emoji-id': {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
tag: this.options.allowBase64
|
|
|
|
? 'img[src]'
|
|
|
|
: 'img[src]:not([src^="data:"])',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
|
|
|
|
renderHTML({ HTMLAttributes }) {
|
|
|
|
return ['img', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)]
|
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
|
|
|
insertCustomEmoji: options => ({ commands }) => {
|
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
|
|
|
attrs: options,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
|
|
|
return [
|
|
|
|
nodeInputRule({
|
|
|
|
find: inputRegex,
|
|
|
|
type: this.type,
|
|
|
|
getAttributes: (match) => {
|
|
|
|
const [,, alt, src, title] = match
|
|
|
|
|
|
|
|
return { src, alt, title }
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
},
|
|
|
|
})
|