EmojiPickerHelper.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import i18n from 'i18next';
  2. export default class EmojiPickerHelper {
  3. editor;
  4. pattern: RegExp;
  5. constructor(editor) {
  6. this.editor = editor;
  7. this.pattern = /:[^:\s]+/;
  8. }
  9. getSearchCursor() {
  10. const currentPos = this.editor.getCursor();
  11. const sc = this.editor.getSearchCursor(this.pattern, currentPos, { multiline: false });
  12. return sc;
  13. }
  14. // Add emoji when triggered by search
  15. addEmojiOnSearch = (emoji) => {
  16. const currentPos = this.editor.getCursor();
  17. const sc = this.getSearchCursor();
  18. if (sc.findPrevious()) {
  19. sc.replace(emoji.colons, this.editor.getTokenAt(currentPos).string);
  20. this.editor.focus();
  21. this.editor.refresh();
  22. }
  23. }
  24. // Add emoji when triggered by click emoji icon on top of editor
  25. addEmoji = (emoji) => {
  26. const currentPos = this.editor.getCursor();
  27. const doc = this.editor.getDoc();
  28. doc.replaceRange(emoji.colons, currentPos);
  29. this.editor.focus();
  30. this.editor.refresh();
  31. }
  32. getEmoji = () => {
  33. const sc = this.getSearchCursor();
  34. const currentPos = this.editor.getCursor();
  35. if (sc.findPrevious()) {
  36. const isInputtingEmoji = (currentPos.line === sc.to().line && currentPos.ch === sc.to().ch);
  37. // current search cursor position
  38. if (!isInputtingEmoji) {
  39. return;
  40. }
  41. const pos = {
  42. line: sc.to().line,
  43. ch: sc.to().ch,
  44. };
  45. const currentSearchText = sc.matches(true, pos).match[0];
  46. const searchWord = currentSearchText.replace(':', '');
  47. return searchWord;
  48. }
  49. return;
  50. }
  51. }
  52. export const getEmojiTranslation = () => {
  53. const categories = {};
  54. [
  55. 'search',
  56. 'recent',
  57. 'smileys',
  58. 'people',
  59. 'nature',
  60. 'foods',
  61. 'activity',
  62. 'places',
  63. 'objects',
  64. 'symbols',
  65. 'flags',
  66. 'custom',
  67. ].forEach((category) => {
  68. categories[category] = i18n.t(`emoji.categories.${category}`);
  69. });
  70. const skintones = {};
  71. (Array.from(Array(6).keys())).forEach((tone) => {
  72. skintones[tone + 1] = i18n.t(`emoji.skintones.${tone + 1}`);
  73. });
  74. const translation = {
  75. search: i18n.t('emoji.search'),
  76. clear: i18n.t('emoji.clear'),
  77. notfound: i18n.t('emoji.notfound'),
  78. skintext: i18n.t('emoji.skintext'),
  79. categories,
  80. categorieslabel: i18n.t('emoji.categorieslabel'),
  81. skintones,
  82. title: i18n.t('emoji.title'),
  83. };
  84. return translation;
  85. };