1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-12-04 14:46:57 -05:00

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.
This commit is contained in:
silverwind 2024-09-01 17:15:29 +02:00 committed by GitHub
parent 286ede47ad
commit 5550226571
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 8 deletions

View File

@ -3,7 +3,7 @@ import '@github/text-expander-element';
import $ from 'jquery'; import $ from 'jquery';
import {attachTribute} from '../tribute.ts'; import {attachTribute} from '../tribute.ts';
import {hideElem, showElem, autosize, isElemVisible} from '../../utils/dom.ts'; import {hideElem, showElem, autosize, isElemVisible} from '../../utils/dom.ts';
import {initEasyMDEPaste, initTextareaUpload} from './EditorUpload.ts'; import {initEasyMDEPaste, initTextareaEvents} from './EditorUpload.ts';
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.ts'; import {handleGlobalEnterQuickSubmit} from './QuickSubmit.ts';
import {renderPreviewPanelContent} from '../repo-editor.ts'; import {renderPreviewPanelContent} from '../repo-editor.ts';
import {easyMDEToolbarActions} from './EasyMDEToolbarActions.ts'; import {easyMDEToolbarActions} from './EasyMDEToolbarActions.ts';
@ -110,9 +110,7 @@ class ComboMarkdownEditor {
}); });
initTextareaMarkdown(this.textarea); initTextareaMarkdown(this.textarea);
if (this.dropzone) { initTextareaEvents(this.textarea, this.dropzone);
initTextareaUpload(this.textarea, this.dropzone);
}
} }
async setupDropzone() { async setupDropzone() {

View File

@ -119,7 +119,7 @@ function handleClipboardText(textarea, e, {text, isShiftDown}) {
const {value, selectionStart, selectionEnd} = textarea; const {value, selectionStart, selectionEnd} = textarea;
const selectedText = value.substring(selectionStart, selectionEnd); const selectedText = value.substring(selectionStart, selectionEnd);
const trimmedText = text.trim(); const trimmedText = text.trim();
if (selectedText && isUrl(trimmedText)) { if (selectedText && isUrl(trimmedText) && !isUrl(selectedText)) {
e.preventDefault(); e.preventDefault();
replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`); replaceTextareaSelection(textarea, `[${selectedText}](${trimmedText})`);
} }
@ -156,7 +156,7 @@ export function initEasyMDEPaste(easyMDE, dropzoneEl) {
}); });
} }
export function initTextareaUpload(textarea, dropzoneEl) { export function initTextareaEvents(textarea, dropzoneEl) {
let isShiftDown = false; let isShiftDown = false;
textarea.addEventListener('keydown', (e) => { textarea.addEventListener('keydown', (e) => {
if (e.shiftKey) isShiftDown = true; if (e.shiftKey) isShiftDown = true;
@ -166,7 +166,7 @@ export function initTextareaUpload(textarea, dropzoneEl) {
}); });
textarea.addEventListener('paste', (e) => { textarea.addEventListener('paste', (e) => {
const {images, text} = getPastedContent(e); const {images, text} = getPastedContent(e);
if (images.length) { if (images.length && dropzoneEl) {
handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, images, e); handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, images, e);
} else if (text) { } else if (text) {
handleClipboardText(textarea, e, {text, isShiftDown}); handleClipboardText(textarea, e, {text, isShiftDown});
@ -176,7 +176,7 @@ export function initTextareaUpload(textarea, dropzoneEl) {
if (!e.dataTransfer.files.length) return; if (!e.dataTransfer.files.length) return;
handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, e.dataTransfer.files, e); handleUploadFiles(new TextareaEditor(textarea), dropzoneEl, e.dataTransfer.files, e);
}); });
dropzoneEl.dropzone.on(DropzoneCustomEventRemovedFile, ({fileUuid}) => { dropzoneEl?.dropzone.on(DropzoneCustomEventRemovedFile, ({fileUuid}) => {
const newText = removeAttachmentLinksFromMarkdown(textarea.value, fileUuid); const newText = removeAttachmentLinksFromMarkdown(textarea.value, fileUuid);
if (textarea.value !== newText) textarea.value = newText; if (textarea.value !== newText) textarea.value = newText;
}); });