forked from MarseyWorld/MarseyWorld
fix pasting in file-upload
parent
d665250bca
commit
6872f7760b
|
@ -609,11 +609,14 @@ function handle_files(input, newfiles) {
|
||||||
|
|
||||||
|
|
||||||
file_upload = document.getElementById('file-upload');
|
file_upload = document.getElementById('file-upload');
|
||||||
|
file_upload_edit_attachment = document.getElementById('file-upload-edit-attachment');
|
||||||
|
file_upload_or_edit = file_upload ? file_upload : file_upload_edit_attachment
|
||||||
|
|
||||||
|
if (file_upload_or_edit) {
|
||||||
|
|
||||||
if (file_upload) {
|
|
||||||
function remove_attachment() {
|
function remove_attachment() {
|
||||||
file_upload.value = null;
|
file_upload_or_edit.value = null;
|
||||||
file_upload.previousElementSibling.textContent = 'Select File';
|
file_upload_or_edit.previousElementSibling.textContent = 'Select File';
|
||||||
document.getElementById('image-preview').classList.add('d-none');
|
document.getElementById('image-preview').classList.add('d-none');
|
||||||
document.getElementById('image-preview').classList.remove('mr-2');
|
document.getElementById('image-preview').classList.remove('mr-2');
|
||||||
document.getElementById('urlblock').classList.remove('d-none');
|
document.getElementById('urlblock').classList.remove('d-none');
|
||||||
|
@ -626,15 +629,15 @@ if (file_upload) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function display_url_image() {
|
function display_url_image() {
|
||||||
if (file_upload.files)
|
if (file_upload_or_edit.files)
|
||||||
{
|
{
|
||||||
const file = file_upload.files[0]
|
const file = file_upload_or_edit.files[0]
|
||||||
if (check_file_size(file_upload, file)) {
|
if (check_file_size(file_upload_or_edit, file)) {
|
||||||
const char_limit = innerWidth >= 768 ? 50 : 13;
|
const char_limit = innerWidth >= 768 ? 50 : 13;
|
||||||
file_upload.previousElementSibling.textContent = file.name.substr(0, char_limit);
|
file_upload_or_edit.previousElementSibling.textContent = file.name.substr(0, char_limit);
|
||||||
if (file.type.startsWith('image/')) {
|
if (file.type.startsWith('image/')) {
|
||||||
const fileReader = new FileReader();
|
const fileReader = new FileReader();
|
||||||
fileReader.readAsDataURL(file_upload.files[0]);
|
fileReader.readAsDataURL(file_upload_or_edit.files[0]);
|
||||||
fileReader.onload = function() {
|
fileReader.onload = function() {
|
||||||
document.getElementById('image-preview').setAttribute('src', this.result);
|
document.getElementById('image-preview').setAttribute('src', this.result);
|
||||||
document.getElementById('image-preview').classList.remove('d-none');
|
document.getElementById('image-preview').classList.remove('d-none');
|
||||||
|
@ -661,17 +664,16 @@ if (file_upload) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file_upload.onchange = () => {
|
file_upload_or_edit.onchange = () => {
|
||||||
display_url_image()
|
display_url_image()
|
||||||
if (typeof submit_save_files === "function") {
|
if (typeof submit_save_files === "function") {
|
||||||
const array = []
|
const array = []
|
||||||
for (const x of file_upload.files) {
|
for (const x of file_upload_or_edit.files) {
|
||||||
array.push(x)
|
array.push(x)
|
||||||
}
|
}
|
||||||
submit_save_files("attachment", array);
|
submit_save_files("attachment", array);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
document.onpaste = function(event) {
|
document.onpaste = function(event) {
|
||||||
|
@ -681,10 +683,7 @@ document.onpaste = function(event) {
|
||||||
const focused = document.activeElement;
|
const focused = document.activeElement;
|
||||||
let input;
|
let input;
|
||||||
|
|
||||||
if (focused) {
|
if (file_upload) {
|
||||||
input = focused.parentElement.querySelector('input[type="file"]:not(.nofocuspasting)')
|
|
||||||
}
|
|
||||||
else if (file_upload) {
|
|
||||||
if (location.pathname.endsWith('/submit') && focused && focused.id == 'post-text') {
|
if (location.pathname.endsWith('/submit') && focused && focused.id == 'post-text') {
|
||||||
input = document.getElementById('file-upload-submit')
|
input = document.getElementById('file-upload-submit')
|
||||||
}
|
}
|
||||||
|
@ -701,6 +700,9 @@ document.onpaste = function(event) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (focused) {
|
||||||
|
input = focused.parentElement.querySelector('input[type="file"]:not(.nofocuspasting)')
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
input = document.querySelector('input[type="file"]')
|
input = document.querySelector('input[type="file"]')
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,9 +227,9 @@
|
||||||
|
|
||||||
<div style="display:flex;align-items:center" class="mb-3">
|
<div style="display:flex;align-items:center" class="mb-3">
|
||||||
<img loading="lazy" id="image-preview" class="mt-2 mb-3" style="max-width:50%" data-nonce="{{g.nonce}}" data-onclick="expandImage()">
|
<img loading="lazy" id="image-preview" class="mt-2 mb-3" style="max-width:50%" data-nonce="{{g.nonce}}" data-onclick="expandImage()">
|
||||||
<label class="btn btn-secondary m-0 mr-1" for="file-upload">
|
<label class="btn btn-secondary m-0 mr-1" for="file-upload-edit-attachement">
|
||||||
<div>Select File</div>
|
<div>Select File</div>
|
||||||
<input autocomplete="off" class="nofocuspasting" id="file-upload" accept="image/*, video/*, audio/*" type="file" name="file-url" {% if g.is_tor %}disabled{% endif %} hidden>
|
<input autocomplete="off" class="nofocuspasting" id="file-upload-edit-attachement" accept="image/*, video/*, audio/*" type="file" name="file-url" {% if g.is_tor %}disabled{% endif %} hidden>
|
||||||
</label>
|
</label>
|
||||||
<button id="remove-attachment" type="button" style="font-size:25px" class="text-danger font-weight-bold ml-2 d-none" data-nonce="{{g.nonce}}" data-onclick="remove_attachment()">X</button>
|
<button id="remove-attachment" type="button" style="font-size:25px" class="text-danger font-weight-bold ml-2 d-none" data-nonce="{{g.nonce}}" data-onclick="remove_attachment()">X</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue