2025-08-10 22:44:01 +02:00
|
|
|
import {emojiKeys} from '../features/emoji.js';
|
2023-04-22 18:32:34 +03:00
|
|
|
|
|
|
|
const maxMatches = 6;
|
|
|
|
|
|
|
|
function sortAndReduce(map) {
|
2023-05-18 03:14:31 +02:00
|
|
|
const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1]));
|
2023-04-22 18:32:34 +03:00
|
|
|
return Array.from(sortedMap.keys()).slice(0, maxMatches);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function matchEmoji(queryText) {
|
|
|
|
const query = queryText.toLowerCase().replaceAll('_', ' ');
|
2025-08-10 22:44:01 +02:00
|
|
|
if (!query) return emojiKeys.slice(0, maxMatches);
|
2023-04-22 18:32:34 +03:00
|
|
|
|
|
|
|
// results is a map of weights, lower is better
|
|
|
|
const results = new Map();
|
2025-08-10 22:44:01 +02:00
|
|
|
for (const emojiKey of emojiKeys) {
|
|
|
|
const index = emojiKey.replaceAll('_', ' ').indexOf(query);
|
|
|
|
if (index === -1) continue;
|
|
|
|
results.set(emojiKey, index);
|
2023-04-22 18:32:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return sortAndReduce(results);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function matchMention(queryText) {
|
|
|
|
const query = queryText.toLowerCase();
|
|
|
|
|
|
|
|
// results is a map of weights, lower is better
|
|
|
|
const results = new Map();
|
2023-08-12 16:36:23 +08:00
|
|
|
for (const obj of window.config.mentionValues ?? []) {
|
2023-04-22 18:32:34 +03:00
|
|
|
const index = obj.key.toLowerCase().indexOf(query);
|
|
|
|
if (index === -1) continue;
|
|
|
|
const existing = results.get(obj);
|
|
|
|
results.set(obj, existing ? existing - index : index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sortAndReduce(results);
|
|
|
|
}
|