Skip to content

Commit 5a61c9b

Browse files
Signior-Xtimabbott
authored andcommitted
compose: Convert pasted url to named link.
This commit changes the pastehandler to have a case when a url is tried to paste on composebox. It instead of directly pasting it, changes the content as we do using ctrl + shift + L, just the difference is here it uses, the copied text instead of url and cursor moves forward. Fixes zulip#18692
1 parent 3bdc743 commit 5a61c9b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

static/js/copy_and_paste.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,19 @@ export function paste_handler_converter(paste_html) {
306306
return markdown_text;
307307
}
308308

309+
// https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
310+
function isValidHttpUrl(string) {
311+
let url;
312+
313+
try {
314+
url = new URL(string);
315+
} catch {
316+
return false;
317+
}
318+
319+
return url.protocol === "http:" || url.protocol === "https:";
320+
}
321+
309322
export function paste_handler(event) {
310323
const clipboardData = event.originalEvent.clipboardData;
311324
if (!clipboardData) {
@@ -318,6 +331,20 @@ export function paste_handler(event) {
318331
}
319332

320333
if (clipboardData.getData) {
334+
const paste_text = clipboardData.getData("text");
335+
if (paste_text && isValidHttpUrl(paste_text) && document.getSelection().type === "Range") {
336+
event.preventDefault();
337+
event.stopPropagation();
338+
339+
const textarea = $(this);
340+
const prefix = "[";
341+
const suffix = "](" + paste_text + ")";
342+
compose_ui.wrap_text_with_markdown(textarea, prefix, suffix);
343+
compose_ui.autosize_textarea(textarea);
344+
345+
return;
346+
}
347+
321348
const paste_html = clipboardData.getData("text/html");
322349
if (paste_html && page_params.development_environment) {
323350
const text = paste_handler_converter(paste_html);

0 commit comments

Comments
 (0)