EmojiAutoCompleteHelper.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import emojione from 'emojione';
  2. import emojiStrategy from 'emojione/emoji_strategy.json';
  3. class EmojiAutoCompleteHelper {
  4. constructor() {
  5. this.initEmojiImageMap = this.initEmojiImageMap.bind(this);
  6. this.showHint = this.showHint.bind(this);
  7. this.initEmojiImageMap()
  8. }
  9. initEmojiImageMap() {
  10. this.emojiShortnameImageMap = {};
  11. for (let unicode in emojiStrategy) {
  12. const data = emojiStrategy[unicode];
  13. const shortname = data.shortname;
  14. // add image tag
  15. this.emojiShortnameImageMap[shortname] = emojione.shortnameToImage(shortname);
  16. }
  17. }
  18. /**
  19. * try to find emoji terms and show hint
  20. * @param {any} editor An editor instance of CodeMirror
  21. */
  22. showHint(editor) {
  23. // see https://regex101.com/r/gy3i03/1
  24. const pattern = /:[^:\s]+/
  25. const currentPos = editor.getCursor();
  26. // find previous ':shortname'
  27. const sc = editor.getSearchCursor(pattern, currentPos, { multiline: false });
  28. if (sc.findPrevious()) {
  29. const isInputtingEmoji = (currentPos.line === sc.to().line && currentPos.ch === sc.to().ch);
  30. // return if it isn't inputting emoji
  31. if (!isInputtingEmoji) {
  32. return;
  33. }
  34. }
  35. else {
  36. return;
  37. }
  38. // see https://codemirror.net/doc/manual.html#addon_show-hint
  39. editor.showHint({
  40. completeSingle: false,
  41. // closeOnUnfocus: false, // for debug
  42. hint: () => {
  43. const matched = editor.getDoc().getRange(sc.from(), sc.to());
  44. const term = matched.replace(':', ''); // remove ':' in the head
  45. // get a list of shortnames
  46. const shortnames = this.searchEmojiShortnames(term);
  47. if (shortnames.length >= 1) {
  48. return {
  49. list: this.generateEmojiRenderer(shortnames),
  50. from: sc.from(),
  51. to: sc.to(),
  52. };
  53. }
  54. },
  55. });
  56. }
  57. /**
  58. * see https://codemirror.net/doc/manual.html#addon_show-hint
  59. * @param {string[]} emojiShortnames a list of shortname
  60. */
  61. generateEmojiRenderer(emojiShortnames) {
  62. return emojiShortnames.map((shortname) => {
  63. return {
  64. text: shortname,
  65. className: 'crowi-emoji-autocomplete',
  66. render: (element) => {
  67. element.innerHTML =
  68. `<div class="img-container">${this.emojiShortnameImageMap[shortname]}</div>` +
  69. `<span class="shortname-container">${shortname}</span>`;
  70. }
  71. }
  72. });
  73. }
  74. /**
  75. * transplanted from https://github.com/emojione/emojione/blob/master/examples/OTHER.md
  76. * @param {string} term
  77. * @returns {string[]} a list of shortname
  78. */
  79. searchEmojiShortnames(term) {
  80. const maxLength = 12;
  81. let results1 = [], results2 = [], results3 = [], results4 = [];
  82. const countLen1 = () => { results1.length; }
  83. const countLen2 = () => { countLen1() + results2.length; }
  84. const countLen3 = () => { countLen2() + results3.length; }
  85. const countLen4 = () => { countLen3() + results4.length; }
  86. // TODO performance tune
  87. // when total length of all results is less than `maxLength`
  88. for (let unicode in emojiStrategy) {
  89. const data = emojiStrategy[unicode];
  90. if (maxLength <= countLen1()) { break; }
  91. // prefix match to shortname
  92. else if (data.shortname.indexOf(`:${term}`) > -1) {
  93. results1.push(data.shortname);
  94. continue;
  95. }
  96. else if (maxLength <= countLen2()) { continue; }
  97. // partial match to shortname
  98. else if (data.shortname.indexOf(term) > -1) {
  99. results2.push(data.shortname);
  100. continue;
  101. }
  102. else if (maxLength <= countLen3()) { continue; }
  103. // partial match to elements of aliases
  104. else if ((data.aliases != null) && data.aliases.find(elem => elem.indexOf(term) > -1)) {
  105. results3.push(data.shortname);
  106. continue;
  107. }
  108. else if (maxLength <= countLen4()) { continue; }
  109. // partial match to elements of keywords
  110. else if ((data.keywords != null) && data.keywords.find(elem => elem.indexOf(term) > -1)) {
  111. results4.push(data.shortname);
  112. }
  113. };
  114. let results = results1.concat(results2).concat(results3).concat(results4);
  115. results = results.slice(0, maxLength);
  116. return results;
  117. }
  118. }
  119. // singleton pattern
  120. const instance = new EmojiAutoCompleteHelper();
  121. Object.freeze(instance);
  122. export default instance;