mirror of
				https://codeberg.org/forgejo/forgejo.git
				synced 2025-11-04 08:21:11 +00:00 
			
		
		
		
	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).
		
			
				
	
	
		
			38 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			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);
 | 
						|
}
 |