-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule-textarea-emoji.js
151 lines (128 loc) · 5.93 KB
/
module-textarea-emoji.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import Quill from 'quill';
import Fuse from 'fuse.js';
import emojiList from './emoji-list.js';
const Module = Quill.import('core/module');
class TextAreaEmoji extends Module {
constructor(quill, options){
super(quill, options);
this.quill = quill;
this.container = document.createElement('button');
this.container.classList.add('textarea-emoji-control');
this.container.classList.add('ql-list');
this.container.innerHTML = options.buttonIcon;
this.quill.container.parentElement.querySelector(".ql-toolbar > .ql-formats:last-child").appendChild(this.container)
this.container.addEventListener('click', this.checkEmojiBoxExist.bind(this),false);
// this.quill.root.addEventListener("click",fn_close.bind(this),false)
}
checkEmojiBoxExist(){
let elementExists = document.getElementById("textarea-emoji");
if (elementExists) {
elementExists.remove();
}
else{
let ele_emoji_area = document.createElement('div');
ele_emoji_area.id = 'textarea-emoji';
this.quill.container.parentElement.querySelector(".ql-toolbar").appendChild(ele_emoji_area)
let tabToolbar = document.createElement('div');
tabToolbar.id="tab-toolbar";
ele_emoji_area.appendChild(tabToolbar);
var emojiType = [
{'type':'p','name':'people','content':'<div class="i-people"></div>'},
{'type':'n','name':'nature','content':'<div class="i-nature"></div>'},
{'type':'d','name':'food','content':'<div class="i-food"></div>'},
{'type':'s','name':'symbols','content':'<div class="i-symbols"></div>'},
{'type':'a','name':'activity','content':'<div class="i-activity"></div>'},
{'type':'t','name':'travel','content':'<div class="i-travel"></div>'},
{'type':'o','name':'objects','content':'<div class="i-objects"></div>'},
{'type':'f','name':'flags','content':'<div class="i-flags"></div>'}
];
let tabElementHolder = document.createElement('ul');
tabToolbar.appendChild(tabElementHolder);
let panel = document.createElement('div');
panel.id="tab-panel";
ele_emoji_area.appendChild(panel);
let innerQuill = this.quill;
emojiType.map(function(emojiType) {
let tabElement = document.createElement('li');
tabElement.classList.add('emoji-tab');
tabElement.classList.add('filter-'+emojiType.name);
let tabValue = emojiType.content;
tabElement.innerHTML = tabValue;
tabElement.dataset.filter = emojiType.type;
tabElementHolder.appendChild(tabElement);
let emojiFilter = document.querySelector('.filter-'+emojiType.name);
emojiFilter.addEventListener('click',function(){
const emojiContainer = document.getElementById("textarea-emoji");
const tab = emojiContainer && emojiContainer.querySelector('.active');
if (tab) {
tab.classList.remove('active');
}
emojiFilter.classList.toggle('active');
while (panel.firstChild) {
panel.removeChild(panel.firstChild);
}
let type = emojiFilter.dataset.filter;
fn_emojiElementsToPanel(type,panel,innerQuill);
})
});
let windowHeight = window.innerHeight;
let editorPos = this.quill.container.getBoundingClientRect().top;
if (editorPos > windowHeight/2) {
ele_emoji_area.style.top = '-250px';
}
fn_emojiPanelInit(panel,this.quill);
}
}
}
TextAreaEmoji.DEFAULTS = {
buttonIcon: '<svg viewbox="0 0 18 18"><circle class="ql-fill" cx="7" cy="7" r="1"></circle><circle class="ql-fill" cx="11" cy="7" r="1"></circle><path class="ql-stroke" d="M7,10a2,2,0,0,0,4,0H7Z"></path><circle class="ql-stroke" cx="9" cy="9" r="6"></circle></svg>'
}
function fn_close(){
let ele_emoji_plate = document.getElementById('textarea-emoji');
if (ele_emoji_plate) {ele_emoji_plate.remove()}
}
function fn_emojiPanelInit(panel,quill){
fn_emojiElementsToPanel('p', panel, quill);
document.querySelector('.filter-people').classList.add('active');
}
function fn_emojiElementsToPanel(type,panel,quill){
let fuseOptions = {
shouldSort: true,
matchAllTokens: true,
threshold: 0.3,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 3,
keys: [
"category"
]
};
let fuse = new Fuse(emojiList, fuseOptions);
let result = fuse.search(type);
result.sort(function (a, b) {
return a.emoji_order - b.emoji_order;
});
quill.focus()
result.map((emojiItem)=> {
let emoji = emojiItem.item
let span = document.createElement('span');
let t = document.createTextNode(emoji.shortname);
span.appendChild(t);
span.classList.add('bem');
span.classList.add('bem-' + emoji.name);
span.classList.add('ap');
span.innerText = emoji.unicode;
panel.appendChild(span);
let customButton = document.querySelector('.bem-' + emoji.name);
if (customButton) {
customButton.addEventListener('click', function() {
let range = quill.getSelection(true);
quill.insertText(range.index, emoji.unicode, Quill.sources.USER);
range.index += 1;
setTimeout(() => quill.setSelection(range.index + 1), 0);
});
}
});
}
export default TextAreaEmoji;