From 9003768d56113dcb9b28c7421d8933dd3a68c73d Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 30 Jan 2019 19:51:57 +0200 Subject: [PATCH] Fix up link insertion after uploads to be saner --- client/js/socket-events/uploads.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/client/js/socket-events/uploads.js b/client/js/socket-events/uploads.js index 308f1701..c6f5bebf 100644 --- a/client/js/socket-events/uploads.js +++ b/client/js/socket-events/uploads.js @@ -1,10 +1,24 @@ "use strict"; const socket = require("../socket"); -const wrapCursor = require("undate").wrapCursor; +const updateCursor = require("undate").update; socket.on("upload:success", (url) => { - const fullURL = new URL(url, location); + const fullURL = (new URL(url, location)).toString(); const textbox = document.getElementById("input"); - wrapCursor(textbox, fullURL, " "); + const initStart = textbox.selectionStart; + + // Get the text before the cursor, and add a space if it's not in the beginning + const headToCursor = initStart > 0 ? (textbox.value.substr(0, initStart) + " ") : ""; + + // Get the remaining text after the cursor + const cursorToTail = textbox.value.substr(initStart); + + // Construct the value until the point where we want the cursor to be + const textBeforeTail = headToCursor + fullURL + " "; + + updateCursor(textbox, textBeforeTail + cursorToTail); + + // Set the cursor after the link and a space + textbox.selectionStart = textbox.selectionEnd = textBeforeTail.length; });