Welcome to mirror list, hosted at ThFree Co, Russian Federation.

emoji.js « extensions « content_editor « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de608c3aaa22a91fae78401c78ec121e12b2abc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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,
        parseHTML: (element) => element.textContent,
      },
      name: {
        default: null,
        parseHTML: (element) => element.dataset.name,
      },
      title: {
        default: null,
      },
      unicodeVersion: {
        default: '6.0',
        parseHTML: (element) => element.dataset.unicodeVersion,
      },
    };
  },

  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();
  },
});