This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
index.js
192 lines (156 loc) · 4.7 KB
/
index.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
import '../css/emoji-button.css';
import Emitter from 'tiny-emitter';
import Popper from 'popper.js';
import emojiData from './data/emoji';
import {
EMOJI,
SHOW_SEARCH_RESULTS,
SHOW_TABS,
HIDE_TABS,
HIDE_VARIANT_POPUP
} from './events';
import { EmojiPreview } from './preview';
import { Search } from './search';
import { Tabs } from './tabs';
import { createElement, empty } from './util';
import { VariantPopup } from './variantPopup';
import { i18n } from './i18n';
const CLASS_PICKER = 'emoji-picker';
const CLASS_PICKER_CONTENT = 'emoji-picker__content';
const DEFAULT_OPTIONS = {
position: 'right-start',
autoHide: true,
autoFocusSearch: true,
showPreview: true,
showSearch: true,
showRecents: true,
showVariants: true,
recentsCount: 50
};
export default class EmojiButton {
constructor(options = {}) {
this.pickerVisible = false;
this.options = { ...DEFAULT_OPTIONS, ...options };
if (!this.options.rootElement) {
this.options.rootElement = document.body;
}
this.i18n = {
...i18n,
...options.i18n
};
this.onDocumentClick = this.onDocumentClick.bind(this);
this.onDocumentKeydown = this.onDocumentKeydown.bind(this);
this.events = new Emitter();
this.publicEvents = new Emitter();
}
on(event, callback) {
this.publicEvents.on(event, callback);
}
off(event, callback) {
this.publicEvents.off(event, callback);
}
buildPicker() {
this.pickerEl = createElement('div', CLASS_PICKER);
if (this.options.zIndex) {
this.pickerEl.style.zIndex = this.options.zIndex;
}
const pickerContent = createElement('div', CLASS_PICKER_CONTENT);
if (this.options.showSearch) {
const searchContainer = new Search(
this.events,
this.i18n,
this.options,
emojiData,
this.options.autoFocusSearch
).render();
this.pickerEl.appendChild(searchContainer);
}
this.pickerEl.appendChild(pickerContent);
const tabs = new Tabs(this.events, this.i18n, this.options).render();
pickerContent.appendChild(tabs);
this.events.on(HIDE_TABS, () => {
if (pickerContent.contains(tabs)) {
pickerContent.removeChild(tabs);
}
});
this.events.on(SHOW_TABS, () => {
if (!pickerContent.contains(tabs)) {
empty(pickerContent);
pickerContent.appendChild(tabs);
}
});
this.events.on(SHOW_SEARCH_RESULTS, searchResults => {
empty(pickerContent);
searchResults.classList.add('search-results');
pickerContent.appendChild(searchResults);
});
if (this.options.showPreview) {
this.pickerEl.appendChild(new EmojiPreview(this.events).render());
}
let variantPopup;
this.events.on(EMOJI, ({ emoji, showVariants }) => {
if (emoji.v && showVariants && this.options.showVariants) {
variantPopup = new VariantPopup(
this.events,
emoji,
this.options
).render();
this.pickerEl.appendChild(variantPopup);
} else {
if (variantPopup && variantPopup.parentNode === this.pickerEl) {
this.pickerEl.removeChild(variantPopup);
}
this.publicEvents.emit('emoji', emoji.e);
if (this.options.autoHide) {
this.hidePicker();
}
}
});
this.events.on(HIDE_VARIANT_POPUP, () => {
this.pickerEl.removeChild(variantPopup);
variantPopup = null;
});
this.options.rootElement.appendChild(this.pickerEl);
setTimeout(() => {
document.addEventListener('click', this.onDocumentClick);
document.addEventListener('keydown', this.onDocumentKeydown);
});
}
onDocumentClick(event) {
if (!this.pickerEl.contains(event.target)) {
this.hidePicker();
}
}
hidePicker() {
// this.pickerEl.style.opacity = 0;
this.pickerEl.classList.remove('visible');
this.pickerVisible = false;
this.events.off(EMOJI);
this.events.off(HIDE_VARIANT_POPUP);
this.hideInProgress = true;
setTimeout(() => {
this.options.rootElement.removeChild(this.pickerEl);
this.popper.destroy();
this.pickerEl.style.transition = '';
this.hideInProgress = false;
}, 500);
document.removeEventListener('click', this.onDocumentClick);
document.removeEventListener('keydown', this.onDocumentKeydown);
}
showPicker(referenceEl, options = {}) {
if (this.hideInProgress) {
return;
}
this.pickerVisible = true;
this.buildPicker();
this.popper = new Popper(referenceEl, this.pickerEl, {
placement: options.position || this.options.position
});
this.pickerEl.classList.add('visible');
}
onDocumentKeydown(event) {
if (event.key === 'Escape') {
this.hidePicker();
}
}
}