2017-10-19 07:45:52 +00:00
|
|
|
import Dropzone from 'dropzone';
|
2021-02-14 18:09:20 +00:00
|
|
|
import $ from 'jquery';
|
2020-04-21 18:09:31 +00:00
|
|
|
import { escape } from 'lodash';
|
2018-09-13 21:57:38 +00:00
|
|
|
import './behaviors/preview_markdown';
|
2020-10-23 09:08:41 +00:00
|
|
|
import { spriteIcon } from '~/lib/utils/common_utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { getFilename } from '~/lib/utils/file_upload';
|
2021-09-09 12:09:09 +00:00
|
|
|
import { truncate } from '~/lib/utils/text_utility';
|
2021-02-14 18:09:20 +00:00
|
|
|
import { n__, __ } from '~/locale';
|
2021-02-01 15:08:56 +00:00
|
|
|
import PasteMarkdownTable from './behaviors/markdown/paste_markdown_table';
|
|
|
|
import axios from './lib/utils/axios_utils';
|
2021-02-14 18:09:20 +00:00
|
|
|
import csrf from './lib/utils/csrf';
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2017-12-19 10:12:32 +00:00
|
|
|
Dropzone.autoDiscover = false;
|
|
|
|
|
2018-09-03 04:46:20 +00:00
|
|
|
/**
|
|
|
|
* Return the error message string from the given response.
|
|
|
|
*
|
|
|
|
* @param {String|Object} res
|
|
|
|
*/
|
|
|
|
function getErrorMessage(res) {
|
2020-04-21 15:21:10 +00:00
|
|
|
if (!res || typeof res === 'string') {
|
2018-09-03 04:46:20 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.message;
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:09:50 +00:00
|
|
|
export default function dropzoneInput(form, config = { parallelUploads: 2 }) {
|
2017-10-19 07:45:52 +00:00
|
|
|
const divHover = '<div class="div-dropzone-hover"></div>';
|
2020-10-26 15:08:40 +00:00
|
|
|
const iconPaperclip = spriteIcon('paperclip', 'div-dropzone-icon s24');
|
2017-10-19 07:45:52 +00:00
|
|
|
const $attachButton = form.find('.button-attach-file');
|
|
|
|
const $attachingFileMessage = form.find('.attaching-file-message');
|
|
|
|
const $cancelButton = form.find('.button-cancel-uploading-files');
|
|
|
|
const $retryLink = form.find('.retry-uploading-link');
|
|
|
|
const $uploadProgress = form.find('.uploading-progress');
|
|
|
|
const $uploadingErrorContainer = form.find('.uploading-error-container');
|
|
|
|
const $uploadingErrorMessage = form.find('.uploading-error-message');
|
|
|
|
const $uploadingProgressContainer = form.find('.uploading-progress-container');
|
2018-09-03 04:09:24 +00:00
|
|
|
const uploadsPath = form.data('uploads-path') || window.uploads_path || null;
|
2017-10-19 07:45:52 +00:00
|
|
|
const maxFileSize = gon.max_file_size || 10;
|
|
|
|
const formTextarea = form.find('.js-gfm-input');
|
|
|
|
let handlePaste;
|
|
|
|
let pasteText;
|
|
|
|
let addFileToForm;
|
|
|
|
let updateAttachingMessage;
|
|
|
|
let uploadFile;
|
2021-11-18 18:14:27 +00:00
|
|
|
let hasPlainText;
|
2017-10-19 07:45:52 +00:00
|
|
|
|
|
|
|
formTextarea.wrap('<div class="div-dropzone"></div>');
|
2020-12-23 18:10:19 +00:00
|
|
|
formTextarea.on('paste', (event) => handlePaste(event));
|
2017-10-19 07:45:52 +00:00
|
|
|
|
|
|
|
// Add dropzone area to the form.
|
|
|
|
const $mdArea = formTextarea.closest('.md-area');
|
|
|
|
const $formDropzone = form.find('.div-dropzone');
|
|
|
|
$formDropzone.parent().addClass('div-dropzone-wrapper');
|
|
|
|
$formDropzone.append(divHover);
|
|
|
|
$formDropzone.find('.div-dropzone-hover').append(iconPaperclip);
|
|
|
|
|
2017-11-22 23:20:23 +00:00
|
|
|
if (!uploadsPath) {
|
|
|
|
$formDropzone.addClass('js-invalid-dropzone');
|
2018-09-05 12:22:52 +00:00
|
|
|
return null;
|
2017-11-22 23:20:23 +00:00
|
|
|
}
|
2017-10-19 07:45:52 +00:00
|
|
|
|
|
|
|
const dropzone = $formDropzone.dropzone({
|
|
|
|
url: uploadsPath,
|
|
|
|
dictDefaultMessage: '',
|
|
|
|
clickable: true,
|
|
|
|
paramName: 'file',
|
|
|
|
maxFilesize: maxFileSize,
|
|
|
|
uploadMultiple: false,
|
|
|
|
headers: csrf.headers,
|
|
|
|
previewContainer: false,
|
2020-04-08 21:09:50 +00:00
|
|
|
...config,
|
2017-10-19 07:45:52 +00:00
|
|
|
dragover: () => {
|
|
|
|
$mdArea.addClass('is-dropzone-hover');
|
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0.7);
|
|
|
|
},
|
|
|
|
dragleave: () => {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0);
|
|
|
|
},
|
|
|
|
drop: () => {
|
|
|
|
$mdArea.removeClass('is-dropzone-hover');
|
|
|
|
form.find('.div-dropzone-hover').css('opacity', 0);
|
|
|
|
formTextarea.focus();
|
|
|
|
},
|
|
|
|
success(header, response) {
|
|
|
|
const processingFileCount = this.getQueuedFiles().length + this.getUploadingFiles().length;
|
|
|
|
const shouldPad = processingFileCount >= 1;
|
|
|
|
|
|
|
|
pasteText(response.link.markdown, shouldPad);
|
|
|
|
// Show 'Attach a file' link only when all files have been uploaded.
|
|
|
|
if (!processingFileCount) $attachButton.removeClass('hide');
|
|
|
|
addFileToForm(response.link.url);
|
|
|
|
},
|
2019-05-21 10:40:24 +00:00
|
|
|
error: (file, errorMessage = __('Attaching the file failed.'), xhr) => {
|
2017-10-19 07:45:52 +00:00
|
|
|
// If 'error' event is fired by dropzone, the second parameter is error message.
|
|
|
|
// If the 'errorMessage' parameter is empty, the default error message is set.
|
|
|
|
// If the 'error' event is fired by backend (xhr) error response, the third parameter is
|
|
|
|
// xhr object (xhr.responseText is error message).
|
|
|
|
// On error we hide the 'Attach' and 'Cancel' buttons
|
|
|
|
// and show an error.
|
2018-09-03 04:46:20 +00:00
|
|
|
const message = getErrorMessage(errorMessage || xhr.responseText);
|
2017-05-16 08:52:17 +00:00
|
|
|
|
2017-10-19 07:45:52 +00:00
|
|
|
$uploadingErrorContainer.removeClass('hide');
|
|
|
|
$uploadingErrorMessage.html(message);
|
|
|
|
$attachButton.addClass('hide');
|
|
|
|
$cancelButton.addClass('hide');
|
|
|
|
},
|
|
|
|
totaluploadprogress(totalUploadProgress) {
|
|
|
|
updateAttachingMessage(this.files, $attachingFileMessage);
|
|
|
|
$uploadProgress.text(`${Math.round(totalUploadProgress)}%`);
|
|
|
|
},
|
|
|
|
sending: () => {
|
|
|
|
// DOM elements already exist.
|
|
|
|
// Instead of dynamically generating them,
|
|
|
|
// we just either hide or show them.
|
|
|
|
$attachButton.addClass('hide');
|
|
|
|
$uploadingErrorContainer.addClass('hide');
|
|
|
|
$uploadingProgressContainer.removeClass('hide');
|
|
|
|
$cancelButton.removeClass('hide');
|
|
|
|
},
|
|
|
|
removedfile: () => {
|
|
|
|
$attachButton.removeClass('hide');
|
|
|
|
$cancelButton.addClass('hide');
|
|
|
|
$uploadingProgressContainer.addClass('hide');
|
|
|
|
$uploadingErrorContainer.addClass('hide');
|
|
|
|
},
|
|
|
|
queuecomplete: () => {
|
|
|
|
$('.dz-preview').remove();
|
|
|
|
$('.markdown-area').trigger('input');
|
|
|
|
|
|
|
|
$uploadingProgressContainer.addClass('hide');
|
|
|
|
$cancelButton.addClass('hide');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const child = $(dropzone[0]).children('textarea');
|
|
|
|
|
|
|
|
// removeAllFiles(true) stops uploading files (if any)
|
|
|
|
// and remove them from dropzone files queue.
|
2020-12-23 18:10:19 +00:00
|
|
|
$cancelButton.on('click', (e) => {
|
2017-10-19 07:45:52 +00:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-11-01 17:58:11 +00:00
|
|
|
Dropzone.forElement($formDropzone.get(0)).removeAllFiles(true);
|
2017-10-19 07:45:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// If 'error' event is fired, we store a failed files,
|
|
|
|
// clear dropzone files queue, change status of failed files to undefined,
|
|
|
|
// and add that files to the dropzone files queue again.
|
|
|
|
// addFile() adds file to dropzone files queue and upload it.
|
2020-12-23 18:10:19 +00:00
|
|
|
$retryLink.on('click', (e) => {
|
2018-10-24 19:17:03 +00:00
|
|
|
const dropzoneInstance = Dropzone.forElement(
|
|
|
|
e.target.closest('.js-main-target-form').querySelector('.div-dropzone'),
|
|
|
|
);
|
2017-10-19 07:45:52 +00:00
|
|
|
const failedFiles = dropzoneInstance.files;
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
// 'true' parameter of removeAllFiles() cancels
|
|
|
|
// uploading of files that are being uploaded at the moment.
|
|
|
|
dropzoneInstance.removeAllFiles(true);
|
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
failedFiles.map((failedFile) => {
|
2017-10-19 07:45:52 +00:00
|
|
|
const file = failedFile;
|
|
|
|
|
|
|
|
if (file.status === Dropzone.ERROR) {
|
|
|
|
file.status = undefined;
|
|
|
|
file.accepted = undefined;
|
2017-03-11 07:30:44 +00:00
|
|
|
}
|
2017-05-16 08:52:17 +00:00
|
|
|
|
2017-10-19 07:45:52 +00:00
|
|
|
return dropzoneInstance.addFile(file);
|
2017-05-16 08:52:17 +00:00
|
|
|
});
|
2017-10-19 07:45:52 +00:00
|
|
|
});
|
2021-10-28 12:10:22 +00:00
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
handlePaste = (event) => {
|
2017-10-19 07:45:52 +00:00
|
|
|
const pasteEvent = event.originalEvent;
|
2019-12-31 18:09:10 +00:00
|
|
|
const { clipboardData } = pasteEvent;
|
|
|
|
if (clipboardData && clipboardData.items) {
|
2020-01-09 09:07:51 +00:00
|
|
|
const converter = new PasteMarkdownTable(clipboardData);
|
2019-12-31 18:09:10 +00:00
|
|
|
// Apple Numbers copies a table as an image, HTML, and text, so
|
|
|
|
// we need to check for the presence of a table first.
|
2020-01-09 09:07:51 +00:00
|
|
|
if (converter.isTable()) {
|
2017-10-19 07:45:52 +00:00
|
|
|
event.preventDefault();
|
2019-12-31 18:09:10 +00:00
|
|
|
const text = converter.convertToTableMarkdown();
|
2017-10-19 07:45:52 +00:00
|
|
|
pasteText(text);
|
2021-11-18 18:14:27 +00:00
|
|
|
} else if (!hasPlainText(pasteEvent)) {
|
2021-10-28 12:10:22 +00:00
|
|
|
const fileList = [...clipboardData.files];
|
|
|
|
fileList.forEach((file) => {
|
|
|
|
if (file.type.indexOf('image') !== -1) {
|
|
|
|
event.preventDefault();
|
|
|
|
const MAX_FILE_NAME_LENGTH = 246;
|
|
|
|
|
|
|
|
const filename = getFilename(file) || 'image.png';
|
|
|
|
const truncateFilename = truncate(filename, MAX_FILE_NAME_LENGTH);
|
|
|
|
const text = `{{${truncateFilename}}}`;
|
|
|
|
pasteText(text);
|
|
|
|
|
|
|
|
uploadFile(file, truncateFilename);
|
|
|
|
}
|
|
|
|
});
|
2017-05-16 08:52:17 +00:00
|
|
|
}
|
2017-10-19 07:45:52 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-18 18:14:27 +00:00
|
|
|
hasPlainText = (data) => {
|
|
|
|
const clipboardDataList = [...data.clipboardData.items];
|
|
|
|
return clipboardDataList.some((item) => item.type === 'text/plain');
|
|
|
|
};
|
|
|
|
|
2017-10-19 07:45:52 +00:00
|
|
|
pasteText = (text, shouldPad) => {
|
|
|
|
let formattedText = text;
|
|
|
|
if (shouldPad) {
|
|
|
|
formattedText += '\n\n';
|
|
|
|
}
|
|
|
|
const textarea = child.get(0);
|
|
|
|
const caretStart = textarea.selectionStart;
|
|
|
|
const caretEnd = textarea.selectionEnd;
|
|
|
|
const textEnd = $(child).val().length;
|
2020-12-23 12:10:26 +00:00
|
|
|
const beforeSelection = $(child).val().substring(0, caretStart);
|
|
|
|
const afterSelection = $(child).val().substring(caretEnd, textEnd);
|
2017-10-19 07:45:52 +00:00
|
|
|
$(child).val(beforeSelection + formattedText + afterSelection);
|
|
|
|
textarea.setSelectionRange(caretStart + formattedText.length, caretEnd + formattedText.length);
|
|
|
|
textarea.style.height = `${textarea.scrollHeight}px`;
|
|
|
|
formTextarea.get(0).dispatchEvent(new Event('input'));
|
|
|
|
return formTextarea.trigger('input');
|
|
|
|
};
|
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
addFileToForm = (path) => {
|
2020-04-21 18:09:31 +00:00
|
|
|
$(form).append(`<input type="hidden" name="files[]" value="${escape(path)}">`);
|
2017-10-19 07:45:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const showSpinner = () => $uploadingProgressContainer.removeClass('hide');
|
|
|
|
|
|
|
|
const closeSpinner = () => $uploadingProgressContainer.addClass('hide');
|
|
|
|
|
2020-12-23 18:10:19 +00:00
|
|
|
const showError = (message) => {
|
2017-10-19 07:45:52 +00:00
|
|
|
$uploadingErrorContainer.removeClass('hide');
|
|
|
|
$uploadingErrorMessage.html(message);
|
|
|
|
};
|
|
|
|
|
|
|
|
const insertToTextArea = (filename, url) => {
|
|
|
|
const $child = $(child);
|
2020-02-25 12:08:48 +00:00
|
|
|
const textarea = $child.get(0);
|
|
|
|
const caretStart = textarea.selectionStart;
|
|
|
|
const caretEnd = textarea.selectionEnd;
|
|
|
|
const formattedText = `{{${filename}}}`;
|
|
|
|
$child.val((index, val) => val.replace(formattedText, url));
|
|
|
|
textarea.setSelectionRange(
|
|
|
|
caretStart - formattedText.length + url.length,
|
|
|
|
caretEnd - formattedText.length + url.length,
|
|
|
|
);
|
2017-10-19 07:45:52 +00:00
|
|
|
$child.trigger('change');
|
|
|
|
};
|
|
|
|
|
|
|
|
uploadFile = (item, filename) => {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('file', item, filename);
|
2018-01-26 10:34:22 +00:00
|
|
|
|
|
|
|
showSpinner();
|
|
|
|
|
2018-10-24 19:17:03 +00:00
|
|
|
axios
|
|
|
|
.post(uploadsPath, formData)
|
2018-01-26 10:34:22 +00:00
|
|
|
.then(({ data }) => {
|
|
|
|
const md = data.link.markdown;
|
|
|
|
|
2017-10-19 07:45:52 +00:00
|
|
|
insertToTextArea(filename, md);
|
2018-01-26 10:34:22 +00:00
|
|
|
closeSpinner();
|
|
|
|
})
|
2020-12-23 18:10:19 +00:00
|
|
|
.catch((e) => {
|
2018-01-26 10:34:22 +00:00
|
|
|
showError(e.response.data.message);
|
|
|
|
closeSpinner();
|
|
|
|
});
|
2017-10-19 07:45:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
updateAttachingMessage = (files, messageContainer) => {
|
2020-12-23 18:10:19 +00:00
|
|
|
const filesCount = files.filter(
|
|
|
|
(file) => file.status === 'uploading' || file.status === 'queued',
|
|
|
|
).length;
|
2019-05-21 10:40:24 +00:00
|
|
|
const attachingMessage = n__('Attaching a file', 'Attaching %d files', filesCount);
|
2017-10-19 07:45:52 +00:00
|
|
|
|
2019-05-21 10:40:24 +00:00
|
|
|
messageContainer.text(`${attachingMessage} -`);
|
2017-10-19 07:45:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
form.find('.markdown-selector').click(function onMarkdownClick(e) {
|
|
|
|
e.preventDefault();
|
2020-12-23 12:10:26 +00:00
|
|
|
$(this).closest('.gfm-form').find('.div-dropzone').click();
|
2017-10-19 07:45:52 +00:00
|
|
|
formTextarea.focus();
|
|
|
|
});
|
2018-09-05 12:22:52 +00:00
|
|
|
|
2019-11-18 15:06:27 +00:00
|
|
|
return $formDropzone.get(0) ? Dropzone.forElement($formDropzone.get(0)) : null;
|
2017-10-19 07:45:52 +00:00
|
|
|
}
|