Files
short-drama-agent-front/src/features/visual-style/VisualStylePage.vue
T

229 lines
11 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { isOperationRunning } from '../workflows/operations'
import ContextNotice from '../../components/ui/ContextNotice.vue'
import WorkspacePage from '../../components/ui/WorkspacePage.vue'
import { NButton } from 'naive-ui'
import { LoaderCircle, LockKeyhole, LockKeyholeOpen, Palette, TriangleAlert } from '@lucide/vue'
import { computed, ref } from 'vue'
import { useQuery } from '../../composables/useQuery'
import { runOperation } from '../workflows/operations'
import { useProjectMutationGuard } from '../workflows/useProjectMutationGuard'
import { useProjectContext } from '../projects/context'
import ConfirmAction from '../workflows/ConfirmAction.vue'
import { visualStyleApi } from './api'
import type { AddStyleImageInput, SaveVisualStyleInput, VisualStyle, VisualStyleImage } from './types'
import StyleEditor from './components/StyleEditor.vue'
import StyleImages from './components/StyleImages.vue'
import VisualStyleSkeleton from './VisualStyleSkeleton.vue'
/** 项目视觉风格工作区:文本编辑、锁定和风格图记录分开管理。 */
const { projectId, operation, blocked } = useProjectMutationGuard()
/** 项目阶段就绪状态沿用父布局的真实响应。 */
const projectContext = useProjectContext()
/** 风格页仅在进入、切换项目或显式刷新时读取。 */
const query = useQuery(projectId, async (id, signal) => {
const style = await visualStyleApi.get(id, signal)
assertProject(style, id)
return { style }
})
/** 已读取的视觉风格,接口返回空值时允许首次人工创建。 */
const style = computed(() => query.data.value?.style ?? null)
/** 仅首读与项目切换显示骨架,刷新已有风格时保留编辑草稿。 */
const initialLoading = computed(() => query.loading.value && !query.data.value)
/** 查询失败或项目活动锁生效时禁止写入。 */
const disabled = computed(() => blocked.value || !!query.error.value || !query.data.value)
/** 保存成功后重建编辑器,确保草稿对应已确认的响应。 */
const editorRevision = ref(0)
/** 添加图片成功后重建组件,关闭弹窗并清空草稿。 */
const imageRevision = ref(0)
/** 仅添加参考图期间锁定添加弹窗,查询失败仍可关闭。 */
const addingImage = computed(() => operation.value.pending && operation.value.label === '添加风格参考图')
/** 添加失败的真实接口反馈同时显示在弹窗中,保留来源与设置。 */
const imageAddError = computed(() => (operation.value.label === '添加风格参考图' ? operation.value.error : ''))
/** 未保存的风格修改用于阻止 AI 覆盖。 */
const dirty = ref(false)
/** 状态是只读说明,与操作按钮分组;读取失败不能被误报成尚未创建。 */
const statusInfo = computed(() => {
if (query.error.value) return { label: '读取失败', hint: '请重试读取,现有内容不会被覆盖', icon: TriangleAlert }
if (!query.data.value) return { label: '正在读取', hint: '正在获取项目视觉风格', icon: LoaderCircle }
if (!style.value) return { label: '尚未创建', hint: '可使用 AI 生成,或在下方手动填写', icon: Palette }
return style.value.isLocked
? { label: '已锁定', hint: 'AI 不会覆盖,仍可在下方手动编辑', icon: LockKeyhole }
: { label: '未锁定', hint: '定稿后可在下方锁定,避免 AI 覆盖', icon: LockKeyholeOpen }
})
/** 防止错误项目的数据进入编辑器;空风格是正常的首次使用状态。 */
function assertProject(value: VisualStyle | null, id: string) {
if (value && (value.projectId !== id || value.images.some(image => image.visualStyleId !== value.id)))
throw new Error('视觉风格与当前项目不匹配,请刷新后重试。')
}
/** 操作成功后重建草稿,失败则保留用户输入;不因查询失败重发写请求。 */
async function writeStyle(label: string, action: (id: string) => Promise<VisualStyle>) {
if (disabled.value) return
const id = projectId.value
const ok = await runOperation(id, label, async () => {
const result = await action(id)
if (!result) throw new Error('视觉风格保存结果未确认,请刷新后核对。')
assertProject(result, id)
if (projectId.value === id) {
query.data.value = { style: result }
editorRevision.value++
dirty.value = false
}
})
if (ok) {
await query.refresh()
await projectContext.refreshStageReadiness()
}
}
/** 人工保存允许修改锁定风格。 */
function save(input: SaveVisualStyleInput) {
void writeStyle('保存项目视觉风格', id => visualStyleApi.save(id, input))
}
/** AI 覆盖必须先保存或放弃草稿,并在已锁定时禁止提交。 */
function generate() {
if (style.value?.isLocked || dirty.value) return
const force = !!style.value
void writeStyle('AI 生成项目视觉风格', id => visualStyleApi.generate(id, force))
}
/** 图片写操作固定项目与风格归属,保存后再读取列表。 */
async function writeImage(label: string, action: (id: string) => Promise<VisualStyleImage>, reset = false) {
if (disabled.value || !style.value) return
const id = projectId.value
const styleId = style.value.id
const ok = await runOperation(id, label, async () => {
const result = await action(id)
if (!result || result.visualStyleId !== styleId) throw new Error('风格参考图变更结果未确认,请刷新后核对。')
})
if (ok && reset && projectId.value === id) imageRevision.value++
await query.refresh()
}
/**
* 添加已有项目素材或图片链接,成功后关闭添加弹窗并刷新列表。
* @param input 图片来源、应用范围、顺序及参与生成设置
*/
function addImage(input: AddStyleImageInput) {
void writeImage('添加风格参考图', id => visualStyleApi.addImage(id, input), true)
}
/** 风格图启停不受文字锁定影响。 */
function toggleImage(image: VisualStyleImage) {
void writeImage('修改风格参考图启用状态', id => visualStyleApi.setImageEnabled(id, image.id, !image.enabled))
}
/** 删除已由子组件二次确认,不删除实际存储文件。 */
function removeImage(image: VisualStyleImage) {
void writeImage('移除风格参考图记录', id => visualStyleApi.removeImage(id, image.id))
}
</script>
<template>
<!-- 以下是视觉风格首次读取的完整骨架屏模块 -->
<VisualStyleSkeleton v-if="initialLoading" />
<!-- 以下是手机提示、风格工具与正文共同滚动的视觉风格工作区 -->
<WorkspacePage v-else compact mobile-scroll class="visual-style-page"
><template #header>
<!-- 以下是标题、锁定状态与生成操作 -->
<div class="workspace-toolbar visual-style-toolbar">
<h2 class="font-semibold">项目视觉风格</h2>
<div class="visual-style-status" role="status">
<component :is="statusInfo.icon" :size="16" aria-hidden="true" />
<strong>{{ statusInfo.label }}</strong>
<p>{{ statusInfo.hint }}</p>
</div>
<div class="visual-style-actions toolbar-actions">
<ConfirmAction
:loading="isOperationRunning(operation, 'AI 生成项目视觉风格')"
:label="style ? 'AI 重新生成风格' : 'AI 生成风格'"
:disabled="disabled || !!style?.isLocked || dirty"
acknowledgement
description="调用文本模型生成整体及分类风格。重新生成会覆盖已保存的文本与硬约束;旧身份、旧图片和已有提示词不会自动更新。"
@confirm="generate"
/>
</div>
</div>
</template>
<div class="workspace-section-stack">
<ContextNotice v-if="query.error.value" role="alert" type="error" :show-icon="false"
>{{ query.error.value }} 当前内容读取失败;已有内容不会被覆盖。
<NButton text class="ml-3" :loading="query.loading.value" @click="query.refresh">重试读取</NButton>
</ContextNotice>
<p v-if="dirty" class="mb-4 text-xs text-muted">
有未保存修改,AI 重生成暂不可用。刷新不会覆盖编辑内容;离开页面不会自动保存。
</p>
<!-- 以下是始终显示的风格生成规则模块 -->
<section v-if="query.data.value" class="panel workspace-panel visual-style-rules">
<h3 class="font-medium">风格生成规则</h3>
<p class="mt-3 text-xs leading-6 text-muted">
AI
生成时,若项目和世界观没有明确其它人物背景,人物风格默认采用中国人物选角基线;人工填写的人物背景和角色事实优先。修改风格只影响之后的生成,旧资产不会自动更新。
</p>
</section>
<!-- 以下是风格编辑与始终展开的分类补充模块 -->
<StyleEditor
v-if="query.data.value"
:key="editorRevision"
:visual-style="style"
:saving="isOperationRunning(operation, '保存项目视觉风格')"
:disabled="disabled"
@save="save"
@dirty="dirty = $event"
/>
<!-- 以下是读取成功后的参考图列表与添加入口模块 -->
<StyleImages
v-if="query.data.value"
:key="imageRevision"
:project-id="projectId"
:images="style?.images ?? []"
:has-style="!!style"
:adding="addingImage"
:add-error="imageAddError"
:disabled="disabled || !style"
@add="addImage"
@toggle="toggleImage"
@remove="removeImage"
/>
</div>
</WorkspacePage>
</template>
<style>
/* 本组件专属布局与 Naive 内部结构覆盖。 */
@reference "../../styles/styles.css";
.visual-style-page {
container: visual-style-header / inline-size;
}
.visual-style-status {
@apply flex items-center gap-2 min-w-0 flex-1 flex-nowrap;
}
.visual-style-status > svg {
@apply shrink-0 text-muted;
}
.visual-style-status strong {
@apply text-xs font-medium whitespace-nowrap;
}
.visual-style-status p {
@apply mt-0 mx-0 mb-0 min-w-0 truncate text-muted text-xs leading-[1.6];
}
.visual-style-actions {
@apply shrink-0;
}
/* 窄内容区让说明占满首行剩余空间,操作独占下一行并从左侧排开。 */
@container visual-style-header (max-width: 760px) {
.visual-style-page .workspace-toolbar {
@apply gap-x-3 gap-y-2;
}
.visual-style-actions.toolbar-actions {
@apply ml-0 w-full justify-start;
}
}
</style>