Skip to content

Commit 842a757

Browse files
silverwindtechknowlogicklafriks
authored
Refactor image paste code (#13354)
Some minor refactors I did while investigating another issue. Functionalily should be pretty much the same as before. Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent fc40cdf commit 842a757

File tree

1 file changed

+33
-47
lines changed

1 file changed

+33
-47
lines changed

web_src/js/index.js

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -305,42 +305,32 @@ function replaceAndKeepCursor(field, oldval, newval) {
305305
}
306306
}
307307

308-
function retrieveImageFromClipboardAsBlob(pasteEvent, callback) {
309-
if (!pasteEvent.clipboardData) {
310-
return;
311-
}
308+
function getPastedImages(e) {
309+
if (!e.clipboardData) return [];
312310

313-
const {items} = pasteEvent.clipboardData;
314-
if (typeof items === 'undefined') {
315-
return;
311+
const files = [];
312+
for (const item of e.clipboardData.items || []) {
313+
if (!item.type || !item.type.startsWith('image/')) continue;
314+
files.push(item.getAsFile());
316315
}
317316

318-
for (let i = 0; i < items.length; i++) {
319-
if (!items[i].type.includes('image')) continue;
320-
const blob = items[i].getAsFile();
321-
322-
if (typeof (callback) === 'function') {
323-
pasteEvent.preventDefault();
324-
pasteEvent.stopPropagation();
325-
callback(blob);
326-
}
317+
if (files.length) {
318+
e.preventDefault();
319+
e.stopPropagation();
327320
}
321+
return files;
328322
}
329323

330-
function uploadFile(file, callback) {
331-
const xhr = new XMLHttpRequest();
332-
333-
xhr.addEventListener('load', () => {
334-
if (xhr.status === 200) {
335-
callback(xhr.responseText);
336-
}
337-
});
338-
339-
xhr.open('post', $('#dropzone').data('upload-url'), true);
340-
xhr.setRequestHeader('X-Csrf-Token', csrf);
324+
async function uploadFile(file) {
341325
const formData = new FormData();
342326
formData.append('file', file, file.name);
343-
xhr.send(formData);
327+
328+
const res = await fetch($('#dropzone').data('upload-url'), {
329+
method: 'POST',
330+
headers: {'X-Csrf-Token': csrf},
331+
body: formData,
332+
});
333+
return await res.json();
344334
}
345335

346336
function reload() {
@@ -350,33 +340,29 @@ function reload() {
350340
function initImagePaste(target) {
351341
target.each(function () {
352342
const field = this;
353-
field.addEventListener('paste', (event) => {
354-
retrieveImageFromClipboardAsBlob(event, (img) => {
343+
field.addEventListener('paste', async (e) => {
344+
for (const img of getPastedImages(e)) {
355345
const name = img.name.substr(0, img.name.lastIndexOf('.'));
356346
insertAtCursor(field, `![${name}]()`);
357-
uploadFile(img, (res) => {
358-
const data = JSON.parse(res);
359-
replaceAndKeepCursor(field, `![${name}]()`, `![${name}](${AppSubUrl}/attachments/${data.uuid})`);
360-
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
361-
$('.files').append(input);
362-
});
363-
});
347+
const data = await uploadFile(img);
348+
replaceAndKeepCursor(field, `![${name}]()`, `![${name}](${AppSubUrl}/attachments/${data.uuid})`);
349+
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
350+
$('.files').append(input);
351+
}
364352
}, false);
365353
});
366354
}
367355

368356
function initSimpleMDEImagePaste(simplemde, files) {
369-
simplemde.codemirror.on('paste', (_, event) => {
370-
retrieveImageFromClipboardAsBlob(event, (img) => {
357+
simplemde.codemirror.on('paste', async (_, e) => {
358+
for (const img of getPastedImages(e)) {
371359
const name = img.name.substr(0, img.name.lastIndexOf('.'));
372-
uploadFile(img, (res) => {
373-
const data = JSON.parse(res);
374-
const pos = simplemde.codemirror.getCursor();
375-
simplemde.codemirror.replaceRange(`![${name}](${AppSubUrl}/attachments/${data.uuid})`, pos);
376-
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
377-
files.append(input);
378-
});
379-
});
360+
const data = await uploadFile(img);
361+
const pos = simplemde.codemirror.getCursor();
362+
simplemde.codemirror.replaceRange(`![${name}](${AppSubUrl}/attachments/${data.uuid})`, pos);
363+
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
364+
files.append(input);
365+
}
380366
});
381367
}
382368

0 commit comments

Comments
 (0)