2022-01-28 16:00:11 -05:00
|
|
|
import $ from 'jquery';
|
2021-11-08 01:27:40 -05:00
|
|
|
const {csrfToken} = window.config;
|
2021-10-16 13:28:04 -04:00
|
|
|
|
|
|
|
async function uploadFile(file, uploadUrl) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', file, file.name);
|
|
|
|
|
|
|
|
const res = await fetch(uploadUrl, {
|
|
|
|
method: 'POST',
|
2021-10-21 03:37:43 -04:00
|
|
|
headers: {'X-Csrf-Token': csrfToken},
|
2021-10-16 13:28:04 -04:00
|
|
|
body: formData,
|
|
|
|
});
|
|
|
|
return await res.json();
|
|
|
|
}
|
|
|
|
|
|
|
|
function clipboardPastedImages(e) {
|
|
|
|
if (!e.clipboardData) return [];
|
|
|
|
|
|
|
|
const files = [];
|
|
|
|
for (const item of e.clipboardData.items || []) {
|
|
|
|
if (!item.type || !item.type.startsWith('image/')) continue;
|
|
|
|
files.push(item.getAsFile());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (files.length) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function insertAtCursor(field, value) {
|
|
|
|
if (field.selectionStart || field.selectionStart === 0) {
|
|
|
|
const startPos = field.selectionStart;
|
|
|
|
const endPos = field.selectionEnd;
|
|
|
|
field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
|
|
|
|
field.selectionStart = startPos + value.length;
|
|
|
|
field.selectionEnd = startPos + value.length;
|
|
|
|
} else {
|
|
|
|
field.value += value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceAndKeepCursor(field, oldval, newval) {
|
|
|
|
if (field.selectionStart || field.selectionStart === 0) {
|
|
|
|
const startPos = field.selectionStart;
|
|
|
|
const endPos = field.selectionEnd;
|
|
|
|
field.value = field.value.replace(oldval, newval);
|
|
|
|
field.selectionStart = startPos + newval.length - oldval.length;
|
|
|
|
field.selectionEnd = endPos + newval.length - oldval.length;
|
|
|
|
} else {
|
|
|
|
field.value = field.value.replace(oldval, newval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initCompImagePaste($target) {
|
|
|
|
$target.each(function () {
|
|
|
|
const dropzone = this.querySelector('.dropzone');
|
|
|
|
if (!dropzone) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-22 03:19:01 -05:00
|
|
|
const uploadUrl = dropzone.getAttribute('data-upload-url');
|
2021-10-16 13:28:04 -04:00
|
|
|
const dropzoneFiles = dropzone.querySelector('.files');
|
|
|
|
for (const textarea of this.querySelectorAll('textarea')) {
|
|
|
|
textarea.addEventListener('paste', async (e) => {
|
|
|
|
for (const img of clipboardPastedImages(e)) {
|
2022-02-18 01:50:36 -05:00
|
|
|
const name = img.name.slice(0, img.name.lastIndexOf('.'));
|
2021-10-16 13:28:04 -04:00
|
|
|
insertAtCursor(textarea, `![${name}]()`);
|
|
|
|
const data = await uploadFile(img, uploadUrl);
|
2021-11-08 01:27:40 -05:00
|
|
|
replaceAndKeepCursor(textarea, `![${name}]()`, `![${name}](/attachments/${data.uuid})`);
|
2021-10-16 13:28:04 -04:00
|
|
|
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
|
|
|
|
dropzoneFiles.appendChild(input[0]);
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-09 21:51:27 -05:00
|
|
|
export function initEasyMDEImagePaste(easyMDE, dropzone, files) {
|
2021-11-22 03:19:01 -05:00
|
|
|
const uploadUrl = dropzone.getAttribute('data-upload-url');
|
2021-12-09 21:51:27 -05:00
|
|
|
easyMDE.codemirror.on('paste', async (_, e) => {
|
2021-10-16 13:28:04 -04:00
|
|
|
for (const img of clipboardPastedImages(e)) {
|
2022-02-18 01:50:36 -05:00
|
|
|
const name = img.name.slice(0, img.name.lastIndexOf('.'));
|
2021-10-16 13:28:04 -04:00
|
|
|
const data = await uploadFile(img, uploadUrl);
|
2021-12-09 21:51:27 -05:00
|
|
|
const pos = easyMDE.codemirror.getCursor();
|
|
|
|
easyMDE.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
|
2021-10-16 13:28:04 -04:00
|
|
|
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
|
|
|
|
files.append(input);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|