From 3a67c1647dd9681ac5ed9600459e8bcb644d5e97 Mon Sep 17 00:00:00 2001 From: Aevann Date: Sat, 4 Mar 2023 21:54:06 +0200 Subject: [PATCH] fix bug with pasting in /submit + dedup --- files/assets/js/admin/update_assets.js | 40 ---------------- files/assets/js/core.js | 59 +++++++++++++++--------- files/assets/js/submit.js | 21 +-------- files/assets/js/submit_hats.js | 41 ---------------- files/assets/js/submit_marseys.js | 41 ---------------- files/templates/admin/update_assets.html | 7 +-- files/templates/sub/create_hole.html | 2 +- files/templates/submit.html | 4 +- files/templates/submit_hats.html | 4 +- files/templates/submit_marseys.html | 4 +- files/templates/util/macros.html | 4 ++ 11 files changed, 50 insertions(+), 177 deletions(-) delete mode 100644 files/assets/js/admin/update_assets.js diff --git a/files/assets/js/admin/update_assets.js b/files/assets/js/admin/update_assets.js deleted file mode 100644 index f8492268e..000000000 --- a/files/assets/js/admin/update_assets.js +++ /dev/null @@ -1,40 +0,0 @@ -const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',') - -document.onpaste = function(event) { - files = structuredClone(event.clipboardData.files); - - filename = files[0] - - if (filename) - { - filename = filename.name.toLowerCase() - f=document.getElementById('file-upload'); - f.files = files; - document.getElementById('filename-show').textContent = filename; - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - } - } -} - -document.getElementById('file-upload').addEventListener('change', function(){ - f=document.getElementById('file-upload'); - document.getElementById('filename-show').textContent = document.getElementById('file-upload').files[0].name.substr(0, 20); - filename = f.files[0].name.toLowerCase() - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - document.getElementById('submit-asset').disabled = false; - } -}) diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 66f11ba25..f30695355 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -516,6 +516,38 @@ function handle_files(input, newfiles) { } +file_upload = document.getElementById('file-upload'); + +if (file_upload) { + const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',') + + function process_url_image() { + if (file_upload.files) + { + const filename = file_upload.files[0].name + document.getElementById('filename-show').textContent = filename.substr(0, 20); + if (IMAGE_FORMATS.some(s => filename.toLowerCase().endsWith(s))) + { + const fileReader = new FileReader(); + fileReader.readAsDataURL(file_upload.files[0]); + fileReader.addEventListener("load", function () { + document.getElementById('image-preview').setAttribute('src', this.result); + document.getElementById('image-preview').classList.remove('d-none'); + }); + } + + if (typeof checkForRequired === "function") { + document.getElementById('urlblock').classList.add('d-none'); + checkForRequired(); + } + else { + document.getElementById('submit-btn').disabled = false; + } + } + } + file_upload.onchange = process_url_image +} + document.onpaste = function(event) { const files = structuredClone(event.clipboardData.files); if (!files.length) return @@ -523,32 +555,13 @@ document.onpaste = function(event) { const focused = document.activeElement; let input; - if (location.pathname.endsWith('/submit')) { - if (focused) { + if (file_upload) { + if (location.pathname.endsWith('/submit') && focused && focused.id == 'post-text') { input = document.getElementById('file-upload-submit') } else { - f=document.getElementById('file-upload'); - f.files += files; - - if (f.files.length > 20) - { - alert("You can't upload more than 20 files at one time!") - return - } - - document.getElementById('filename-show').textContent = filename; - document.getElementById('urlblock').classList.add('d-none'); - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () {document.getElementById('image-preview').setAttribute('src', this.result);}); - } - document.getElementById('post-url').value = null; - localStorage.setItem("post-url", "") - document.getElementById('image-upload-block').classList.remove('d-none') - checkForRequired(); + file_upload.files = files; + process_url_image(); return; } } diff --git a/files/assets/js/submit.js b/files/assets/js/submit.js index 36d2e9fea..13531d358 100644 --- a/files/assets/js/submit.js +++ b/files/assets/js/submit.js @@ -1,5 +1,4 @@ -const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',') -const submitButton = document.getElementById('create_button') +const submitButton = document.getElementById('submit') document.getElementById('post-title').value = localStorage.getItem("post-title") document.getElementById('post-text').value = localStorage.getItem("post-text") @@ -64,24 +63,6 @@ function hide_image() { } } - -document.getElementById('file-upload').addEventListener('change', function(){ - const f = document.getElementById('file-upload'); - if (f.files) - { - document.getElementById('urlblock').classList.add('d-none'); - const filename = f.files[0].name - document.getElementById('filename-show').textContent = filename.substr(0, 20); - if (IMAGE_FORMATS.some(s => filename.toLowerCase().endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () {document.getElementById('image-preview').setAttribute('src', this.result);}); - } - checkForRequired(); - } -}) - const saved_values = ['post-title', 'post-text', 'post-url', 'sub'] function savetext() { diff --git a/files/assets/js/submit_hats.js b/files/assets/js/submit_hats.js index 5fc107042..9ca1bf1b5 100644 --- a/files/assets/js/submit_hats.js +++ b/files/assets/js/submit_hats.js @@ -1,44 +1,3 @@ -const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',') - -document.onpaste = function(event) { - files = structuredClone(event.clipboardData.files); - - filename = files[0] - - if (filename) - { - filename = filename.name.toLowerCase() - f=document.getElementById('file-upload'); - f.files = files; - document.getElementById('filename-show').textContent = filename; - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - } - } -} - -document.getElementById('file-upload').addEventListener('change', function(){ - f=document.getElementById('file-upload'); - document.getElementById('filename-show').textContent = document.getElementById('file-upload').files[0].name.substr(0, 30); - filename = f.files[0].name.toLowerCase() - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - document.getElementById('submit-hat').disabled = false; - } -}) - function approve_hat(t, name) { postToast(t, `/admin/approve/hat/${name}`, { diff --git a/files/assets/js/submit_marseys.js b/files/assets/js/submit_marseys.js index 233d24609..88d0f2aeb 100644 --- a/files/assets/js/submit_marseys.js +++ b/files/assets/js/submit_marseys.js @@ -1,44 +1,3 @@ -const IMAGE_FORMATS = document.getElementById('IMAGE_FORMATS').value.split(',') - -document.onpaste = function(event) { - files = structuredClone(event.clipboardData.files); - - filename = files[0] - - if (filename) - { - filename = filename.name.toLowerCase() - f=document.getElementById('file-upload'); - f.files = files; - document.getElementById('filename-show').textContent = filename; - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - } - } -} - -document.getElementById('file-upload').addEventListener('change', function(){ - f=document.getElementById('file-upload'); - document.getElementById('filename-show').textContent = document.getElementById('file-upload').files[0].name.substr(0, 30); - filename = f.files[0].name.toLowerCase() - if (IMAGE_FORMATS.some(s => filename.endsWith(s))) - { - const fileReader = new FileReader(); - fileReader.readAsDataURL(f.files[0]); - fileReader.addEventListener("load", function () { - document.getElementById('image-preview').setAttribute('src', this.result); - document.getElementById('image-preview').classList.remove('d-none'); - }); - document.getElementById('submit-marsey').disabled = false; - } -}) - function approve_marsey(t, name) { postToast(t, `/admin/approve/marsey/${name}`, { diff --git a/files/templates/admin/update_assets.html b/files/templates/admin/update_assets.html index 56aa746b1..7f6f735df 100644 --- a/files/templates/admin/update_assets.html +++ b/files/templates/admin/update_assets.html @@ -32,7 +32,7 @@ {% endif %} @@ -40,8 +40,5 @@ - - - - + {{macros.image_formats()}} {% endblock %} diff --git a/files/templates/sub/create_hole.html b/files/templates/sub/create_hole.html index 7b490bf3b..1b7547f35 100644 --- a/files/templates/sub/create_hole.html +++ b/files/templates/sub/create_hole.html @@ -19,7 +19,7 @@ {{error}}

{% endif %} - +

Cost: {{cost}} coins

diff --git a/files/templates/submit.html b/files/templates/submit.html index 660bae576..269ff4b62 100644 --- a/files/templates/submit.html +++ b/files/templates/submit.html @@ -101,7 +101,7 @@ - + @@ -110,7 +110,7 @@ {% endblock %} - + {{macros.image_formats()}} {% include "modals/emoji.html" %} {% include "modals/gif.html" %} diff --git a/files/templates/submit_hats.html b/files/templates/submit_hats.html index 8b3093ef7..831726f3b 100644 --- a/files/templates/submit_hats.html +++ b/files/templates/submit_hats.html @@ -38,7 +38,7 @@ @@ -47,7 +47,7 @@ - + {{macros.image_formats()}} diff --git a/files/templates/submit_marseys.html b/files/templates/submit_marseys.html index f1c357aba..f7aa65b03 100644 --- a/files/templates/submit_marseys.html +++ b/files/templates/submit_marseys.html @@ -33,7 +33,7 @@ @@ -42,7 +42,7 @@ - + {{macros.image_formats()}} diff --git a/files/templates/util/macros.html b/files/templates/util/macros.html index f302e6e96..e9229c4da 100644 --- a/files/templates/util/macros.html +++ b/files/templates/util/macros.html @@ -208,3 +208,7 @@ {% endif %} {% endmacro %} + +{% macro image_formats() %} + +{% endmacro %}