diff --git a/.env.example b/.env.example index 9e16f04..a879776 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ DEEPSEEK_API_KEY= -DEEPSEEK_BASE_URL=https://api.deepseek.com +DEEPSEEK_BASE_URL=https://coding.simgreek.com/v1 DEEPSEEK_MODEL=deepseek-chat diff --git a/admin.js b/admin.js index 63c55bc..01f01bd 100644 --- a/admin.js +++ b/admin.js @@ -153,9 +153,10 @@ async function loadAiStatus() { if (!els.aiStatus) return; try { const status = await apiRequest("/api/ai/status"); - els.aiStatus.textContent = status.message || "AI 状态未知。"; + const buttonState = getAiSubtitleButtonState(status, editingVideo); + els.aiStatus.textContent = buttonState.hint || status.message || "AI 状态未知。"; if (els.processAiSubtitlesBtn) { - els.processAiSubtitlesBtn.disabled = !status.configured || !editingVideo; + els.processAiSubtitlesBtn.disabled = buttonState.disabled; } } catch (error) { els.aiStatus.textContent = error.message || "AI 状态读取失败。"; @@ -163,6 +164,27 @@ async function loadAiStatus() { } } +function getAiSubtitleButtonState(status, selectedVideo) { + if (!status?.configured) { + return { + disabled: true, + hint: "请先配置 DeepSeek API。", + }; + } + + if (!selectedVideo) { + return { + disabled: true, + hint: "请先在右侧素材列表点“编辑”。", + }; + } + + return { + disabled: false, + hint: "", + }; +} + async function testAiConnection() { try { const result = await apiRequest("/api/ai/test", { method: "POST" }); @@ -315,6 +337,19 @@ function resetFormMode() { loadAiStatus(); } +function pickVideoAfterSave(videoList, response, previousEditingVideo) { + const savedVideo = response?.video || response; + if (savedVideo?.id) { + return videoList.find((video) => String(video.id) === String(savedVideo.id)) || savedVideo; + } + + if (previousEditingVideo?.id) { + return videoList.find((video) => String(video.id) === String(previousEditingVideo.id)) || previousEditingVideo; + } + + return null; +} + async function addVideo(event) { event.preventDefault(); clearMessage(); @@ -342,18 +377,24 @@ async function addVideo(event) { if (els.subtitleEn.files?.[0]) formData.append("subtitle_en", els.subtitleEn.files[0]); if (els.subtitleZh.files?.[0]) formData.append("subtitle_zh", els.subtitleZh.files[0]); if (editingVideo) appendEditedSubtitleContent(formData); + const previousEditingVideo = editingVideo; try { const path = editingVideo ? `/api/videos/${editingVideo.id}` : "/api/videos"; const method = editingVideo ? "PATCH" : "POST"; - await apiRequest(path, { + const response = await apiRequest(path, { method, body: formData, }); const successMessage = editingVideo ? "素材已更新。" : "素材已添加。"; requestSucceeded = true; - resetFormMode(); await loadVideos(); + const savedVideo = pickVideoAfterSave(videos, response, previousEditingVideo); + if (savedVideo) { + startEditingVideo(savedVideo); + } else { + resetFormMode(); + } showMessage(successMessage); } catch (error) { showMessage(error.message || (editingVideo ? "更新素材失败。" : "添加素材失败。"), true); @@ -364,7 +405,13 @@ async function addVideo(event) { } if (typeof module !== "undefined" && module.exports) { - module.exports = { getFileInputState, parseSubtitleBlocks, buildWebVtt }; + module.exports = { + getFileInputState, + parseSubtitleBlocks, + buildWebVtt, + getAiSubtitleButtonState, + pickVideoAfterSave, + }; } function createEmptySubtitleEditState() { diff --git a/tests/admin-ai-state.test.mjs b/tests/admin-ai-state.test.mjs new file mode 100644 index 0000000..3ee70fc --- /dev/null +++ b/tests/admin-ai-state.test.mjs @@ -0,0 +1,24 @@ +import assert from "node:assert/strict"; +import adminHelpers from "../admin.js"; + +const { getAiSubtitleButtonState, pickVideoAfterSave } = adminHelpers; + +assert.deepEqual(getAiSubtitleButtonState({ configured: false }, { id: 1 }), { + disabled: true, + hint: "请先配置 DeepSeek API。", +}); + +assert.deepEqual(getAiSubtitleButtonState({ configured: true }, null), { + disabled: true, + hint: "请先在右侧素材列表点“编辑”。", +}); + +assert.deepEqual(getAiSubtitleButtonState({ configured: true }, { id: 1 }), { + disabled: false, + hint: "", +}); + +const created = { id: 9, title: "New material" }; +const previous = { id: 7, title: "Old material" }; +assert.equal(pickVideoAfterSave([previous, created], { video: created }, null), created); +assert.equal(pickVideoAfterSave([previous, created], { ok: true }, previous), previous);