2024-03-05 04:56:59 +09:00
|
|
|
import type { ExtendedRegExpMatchArray, InputRuleFinder, nodeInputRule } from '@tiptap/core'
|
|
|
|
import type { NodeType } from '@tiptap/pm/model'
|
2022-12-28 03:38:57 +09:00
|
|
|
import {
|
2024-03-05 04:56:59 +09:00
|
|
|
callOrReturn,
|
2024-09-30 17:11:56 +09:00
|
|
|
InputRule,
|
2022-12-28 03:38:57 +09:00
|
|
|
mergeAttributes,
|
2024-09-30 17:11:56 +09:00
|
|
|
Node,
|
2023-01-05 17:50:11 +09:00
|
|
|
nodePasteRule,
|
2022-12-28 03:38:57 +09:00
|
|
|
} from '@tiptap/core'
|
2023-01-02 13:53:53 +09:00
|
|
|
import { emojiRegEx, getEmojiAttributes } from '~/config/emojis'
|
2022-12-28 03:38:57 +09:00
|
|
|
|
2024-02-25 04:26:14 +09:00
|
|
|
function wrapHandler<T extends (...args: any[]) => any>(handler: T): T {
|
|
|
|
return <T>((...args: any[]) => {
|
|
|
|
try {
|
|
|
|
return handler(...args)
|
|
|
|
}
|
2024-08-16 23:52:08 +09:00
|
|
|
catch {
|
2024-02-25 04:26:14 +09:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-05 23:48:58 +09:00
|
|
|
function createEmojiRule<NR extends typeof nodeInputRule | typeof nodePasteRule>(
|
|
|
|
nodeRule: NR,
|
|
|
|
type: Parameters<NR>[0]['type'],
|
|
|
|
): ReturnType<NR>[] {
|
2023-01-05 17:50:11 +09:00
|
|
|
const rule = nodeRule({
|
|
|
|
find: emojiRegEx as RegExp,
|
|
|
|
type,
|
2024-02-25 04:26:14 +09:00
|
|
|
getAttributes: (match: ExtendedRegExpMatchArray) => {
|
2023-01-05 17:50:11 +09:00
|
|
|
const [native] = match
|
|
|
|
return getEmojiAttributes(native)
|
|
|
|
},
|
|
|
|
}) as ReturnType<NR>
|
|
|
|
|
|
|
|
// Error catch for unsupported emoji
|
2024-02-25 04:26:14 +09:00
|
|
|
rule.handler = wrapHandler(rule.handler.bind(rule))
|
2023-01-05 17:50:11 +09:00
|
|
|
|
2024-02-25 04:26:14 +09:00
|
|
|
return [rule]
|
2023-01-05 17:50:11 +09:00
|
|
|
}
|
|
|
|
|
2023-01-16 20:40:47 +09:00
|
|
|
export const TiptapPluginEmoji = Node.create({
|
2022-12-28 04:13:50 +09:00
|
|
|
name: 'em-emoji',
|
2022-12-28 03:38:57 +09:00
|
|
|
|
2022-12-28 04:13:50 +09:00
|
|
|
inline: () => true,
|
|
|
|
group: () => 'inline',
|
|
|
|
draggable: false,
|
2022-12-28 03:38:57 +09:00
|
|
|
|
2022-12-28 04:13:50 +09:00
|
|
|
parseHTML() {
|
|
|
|
return [
|
|
|
|
{
|
2023-01-02 13:53:53 +09:00
|
|
|
tag: 'img.iconify-emoji',
|
2022-12-28 04:13:50 +09:00
|
|
|
},
|
|
|
|
]
|
2022-12-28 03:38:57 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
addAttributes() {
|
|
|
|
return {
|
2023-01-02 13:53:53 +09:00
|
|
|
alt: {
|
2022-12-28 03:38:57 +09:00
|
|
|
default: null,
|
|
|
|
},
|
2023-01-02 13:53:53 +09:00
|
|
|
src: {
|
|
|
|
default: null,
|
|
|
|
},
|
|
|
|
class: {
|
2022-12-28 05:42:58 +09:00
|
|
|
default: null,
|
|
|
|
},
|
2022-12-28 03:38:57 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2022-12-28 04:13:50 +09:00
|
|
|
renderHTML(args) {
|
2023-01-02 13:53:53 +09:00
|
|
|
return ['img', mergeAttributes(this.options.HTMLAttributes, args.HTMLAttributes)]
|
2022-12-28 03:38:57 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
addCommands() {
|
|
|
|
return {
|
2023-01-02 13:53:53 +09:00
|
|
|
insertEmoji: code => ({ commands }) => {
|
2022-12-28 03:38:57 +09:00
|
|
|
return commands.insertContent({
|
|
|
|
type: this.name,
|
2023-01-02 13:53:53 +09:00
|
|
|
attrs: getEmojiAttributes(code),
|
2022-12-28 03:38:57 +09:00
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
addInputRules() {
|
2024-03-05 04:56:59 +09:00
|
|
|
function emojiInputRule(config: {
|
|
|
|
find: InputRuleFinder
|
|
|
|
type: NodeType
|
|
|
|
getAttributes?:
|
2024-03-05 23:48:58 +09:00
|
|
|
| Record<string, any>
|
|
|
|
| ((match: ExtendedRegExpMatchArray) => Record<string, any>)
|
|
|
|
| false
|
|
|
|
| null
|
2024-03-05 04:56:59 +09:00
|
|
|
}) {
|
|
|
|
return new InputRule({
|
|
|
|
find: config.find,
|
|
|
|
handler: ({ state, range, match }) => {
|
|
|
|
const attributes = callOrReturn(config.getAttributes, undefined, match) || {}
|
|
|
|
const { tr } = state
|
|
|
|
const start = range.from
|
|
|
|
const end = range.to
|
|
|
|
|
|
|
|
tr.insert(start, config.type.create(attributes)).delete(
|
|
|
|
tr.mapping.map(start),
|
|
|
|
tr.mapping.map(end),
|
|
|
|
)
|
|
|
|
|
|
|
|
tr.scrollIntoView()
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return createEmojiRule(emojiInputRule, this.type)
|
2023-01-05 17:50:11 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
addPasteRules() {
|
|
|
|
return createEmojiRule(nodePasteRule, this.type)
|
2022-12-28 03:38:57 +09:00
|
|
|
},
|
|
|
|
})
|