You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tribute.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {emojiKeys, emojiHTML, emojiString} from './emoji.js';
  2. import {htmlEscape} from 'escape-goat';
  3. function makeCollections({mentions, emoji}) {
  4. const collections = [];
  5. if (emoji) {
  6. collections.push({
  7. trigger: ':',
  8. requireLeadingSpace: true,
  9. values: (query, cb) => {
  10. const matches = [];
  11. for (const name of emojiKeys) {
  12. if (name.includes(query)) {
  13. matches.push(name);
  14. if (matches.length > 5) break;
  15. }
  16. }
  17. cb(matches);
  18. },
  19. lookup: (item) => item,
  20. selectTemplate: (item) => {
  21. if (item === undefined) return null;
  22. return emojiString(item.original);
  23. },
  24. menuItemTemplate: (item) => {
  25. return `<div class="tribute-item">${emojiHTML(item.original)}<span>${htmlEscape(item.original)}</span></div>`;
  26. },
  27. });
  28. }
  29. if (mentions) {
  30. collections.push({
  31. values: window.config.mentionValues ?? [],
  32. requireLeadingSpace: true,
  33. menuItemTemplate: (item) => {
  34. return `
  35. <div class="tribute-item">
  36. <img src="${htmlEscape(item.original.avatar)}" class="gt-mr-3"/>
  37. <span class="name">${htmlEscape(item.original.name)}</span>
  38. ${item.original.fullname && item.original.fullname !== '' ? `<span class="fullname">${htmlEscape(item.original.fullname)}</span>` : ''}
  39. </div>
  40. `;
  41. },
  42. });
  43. }
  44. return collections;
  45. }
  46. export async function attachTribute(element, {mentions, emoji} = {}) {
  47. const {default: Tribute} = await import(/* webpackChunkName: "tribute" */'tributejs');
  48. const collections = makeCollections({mentions, emoji});
  49. const tribute = new Tribute({collection: collections, noMatchTemplate: ''});
  50. tribute.attach(element);
  51. return tribute;
  52. }