debian-mirror-gitlab/app/assets/javascripts/content_editor/extensions/emoji.js

84 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-12-11 22:18:48 +05:30
import { Node, InputRule } from '@tiptap/core';
2021-10-27 15:23:28 +05:30
import { initEmojiMap, getAllEmoji } from '~/emoji';
export default Node.create({
name: 'emoji',
inline: true,
group: 'inline',
draggable: true,
addAttributes() {
return {
moji: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => element.textContent,
2021-10-27 15:23:28 +05:30
},
name: {
default: null,
2021-11-11 11:23:49 +05:30
parseHTML: (element) => element.dataset.name,
2021-10-27 15:23:28 +05:30
},
title: {
default: null,
},
unicodeVersion: {
default: '6.0',
2021-11-11 11:23:49 +05:30
parseHTML: (element) => element.dataset.unicodeVersion,
2021-10-27 15:23:28 +05:30
},
};
},
parseHTML() {
return [
{
tag: 'gl-emoji',
},
];
},
renderHTML({ node }) {
return [
'gl-emoji',
{
'data-name': node.attrs.name,
title: node.attrs.title,
'data-unicode-version': node.attrs.unicodeVersion,
},
node.attrs.moji,
];
},
addInputRules() {
2021-12-11 22:18:48 +05:30
const emojiInputRegex = /(?:^|\s)(:(\w+):)$/;
2021-10-27 15:23:28 +05:30
return [
2021-12-11 22:18:48 +05:30
new InputRule({
find: emojiInputRegex,
handler: ({ state, range: { from, to }, match }) => {
const [, , name] = match;
const emojis = getAllEmoji();
const emoji = emojis[name];
const { tr } = state;
2021-10-27 15:23:28 +05:30
2021-12-11 22:18:48 +05:30
if (emoji) {
tr.replaceWith(from, to, [
state.schema.text(' '),
this.type.create({ name, moji: emoji.e, unicodeVersion: emoji.u, title: emoji.d }),
]);
2021-10-27 15:23:28 +05:30
2021-12-11 22:18:48 +05:30
return tr;
}
2021-10-27 15:23:28 +05:30
2021-12-11 22:18:48 +05:30
return null;
},
2021-10-27 15:23:28 +05:30
}),
];
},
onCreate() {
initEmojiMap();
},
});