EmojiPickerHelper.ts 2.9 KB

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