Skip to content

Commit

Permalink
Fix up link insertion after uploads to be saner
Browse files Browse the repository at this point in the history
  • Loading branch information
xPaw committed Jan 30, 2019
1 parent 314a634 commit 8d70f1e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions client/js/socket-events/uploads.js
@@ -1,10 +1,22 @@
"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 append a space if it's not in the beginning
const headToCursor = textbox.value.substr(0, textbox.selectionStart) + (textbox.selectionStart > 0 ? " " : "");

// Get the remaining text after the cursor and append a space
const cursorToTail = " " + textbox.value.substr(textbox.selectionStart);

updateCursor(textbox, headToCursor + fullURL + cursorToTail);

// Set the cursor after the link with a space
// If we also inserted a space before the url, we add extra 2 instead of 1 to the position
textbox.selectionEnd = initStart + fullURL.length + (textbox.selectionStart > 0 ? 2 : 1);
});

0 comments on commit 8d70f1e

Please sign in to comment.