@@ -37324,6 +37324,61 @@ $.ajaxSetup({
3732437324 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
3732537325 }
3732637326});
37327+ addEventListener("trix-attachment-add", function (event) {
37328+ if (event.attachment.file) {
37329+ uploadFileAttachment(event.attachment);
37330+ }
37331+ });
37332+
37333+ function uploadFileAttachment(attachment) {
37334+ uploadFile(attachment.file, setProgress, setAttributes);
37335+
37336+ function setProgress(progress) {
37337+ attachment.setUploadProgress(progress);
37338+ }
37339+
37340+ function setAttributes(attributes) {
37341+ attachment.setAttributes(attributes);
37342+ }
37343+ }
37344+
37345+ function uploadFile(file, progressCallback, successCallback) {
37346+ var formData = createFormData(file);
37347+ var xhr = new XMLHttpRequest();
37348+ xhr.open("POST", base_url + 'trix-upload', true);
37349+ xhr.setRequestHeader('X-CSRF-TOKEN', getMeta('csrf-token'));
37350+ xhr.upload.addEventListener("progress", function (event) {
37351+ var progress = event.loaded / event.total * 100;
37352+ progressCallback(progress);
37353+ });
37354+ xhr.addEventListener("load", function (event) {
37355+ var attributes = {
37356+ url: xhr.responseText,
37357+ href: xhr.responseText + "?content-disposition=attachment"
37358+ };
37359+ successCallback(attributes);
37360+ });
37361+ xhr.send(formData);
37362+ }
37363+
37364+ function createFormData(file) {
37365+ var data = new FormData();
37366+ data.append("Content-Type", file.type);
37367+ data.append("file", file);
37368+ return data;
37369+ }
37370+
37371+ function getMeta(metaName) {
37372+ var metas = document.getElementsByTagName('meta');
37373+
37374+ for (var i = 0; i < metas.length; i++) {
37375+ if (metas[i].getAttribute('name') === metaName) {
37376+ return metas[i].getAttribute('content');
37377+ }
37378+ }
37379+
37380+ return '';
37381+ }
3732737382/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! jquery */ "./node_modules/jquery/src/jquery.js"), __webpack_require__(/*! jquery */ "./node_modules/jquery/src/jquery.js")))
3732837383
3732937384/***/ }),
0 commit comments