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
26 changes: 26 additions & 0 deletions js/text-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ Bugzilla.TextEditor = class TextEditor {
this.textareaOnInput(event);
});

this.$textarea.addEventListener('paste', (/** @type {ClipboardEvent} */ event) => {
this.textareaOnPaste(event);
});

this.togglePreviewTab();

if (this.useMarkdown) {
Expand Down Expand Up @@ -284,6 +288,28 @@ Bugzilla.TextEditor = class TextEditor {
this.togglePreviewTab();
}

/**
* Called whenever content is pasted to the `<textarea>`. If the pasted content is a URL and there
* is a selection, insert a Markdown link with the URL and the selected text as the label.
* @param {ClipboardEvent} event `paste` event.
*/
textareaOnPaste(event) {
const data = event.clipboardData?.getData('text');

if (data.match(/^https?:\/\//) && URL.canParse(data)) {
const { start, end, beforeText, selectedText, afterText } = this.getSelection();

if (selectedText) {
event.preventDefault();

this.updateText(`${beforeText}[${selectedText}](${data})${afterText}`, {
start: start + 1,
end: end + 1,
});
}
}
}

/**
* Get information related to the selected text in the `<textarea>`.
* @returns {{ start: number, end: number, beforeText: string, selectedText: string, afterText:
Expand Down