文件上传改为Base64
This commit is contained in:
+3
-3
@@ -106,11 +106,11 @@
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://gitee.com/AI-Labs/uai-editor"
|
||||
"url": "https://github.com/uai-team/uai-editor"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://gitee.com/AI-Labs/uai-editor/issues"
|
||||
"url": "https://github.com/uai-team/uai-editor/issues"
|
||||
},
|
||||
"homepage": "https://gitee.com/AI-Labs/uai-editor#readme",
|
||||
"homepage": "https://github.com/uai-team/uai-editor#readme",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
||||
@@ -110,6 +110,12 @@ export type UAIEditorOptions = {
|
||||
uploadFormName?: string,
|
||||
uploader?: Uploader,
|
||||
},
|
||||
file?: {
|
||||
uploadUrl?: string,
|
||||
uploadHeaders?: (() => Record<string, any>) | Record<string, any>,
|
||||
uploadFormName?: string,
|
||||
uploader?: Uploader,
|
||||
},
|
||||
ai?: {
|
||||
chat?: {
|
||||
models?: Record<string, AIChatConfig>,
|
||||
|
||||
@@ -7,7 +7,7 @@ import { t } from 'i18next';
|
||||
import prettyBytes from 'pretty-bytes';
|
||||
|
||||
import { FullScreenModal } from '../components/modals/FullScreenModal.ts';
|
||||
import { getFileIcon } from '../utils/File.ts';
|
||||
import { base64ToBlob, getFileIcon } from '../utils/File.ts';
|
||||
|
||||
import downloadIcon from "../assets/icons/download.svg";
|
||||
import previewIcon from "../assets/icons/view.svg";
|
||||
@@ -138,7 +138,7 @@ export default Node.create({
|
||||
const body = document.createElement('div');
|
||||
body.classList.add("uai-file-preview-modal-body");
|
||||
const iframe = document.createElement('iframe');
|
||||
iframe.src = attrs.url;
|
||||
iframe.src = URL.createObjectURL(base64ToBlob(attrs.url, attrs.previewType));
|
||||
body.appendChild(iframe);
|
||||
modal.modalBody.appendChild(body);
|
||||
modal.show();
|
||||
|
||||
@@ -167,20 +167,24 @@ export default Node.create<SelectFileOptions>({
|
||||
});
|
||||
});
|
||||
} else {
|
||||
previewType = "file";
|
||||
// 插入节点
|
||||
view.dispatch(tr.setMeta(actionKey, { type: "remove", id }));
|
||||
this.editor.commands.insertContentAt(tr.selection.from, {
|
||||
type: autoType ? (previewType ?? 'file') : 'file',
|
||||
attrs: {
|
||||
[previewType === 'file' ? 'url' : 'src']: URL.createObjectURL(file),
|
||||
name,
|
||||
type: fileType,
|
||||
size,
|
||||
file,
|
||||
previewType: Object.keys(mimeTypes).includes(fileType.split("/")[0]) ? fileType.split("/")[0] : fileType,
|
||||
},
|
||||
})
|
||||
previewType = "file";
|
||||
uploader = editorOptions.file?.uploader ?? Base64Uploader;
|
||||
uploader(file, editorOptions.file?.uploadUrl, editorOptions.file?.uploadHeaders, editorOptions.file?.uploadFormName || "file")
|
||||
.then(json => {
|
||||
view.dispatch(tr.setMeta(actionKey, { type: "remove", id }));
|
||||
this.editor.commands.insertContentAt(tr.selection.from, {
|
||||
type: autoType ? (previewType ?? 'file') : 'file',
|
||||
attrs: {
|
||||
[previewType === 'file' ? 'url' : 'src']: json.data.src,
|
||||
name,
|
||||
type: fileType,
|
||||
size,
|
||||
file,
|
||||
previewType: Object.keys(mimeTypes).includes(fileType.split("/")[0]) ? fileType.split("/")[0] : fileType,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
@@ -52,3 +52,16 @@ export const getFileIcon = (filename: string) => {
|
||||
}
|
||||
return iconName
|
||||
}
|
||||
|
||||
export const base64ToBlob = (fileBase64: string, fileType: string) => {
|
||||
if (fileBase64.startsWith("data")) {
|
||||
fileBase64 = fileBase64.split(",")[1];
|
||||
}
|
||||
let raw = atob(fileBase64);
|
||||
let rawLength = raw.length;
|
||||
let uint8Array = new Uint8Array(rawLength);
|
||||
while (rawLength--) {
|
||||
uint8Array[rawLength] = raw.charCodeAt(rawLength);
|
||||
}
|
||||
return new Blob([uint8Array], { type: fileType });
|
||||
}
|
||||
Reference in New Issue
Block a user