From c47323e358b9841eb287c71e54ae549ea5f6d62d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakobus=20Sch=C3=BCrz?= Date: Tue, 14 Jan 2025 05:49:48 +0100 Subject: [PATCH 1/2] do not change cursor position while replacing upload-placeholder fixes #14695 Save caret position just before replacing the upload-placeholder. replace upload-placeholder Set caret position to the position in textarea as before replacing action. Setting caret position considers the position before or after placeholder and therefore the textlength of server-response to find the right place. --- view/js/dropzone-factory.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/view/js/dropzone-factory.js b/view/js/dropzone-factory.js index dcc0468cb2..5c97394721 100644 --- a/view/js/dropzone-factory.js +++ b/view/js/dropzone-factory.js @@ -35,7 +35,13 @@ var DzFactory = function (max_imagesize) { if (targetTextarea.setRangeText) { //if setRangeText function is supported by current browser let u = "[upload-" + file.name + "]"; - targetTextarea.setRangeText(serverResponse, targetTextarea.value.indexOf(u), targetTextarea.value.indexOf(u) + u.length, "end"); + let c = targetTextarea.selectionStart; + if (c > targetTextarea.value.indexOf(u)) { + c = c + serverResponse.length - u.length; + } + targetTextarea.setRangeText(serverResponse, targetTextarea.value.indexOf(u), targetTextarea.value.indexOf(u) + u.length); + targetTextarea.selectionStart = c; + targetTextarea.selectionEnd = c; } else { targetTextarea.focus(); document.execCommand('insertText', false /*no UI*/, serverResponse); From 6e6903e073e64e44c5462dc612dbc01020abdd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakobus=20Sch=C3=BCrz?= Date: Tue, 14 Jan 2025 11:17:48 +0100 Subject: [PATCH 2/2] add upload for video/*, audio/* and application/* via dropzone Upload videos, audios and documents (all mimetype application/*) via Drag&Drop and Copy&Paste in new postings and comments as attachments. The same as images before with placeholder on insert-position. You can write text while upload is done. --- view/js/dropzone-factory.js | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/view/js/dropzone-factory.js b/view/js/dropzone-factory.js index 5c97394721..33fe796dd3 100644 --- a/view/js/dropzone-factory.js +++ b/view/js/dropzone-factory.js @@ -9,7 +9,7 @@ var DzFactory = function (max_imagesize) { paramName: 'userfile', // The name that will be used to transfer the file maxFilesize: max_imagesize, // MB url: '/media/photo/upload?album=', - acceptedFiles: 'image/*', + acceptedFiles: 'image/*,video/*,audio/*,application/*', clickable: true, dictDefaultMessage: dzStrings.dictDefaultMessage, dictFallbackMessage: dzStrings.dictFallbackMessage, @@ -25,21 +25,45 @@ var DzFactory = function (max_imagesize) { accept: function(file, done) { const targetTextarea = document.getElementById(textareaElementId); if (targetTextarea.setRangeText) { - targetTextarea.setRangeText("\n[upload-" + file.name + "]\n", targetTextarea.selectionStart, targetTextarea.selectionEnd, "end"); + targetTextarea.setRangeText("\n[!upload-" + file.name + "]\n", targetTextarea.selectionStart, targetTextarea.selectionEnd, "end"); } done(); }, init: function() { + this.on("processing", function(file) { + switch(file.type) { + case String(file.type.match(/image\/.*/)): + this.options.url = "/media/photo/upload?album="; + break; + default: + this.options.url = "/media/attachment/upload?response=json"; + } + }); this.on('success', function(file, serverResponse) { const targetTextarea = document.getElementById(textareaElementId); if (targetTextarea.setRangeText) { //if setRangeText function is supported by current browser - let u = "[upload-" + file.name + "]"; + let u = "[!upload-" + file.name + "]"; + let srp = serverResponse; + if (typeof serverResponse === 'object' && + serverResponse.constructor === Object) { + if (serverResponse.ok) { + srp = "[attachment]" + + window.location.protocol + + "//" + + window.location.host + + "/attach/" + + serverResponse.id + + "[/attachment]"; + } else { + srp = "Upload failed"; + } + } let c = targetTextarea.selectionStart; if (c > targetTextarea.value.indexOf(u)) { c = c + serverResponse.length - u.length; } - targetTextarea.setRangeText(serverResponse, targetTextarea.value.indexOf(u), targetTextarea.value.indexOf(u) + u.length); + targetTextarea.setRangeText(srp, targetTextarea.value.indexOf(u), targetTextarea.value.indexOf(u) + u.length); targetTextarea.selectionStart = c; targetTextarea.selectionEnd = c; } else {