Skip to content

Commit 5550226

Browse files
authored
Improve textarea paste (#31948)
- When pasting a URL over another URL, replace the URL instead of creating a useless `[url](url)`. This is the 1-line change [here](https://github.com/go-gitea/gitea/pull/31948/files#diff-be8e94d7e3da33b187381f53d28095107bd0cf29ae9a9e997e4f422f4a54479cR122). - Always run `initTextareaEvents`, previously it was not run when `dropzoneEl` was not present like when attachements are disabled on the server. Refactored the function to gracefully handle absent `dropzoneEl` and rename the function to a better name.
1 parent 286ede4 commit 5550226

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

web_src/js/features/comp/ComboMarkdownEditor.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import '@github/text-expander-element';
33
import $ from 'jquery';
44
import {attachTribute} from '../tribute.ts';
55
import {hideElem, showElem, autosize, isElemVisible} from '../../utils/dom.ts';
6-
import {initEasyMDEPaste, initTextareaUpload} from './EditorUpload.ts';
6+
import {initEasyMDEPaste, initTextareaEvents} from './EditorUpload.ts';
77
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.ts';
88
import {renderPreviewPanelContent} from '../repo-editor.ts';
99
import {easyMDEToolbarActions} from './EasyMDEToolbarActions.ts';
@@ -110,9 +110,7 @@ class ComboMarkdownEditor {
110110
});
111111

112112
initTextareaMarkdown(this.textarea);
113-
if (this.dropzone) {
114-
initTextareaUpload(this.textarea, this.dropzone);
115-
}
113+
initTextareaEvents(this.textarea, this.dropzone);
116114
}
117115

118116
async setupDropzone() {

web_src/js/features/comp/EditorUpload.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function handleClipboardText(textarea, e, {text, isShiftDown}) {
119119
const {value, selectionStart, selectionEnd} = textarea;
120120
const selectedText = value.substring(selectionStart, selectionEnd);
121121
const trimmedText = text.trim();
122-
if (selectedText && isUrl(trimmedText)) {
122+
if (selectedText && isUrl(trimmedText) && !isUrl(selectedText)) {
123123
e.preventDefault();
124124
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);
125125
}
@@ -156,7 +156,7 @@ export function initEasyMDEPaste(easyMDE, dropzoneEl) {
156156
});
157157
}
158158

159-
export function initTextareaUpload(textarea, dropzoneEl) {
159+
export function initTextareaEvents(textarea, dropzoneEl) {
160160
let isShiftDown = false;
161161
textarea.addEventListener('keydown', (e) => {
162162
if (e.shiftKey) isShiftDown = true;
@@ -166,7 +166,7 @@ export function initTextareaUpload(textarea, dropzoneEl) {
166166
});
167167
textarea.addEventListener('paste', (e) => {
168168
const {images, text} = getPastedContent(e);
169-
if (images.length) {
169+
if (images.length && dropzoneEl) {
170170
handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, images, e);
171171
} else if (text) {
172172
handleClipboardText(textarea, e, {text, isShiftDown});
@@ -176,7 +176,7 @@ export function initTextareaUpload(textarea, dropzoneEl) {
176176
if (!e.dataTransfer.files.length) return;
177177
handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, e.dataTransfer.files, e);
178178
});
179-
dropzoneEl.dropzone.on(DropzoneCustomEventRemovedFile, ({fileUuid}) => {
179+
dropzoneEl?.dropzone.on(DropzoneCustomEventRemovedFile, ({fileUuid}) => {
180180
const newText = removeAttachmentLinksFromMarkdown(textarea.value, fileUuid);
181181
if (textarea.value !== newText) textarea.value = newText;
182182
});

0 commit comments

Comments
 (0)