code.gitea.io/gitea@v1.22.3/web_src/js/features/tribute.js (about)

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