|
|
@@ -1,23 +1,33 @@
|
|
|
-import emojione from 'emojione';
|
|
|
-import emojiStrategy from 'emojione/emoji_strategy.json';
|
|
|
+import axios from 'axios';
|
|
|
|
|
|
class EmojiAutoCompleteHelper {
|
|
|
|
|
|
constructor() {
|
|
|
- this.initEmojiImageMap = this.initEmojiImageMap.bind(this);
|
|
|
- this.showHint = this.showHint.bind(this);
|
|
|
+ this.emojiStrategy = {};
|
|
|
+ this.emojiShortnameImageMap = {}
|
|
|
|
|
|
this.initEmojiImageMap()
|
|
|
+ .then(() => {
|
|
|
+ Object.freeze(this); // freeze after initializing data
|
|
|
+ })
|
|
|
+
|
|
|
+ this.initEmojiImageMap = this.initEmojiImageMap.bind(this);
|
|
|
+ this.showHint = this.showHint.bind(this);
|
|
|
}
|
|
|
|
|
|
initEmojiImageMap() {
|
|
|
- this.emojiShortnameImageMap = {};
|
|
|
- for (let unicode in emojiStrategy) {
|
|
|
- const data = emojiStrategy[unicode];
|
|
|
- const shortname = data.shortname;
|
|
|
- // add image tag
|
|
|
- this.emojiShortnameImageMap[shortname] = emojione.shortnameToImage(shortname);
|
|
|
- }
|
|
|
+ const emojiStrategyUrl = 'https://cdn.jsdelivr.net/npm/emojione@3.1.2/emoji_strategy.json';
|
|
|
+
|
|
|
+ return axios.get(emojiStrategyUrl)
|
|
|
+ .then((res) => {
|
|
|
+ this.emojiStrategy = res.data;
|
|
|
+ for (let unicode in this.emojiStrategy) {
|
|
|
+ const data = this.emojiStrategy[unicode];
|
|
|
+ const shortname = data.shortname;
|
|
|
+ // add image tag
|
|
|
+ this.emojiShortnameImageMap[shortname] = emojione.shortnameToImage(shortname);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -46,6 +56,7 @@ class EmojiAutoCompleteHelper {
|
|
|
editor.showHint({
|
|
|
completeSingle: false,
|
|
|
// closeOnUnfocus: false, // for debug
|
|
|
+ closeOnUnfocus: false, // for debug
|
|
|
hint: () => {
|
|
|
const matched = editor.getDoc().getRange(sc.from(), sc.to());
|
|
|
const term = matched.replace(':', ''); // remove ':' in the head
|
|
|
@@ -96,8 +107,8 @@ class EmojiAutoCompleteHelper {
|
|
|
const countLen4 = () => { countLen3() + results4.length; }
|
|
|
// TODO performance tune
|
|
|
// when total length of all results is less than `maxLength`
|
|
|
- for (let unicode in emojiStrategy) {
|
|
|
- const data = emojiStrategy[unicode];
|
|
|
+ for (let unicode in this.emojiStrategy) {
|
|
|
+ const data = this.emojiStrategy[unicode];
|
|
|
|
|
|
if (maxLength <= countLen1()) { break; }
|
|
|
// prefix match to shortname
|
|
|
@@ -134,5 +145,4 @@ class EmojiAutoCompleteHelper {
|
|
|
|
|
|
// singleton pattern
|
|
|
const instance = new EmojiAutoCompleteHelper();
|
|
|
-Object.freeze(instance);
|
|
|
export default instance;
|