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
@@ -1,11 +1,8 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import Notification from 'vj/components/notification';
|
|
||||||
import DOMAttachedObject from 'vj/components/DOMAttachedObject';
|
import DOMAttachedObject from 'vj/components/DOMAttachedObject';
|
||||||
import type { Node } from 'prosemirror-model';
|
import type { Node } from 'prosemirror-model';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import i18n from 'vj/utils/i18n';
|
|
||||||
import request from 'vj/utils/request';
|
import request from 'vj/utils/request';
|
||||||
import tpl from 'vj/utils/tpl';
|
|
||||||
|
|
||||||
interface MonacoOptions {
|
interface MonacoOptions {
|
||||||
language?: string;
|
language?: string;
|
||||||
@@ -183,14 +180,16 @@ export default class VjEditor extends DOMAttachedObject {
|
|||||||
}
|
}
|
||||||
const nodes: Node[] = await Promise.all(
|
const nodes: Node[] = await Promise.all(
|
||||||
images.map(async (image) => {
|
images.map(async (image) => {
|
||||||
console.log(image);
|
const filename = `${nanoid()}.${image.name.split('.').pop()}`;
|
||||||
// TODO
|
const data = new FormData();
|
||||||
return null;
|
data.append('filename', filename);
|
||||||
const src = await YourUploadAPI(image);
|
data.append('file', image);
|
||||||
const alt = image.name;
|
data.append('operation', 'upload_file');
|
||||||
|
if (isProblemEdit) data.append('type', 'additional_file');
|
||||||
|
await request.postFile(isProblemEdit ? './files' : '/file', data);
|
||||||
return schema.nodes.image.createAndFill({
|
return schema.nodes.image.createAndFill({
|
||||||
src,
|
src: `${isProblemPage ? 'file://' : `/file/${UserContext._id}/`}${filename}`,
|
||||||
alt,
|
alt: filename,
|
||||||
}) as Node;
|
}) as Node;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
@@ -217,7 +216,7 @@ export default class VjEditor extends DOMAttachedObject {
|
|||||||
this.options?.onChange?.(markdown);
|
this.options?.onChange?.(markdown);
|
||||||
this.lock = false;
|
this.lock = false;
|
||||||
});
|
});
|
||||||
ctx.get(listenerCtx).updated((c) => {
|
ctx.get(listenerCtx).updated(() => {
|
||||||
this.height = $(ele).height();
|
this.height = $(ele).height();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -69,17 +69,15 @@ const pagename = document.documentElement.getAttribute('data-page');
|
|||||||
const isProblemPage = ['problem_create', 'problem_edit'].includes(pagename);
|
const isProblemPage = ['problem_create', 'problem_edit'].includes(pagename);
|
||||||
const isProblemEdit = pagename === 'problem_edit';
|
const isProblemEdit = pagename === 'problem_edit';
|
||||||
function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
||||||
window.addEventListener('paste', (ev: ClipboardEvent) => {
|
function handlePaste(file: DataTransferItem | File) {
|
||||||
if (!editor.hasTextFocus()) return;
|
|
||||||
const selection = editor.getSelection();
|
const selection = editor.getSelection();
|
||||||
const { items } = ev.clipboardData;
|
|
||||||
let wrapper = ['', ''];
|
let wrapper = ['', ''];
|
||||||
let ext;
|
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) {
|
if (matches) {
|
||||||
wrapper = [''];
|
wrapper = [''];
|
||||||
[, ext] = matches;
|
[, ext] = matches;
|
||||||
} else if (items[0].type === 'application/x-zip-compressed') {
|
} else if (file.type === 'application/x-zip-compressed') {
|
||||||
wrapper = ['[file](', ')'];
|
wrapper = ['[file](', ')'];
|
||||||
ext = 'zip';
|
ext = 'zip';
|
||||||
}
|
}
|
||||||
@@ -87,7 +85,7 @@ function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
|||||||
const filename = `${nanoid()}.${ext}`;
|
const filename = `${nanoid()}.${ext}`;
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
data.append('filename', filename);
|
data.append('filename', filename);
|
||||||
data.append('file', items[0].getAsFile());
|
data.append('file', ('getAsFile' in file) ? file.getAsFile() : file);
|
||||||
data.append('operation', 'upload_file');
|
data.append('operation', 'upload_file');
|
||||||
if (isProblemEdit) data.append('type', 'additional_file');
|
if (isProblemEdit) data.append('type', 'additional_file');
|
||||||
let range: monaco.Range = null;
|
let range: monaco.Range = null;
|
||||||
@@ -128,6 +126,11 @@ function handlePasteEvent(editor: monaco.editor.IStandaloneCodeEditor) {
|
|||||||
console.error(e);
|
console.error(e);
|
||||||
updateText(`${i18n('Upload Failed')}: ${e.message}`);
|
updateText(`${i18n('Upload Failed')}: ${e.message}`);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
window.addEventListener('paste', (ev: ClipboardEvent) => {
|
||||||
|
if (!editor.hasTextFocus()) return;
|
||||||
|
const { items } = ev.clipboardData;
|
||||||
|
handlePaste(items[0]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -184,7 +184,7 @@
|
|||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
||||||
{% macro form_textarea(args) %}
|
{% macro form_textarea(args) %}
|
||||||
{{ form_begin({columns:10}|assign(args)) }}
|
{{ form_begin({columns:12}|assign(args)) }}
|
||||||
{{ textarea(args) }}
|
{{ textarea(args) }}
|
||||||
{{ form_end(args) }}
|
{{ form_end(args) }}
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|||||||
Reference in new issue
Block a user