gitea源码

tribute.ts 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import {emojiKeys, emojiHTML, emojiString} from './emoji.ts';
  2. import {html, htmlRaw} from '../utils/html.ts';
  3. type TributeItem = Record<string, any>;
  4. export async function attachTribute(element: HTMLElement) {
  5. const {default: Tribute} = await import(/* webpackChunkName: "tribute" */'tributejs');
  6. const collections = [
  7. { // emojis
  8. trigger: ':',
  9. requireLeadingSpace: true,
  10. values: (query: string, cb: (matches: Array<string>) => void) => {
  11. const matches = [];
  12. for (const name of emojiKeys) {
  13. if (name.includes(query)) {
  14. matches.push(name);
  15. if (matches.length > 5) break;
  16. }
  17. }
  18. cb(matches);
  19. },
  20. lookup: (item: TributeItem) => item,
  21. selectTemplate: (item: TributeItem) => {
  22. if (item === undefined) return null;
  23. return emojiString(item.original);
  24. },
  25. menuItemTemplate: (item: TributeItem) => {
  26. return html`<div class="tribute-item">${htmlRaw(emojiHTML(item.original))}<span>${item.original}</span></div>`;
  27. },
  28. }, { // mentions
  29. values: window.config.mentionValues ?? [],
  30. requireLeadingSpace: true,
  31. menuItemTemplate: (item: TributeItem) => {
  32. const fullNameHtml = item.original.fullname && item.original.fullname !== '' ? html`<span class="fullname">${item.original.fullname}</span>` : '';
  33. return html`
  34. <div class="tribute-item">
  35. <img alt src="${item.original.avatar}" width="21" height="21"/>
  36. <span class="name">${item.original.name}</span>
  37. ${htmlRaw(fullNameHtml)}
  38. </div>
  39. `;
  40. },
  41. },
  42. ];
  43. // @ts-expect-error TS2351: This expression is not constructable (strange, why)
  44. const tribute = new Tribute({collection: collections, noMatchTemplate: ''});
  45. tribute.attach(element);
  46. return tribute;
  47. }