forgejo/web_src/js/utils/match.js
Gusted fe5f16205f
feat: make text expander aware of custom emojis
The old MDE editor is aware of custom emojis and shows them as
suggestions, but the new text expander is not aware of them and seems to
re-implement some logic.

Simplify it by using what `emoji.js` already provides.

Custom emojis require a bit more work to get shown correctly (HTML and
adding a `gap` to fake a space).
2025-08-10 23:11:41 +02:00

38 lines
1.1 KiB
JavaScript

import {emojiKeys} from '../features/emoji.js';
const maxMatches = 6;
function sortAndReduce(map) {
const sortedMap = new Map(Array.from(map.entries()).sort((a, b) => a[1] - b[1]));
return Array.from(sortedMap.keys()).slice(0, maxMatches);
}
export function matchEmoji(queryText) {
const query = queryText.toLowerCase().replaceAll('_', ' ');
if (!query) return emojiKeys.slice(0, maxMatches);
// results is a map of weights, lower is better
const results = new Map();
for (const emojiKey of emojiKeys) {
const index = emojiKey.replaceAll('_', ' ').indexOf(query);
if (index === -1) continue;
results.set(emojiKey, index);
}
return sortAndReduce(results);
}
export function matchMention(queryText) {
const query = queryText.toLowerCase();
// results is a map of weights, lower is better
const results = new Map();
for (const obj of window.config.mentionValues ?? []) {
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);
}