-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
277 lines (239 loc) · 8.18 KB
/
functions.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
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
// https://javascript.info/async-await
// https://stackoverflow.com/questions/48617486/why-async-await-doesnt-work-in-my-case
// https://javascript.info/callbacks
// https://stackoverflow.com/questions/46399223/async-await-in-image-loading
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
// https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
// binding dom
let fileSelector = document.querySelector(".file-input");
let radio = document.querySelector(".control");
let messages = document.querySelector("#messages");
let btn = document.querySelector("button");
let form = document.querySelector("form");
let prog = document.querySelector("progress");
let notification = document.querySelector(".notification");
let selectedFilesText = document.querySelector('.file-name');
let codeText = document.querySelector('.code');
let copyBtn = document.querySelector('#copy');
// global vars :(
let fileList = [];
let entries = [];
let dim; // image dimensions old and new
let prePublii = '<div class="gallery" contenteditable="false" \
data-is-empty="false" data-columns="3">';
let postPublii = '</div>';
let getMaxAllowedImageSize = function () {
return fileSelector.getAttribute("data-max-size") * 1024;
}
let getSelectedFormat = function () {
return document.querySelector('input[name="format"]:checked').value;
}
//this is called multiple times
//it takes time to upload the img (async)
let upload = async function (file) {
if (file.length) { // pointless?
alert("Please Select files first")
Promise.reject(new Error('No Files'));
return; // to be able to interact with the ui again
}
if (file.size > getMaxAllowedImageSize()) {
alert(file.name + " is too big.");
Promise.reject(new Error('Too Big'));
return;
}
if (sleepIsNeeded()) {
// Imgur freq limit 50 Images/hour
// Period = 1/50*60 mins = 1,2 mins = 72 secs
await sleep(75);
}
// Begin file upload
console.log("Uploading image to Imgur...");
// Replace with your own API key
// this API key has no value
var apiUrl = 'https://api.imgur.com/3/image';
var apiKey = '28aaa2e823b03b1';
var settings = {
async: true, // execution isn't paused
crossDomain: true,
processData: false,
contentType: false,
type: 'POST',
url: apiUrl,
headers: {
Authorization: 'Client-ID ' + apiKey,
Accept: 'application/json'
},
mimeType: 'multipart/form-data'
};
var formData = new FormData();
formData.append("image", file);
settings.data = formData;
return new Promise((resolve, reject) => {
// Response contains stringified JSON
// Image URL available at response.data.link
$.ajax(settings).done(function (response) {
let result = JSON.parse(response);
entries.push(result);
url = result.data.link;
let message = "✅ " + file.name + " --- " + url;
console.log(message);
showMessage(message, url);
progressAddOne();
resolve(url);
// resolve(url); this is what whis function returns
// with resolve we trigger the next ".then(funName(resolveParam))"
// return new Promise.resolve(url); user has to switch format and then it works?
}).fail(function (response) {
let result = JSON.parse(response.responseText);
let message = "❌ " + file.name + " --- " + result.data.error.message
console.log(message);
showMessage(message);
reject(new Error('fail')).then(resolvedNotcalled, () => {
console.debug(result);
});
});
})
}
let imageProcess = async function (url) {
return new Promise((resolve, reject) => {
dim = {
old: {},
new: {}
}
let img = new Image();
let fixedWidth = 768; // Mansory Layout
img.src = url;
img.onload = () => {
dim.old.width = img.width;
dim.old.height = img.height;
let ratio = img.height / img.width;
dim.new.width = fixedWidth
dim.new.height = Math.floor(ratio * dim.new.width)
resolve();
}
})
}
//it takes time to load the img (async)
let formatPublii = async function (url) {
await imageProcess(url);
return new Promise((resolve, reject) => {
code = '<figure class="gallery__item"><a href="' + url + '" data-size="' + dim
.old.width +
"x" + dim.old.height + '"><img src="' + url + '" alt="" width="' + dim.new
.width +
'" height="' + dim.new.height + '"></a></figure>';
resolve(code);
});
}
let formatMarkdown = async function (url) {
return new Promise((resolve, reject) => {
// ATTENTION this "\n" character is needed for NetlifyCMS to recognize the markdown entries as images
resolve('![ ](' + url + ')\n');
})
}
let appendPublii = function (code) {
mid += code + '\n';
codeText.value = prePublii + '\n' + mid + postPublii;
}
let append = async function (code) {
codeText.value += code + '\n';
}
let progressAddOne = function () {
let val = parseInt(prog.getAttribute("value"));
val = val + 1;
prog.setAttribute("value", val);
if ((val) >= prog.getAttribute("max")) {
lock();
}
}
let refreshUI = function () {
if (notification.classList.contains('is-success')) {
notification.classList.remove('is-success');
notification.classList.add('is-warning');
}
if (fileList.length > 0) {
btn.removeAttribute('disabled');
prog.setAttribute("max", fileList.length);
}
prog.setAttribute("value", "0");
}
let createFileList = function () {
fileList = [];
// Notice how the fileList array is reset inside the change handler.
// This is in case the user selects files more than once.
// We don't want to upload the same image multiple times
for (let i = 0; i < fileSelector.files.length; i++) {
fileList.push(fileSelector.files[i]);
}
}
let showFileSelectorText = function () {
if (fileSelector.files.length > 0) {
let f = (fileSelector.files.length > 1) ? ' Files' : ' File'; // Plural
selectedFilesText.innerHTML = fileSelector.files.length + f + ' Selected';
}
}
let lock = function () {
notification.classList.remove('is-warning');
notification.classList.add('is-success');
btnLock();
}
let btnLock = function () {
btn.setAttribute('disabled', '');
}
let newCode = function (option) {
codeText.value = '';
// refreshUI();
mid = ''; // for Publii
if (entries.length <= 0) {
return;
}
if (option == 'publii') {
asyncForEach(entries,
async function (entry) {
await formatPublii(entry.data.link).then(appendPublii);
});
} else if (option == "markdown") {
entries.forEach(function (entry) {
formatMarkdown(entry.data.link).then(append);
});
} else {
entries.forEach(function (entry) {
append(entry.data.link);
});
}
}
// IMPORTANT classic forEach is not async compatible
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
// we dont use index or array
// we only use arra[index] (file)
}
}
function sleep(sec) {
let message = "💤 taking a nap for: " + sec + "secs... (Imgur Rate Limiting)";
console.log(message);
showMessage(message);
return new Promise(resolve => setTimeout(resolve, sec * 1000));
}
let sleepIsNeeded = function () {
if (fileList.length >= 50) {
return true;
} else {
return false;
}
}
let showMessage = function (message, url) {
let text = document.createTextNode(message);
let p = document.createElement("p");
let a = document.createElement('a');
a.href = url;
a.target = "_blank"
if (url) {
a.appendChild(text);
p.appendChild(a);
} else {
p.appendChild(text);
}
messages.appendChild(p);
}