From 5502be0f9e052a53b4056d4b1a65e87b9994436c Mon Sep 17 00:00:00 2001 From: wux_labs Date: Sat, 1 Mar 2025 17:54:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E6=94=B9?= =?UTF-8?q?=E4=B8=BABase64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 6 +++--- src/core/UAIEditor.ts | 6 ++++++ src/extensions/File.ts | 4 ++-- src/extensions/SelectFile.ts | 30 +++++++++++++++++------------- src/utils/File.ts | 13 +++++++++++++ 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 50e503e..b43d790 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/core/UAIEditor.ts b/src/core/UAIEditor.ts index fdabd4f..393920d 100644 --- a/src/core/UAIEditor.ts +++ b/src/core/UAIEditor.ts @@ -110,6 +110,12 @@ export type UAIEditorOptions = { uploadFormName?: string, uploader?: Uploader, }, + file?: { + uploadUrl?: string, + uploadHeaders?: (() => Record) | Record, + uploadFormName?: string, + uploader?: Uploader, + }, ai?: { chat?: { models?: Record, diff --git a/src/extensions/File.ts b/src/extensions/File.ts index 8f349c0..360bc6b 100644 --- a/src/extensions/File.ts +++ b/src/extensions/File.ts @@ -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(); diff --git a/src/extensions/SelectFile.ts b/src/extensions/SelectFile.ts index 40d4833..d4f7fde 100644 --- a/src/extensions/SelectFile.ts +++ b/src/extensions/SelectFile.ts @@ -167,20 +167,24 @@ export default Node.create({ }); }); } 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; }, diff --git a/src/utils/File.ts b/src/utils/File.ts index d655b54..2be0609 100644 --- a/src/utils/File.ts +++ b/src/utils/File.ts @@ -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 }); +} \ No newline at end of file