mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: milkdown: paste image
This commit is contained in:
1 parent
f580263844
commit
78814f05da
3 files changed
+20
-18
No files matched your search
@@ -69,17 +69,15 @@ const pagename = document.documentElement.getAttribute('data-page');
|
||||
const isProblemPage = ['problem_create', 'problem_edit'].includes(pagename);
|
||||
const isProblemEdit = pagename === 'problem_edit';
|
||||
function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
||||
window.addEventListener('paste', (ev: ClipboardEvent) => {
|
||||
if (!editor.hasTextFocus()) return;
|
||||
function handlePaste(file: DataTransferItem | File) {
|
||||
const selection = editor.getSelection();
|
||||
const { items } = ev.clipboardData;
|
||||
let wrapper = ['', ''];
|
||||
let ext;
|
||||
const matches = items[0].type.match(/^image\/(png|jpg|jpeg|gif)$/i);
|
||||
const matches = file.type.match(/^image\/(png|jpg|jpeg|gif)$/i);
|
||||
if (matches) {
|
||||
wrapper = [''];
|
||||
[, ext] = matches;
|
||||
} else if (items[0].type === 'application/x-zip-compressed') {
|
||||
} else if (file.type === 'application/x-zip-compressed') {
|
||||
wrapper = ['[file](', ')'];
|
||||
ext = 'zip';
|
||||
}
|
||||
@@ -87,7 +85,7 @@ function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
||||
const filename = `${nanoid()}.${ext}`;
|
||||
const data = new FormData();
|
||||
data.append('filename', filename);
|
||||
data.append('file', items[0].getAsFile());
|
||||
data.append('file', ('getAsFile' in file) ? file.getAsFile() : file);
|
||||
data.append('operation', 'upload_file');
|
||||
if (isProblemEdit) data.append('type', 'additional_file');
|
||||
let range: monaco.Range = null;
|
||||
@@ -128,6 +126,11 @@ function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
||||
console.error(e);
|
||||
updateText(`${i18n('Upload Failed')}: ${e.message}`);
|
||||
});
|
||||
}
|
||||
window.addEventListener('paste', (ev: ClipboardEvent) => {
|
||||
if (!editor.hasTextFocus()) return;
|
||||
const { items } = ev.clipboardData;
|
||||
handlePaste(items[0]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user