-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.jsx
202 lines (179 loc) · 5.39 KB
/
index.jsx
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
200
201
202
import {
Component,
h
} from 'skatejs';
import stylesDefinition from './styles';
import icons from './icons';
import emojiData from './emoji_data';
import {define, styles} from 'skateparkjs-core';
import 'skateparkjs-growy';
import 'skateparkjs-spinner';
class SKEmoji extends Component {
static get props() {
return {
visible: {
attribute: true,
default: false
},
emojis: {
default: emojiData
},
activeCategory: {
default: 'people'
},
isSearching: {
default: false
},
searchResults: {
default: []
},
showSpinner: {
default: false
}
};
}
createCategory(emojis, categoryName) {
const content = emojis.map(e => {
return h('i', e);
});
return h('li', {
class: 'emoji-category-content',
'data-category': categoryName
}, h('div', {
class: 'emoji-category-header'
}, categoryName), h('div', {
class: 'emojis'
}, ...content));
}
renderCallback() {
const visible = this.visible ? 'visible' : '';
const contentVisible = this.isSearching ? '' : 'visible';
const isSearching = this.isSearching ? 'visible' : '';
const categories = Object.keys(this.emojis);
const emojiContent = categories.map(category => {
return this.createCategory(this.emojis[category], category);
});
const categoriesContent = categories.map(c => {
const active = this.activeCategory === c ? 'active' : '';
return h('div', {
style: `background-image: url(${icons[c]})`,
class: `cat ${active}`,
onclick: this.goToCategory(c)
});
});
const searchResults = this.createCategory(this.searchResults, 'Search results');
const spinnerClass = this.showSpinner ? 'visible' : '';
return [
h('style', stylesDefinition),
h('sk-growy', {
class: 'text',
'reset-on-enter': 'false',
'min-height': 60,
styles: styles({
textarea: {
height: '30px',
'font-size': '16px',
padding: '10px'
}
})
}),
h('img', {
class: 'toggle',
src: icons.people,
onclick: this.toggle.bind(this)
}),
h('div', {
class: `emojis-wrapper ${visible}`
}, h('div', {
class: 'header'
}, h('input', {
type: 'search',
oninput: this.onSearch(this),
class: 'emoji-search'
}), h('sk-spinner', {
class: spinnerClass,
type: 'rect',
color: '#aaa',
style: 'width: 30px; height: 30px;'
})),
h('div', {
class: `emojis-content`,
onclick: this.onEmojiClick(this)
}, h('ul', {
class: `emoji-data ${contentVisible}`
}, ...emojiContent), h('div', {
class: `emoji-search-results ${isSearching}`
}, searchResults)), h('div', {
class: `categories ${contentVisible}`
}, categoriesContent))
];
}
renderedCallback() {
if (this.intersectionObserver) return;
this.intersectionObserver = new IntersectionObserver((entries, observer) => {
const intersection = entries[0];
const target = intersection.target;
const category = target.getAttribute('data-category');
const categories = Object.keys(this.emojis);
const activeIndex = categories.indexOf(this.activeCategory);
const currentIndex = categories.indexOf(category);
if (this.activeCategory === category) {
if (intersection.intersectionRatio === 0) {
this.activeCategory = categories[currentIndex + 1];
}
} else if (activeIndex - currentIndex === 1) {
this.activeCategory = categories[activeIndex - 1];
}
}, {
threshold: [0, 0.1]
});
Object.keys(this.emojis).forEach((category) => {
this.intersectionObserver.observe(
this.shadowRoot.querySelector(`[data-category="${category}"]`)
);
});
}
goToCategory(category) {
return function() {
const categoryContent = this.shadowRoot.querySelector(`.emoji-category-content[data-category="${category}"]`);
categoryContent.scrollIntoView();
setTimeout(() => this.activeCategory = category, 10);
}.bind(this)
}
onEmojiClick(component) {
return function(e) {
const target = e.target;
const isIcon = target.tagName === 'I';
if (!isIcon) return;
const emoji = target.textContent;
const growy = component.shadowRoot.querySelector('.text');
growy.addText(emoji);
}
}
onSearch(component) {
return function() {
const value = this.value.trim();
clearTimeout(component.searchingDelay);
if (!value) {
component.isSearching = false;
component.showSpinner = false;
return;
}
component.isSearching = true;
component.showSpinner = true;
component.searchResults = [];
component.searchingDelay = setTimeout(() => {
fetch(`https://emoji.getdango.com/api/emoji?q=${value}`).then(r => r.json()).then(response => {
component.showSpinner = false;
component.searchResults = response.results.map(e => e.text);
});
}, 300);
}
}
toggle() {
this.visible = !this.visible;
setTimeout(() => this.shadowRoot.querySelector('.emoji-search').focus(), 10);
}
}
define('sk-emoji', SKEmoji);
export default SKEmoji;