-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.html
311 lines (291 loc) · 10.5 KB
/
ui.html
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
@font-face {
font-family: "Inter";
font-style: normal;
font-weight: 400;
font-display: fallback;
src: url("https://rsms.me/inter/font-files/Inter-Regular.woff2?v=3.7") format("woff2"), url("https://rsms.me/inter/font-files/Inter-Regular.woff?v=3.7") format("woff");
}
body {
margin: 0;
padding: 16px;
font: 12px "Inter", sans-serif;
user-select: none;
}
#drop-area, #message {
background-color: #FAFAFA;
border: 2px solid rgba(0, 0, 0, 0.1);
text-align: center;
border-radius: 8px;
width: 208px;
height: 166px;
margin: 0 auto 16px;
padding: 80px 16px 16px;
box-sizing: border-box;
font-size: 11px;
color: rgba(0, 0, 0, 0.3);
background-position: 50% 32px;
background-repeat: no-repeat;
animation: changeIcon 4s step-end infinite;
cursor: pointer;
}
#drop-area.highlight {
border-color: #2F80ED;
}
#drop-area.icon-text {
background-image: url("data:image/svg+xml,%3Csvg width='40px' height='40px' viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg'%3E%3Cg stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E%3Cg stroke='%23BDBDBD' stroke-width='2'%3E%3Cpath d='M23,9 L13,9 C11.8954,9 11,9.89543 11,11 L11,29 C11,30.1046 11.8954,31 13,31 L27,31 C28.1046,31 29,30.1046 29,29 L29,15 M23,9 L29,15 M23,9 L23,14 C23,14.5523 23.4477,15 24,15 L29,15 M14,27 L26,27 M14,23 L26,23 M14,19 L26,19'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}
#drop-area.icon-image {
background-image: url("data:image/svg+xml,%3Csvg width='40px' height='40px' viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg'%3E%3Cg stroke='none' stroke-width='1' fill='none' fill-rule='evenodd'%3E%3Cg stroke='%23BDBDBD' stroke-width='2'%3E%3Cpath d='M9,25 L14.2929,19.7071 C14.6834,19.3166 15.3166,19.3166 15.7071,19.7071 L27,31 M27,16 C27,17.6569 25.6569,19 24,19 C22.3431,19 21,17.6569 21,16 C21,14.3431 22.3431,13 24,13 C25.6569,13 27,14.3431 27,16 Z M31,11 L31,29 C31,30.1046 30.1046,31 29,31 L11,31 C9.89543,31 9,30.1046 9,29 L9,11 C9,9.89543 9.89543,9 11,9 L29,9 C30.1046,9 31,9.89543 31,11 Z'%3E%3C/path%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}
#message {
background-color: transparent;
border-color: transparent;
cursor: default;
background-image: url("data:image/svg+xml,%3Csvg width='40' height='40' viewBox='0 0 40 40' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 0H40V40H0V0Z' fill='white'/%3E%3Cpath d='M20 15V20M31 20C31 26.0751 26.0751 31 20 31C13.9249 31 9 26.0751 9 20C9 13.9249 13.9249 9 20 9C26.0751 9 31 13.9249 31 20Z' stroke='%23BDBDBD' stroke-width='2' stroke-linecap='round'/%3E%3Ccircle cx='20' cy='24.5' r='1.5' fill='%23C4C4C4'/%3E%3C/svg%3E%0A");
}
.switch {
position: relative;
}
.switch .switch__toggle {
display: none;
}
.switch .switch__label {
position: relative;
padding-left: 32px;
}
.switch .switch__label::before {
position: absolute;
content: "";
top: 0;
left: 0;
width: 24px;
height: 14px;
border-radius: 7px;
background-color: #BDBDBD;
transition: .2s;
}
.switch .switch__label::after {
position: absolute;
content: "";
height: 10px;
width: 10px;
top: 2px;
left: 2px;
border-radius: 10px;
background-color: #FFFFFF;
transition: .2s;
}
.switch .switch__toggle:checked + .switch__label::before {
background-color: #2F80ED;
}
.switch .switch__toggle:checked + .switch__label::after {
transform: translateX(10px);
}
</style>
</head>
<body>
<div id="drop-area" style="display:none;"></div>
<div id="switch-toggle" class="switch" style="display:none;">
<input class="switch__toggle" type="checkbox" id="randomOrder" checked>
<label class="switch__label" for="randomOrder" id="randomOrderLabel">Random Order ON</label>
</div>
<div id="message" style="display:none;"></div>
<script>
const dropArea = document.getElementById('drop-area');
const switchToggle = document.getElementById('switch-toggle');
const message = document.getElementById('message');
// Random switch
const randomOrderCheckBox = document.getElementById('randomOrder');
const randomOrderLabel = document.getElementById('randomOrderLabel');
randomOrderCheckBox.onchange = function() {
if (this.checked === true) {
randomOrderLabel.textContent = 'Random Order ON';
} else {
randomOrderLabel.textContent = 'Random Order OFF';
}
};
// Drag and drop event
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false);
});
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false);
});
dropArea.addEventListener('drop', handleDrop, false);
window.onmessage = (event) => {
const pluginMessage = event.data.pluginMessage;
const dataType = pluginMessage.type;
const count = pluginMessage.count;
if (dataType === 'message') {
dropArea.style.display = 'none';
switchToggle.style.display = 'none';
message.style.display = 'block';
message.textContent = pluginMessage.text;
} else {
dropArea.style.display = 'block';
switchToggle.style.display = 'block';
message.style.display = 'none';
// Browser data file
if (dataType === 'text') {
dropArea.textContent = 'Click here to choose a text file, or drag it here.';
dropArea.className = 'icon-text';
} else if (dataType === 'image') {
dropArea.textContent = 'Click here to choose some image files, or drag them here.';
dropArea.className = 'icon-image';
}
dropArea.setAttribute('data-count', count);
// Click event
dropArea.onclick = () => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
if (dataType === 'text') {
fileInput.multiple = false;
fileInput.accept = '.txt';
fileInput.click();
fileInput.onchange = function() {
const file = fileInput.files[0];
textArrayFromFile(file, count, randomOrderCheckBox.checked);
};
} else if (dataType === 'image') {
fileInput.multiple = true;
fileInput.accept = '.png, .jpg, .jpeg';
fileInput.click();
fileInput.onchange = function() {
const files = fileInput.files;
imageDataFromFiles(files, count, randomOrderCheckBox.checked);
};
}
fileInput.remove();
};
}
}
function preventDefaults (e) {
e.preventDefault();
e.stopPropagation();
}
function highlight(e) {
dropArea.classList.add('highlight');
}
function unhighlight(e) {
dropArea.classList.remove('highlight');
}
function handleDrop(e) {
let files = e.dataTransfer.files;
let count = parseInt(dropArea.getAttribute('data-count'));
if (dropArea.className === 'icon-text') {
if (files.length === 1) {
const file = files[0];
textArrayFromFile(file, count, randomOrderCheckBox.checked);
} else {
window.parent.postMessage({pluginMessage: {
type: 'error',
data: 'You can only drag and drop 1 txt file here.'
}}, '*');
}
} else if (dropArea.className === 'icon-image') {
// Checking all files is image.
let allFileIsImage = Array.from(files).every(function(file) {
return ['image/png', 'image/jpeg', 'image/jpg'].includes(file.type);
});
if (allFileIsImage) {
imageDataFromFiles(files, count, randomOrderCheckBox.checked);
} else {
window.parent.postMessage({pluginMessage: {
type: 'error',
data: 'You can only drag and drop JPG or PNG file here.'
}}, '*');
}
}
}
function imageDataFromFiles(files, length, shuffle) {
const filesArray = sliceFileListWithLength(files, length, shuffle);
const fileReaderTask = filesArray.map(function(file) {
return getImageDataFromFile(file);
});
Promise.all(fileReaderTask).then(function(images) {
const pluginMessage = {
type: 'image',
data: images
}
window.parent.postMessage({pluginMessage}, '*');
});
}
function textArrayFromFile(file, length, shuffle) {
if (file.type === 'text/plain') {
const reader = new FileReader();
reader.onload = function() {
let content = reader.result;
content = content.split(/\n/);
content = content.map(function(item) {
return item.trim();
});
content = content.filter(function(item) {
return item !== '';
});
const pluginMessage = {
type: 'text',
data: sliceArrayWithLength(content, length, shuffle)
}
window.parent.postMessage({pluginMessage}, '*');
};
reader.readAsText(file, 'utf-8');
} else {
window.parent.postMessage({pluginMessage: {
type: 'error',
data: 'No a text file.'
}}, '*');
}
}
function getImageDataFromFile(file) {
return new Promise(function(resolve, reject) {
const reader = new FileReader();
reader.onload = function() {
resolve(new Uint8Array(reader.result));
}
reader.onerror = reject;
reader.readAsArrayBuffer(file);
});
}
function sliceArrayWithLength(array, length, shuffle) {
let newArray = array.slice();
while (newArray.length <= length) {
newArray = newArray.concat(array);
}
if (shuffle) {
shuffleArray(newArray);
}
return newArray.slice(0, length);
}
function sliceFileListWithLength(files, length, shuffle) {
let newArray = Array.from(files);
while (newArray.length <= length) {
newArray = newArray.concat(newArray);
}
if (shuffle) {
shuffleArray(newArray);
}
return newArray.slice(0, length);
}
function shuffleArray(array) {
let currentIndex = array.length;
let temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
</script>
</body>
</html>