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 #18692
This commit is contained in:
Signior-X 2021-06-15 15:37:29 +05:30 committed by Tim Abbott
parent 3bdc7432b9
commit 5a61c9bb14
1 changed files with 27 additions and 0 deletions

View File

@ -306,6 +306,19 @@ export function paste_handler_converter(paste_html) {
return markdown_text; return markdown_text;
} }
// https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
function isValidHttpUrl(string) {
let url;
try {
url = new URL(string);
} catch {
return false;
}
return url.protocol === "http:" || url.protocol === "https:";
}
export function paste_handler(event) { export function paste_handler(event) {
const clipboardData = event.originalEvent.clipboardData; const clipboardData = event.originalEvent.clipboardData;
if (!clipboardData) { if (!clipboardData) {
@ -318,6 +331,20 @@ export function paste_handler(event) {
} }
if (clipboardData.getData) { if (clipboardData.getData) {
const paste_text = clipboardData.getData("text");
if (paste_text && isValidHttpUrl(paste_text) && document.getSelection().type === "Range") {
event.preventDefault();
event.stopPropagation();
const textarea = $(this);
const prefix = "[";
const suffix = "](" + paste_text + ")";
compose_ui.wrap_text_with_markdown(textarea, prefix, suffix);
compose_ui.autosize_textarea(textarea);
return;
}
const paste_html = clipboardData.getData("text/html"); const paste_html = clipboardData.getData("text/html");
if (paste_html && page_params.development_environment) { if (paste_html && page_params.development_environment) {
const text = paste_handler_converter(paste_html); const text = paste_handler_converter(paste_html);