EmojiPickerHelper.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. class EmojiPickerHelper {
  2. constructor(editor) {
  3. this.editor = editor;
  4. this.getSearchCursor = this.getSearchCursor.bind(this);
  5. this.addEmojiOnSearch = this.addEmojiOnSearch.bind(this);
  6. this.addEmoji = this.addEmoji.bind(this);
  7. }
  8. getSearchCursor() {
  9. const pattern = /:[^:\s]+/;
  10. const currentPos = this.editor.getCursor();
  11. const sc = this.editor.getSearchCursor(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 cm = this.editor;
  34. const sc = this.getSearchCursor();
  35. const currentPos = cm.getCursor();
  36. if (sc.findPrevious()) {
  37. const isInputtingEmoji = (currentPos.line === sc.to().line && currentPos.ch === sc.to().ch);
  38. // current search cursor position
  39. if (!isInputtingEmoji) {
  40. return;
  41. }
  42. const pos = {
  43. line: sc.to().line,
  44. ch: sc.to().ch,
  45. };
  46. const currentSearchText = sc.matches(true, pos).match[0];
  47. const searchValue = currentSearchText.replace(':', '');
  48. return searchValue;
  49. }
  50. return;
  51. }
  52. }
  53. export default EmojiPickerHelper;