Skip to content

Commit

Permalink
Format editor.js and remove client-side key
Browse files Browse the repository at this point in the history
  • Loading branch information
parisk committed Aug 18, 2023
1 parent 79ad275 commit 39e9d73
Showing 1 changed file with 44 additions and 54 deletions.
98 changes: 44 additions & 54 deletions prose/static/prose/editor.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
// This code goes in /static/prose/editor.js or similar
function uploadAttachment(host, attachment) {
uploadFile(host, attachment.file, setProgress, setAttributes);
uploadFile(host, attachment.file, setProgress, setAttributes);

function setProgress(progress) {
attachment.setUploadProgress(progress);
}
function setProgress(progress) {
attachment.setUploadProgress(progress);
}

function setAttributes(attributes) {
attachment.setAttributes(attributes);
}
function setAttributes(attributes) {
attachment.setAttributes(attributes);
}
}

function uploadFile(host, file, progressCallback, successCallback) {
var key = createStorageKey(file);
var formData = createFormData(key, file);
var xhr = new XMLHttpRequest();

const csrfToken = document.querySelector("input[name=csrfmiddlewaretoken]").value;

formData.append('csrfmiddlewaretoken', csrfToken);

xhr.open("POST", host, true);

xhr.upload.addEventListener("progress", function(event) {
var progress = event.loaded / event.total * 100;
progressCallback(progress);
});

xhr.addEventListener("load", function(event) {
if (xhr.status == 201) {
const data = JSON.parse(xhr.response);

var attributes = {
url: data.url,
href: `${data.url}?content-disposition=attachment`
};
successCallback(attributes);
}
});

xhr.send(formData);
}
const formData = createFormData(file);
const xhr = new XMLHttpRequest();
const csrfToken = document.querySelector("input[name=csrfmiddlewaretoken]").value;

formData.append("csrfmiddlewaretoken", csrfToken);

xhr.open("POST", host, true);

xhr.upload.addEventListener("progress", function (event) {
const progress = event.loaded / event.total * 100;
progressCallback(progress);
});

xhr.addEventListener("load", function (event) {
if (xhr.status == 201) {
const data = JSON.parse(xhr.response);
const attributes = {
url: data.url,
href: `${data.url}?content-disposition=attachment`
};
successCallback(attributes);
}
});

function createStorageKey(file) {
var date = new Date();
var day = date.toISOString().slice(0,10);
var name = date.getTime() + "-" + file.name;
return [day, name].join("/");
xhr.send(formData);
}

function createFormData(key, file) {
var data = new FormData();
data.append("key", key);
data.append("file", file);
data.append("Content-Type", file.type);
return data;
function createFormData(file) {
const data = new FormData();
data.append("file", file);
data.append("Content-Type", file.type);
return data;
}

function initializeEditors() {
const editors = document.querySelectorAll('.django-prose-editor:not(.initialized)');
const editors = document.querySelectorAll(".django-prose-editor:not(.initialized)");

editors.forEach(editor => {
editor.addEventListener("trix-attachment-add", function(event) {
uploadAttachment(editor.dataset.uploadAttachmentUrl, event.attachment);
});
editor.classList.add('initialized');
editors.forEach(editor => {
editor.addEventListener("trix-attachment-add", function (event) {
uploadAttachment(editor.dataset.uploadAttachmentUrl, event.attachment);
});
editor.classList.add("initialized");
});
}

// When the DOM is initially loaded
document.addEventListener("DOMContentLoaded", initializeEditors);

// Export the initializeEditors function so it can be called from other scripts
window.initializeEditors = initializeEditors;
window.djangoProse = window.djangoProse || {}
window.djangoProse.initializeEditors = initializeEditors;

0 comments on commit 39e9d73

Please sign in to comment.