Files
Atay-Makhzan/web_src/js/features/tribute.ts
T

52 lines
1.8 KiB
TypeScript
Raw Normal View History

import {emojiKeys, emojiHTML, emojiString} from './emoji.ts';
2025-07-01 21:44:05 +08:00
import {html, htmlRaw} from '../utils/html.ts';
2025-01-22 08:11:51 +01:00
type TributeItem = Record<string, any>;
2020-05-21 04:00:43 +02:00
2025-01-22 08:11:51 +01:00
export async function attachTribute(element: HTMLElement) {
const {default: Tribute} = await import(/* webpackChunkName: "tribute" */'tributejs');
const collections = [
{ // emojis
2020-05-21 04:00:43 +02:00
trigger: ':',
requireLeadingSpace: true,
2025-01-22 08:11:51 +01:00
values: (query: string, cb: (matches: Array<string>) => void) => {
2020-05-21 04:00:43 +02:00
const matches = [];
for (const name of emojiKeys) {
if (name.includes(query)) {
matches.push(name);
if (matches.length > 5) break;
}
}
2020-05-21 04:00:43 +02:00
cb(matches);
},
2025-01-22 08:11:51 +01:00
lookup: (item: TributeItem) => item,
selectTemplate: (item: TributeItem) => {
2022-11-22 01:58:55 +01:00
if (item === undefined) return null;
2020-05-21 04:00:43 +02:00
return emojiString(item.original);
},
2025-01-22 08:11:51 +01:00
menuItemTemplate: (item: TributeItem) => {
2025-07-01 21:44:05 +08:00
return html`<div class="tribute-item">${htmlRaw(emojiHTML(item.original))}<span>${item.original}</span></div>`;
},
2025-01-22 08:11:51 +01:00
}, { // mentions
values: window.config.mentionValues ?? [],
requireLeadingSpace: true,
2025-01-22 08:11:51 +01:00
menuItemTemplate: (item: TributeItem) => {
2025-07-01 21:44:05 +08:00
const fullNameHtml = item.original.fullname && item.original.fullname !== '' ? html`<span class="fullname">${item.original.fullname}</span>` : '';
return html`
2020-05-21 04:00:43 +02:00
<div class="tribute-item">
2025-07-01 21:44:05 +08:00
<img alt src="${item.original.avatar}" width="21" height="21"/>
<span class="name">${item.original.name}</span>
${htmlRaw(fullNameHtml)}
2020-05-21 04:00:43 +02:00
</div>
`;
},
2025-01-22 08:11:51 +01:00
},
];
// @ts-expect-error TS2351: This expression is not constructable (strange, why)
const tribute = new Tribute({collection: collections, noMatchTemplate: ''});
tribute.attach(element);
2020-05-21 04:00:43 +02:00
return tribute;
}