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

82 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-10-27 15:23:28 +05:30
import { Node } from '@tiptap/core';
import { InputRule } from 'prosemirror-inputrules';
import { initEmojiMap, getAllEmoji } from '~/emoji';
export const emojiInputRegex = /(?:^|\s)((?::)((?:\w+))(?::))$/;
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() {
return [
new InputRule(emojiInputRegex, (state, match, start, end) => {
const [, , name] = match;
const emojis = getAllEmoji();
const emoji = emojis[name];
const { tr } = state;
if (emoji) {
tr.replaceWith(start, end, [
state.schema.text(' '),
this.type.create({ name, moji: emoji.e, unicodeVersion: emoji.u, title: emoji.d }),
]);
return tr;
}
return null;
}),
];
},
onCreate() {
initEmojiMap();
},
});