Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit dfc7224

Browse files
Converting selected text to MD link when pasting a URL (#8242)
* Converting selected text to MD link when pasting a URL * Update src/editor/operations.ts Co-authored-by: Michael Telatynski <[email protected]> * Converting selected text to MD link when pasting a URL Co-authored-by: Michael Telatynski <[email protected]>
1 parent e980c14 commit dfc7224

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/components/views/rooms/BasicMessageComposer.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import { logger } from "matrix-js-sdk/src/logger";
2424
import EditorModel from '../../../editor/model';
2525
import HistoryManager from '../../../editor/history';
2626
import { Caret, setSelection } from '../../../editor/caret';
27-
import { formatRange, replaceRangeAndMoveCaret, toggleInlineFormat } from '../../../editor/operations';
27+
import { formatRange, formatRangeAsLink, replaceRangeAndMoveCaret, toggleInlineFormat }
28+
from '../../../editor/operations';
2829
import { getCaretOffsetAndText, getRangeForSelection } from '../../../editor/dom';
2930
import Autocomplete, { generateCompletionDomId } from '../rooms/Autocomplete';
3031
import { getAutoCompleteCreator, Type } from '../../../editor/parts';
@@ -45,6 +46,7 @@ import { ICompletion } from "../../../autocomplete/Autocompleter";
4546
import { getKeyBindingsManager } from '../../../KeyBindingsManager';
4647
import { ALTERNATE_KEY_NAME, KeyBindingAction } from '../../../accessibility/KeyboardShortcuts';
4748
import { _t } from "../../../languageHandler";
49+
import { linkify } from '../../../linkify-matrix';
4850

4951
// matches emoticons which follow the start of a line or whitespace
5052
const REGEX_EMOTICON_WHITESPACE = new RegExp('(?:^|\\s)(' + EMOTICON_REGEX.source + ')\\s|:^$');
@@ -348,9 +350,14 @@ export default class BasicMessageEditor extends React.Component<IProps, IState>
348350
const text = event.clipboardData.getData("text/plain");
349351
parts = parsePlainTextMessage(text, partCreator, { shouldEscape: false });
350352
}
353+
const textToInsert = event.clipboardData.getData("text/plain");
351354
this.modifiedFlag = true;
352355
const range = getRangeForSelection(this.editorRef.current, model, document.getSelection());
353-
replaceRangeAndMoveCaret(range, parts);
356+
if (textToInsert && linkify.test(textToInsert)) {
357+
formatRangeAsLink(range, textToInsert);
358+
} else {
359+
replaceRangeAndMoveCaret(range, parts);
360+
}
354361
};
355362

356363
private onInput = (event: Partial<InputEvent>): void => {

src/editor/operations.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export function formatRangeAsCode(range: Range): void {
216216
replaceRangeAndExpandSelection(range, parts);
217217
}
218218

219-
export function formatRangeAsLink(range: Range) {
219+
export function formatRangeAsLink(range: Range, text?: string) {
220220
const { model } = range;
221221
const { partCreator } = model;
222222
const linkRegex = /\[(.*?)\]\(.*?\)/g;
@@ -229,7 +229,7 @@ export function formatRangeAsLink(range: Range) {
229229
replaceRangeAndAutoAdjustCaret(range, newParts, true, prefixLength, suffixLength);
230230
} else {
231231
// We set offset to -1 here so that the caret lands between the brackets
232-
replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "()")], -1);
232+
replaceRangeAndMoveCaret(range, [partCreator.plain("[" + range.text + "]" + "(" + (text ?? "") + ")")], -1);
233233
}
234234
}
235235

0 commit comments

Comments
 (0)