Skip to content

Commit 8d45f2e

Browse files
committed
fix
1 parent bb995be commit 8d45f2e

File tree

6 files changed

+43
-39
lines changed

6 files changed

+43
-39
lines changed

web_src/js/features/comp/ComboMarkdownEditor.js

Lines changed: 6 additions & 7 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.js';
55
import {hideElem, showElem, autosize, isElemVisible} from '../../utils/dom.js';
6-
import {initEasyMDEPaste, initTextareaPaste} from './Paste.js';
6+
import {initEasyMDEPaste, initTextareaPaste} from './EditorUpload.js';
77
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.js';
88
import {renderPreviewPanelContent} from '../repo-editor.js';
99
import {easyMDEToolbarActions} from './EasyMDEToolbarActions.js';
@@ -291,6 +291,11 @@ class ComboMarkdownEditor {
291291
}
292292
}
293293

294+
export function getComboMarkdownEditor(el) {
295+
if (el instanceof $) el = el[0];
296+
return el?._giteaComboMarkdownEditor;
297+
}
298+
294299
export async function initComboMarkdownEditor(container, options = {}) {
295300
if (container instanceof $) {
296301
if (container.length !== 1) {
@@ -305,9 +310,3 @@ export async function initComboMarkdownEditor(container, options = {}) {
305310
await editor.init();
306311
return editor;
307312
}
308-
309-
export function removeLinksInTextarea(editor, file) {
310-
const fileName = file.name.slice(0, file.name.lastIndexOf('.'));
311-
const fileText = `\\[${fileName}\\]\\(/attachments/${file.uuid}\\)`;
312-
editor.value(editor.value().replace(new RegExp(`<img [\\s\\w"=]+ alt="${fileName}" src="/attachments/${file.uuid}">`, 'g'), '').replace(new RegExp(`\\!${fileText}`, 'g'), '').replace(new RegExp(fileText, 'g'), ''));
313-
}

web_src/js/features/comp/EditorMarkdown.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import {triggerEditorContentChanged} from './Paste.js';
1+
export function triggerEditorContentChanged(target) {
2+
target.dispatchEvent(new CustomEvent('ce-editor-content-changed', {bubbles: true}));
3+
}
24

35
function handleIndentSelection(textarea, e) {
46
const selStart = textarea.selectionStart;

web_src/js/features/comp/Paste.js renamed to web_src/js/features/comp/EditorUpload.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import {htmlEscape} from 'escape-goat';
22
import {POST} from '../../modules/fetch.js';
33
import {imageInfo} from '../../utils/image.js';
4-
import {getPastedContent, replaceTextareaSelection} from '../../utils/dom.js';
4+
import {replaceTextareaSelection} from '../../utils/dom.js';
55
import {isUrl} from '../../utils/url.js';
6+
import {extname} from '../../utils.js';
7+
import {triggerEditorContentChanged} from './EditorMarkdown.js';
8+
import {getComboMarkdownEditor} from './ComboMarkdownEditor.js';
69

710
async function uploadFile(file, uploadUrl) {
811
const formData = new FormData();
@@ -12,10 +15,6 @@ async function uploadFile(file, uploadUrl) {
1215
return await res.json();
1316
}
1417

15-
export function triggerEditorContentChanged(target) {
16-
target.dispatchEvent(new CustomEvent('ce-editor-content-changed', {bubbles: true}));
17-
}
18-
1918
class TextareaEditor {
2019
constructor(editor) {
2120
this.editor = editor;
@@ -82,23 +81,21 @@ class CodeMirrorEditor {
8281
}
8382
}
8483

84+
// FIXME: handle non-image files
8585
async function handleClipboardImages(editor, dropzone, images, e) {
8686
const uploadUrl = dropzone.getAttribute('data-upload-url');
8787
const filesContainer = dropzone.querySelector('.files');
8888

89-
if (!dropzone || !uploadUrl || !filesContainer || !images.length) return;
90-
9189
e.preventDefault();
9290
e.stopPropagation();
9391

9492
for (const img of images) {
9593
const name = img.name.slice(0, img.name.lastIndexOf('.'));
96-
94+
const {width, dppx} = await imageInfo(img);
9795
const placeholder = `![${name}](uploading ...)`;
98-
editor.insertPlaceholder(placeholder);
9996

97+
editor.insertPlaceholder(placeholder);
10098
const {uuid} = await uploadFile(img, uploadUrl);
101-
const {width, dppx} = await imageInfo(img);
10299

103100
let text;
104101
if (width > 0 && dppx > 1) {
@@ -139,6 +136,18 @@ function handleClipboardText(textarea, e, {text, isShiftDown}) {
139136
// else, let the browser handle it
140137
}
141138

139+
// extract text and images from "paste" event
140+
function getPastedContent(e) {
141+
const images = [];
142+
for (const item of e.clipboardData?.items ?? []) {
143+
if (item.type?.startsWith('image/')) {
144+
images.push(item.getAsFile());
145+
}
146+
}
147+
const text = e.clipboardData?.getData?.('text') ?? '';
148+
return {text, images};
149+
}
150+
142151
export function initEasyMDEPaste(easyMDE, dropzone) {
143152
easyMDE.codemirror.on('paste', (_, e) => {
144153
const {images} = getPastedContent(e);
@@ -164,4 +173,17 @@ export function initTextareaPaste(textarea, dropzone) {
164173
handleClipboardText(textarea, e, {text, isShiftDown});
165174
}
166175
});
176+
textarea.addEventListener('drop', (e) => {
177+
const acceptedFiles = getComboMarkdownEditor(textarea).dropzone.getAttribute('data-accepts');
178+
const files = [];
179+
for (const item of e.dataTransfer?.items ?? []) {
180+
if (item?.kind !== 'file') continue;
181+
const file = item.getAsFile();
182+
if (acceptedFiles.includes(extname(file.name))) {
183+
files.push(file);
184+
}
185+
}
186+
// FIXME: handle upload files
187+
handleClipboardImages(new TextareaEditor(textarea), dropzone, files, e);
188+
});
167189
}

web_src/js/features/dropzone.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function initDropzone(el) {
7070
data: new URLSearchParams({file: file.uuid}),
7171
});
7272
}
73+
// TODO: remove the link from editor and maybe merge the duplicate code
7374
});
7475
this.on('error', function (file, message) {
7576
showErrorToast(message);

web_src/js/features/repo-issue-edit.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ async function onEditContent(event) {
6464
console.error(error);
6565
}
6666
}
67+
// TODO: remove the link from editor and maybe merge the duplicate code
6768
});
6869
this.on('submit', () => {
6970
for (const fileUuid of Object.keys(fileUuidDict)) {

web_src/js/utils/dom.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,27 +263,6 @@ export function isElemVisible(element) {
263263
return Boolean(element.offsetWidth || element.offsetHeight || element.getClientRects().length);
264264
}
265265

266-
export function getComboMarkdownEditor(el) {
267-
return el?._giteaComboMarkdownEditor;
268-
}
269-
270-
// extract text and images from "paste" event
271-
export function getPastedContent(e) {
272-
const acceptedFiles = getComboMarkdownEditor(e.currentTarget).dropzone.getAttribute('data-accepts');
273-
const files = [];
274-
const data = e.clipboardData?.items || e.dataTransfer?.items;
275-
for (const item of data ?? []) {
276-
if (item?.kind === 'file') {
277-
const file = item.getAsFile();
278-
if (acceptedFiles.includes(extname(file.name))) {
279-
files.push(file);
280-
}
281-
}
282-
}
283-
const text = e.clipboardData?.getData?.('text') ?? '';
284-
return {text, files};
285-
}
286-
287266
// replace selected text in a textarea while preserving editor history, e.g. CTRL-Z works after this
288267
export function replaceTextareaSelection(textarea, text) {
289268
const before = textarea.value.slice(0, textarea.selectionStart ?? undefined);

0 commit comments

Comments
 (0)