Skip to content

Commit 9d723a8

Browse files
committed
make it testable and add tests
1 parent ff65057 commit 9d723a8

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

web_src/js/features/comp/EditorUpload.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {removeAttachmentLinksFromMarkdown} from './EditorUpload.ts';
1+
import {pasteAsMarkdownLink, removeAttachmentLinksFromMarkdown} from './EditorUpload.ts';
22

33
test('removeAttachmentLinksFromMarkdown', () => {
44
expect(removeAttachmentLinksFromMarkdown('a foo b', 'foo')).toBe('a foo b');
@@ -12,3 +12,12 @@ test('removeAttachmentLinksFromMarkdown', () => {
1212
expect(removeAttachmentLinksFromMarkdown('a <img src="/attachments/foo"> b', 'foo')).toBe('a b');
1313
expect(removeAttachmentLinksFromMarkdown('a <img src="/attachments/foo" width="100"/> b', 'foo')).toBe('a b');
1414
});
15+
16+
test('preparePasteAsMarkdownLink', () => {
17+
expect(pasteAsMarkdownLink({value: 'foo', selectionStart: 0, selectionEnd: 0}, 'bar')).toBeNull();
18+
expect(pasteAsMarkdownLink({value: 'foo', selectionStart: 0, selectionEnd: 0}, 'https://gitea.com')).toBeNull();
19+
expect(pasteAsMarkdownLink({value: 'foo', selectionStart: 0, selectionEnd: 3}, 'bar')).toBeNull();
20+
expect(pasteAsMarkdownLink({value: 'foo', selectionStart: 0, selectionEnd: 3}, 'https://gitea.com')).toBe('[foo](https://gitea.com)');
21+
expect(pasteAsMarkdownLink({value: '(x)', selectionStart: 0, selectionEnd: 3}, 'https://gitea.com')).toBe('[(x)](https://gitea.com)');
22+
expect(pasteAsMarkdownLink({value: '[](url)', selectionStart: 3, selectionEnd: 6}, 'https://gitea.com')).toBeNull();
23+
});

web_src/js/features/comp/EditorUpload.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,26 @@ export function removeAttachmentLinksFromMarkdown(text: string, fileUuid: string
118118
return text;
119119
}
120120

121-
function handleClipboardText(textarea: HTMLTextAreaElement, e: ClipboardEvent, text: string, isShiftDown: boolean) {
122-
// pasting with "shift" means "paste as original content" in most applications
123-
if (isShiftDown) return; // let the browser handle it
124-
125-
// when pasting links over selected text, turn it into [text](link)
121+
export function pasteAsMarkdownLink(textarea: {value: string, selectionStart: number, selectionEnd: number}, pastedText: string): string | null {
126122
const {value, selectionStart, selectionEnd} = textarea;
127123
const selectedText = value.substring(selectionStart, selectionEnd);
128-
const trimmedText = text.trim();
124+
const trimmedText = pastedText.trim();
129125
const beforeSelection = value.substring(0, selectionStart);
130126
const afterSelection = value.substring(selectionEnd);
131127
const isInMarkdownLink = beforeSelection.endsWith('](') && afterSelection.startsWith(')');
132-
if (selectedText && isUrl(trimmedText) && !isUrl(selectedText) && !isInMarkdownLink) {
128+
const asMarkdownLink = selectedText && isUrl(trimmedText) && !isUrl(selectedText) && !isInMarkdownLink;
129+
return asMarkdownLink ? `[${selectedText}](${trimmedText})` : null;
130+
}
131+
132+
function handleClipboardText(textarea: HTMLTextAreaElement, e: ClipboardEvent, pastedText: string, isShiftDown: boolean) {
133+
// pasting with "shift" means "paste as original content" in most applications
134+
if (isShiftDown) return; // let the browser handle it
135+
136+
// when pasting links over selected text, turn it into [text](link)
137+
const pastedAsMarkdown = pasteAsMarkdownLink(textarea, pastedText);
138+
if (pastedText) {
133139
e.preventDefault();
134-
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);
140+
replaceTextareaSelection(textarea, pastedAsMarkdown);
135141
}
136142
// else, let the browser handle it
137143
}

0 commit comments

Comments
 (0)