You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImagePaste.js 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const {csrfToken} = window.config;
  2. async function uploadFile(file, uploadUrl) {
  3. const formData = new FormData();
  4. formData.append('file', file, file.name);
  5. const res = await fetch(uploadUrl, {
  6. method: 'POST',
  7. headers: {'X-Csrf-Token': csrfToken},
  8. body: formData,
  9. });
  10. return await res.json();
  11. }
  12. function clipboardPastedImages(e) {
  13. if (!e.clipboardData) return [];
  14. const files = [];
  15. for (const item of e.clipboardData.items || []) {
  16. if (!item.type || !item.type.startsWith('image/')) continue;
  17. files.push(item.getAsFile());
  18. }
  19. if (files.length) {
  20. e.preventDefault();
  21. e.stopPropagation();
  22. }
  23. return files;
  24. }
  25. function insertAtCursor(field, value) {
  26. if (field.selectionStart || field.selectionStart === 0) {
  27. const startPos = field.selectionStart;
  28. const endPos = field.selectionEnd;
  29. field.value = field.value.substring(0, startPos) + value + field.value.substring(endPos, field.value.length);
  30. field.selectionStart = startPos + value.length;
  31. field.selectionEnd = startPos + value.length;
  32. } else {
  33. field.value += value;
  34. }
  35. }
  36. function replaceAndKeepCursor(field, oldval, newval) {
  37. if (field.selectionStart || field.selectionStart === 0) {
  38. const startPos = field.selectionStart;
  39. const endPos = field.selectionEnd;
  40. field.value = field.value.replace(oldval, newval);
  41. field.selectionStart = startPos + newval.length - oldval.length;
  42. field.selectionEnd = endPos + newval.length - oldval.length;
  43. } else {
  44. field.value = field.value.replace(oldval, newval);
  45. }
  46. }
  47. export function initCompImagePaste($target) {
  48. $target.each(function () {
  49. const dropzone = this.querySelector('.dropzone');
  50. if (!dropzone) {
  51. return;
  52. }
  53. const uploadUrl = dropzone.getAttribute('data-upload-url');
  54. const dropzoneFiles = dropzone.querySelector('.files');
  55. for (const textarea of this.querySelectorAll('textarea')) {
  56. textarea.addEventListener('paste', async (e) => {
  57. for (const img of clipboardPastedImages(e)) {
  58. const name = img.name.substr(0, img.name.lastIndexOf('.'));
  59. insertAtCursor(textarea, `![${name}]()`);
  60. const data = await uploadFile(img, uploadUrl);
  61. replaceAndKeepCursor(textarea, `![${name}]()`, `![${name}](/attachments/${data.uuid})`);
  62. const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
  63. dropzoneFiles.appendChild(input[0]);
  64. }
  65. }, false);
  66. }
  67. });
  68. }
  69. export function initSimpleMDEImagePaste(simplemde, dropzone, files) {
  70. const uploadUrl = dropzone.getAttribute('data-upload-url');
  71. simplemde.codemirror.on('paste', async (_, e) => {
  72. for (const img of clipboardPastedImages(e)) {
  73. const name = img.name.substr(0, img.name.lastIndexOf('.'));
  74. const data = await uploadFile(img, uploadUrl);
  75. const pos = simplemde.codemirror.getCursor();
  76. simplemde.codemirror.replaceRange(`![${name}](/attachments/${data.uuid})`, pos);
  77. const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
  78. files.append(input);
  79. }
  80. });
  81. }