-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathmodule-toolbar-emoji.js
199 lines (173 loc) · 6.8 KB
/
module-toolbar-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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import Quill from 'quill';
import Fuse from 'fuse.js';
import emojiList from './emoji-list.js';
const Module = Quill.import('core/module');
class ToolbarEmoji extends Module {
constructor(quill, options) {
super(quill, options);
this.quill = quill;
this.toolbar = quill.getModule('toolbar');
if (typeof this.toolbar !== 'undefined')
this.toolbar.addHandler('emoji', this.checkPaletteExists);
var emojiBtns = document.getElementsByClassName('ql-emoji');
if (emojiBtns) {
[].slice.call(emojiBtns).forEach(function (emojiBtn) {
emojiBtn.innerHTML = options.buttonIcon;
});
}
}
checkPaletteExists() {
let quill = this.quill;
fn_checkDialogOpen(quill);
this.quill.on('text-change', function (delta, oldDelta, source) {
if (source === 'user') {
fn_close();
}
});
}
}
ToolbarEmoji.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('emoji-palette');
document.getElementById('emoji-close-div').style.display = "none";
if (ele_emoji_plate) {
ele_emoji_plate.remove()
}
}
function fn_checkDialogOpen(quill) {
let elementExists = document.getElementById("emoji-palette");
if (elementExists) {
elementExists.remove();
} else {
fn_showEmojiPalette(quill);
}
}
function fn_showEmojiPalette(quill) {
const paletteWidthAndHeight = 250;
let ele_emoji_area = document.createElement('div');
let selection = quill.getSelection();
const selectionBounds = quill.getBounds(selection.index);
const editorBounds = quill.container.getBoundingClientRect();
const selectionCenter = (selectionBounds.left + selectionBounds.right) / 2;
const selectionMiddle = (selectionBounds.top + selectionBounds.bottom) / 2;
const paletteLeft = editorBounds.left + selectionCenter + paletteWidthAndHeight <= document.documentElement.clientWidth ? selectionCenter : editorBounds.left - paletteWidthAndHeight;
const paletteTop = editorBounds.top + selectionMiddle + paletteWidthAndHeight + 10 <= document.documentElement.clientHeight ? selectionMiddle + 10 :
editorBounds.top + selectionMiddle - paletteWidthAndHeight - 10 >= 0 ? selectionMiddle - paletteWidthAndHeight - 10 :
document.documentElement.clientHeight - paletteWidthAndHeight - editorBounds.top;
quill.container.appendChild(ele_emoji_area);
ele_emoji_area.id = 'emoji-palette';
ele_emoji_area.style.left = `${paletteLeft}px`;
ele_emoji_area.style.top = `${paletteTop}px`;
let tabToolbar = document.createElement('div');
tabToolbar.id = "tab-toolbar";
ele_emoji_area.appendChild(tabToolbar);
//panel
let panel = document.createElement('div');
panel.id = "tab-panel";
ele_emoji_area.appendChild(panel);
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);
if (document.getElementById('emoji-close-div') === null) {
let closeDiv = document.createElement('div');
closeDiv.id = 'emoji-close-div';
closeDiv.addEventListener("click", fn_close, false);
document.getElementsByTagName('body')[0].appendChild(closeDiv);
} else {
document.getElementById('emoji-close-div').style.display = "block";
}
emojiType.map(function (emojiType) {
//add tab bar
let tabElement = document.createElement('li');
tabElement.classList.add('emoji-tab');
tabElement.classList.add('filter-' + emojiType.name);
tabElement.innerHTML = emojiType.content;
tabElement.dataset.filter = emojiType.type;
tabElementHolder.appendChild(tabElement);
let emojiFilter = document.querySelector('.filter-' + emojiType.name);
emojiFilter.addEventListener('click', function () {
let tab = document.querySelector('.active');
if (tab) {
tab.classList.remove('active');
}
emojiFilter.classList.toggle('active');
fn_updateEmojiContainer(emojiFilter, panel, quill);
})
});
fn_emojiPanelInit(panel, quill);
}
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();
let range = quill.getSelection();
result.map(function (emoji) {
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.classList.add('ap-' + emoji.name);
let output = '' + emoji.code_decimal + '';
span.innerHTML = output + ' ';
panel.appendChild(span);
let customButton = document.querySelector('.bem-' + emoji.name);
if (customButton) {
customButton.addEventListener('click', function () {
makeElement("span", {className: "ico", innerHTML: '' + emoji.code_decimal + ' '});
quill.insertEmbed(range.index, 'emoji', emoji, Quill.sources.USER);
setTimeout(() => quill.setSelection(range.index + 1), 0);
fn_close();
});
}
});
}
function fn_updateEmojiContainer(emojiFilter, panel, quill) {
while (panel.firstChild) {
panel.removeChild(panel.firstChild);
}
let type = emojiFilter.dataset.filter;
fn_emojiElementsToPanel(type, panel, quill);
}
function makeElement(tag, attrs, ...children) {
const elem = document.createElement(tag);
Object.keys(attrs).forEach(key => elem[key] = attrs[key]);
children.forEach(child => {
if (typeof child === "string")
child = document.createTextNode(child);
elem.appendChild(child);
});
return elem;
}
export default ToolbarEmoji;