logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

emoji_mart_search_light.js (4007B)


  1. // This code is largely borrowed from:
  2. // https://github.com/missive/emoji-mart/blob/5f2ffcc/src/utils/emoji-index.js
  3. import data from './emoji_mart_data_light';
  4. import { getData, getSanitizedData, intersect } from './emoji_utils';
  5. let originalPool = {};
  6. let index = {};
  7. let emojisList = {};
  8. let emoticonsList = {};
  9. for (let emoji in data.emojis) {
  10. let emojiData = data.emojis[emoji];
  11. let { short_names, emoticons } = emojiData;
  12. let id = short_names[0];
  13. if (emoticons) {
  14. emoticons.forEach(emoticon => {
  15. if (emoticonsList[emoticon]) {
  16. return;
  17. }
  18. emoticonsList[emoticon] = id;
  19. });
  20. }
  21. emojisList[id] = getSanitizedData(id);
  22. originalPool[id] = emojiData;
  23. }
  24. function addCustomToPool(custom, pool) {
  25. custom.forEach((emoji) => {
  26. let emojiId = emoji.id || emoji.short_names[0];
  27. if (emojiId && !pool[emojiId]) {
  28. pool[emojiId] = getData(emoji);
  29. emojisList[emojiId] = getSanitizedData(emoji);
  30. }
  31. });
  32. }
  33. function search(value, { emojisToShowFilter, maxResults, include, exclude, custom = [] } = {}) {
  34. addCustomToPool(custom, originalPool);
  35. maxResults = maxResults || 75;
  36. include = include || [];
  37. exclude = exclude || [];
  38. let results = null,
  39. pool = originalPool;
  40. if (value.length) {
  41. if (value === '-' || value === '-1') {
  42. return [emojisList['-1']];
  43. }
  44. let values = value.toLowerCase().split(/[\s|,|\-|_]+/),
  45. allResults = [];
  46. if (values.length > 2) {
  47. values = [values[0], values[1]];
  48. }
  49. if (include.length || exclude.length) {
  50. pool = {};
  51. data.categories.forEach(category => {
  52. let isIncluded = include && include.length ? include.indexOf(category.name.toLowerCase()) > -1 : true;
  53. let isExcluded = exclude && exclude.length ? exclude.indexOf(category.name.toLowerCase()) > -1 : false;
  54. if (!isIncluded || isExcluded) {
  55. return;
  56. }
  57. category.emojis.forEach(emojiId => pool[emojiId] = data.emojis[emojiId]);
  58. });
  59. if (custom.length) {
  60. let customIsIncluded = include && include.length ? include.indexOf('custom') > -1 : true;
  61. let customIsExcluded = exclude && exclude.length ? exclude.indexOf('custom') > -1 : false;
  62. if (customIsIncluded && !customIsExcluded) {
  63. addCustomToPool(custom, pool);
  64. }
  65. }
  66. }
  67. allResults = values.map((value) => {
  68. let aPool = pool,
  69. aIndex = index,
  70. length = 0;
  71. for (let charIndex = 0; charIndex < value.length; charIndex++) {
  72. const char = value[charIndex];
  73. length++;
  74. aIndex[char] = aIndex[char] || {};
  75. aIndex = aIndex[char];
  76. if (!aIndex.results) {
  77. let scores = {};
  78. aIndex.results = [];
  79. aIndex.pool = {};
  80. for (let id in aPool) {
  81. let emoji = aPool[id],
  82. { search } = emoji,
  83. sub = value.substr(0, length),
  84. subIndex = search.indexOf(sub);
  85. if (subIndex !== -1) {
  86. let score = subIndex + 1;
  87. if (sub === id) score = 0;
  88. aIndex.results.push(emojisList[id]);
  89. aIndex.pool[id] = emoji;
  90. scores[id] = score;
  91. }
  92. }
  93. aIndex.results.sort((a, b) => {
  94. let aScore = scores[a.id],
  95. bScore = scores[b.id];
  96. return aScore - bScore;
  97. });
  98. }
  99. aPool = aIndex.pool;
  100. }
  101. return aIndex.results;
  102. }).filter(a => a);
  103. if (allResults.length > 1) {
  104. results = intersect.apply(null, allResults);
  105. } else if (allResults.length) {
  106. results = allResults[0];
  107. } else {
  108. results = [];
  109. }
  110. }
  111. if (results) {
  112. if (emojisToShowFilter) {
  113. results = results.filter((result) => emojisToShowFilter(data.emojis[result.id].unified));
  114. }
  115. if (results && results.length > maxResults) {
  116. results = results.slice(0, maxResults);
  117. }
  118. }
  119. return results;
  120. }
  121. export { search };