0.1.14 上线前更改

This commit is contained in:
liaibo
2026-01-11 12:35:53 +08:00
parent be71849aa5
commit 725a60d3aa
44 changed files with 3427 additions and 310 deletions
+50 -15
View File
@@ -2,9 +2,13 @@
import { useEffect, useMemo, useState } from "react";
import { useSearchParams } from "next/navigation";
import { OnlyOfficeAiAgentPanel } from "@/components/onlyoffice/OnlyOfficeAiAgentPanel";
import { rewriteToPublicOrigin } from "@/lib/url/rewritePublicOrigin";
type EditorMode = "view" | "edit";
const MNOTE_AGENT_PLUGIN_GUID = "asc.{F2B9A7B4-3A22-4E0E-8B32-3B0DE7AE8A60}";
const loadScript = (src: string) =>
new Promise<void>((resolve, reject) => {
const existing = document.querySelector(`script[src="${src}"]`);
@@ -34,10 +38,13 @@ const docTypeFromExt = (ext: string) => {
const word = ["doc", "docx", "odt", "rtf"];
const slide = ["ppt", "pptx", "odp"];
const sheet = ["xls", "xlsx", "ods", "csv"];
if (word.includes(ext)) return "text";
if (slide.includes(ext)) return "presentation";
if (sheet.includes(ext)) return "spreadsheet";
return "text";
const pdf = ["pdf"];
// 说明:ONLYOFFICE 文档类型使用 word/cell/slide/pdf(旧的 text/spreadsheet/presentation 已逐步弃用)
if (word.includes(ext)) return "word";
if (slide.includes(ext)) return "slide";
if (sheet.includes(ext)) return "cell";
if (pdf.includes(ext)) return "pdf";
return "word";
};
export default function OnlyOfficePage() {
@@ -46,27 +53,38 @@ export default function OnlyOfficePage() {
const fileName = params.get("fileName") ?? "未命名文档";
const fileType = (params.get("fileType") ?? "docx").toLowerCase();
const mode = (params.get("mode") ?? "edit") as EditorMode;
const assetId = params.get("assetId") ?? "";
const [error, setError] = useState<string | null>(null);
const baseUrl = process.env.NEXT_PUBLIC_ONLYOFFICE_BASE_URL;
const storageHostOverride =
process.env.NEXT_PUBLIC_ONLYOFFICE_STORAGE_HOST_OVERRIDE;
const targetDocType = useMemo(() => docTypeFromExt(fileType), [fileType]);
const targetDocType = useMemo(() => docTypeFromExt(fileType), [fileType]);
const resolvedFileUrl = useMemo(() => {
if (!fileUrl) return "";
// 说明:OnlyOffice 的 document.url 由“文档服务器”拉取(不是浏览器)。
// 如果我们已经配置了专用回源(storageHostOverride),就不要再把 URL 改写成公网,
// 否则会把 http://host.docker.internal:18000 错误改成 https://host.docker.internal:18000
// 导致 ONLYOFFICE 报 “下载失败(EPROTO wrong version number)”。
const base = storageHostOverride
? fileUrl
: rewriteToPublicOrigin(fileUrl, process.env.NEXT_PUBLIC_SUPABASE_URL);
try {
const u = new URL(fileUrl);
if (
storageHostOverride &&
(u.hostname === "127.0.0.1" ||
u.hostname === "localhost" ||
u.hostname === "host.docker.internal")
) {
u.hostname = storageHostOverride;
const u = new URL(base);
if (!storageHostOverride) return u.toString();
// 兼容两种写法:hostname 或完整 originhttps://xxx
if (/^https?:\/\//i.test(storageHostOverride)) {
const ov = new URL(storageHostOverride);
u.protocol = ov.protocol;
u.host = ov.host;
return u.toString();
}
u.hostname = storageHostOverride;
return u.toString();
} catch {
return fileUrl;
return base;
}
}, [fileUrl, storageHostOverride]);
@@ -87,6 +105,7 @@ export default function OnlyOfficePage() {
throw new Error("未检测到 DocsAPI,请检查 ONLYOFFICE 版本。");
}
// eslint-disable-next-line new-cap,@typescript-eslint/no-explicit-any
const pluginConfigUrl = `${window.location.origin}/onlyoffice/plugins/agent-tools/config.json`;
new (window as any).DocsAPI.DocEditor("onlyoffice-frame", {
width: "100%",
height: "100%",
@@ -103,6 +122,10 @@ export default function OnlyOfficePage() {
customization: {
feedback: { visible: false },
},
plugins: {
autostart: [MNOTE_AGENT_PLUGIN_GUID],
pluginsData: [pluginConfigUrl],
},
},
});
})
@@ -120,5 +143,17 @@ export default function OnlyOfficePage() {
);
}
return <div id="onlyoffice-frame" className="h-screen w-screen bg-slate-50" />;
return (
<div className="relative h-screen w-screen bg-slate-50">
<div id="onlyoffice-frame" className="h-full w-full" />
<OnlyOfficeAiAgentPanel
openFile={{
id: assetId || `onlyoffice_${hashKey(`${resolvedFileUrl}-${fileName}`)}`,
title: fileName,
fileUrl: resolvedFileUrl,
mimeType: null,
}}
/>
</div>
);
}