save images in /submit too

master
Aevann 2023-08-07 16:09:46 +03:00
parent f43d9901a9
commit 67a2693409
2 changed files with 53 additions and 1 deletions

View File

@ -514,6 +514,20 @@ function handle_files(input, newfiles) {
autoExpand(ta)
if (typeof checkForRequired === "function") checkForRequired();
if (location.pathname.endsWith('/submit')) {
files_b64 = JSON.parse(localStorage.getItem("files_b64")) || [];
for (const file of newfiles) {
fileReader = new FileReader();
fileReader.onload = function () {
files_b64.push([this.filename, this.result]);
localStorage.setItem("files_b64", JSON.stringify(files_b64));
savetext()
};
fileReader.filename = file.name
fileReader.readAsDataURL(file);
}
}
}
@ -532,6 +546,8 @@ if (file_upload) {
fileReader.readAsDataURL(file_upload.files[0]);
fileReader.onload = function () {
document.getElementById('image-preview').setAttribute('src', this.result);
const str = JSON.stringify([filename, this.result])
localStorage.setItem("attachment_b64", str);
document.getElementById('image-preview').classList.remove('d-none');
document.getElementById('image-preview').classList.add('mr-2');
document.getElementById('image-preview').nextElementSibling.classList.add('mt-3');

View File

@ -15,7 +15,6 @@ for (const key of save_checked) {
const element = document.getElementById(key)
if (element) element.checked = (value == 'true')
}
}
function savetext() {
@ -197,6 +196,9 @@ function submit(form) {
const value = (id == "post-notify")
localStorage.setItem(id, value)
}
localStorage.removeItem("attachment_b64")
localStorage.removeItem("files_b64")
}
location.href = "/post/" + post_id
@ -217,3 +219,37 @@ function submit(form) {
xhr.send(formData);
}
async function array_to_file(array) {
const res = await fetch(array[1]);
const blob = await res.blob();
return new File([blob], array[0], { type: 'image/png' });
}
async function restore_attachment() {
const array = JSON.parse(localStorage.getItem("attachment_b64"))
if (!array) return
const list = new DataTransfer();
const file = await array_to_file(array)
list.items.add(file);
document.getElementById("file-upload").files = list.files
process_url_image()
}
async function restore_files() {
oldfiles["post-text"] = []
const files_b64_get = JSON.parse(localStorage.getItem("files_b64"))
if (!files_b64_get) return
const list = new DataTransfer();
for (const array of files_b64_get) {
const file = await array_to_file(array)
list.items.add(file);
oldfiles["post-text"].push(file)
}
document.getElementById("file-upload-submit").files = list.files
}
restore_attachment()
restore_files()