summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/tribute.js
blob: 02cd4843745e467cbc02414c4dfb5f5d2939dd52 (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
import {emojiKeys, emojiHTML, emojiString} from './emoji.js';
import {htmlEscape} from 'escape-goat';

function makeCollections({mentions, emoji}) {
  const collections = [];

  if (emoji) {
    collections.push({
      trigger: ':',
      requireLeadingSpace: true,
      values: (query, cb) => {
        const matches = [];
        for (const name of emojiKeys) {
          if (name.includes(query)) {
            matches.push(name);
            if (matches.length > 5) break;
          }
        }
        cb(matches);
      },
      lookup: (item) => item,
      selectTemplate: (item) => {
        if (item === undefined) return null;
        return emojiString(item.original);
      },
      menuItemTemplate: (item) => {
        return `<div class="tribute-item">${emojiHTML(item.original)}<span>${htmlEscape(item.original)}</span></div>`;
      },
    });
  }

  if (mentions) {
    collections.push({
      values: window.config.mentionValues ?? [],
      requireLeadingSpace: true,
      menuItemTemplate: (item) => {
        return `
          <div class="tribute-item">
            <img src="${htmlEscape(item.original.avatar)}" class="tw-mr-2"/>
            <span class="name">${htmlEscape(item.original.name)}</span>
            ${item.original.fullname && item.original.fullname !== '' ? `<span class="fullname">${htmlEscape(item.original.fullname)}</span>` : ''}
          </div>
        `;
      },
    });
  }

  return collections;
}

export async function attachTribute(element, {mentions, emoji} = {}) {
  const {default: Tribute} = await import(/* webpackChunkName: "tribute" */'tributejs');
  const collections = makeCollections({mentions, emoji});
  const tribute = new Tribute({collection: collections, noMatchTemplate: ''});
  tribute.attach(element);
  return tribute;
}