From c9791cf24f71716ba65fbacae6106beb83f33d66 Mon Sep 17 00:00:00 2001 From: Sinharitik589 Date: Wed, 6 Apr 2022 14:07:29 +0530 Subject: [PATCH 1/3] Converting selected text to MD link when pasting a URL --- src/components/views/rooms/BasicMessageComposer.tsx | 11 +++++++++-- src/editor/operations.ts | 7 +++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/components/views/rooms/BasicMessageComposer.tsx b/src/components/views/rooms/BasicMessageComposer.tsx index fe5669f8c49..82e7319dbd5 100644 --- a/src/components/views/rooms/BasicMessageComposer.tsx +++ b/src/components/views/rooms/BasicMessageComposer.tsx @@ -24,7 +24,8 @@ import { logger } from "matrix-js-sdk/src/logger"; import EditorModel from '../../../editor/model'; import HistoryManager from '../../../editor/history'; import { Caret, setSelection } from '../../../editor/caret'; -import { formatRange, replaceRangeAndMoveCaret, toggleInlineFormat } from '../../../editor/operations'; +import { formatRange, formatRangeAsLink, replaceRangeAndMoveCaret, toggleInlineFormat } + from '../../../editor/operations'; import { getCaretOffsetAndText, getRangeForSelection } from '../../../editor/dom'; import Autocomplete, { generateCompletionDomId } from '../rooms/Autocomplete'; import { getAutoCompleteCreator, Type } from '../../../editor/parts'; @@ -46,6 +47,7 @@ import { getKeyBindingsManager } from '../../../KeyBindingsManager'; import { replaceableComponent } from "../../../utils/replaceableComponent"; import { ALTERNATE_KEY_NAME, KeyBindingAction } from '../../../accessibility/KeyboardShortcuts'; import { _t } from "../../../languageHandler"; +import { linkify } from '../../../linkify-matrix'; // matches emoticons which follow the start of a line or whitespace const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s|:^$'); @@ -347,9 +349,14 @@ export default class BasicMessageEditor extends React.Component const text = event.clipboardData.getData("text/plain"); parts = parsePlainTextMessage(text, partCreator, { shouldEscape: false }); } + const textToInsert = event.clipboardData.getData("text/plain"); this.modifiedFlag = true; const range = getRangeForSelection(this.editorRef.current, model, document.getSelection()); - replaceRangeAndMoveCaret(range, parts); + if (textToInsert && linkify.test(textToInsert)) { + formatRangeAsLink(range, textToInsert); + } else { + replaceRangeAndMoveCaret(range, parts); + } }; private onInput = (event: Partial): void => { diff --git a/src/editor/operations.ts b/src/editor/operations.ts index f8d4a7b2c4c..67c1cf57ac3 100644 --- a/src/editor/operations.ts +++ b/src/editor/operations.ts @@ -212,7 +212,7 @@ export function formatRangeAsCode(range: Range): void { replaceRangeAndExpandSelection(range, parts); } -export function formatRangeAsLink(range: Range) { +export function formatRangeAsLink(range: Range, text?: string) { const { model } = range; const { partCreator } = model; const linkRegex = /\[(.*?)\]\(.*?\)/g; @@ -225,7 +225,10 @@ export function formatRangeAsLink(range: Range) { replaceRangeAndAutoAdjustCaret(range, newParts, true, prefixLength, suffixLength); } else { // We set offset to -1 here so that the caret lands between the brackets - replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "()")], -1); + replaceRangeAndMoveCaret(range, + text?[partCreator.plain("[" + range.text + "]" + "(" + text + ")")]: + [partCreator.plain("[" + range.text + "]" + "()")], + -1); } } From ed013f438d1a6ec87ab34be953658888123844ba Mon Sep 17 00:00:00 2001 From: Sinharitik589 <67551927+Sinharitik589@users.noreply.github.com> Date: Fri, 8 Apr 2022 18:33:26 +0530 Subject: [PATCH 2/3] Update src/editor/operations.ts Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --- src/editor/operations.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/editor/operations.ts b/src/editor/operations.ts index 67c1cf57ac3..6812e5c9af5 100644 --- a/src/editor/operations.ts +++ b/src/editor/operations.ts @@ -225,10 +225,7 @@ export function formatRangeAsLink(range: Range, text?: string) { replaceRangeAndAutoAdjustCaret(range, newParts, true, prefixLength, suffixLength); } else { // We set offset to -1 here so that the caret lands between the brackets - replaceRangeAndMoveCaret(range, - text?[partCreator.plain("[" + range.text + "]" + "(" + text + ")")]: - [partCreator.plain("[" + range.text + "]" + "()")], - -1); + replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "(" + (text ?? "") + ")")],-1); } } From 7750b6a12d92de554c345fb76cae4ac0f1bb7625 Mon Sep 17 00:00:00 2001 From: Sinharitik589 Date: Fri, 8 Apr 2022 19:18:52 +0530 Subject: [PATCH 3/3] Converting selected text to MD link when pasting a URL --- src/editor/operations.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/editor/operations.ts b/src/editor/operations.ts index 6812e5c9af5..c545b044dda 100644 --- a/src/editor/operations.ts +++ b/src/editor/operations.ts @@ -225,7 +225,7 @@ export function formatRangeAsLink(range: Range, text?: string) { replaceRangeAndAutoAdjustCaret(range, newParts, true, prefixLength, suffixLength); } else { // We set offset to -1 here so that the caret lands between the brackets - replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "(" + (text ?? "") + ")")],-1); + replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "(" + (text ?? "") + ")")], -1); } }