You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TextExpander.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import {matchEmoji, matchMention} from '../../utils/match.js';
  2. import {emojiString} from '../emoji.js';
  3. export function initTextExpander(expander) {
  4. expander?.addEventListener('text-expander-change', ({detail: {key, provide, text}}) => {
  5. if (key === ':') {
  6. const matches = matchEmoji(text);
  7. if (!matches.length) return provide({matched: false});
  8. const ul = document.createElement('ul');
  9. ul.classList.add('suggestions');
  10. for (const name of matches) {
  11. const emoji = emojiString(name);
  12. const li = document.createElement('li');
  13. li.setAttribute('role', 'option');
  14. li.setAttribute('data-value', emoji);
  15. li.textContent = `${emoji} ${name}`;
  16. ul.append(li);
  17. }
  18. provide({matched: true, fragment: ul});
  19. } else if (key === '@') {
  20. const matches = matchMention(text);
  21. if (!matches.length) return provide({matched: false});
  22. const ul = document.createElement('ul');
  23. ul.classList.add('suggestions');
  24. for (const {value, name, fullname, avatar} of matches) {
  25. const li = document.createElement('li');
  26. li.setAttribute('role', 'option');
  27. li.setAttribute('data-value', `${key}${value}`);
  28. const img = document.createElement('img');
  29. img.src = avatar;
  30. li.append(img);
  31. const nameSpan = document.createElement('span');
  32. nameSpan.textContent = name;
  33. li.append(nameSpan);
  34. if (fullname && fullname.toLowerCase() !== name) {
  35. const fullnameSpan = document.createElement('span');
  36. fullnameSpan.classList.add('fullname');
  37. fullnameSpan.textContent = fullname;
  38. li.append(fullnameSpan);
  39. }
  40. ul.append(li);
  41. }
  42. provide({matched: true, fragment: ul});
  43. }
  44. });
  45. expander?.addEventListener('text-expander-value', ({detail}) => {
  46. if (detail?.item) {
  47. // add a space after @mentions as it's likely the user wants one
  48. const suffix = detail.key === '@' ? ' ' : '';
  49. detail.value = `${detail.item.getAttribute('data-value')}${suffix}`;
  50. }
  51. });
  52. }