From 638d6f987908f44766e1aa2f7fc4c566e0f63c04 Mon Sep 17 00:00:00 2001 From: lalka2016 <99399973+lalka2016@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:34:22 +0300 Subject: [PATCH] Fix graffiti and copypaste MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Какого-то хуя копипаста у постов срабатывает два раза. --- Web/static/js/al_wall.js | 102 ++++++++++++++++++++++++--------------- locales/en.strings | 1 + locales/ru.strings | 1 + 3 files changed, 66 insertions(+), 38 deletions(-) diff --git a/Web/static/js/al_wall.js b/Web/static/js/al_wall.js index d3a18471..096a5e79 100644 --- a/Web/static/js/al_wall.js +++ b/Web/static/js/al_wall.js @@ -22,7 +22,7 @@ function trim(string) { return newStr; } -function handleVideoTAreaUpdate(event, id) { +/*function handleVideoTAreaUpdate(event, id) { console.log(event, id); let indicator = u("#post-buttons" + id + " .post-upload"); let file = event.target.files[0]; @@ -34,7 +34,7 @@ function handleVideoTAreaUpdate(event, id) { } document.querySelector("#post-buttons" + id + " #wallAttachmentMenu").classList.add("hidden"); -} +}*/ function initGraffiti(id) { let canvas = null; @@ -43,16 +43,7 @@ function initGraffiti(id) { let fName = "Graffiti-" + Math.ceil(performance.now()).toString() + ".jpeg"; let image = new File([blob], fName, {type: "image/jpeg", lastModified: new Date().getTime()}); - let formdata = new FormData() - formdata.append("hash", u("meta[name=csrf]").attr("value")) - formdata.append("count", 1) - - //ky.post("/photos/upload", {body:}) - let fileSelect = document.querySelector("#post-buttons" + id + " input[name='_pic_attachment']"); - fileSelect.files = trans.files; - - u(fileSelect).trigger("change"); - u("#post-buttons" + id + " #write textarea").trigger("focusin"); + fastUploadImage(id, image) }, "image/jpeg", 0.92); canvas.teardown(); @@ -75,6 +66,61 @@ function initGraffiti(id) { }); } +function fastUploadImage(textareaId, file) { + // uploading images + + let xhr = new XMLHttpRequest + let data = new FormData + + data.append("photo_0", file) + data.append("count", 1) + data.append("hash", u("meta[name=csrf]").attr("value")) + + xhr.open("POST", "/photos/upload") + + xhr.onloadstart = () => { + document.querySelector("#post-buttons"+textareaId+" .upload").insertAdjacentHTML("beforeend", ``) + } + + xhr.onload = () => { + let response = JSON.parse(xhr.responseText) + + appendImage(response, textareaId) + } + + xhr.send(data) +} + +// append image after uploading via /photos/upload +function appendImage(response, textareaId) { + if(!response.success) { + MessageBox(tr("error"), (tr("error_uploading_photo") + response.flash.message), [tr("ok")], [() => {Function.noop}]) + } else { + let form = document.querySelector("#post-buttons"+textareaId) + let photosInput = form.querySelector("input[name='photos']") + let photosIndicator = form.querySelector(".upload") + + for(const phot of response.photos) { + let id = phot.owner + "_" + phot.vid + + photosInput.value += (id + ",") + + u(photosIndicator).append(u(` +
+ × + +
+ `)) + + u(photosIndicator.querySelector(`.upload #aP[data-id='${id}'] .upload-delete`)).on("click", () => { + photosInput.value = photosInput.value.replace(id + ",", "") + u(form.querySelector(`.upload #aP[data-id='${id}']`)).remove() + }) + } + } + u(`#post-buttons${textareaId} .upload #loader`).remove() +} + u(".post-like-button").on("click", function(e) { e.preventDefault(); @@ -96,14 +142,14 @@ u(".post-like-button").on("click", function(e) { }); function setupWallPostInputHandlers(id) { - /* u("#wall-post-input" + id).on("paste", function(e) { + u("#wall-post-input" + id).on("paste", function(e) { + // Вставка из буфера проходит джва раза да хуй его знает почему так, в комментах такого нет. + if(e.clipboardData.files.length === 1) { - var input = u("#post-buttons" + id + " input[name=_pic_attachment]").nodes[0]; - input.files = e.clipboardData.files; - - u(input).trigger("change"); + fastUploadImage(id, e.clipboardData.files[0]) + return; } - }); */ + }); u("#wall-post-input" + id).on("input", function(e) { var boost = 5; @@ -116,26 +162,6 @@ function setupWallPostInputHandlers(id) { // revert to original size if it is larger (possibly changed by user) // textArea.style.height = (newHeight > originalHeight ? (newHeight + boost) : originalHeight) + "px"; }); - - u(`#wall-post-input${id}`).on("paste", function(e) { - if(e.clipboardData.files.length === 1) { - let xhr = new XMLHttpRequest() - let formdat = new FormData() - formdat.append("photo_0", e.clipboardData.files[0]) - formdat.append("count", 1) - formdat.append("hash", u("meta[name=csrf]").attr("value")) - - - xhr.open("POST", "/photos/upload") - - xhr.send(formdat) - console.log(e.clipboardData.files); - } - }); -} - -function removePicture(idA) { - u(`div#aP${idA}`).nodes[0].remove(); } function OpenMiniature(e, photo, post, photo_id, type = "post") { diff --git a/locales/en.strings b/locales/en.strings index b60facc8..f2dd98a4 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -422,6 +422,7 @@ "is_x_photos_other" = "Just $1 photos."; "all_photos" = "All photos"; +"error_uploading_photo" = "Error when uploading photo. Error text: "; /* Notes */ diff --git a/locales/ru.strings b/locales/ru.strings index be34d424..bcf9d357 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -404,6 +404,7 @@ "is_x_photos_other" = "Всего $1 фотографий."; "all_photos" = "Все фотографии"; +"error_uploading_photo" = "Не удалось загрузить фотографию. Текст ошибки: "; /* Notes */