gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import emojis from '../../../assets/emoji.json' with {type: 'json'};
  2. import {GET} from '../modules/fetch.ts';
  3. import type {Issue} from '../types.ts';
  4. const maxMatches = 6;
  5. function sortAndReduce<T>(map: Map<T, number>): T[] {
  6. const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1]));
  7. return Array.from(sortedMap.keys()).slice(0, maxMatches);
  8. }
  9. export function matchEmoji(queryText: string): string[] {
  10. const query = queryText.toLowerCase().replaceAll('_', ' ');
  11. if (!query) return emojis.slice(0, maxMatches).map((e) => e.aliases[0]);
  12. // results is a map of weights, lower is better
  13. const results = new Map<string, number>();
  14. for (const {aliases} of emojis) {
  15. const mainAlias = aliases[0];
  16. for (const [aliasIndex, alias] of aliases.entries()) {
  17. const index = alias.replaceAll('_', ' ').indexOf(query);
  18. if (index === -1) continue;
  19. const existing = results.get(mainAlias);
  20. const rankedIndex = index + aliasIndex;
  21. results.set(mainAlias, existing ? existing - rankedIndex : rankedIndex);
  22. }
  23. }
  24. return sortAndReduce(results);
  25. }
  26. type MentionSuggestion = {value: string; name: string; fullname: string; avatar: string};
  27. export function matchMention(queryText: string): MentionSuggestion[] {
  28. const query = queryText.toLowerCase();
  29. // results is a map of weights, lower is better
  30. const results = new Map<MentionSuggestion, number>();
  31. for (const obj of window.config.mentionValues ?? []) {
  32. const index = obj.key.toLowerCase().indexOf(query);
  33. if (index === -1) continue;
  34. const existing = results.get(obj);
  35. results.set(obj, existing ? existing - index : index);
  36. }
  37. return sortAndReduce(results);
  38. }
  39. export async function matchIssue(owner: string, repo: string, issueIndexStr: string, query: string): Promise<Issue[]> {
  40. const res = await GET(`${window.config.appSubUrl}/${owner}/${repo}/issues/suggestions?q=${encodeURIComponent(query)}`);
  41. const issues: Issue[] = await res.json();
  42. const issueNumber = parseInt(issueIndexStr);
  43. // filter out issue with same id
  44. return issues.filter((i) => i.number !== issueNumber);
  45. }