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