-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Format
editor.js
and remove client-side key
- Loading branch information
Showing
1 changed file
with
44 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |