From e827e9425e8747bb5537de23a30a0ecc0d56a047 Mon Sep 17 00:00:00 2001 From: "949036910@qq.com" <> Date: Mon, 1 Jun 2026 00:03:56 +0800 Subject: [PATCH] 11 --- src/services/subtitleTemplateCatalog.js | 4 +++- src/utils/publicAsset.js | 11 +++++++++++ src/utils/subtitlePreviewStyle.js | 9 +++++++-- 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/utils/publicAsset.js diff --git a/src/services/subtitleTemplateCatalog.js b/src/services/subtitleTemplateCatalog.js index b340404..3def7a1 100644 --- a/src/services/subtitleTemplateCatalog.js +++ b/src/services/subtitleTemplateCatalog.js @@ -1,3 +1,5 @@ +import { publicAssetUrl } from "../utils/publicAsset.js"; + const STORAGE_KEY = "aiclient_custom_subtitle_templates"; let systemTemplatesCache = null; @@ -18,7 +20,7 @@ let systemTemplatesCache = null; */ export async function loadSystemSubtitleTemplates() { if (systemTemplatesCache) return systemTemplatesCache; - const res = await fetch("/subtitle-templates/system-templates.json"); + const res = await fetch(publicAssetUrl("subtitle-templates/system-templates.json")); if (!res.ok) throw new Error("无法加载系统字幕模板"); const data = await res.json(); systemTemplatesCache = (data.subtitleTemplates || []).map((t) => ({ diff --git a/src/utils/publicAsset.js b/src/utils/publicAsset.js new file mode 100644 index 0000000..d0bb15b --- /dev/null +++ b/src/utils/publicAsset.js @@ -0,0 +1,11 @@ +/** + * 解析 public/ 目录下的静态资源 URL。 + * 开发环境 base 为 `/`,发行部署 base 为 `/images/newyaoyan/`(见 vite.config.js)。 + * + * @param {string} path - 相对 public 的路径,如 subtitle-templates/system-templates.json + */ +export function publicAssetUrl(path) { + const base = import.meta.env.BASE_URL || "/"; + const normalized = String(path || "").replace(/^\/+/, ""); + return `${base}${normalized}`; +} diff --git a/src/utils/subtitlePreviewStyle.js b/src/utils/subtitlePreviewStyle.js index 803ef08..73bec54 100644 --- a/src/utils/subtitlePreviewStyle.js +++ b/src/utils/subtitlePreviewStyle.js @@ -1,5 +1,7 @@ /** 字幕模板预览样式(对齐 Electron SubtitlePreviewCard) */ +import { publicAssetUrl } from "./publicAsset.js"; + const ACCENT_KEYWORDS = [ "自己", "节奏", @@ -240,11 +242,14 @@ export function resolveSubtitlePreviewImage(path) { const normalized = path.replace(/\\/g, "/"); if (normalized.includes("subtitle-preview-")) { const name = normalized.split("/").pop(); - return `/subtitle-templates/previews/${name}`; + return publicAssetUrl(`subtitle-templates/previews/${name}`); } if (normalized.startsWith("extra/common/cover-templates/")) { const name = normalized.split("/").pop(); - return `/subtitle-templates/previews/${name}`; + return publicAssetUrl(`subtitle-templates/previews/${name}`); + } + if (normalized.startsWith("subtitle-templates/")) { + return publicAssetUrl(normalized); } return normalized; }