Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/frontend/components/textarea/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,49 @@ export const TextareaFieldComponent = class extends HTMLElement {
unorderedListStyle: "-",
});

// Generate Markdown link syntax (without adding `https:`)
// Place cursor within empty brackets for easier URL pasting
document.addEventListener(
"click",
function (event) {
const linkButton = event.target?.closest(".link");
if (linkButton) {
event.preventDefault();
event.stopPropagation();

const linkTemplate = `[${editor.codemirror.getSelection()}]()`;
editor.codemirror.replaceSelection(linkTemplate);
editor.codemirror.focus();

// Move cursor into the bracket for direct pasting
const cursorPosition = editor.codemirror.getCursor();
editor.codemirror.setSelection(
{ line: cursorPosition.line, ch: cursorPosition.ch - 1 },
{ line: cursorPosition.line, ch: cursorPosition.ch - 1 },
);
}
},
{ capture: true },
);

// Auto convert clipboard URL to Markdown link syntax when pasting a URL
// with a text span highlighted
editor.codemirror.on("paste", function (_cm, event) {
const selectedText = editor.codemirror.getSelection();
if (!selectedText) {
return;
}

const pastedText = event.clipboardData?.getData("text/plain") || "";
const urlMatch = pastedText.match(/https?:\/\/[^\s]+/);

if (urlMatch) {
event.preventDefault();
const link = `[${selectedText}](${urlMatch[0]})`;
editor.codemirror.replaceSelection(link);
}
});

// Restore label behaviour
/** @type {HTMLTextAreaElement} */
const $codeMirrorTextarea = this.querySelector(".CodeMirror textarea");
Expand Down