303 lines
13 KiB
Vue
303 lines
13 KiB
Vue
<script setup lang="ts">
|
||
import { computed, h, onScopeDispose, ref, watch } from 'vue'
|
||
import { NButton, NCheckbox, NFormItem, NInput, NSelect } from 'naive-ui'
|
||
import type { SelectRenderLabel, SelectRenderTag } from 'naive-ui'
|
||
import { projectsApi } from '../../projects/api'
|
||
import { policyDraft, policyIssue } from '../../projects/quality-policy'
|
||
import type { ProjectQualityPolicy, ProjectQualityPolicyResult } from '../../projects/types'
|
||
import { validateQualityPolicyReceipt, qualityPolicySignature } from '../../projects/quality-policy-confirmation'
|
||
import { errorMessage } from '../../../lib/http'
|
||
import WorkspaceSectionHeading from '../../../components/ui/WorkspaceSectionHeading.vue'
|
||
import QualityPolicyExceptions from './QualityPolicyExceptions.vue'
|
||
|
||
/** 项目质量策略独立编辑表单。 */
|
||
const props = defineProps<{
|
||
/** 当前正式项目编号。 */
|
||
projectId: string
|
||
/** 项目有活动操作时禁止保存。 */
|
||
disabled: boolean
|
||
/** 启动入口已经读取的真实回执,复用时不重复请求或使用默认值。 */
|
||
initialPolicy?: ProjectQualityPolicyResult
|
||
}>()
|
||
/** 保存状态交给弹窗锁定关闭与标签切换。 */
|
||
const emit = defineEmits<{ busy: [value: boolean]; saved: [] }>()
|
||
/** 读取期间不以本地默认值覆盖服务端。 */
|
||
const loading = ref(false)
|
||
/** 保存状态。 */
|
||
const saving = ref(false)
|
||
/** 实际读取或保存错误。 */
|
||
const error = ref('')
|
||
/** 真实保存成功提示。 */
|
||
const notice = ref('')
|
||
/** 服务端策略的独立草稿。 */
|
||
const policy = ref<ProjectQualityPolicy | null>(null)
|
||
/** 基础规则的原始内容,用于阻止覆盖其他入口并发修改的通用规则。 */
|
||
let savedGeneralSignature = ''
|
||
/** 整体表现方式及通俗示例,仅说明含义,不自动修改动作许可。 */
|
||
const physicsOptions = [
|
||
{
|
||
value: 'realistic',
|
||
label: '写实物理',
|
||
description: '按现实生活中的规律设计动作,例如跳起需要借力,物品失去支撑会落下。'
|
||
},
|
||
{
|
||
value: 'stylized',
|
||
label: '风格化表现',
|
||
description: '适合卡通、喜剧,让动作更有戏剧感;具体允许哪些夸张动作,还需在下方勾选。'
|
||
},
|
||
{
|
||
value: 'fantasy',
|
||
label: '幻想表现',
|
||
description: '适合仙侠、魔法等故事,按故事设定设计动作;飞行、法术等能力仍需在下方明确允许。'
|
||
}
|
||
]
|
||
/** 当前选择的说明始终显示,关闭下拉菜单后也能了解含义。 */
|
||
const physicsDescription = computed(
|
||
() => physicsOptions.find(option => option.value === policy.value?.physicsMode)?.description ?? ''
|
||
)
|
||
|
||
/**
|
||
* 下拉选项同时展示名称与说明,窄屏可自然换行。
|
||
* @param option Naive UI 提供的当前选项
|
||
* @returns 包含通俗说明的选项内容
|
||
*/
|
||
const renderPhysicsLabel: SelectRenderLabel = option =>
|
||
h('div', { class: 'grid min-w-0 gap-1 py-1 whitespace-normal' }, [
|
||
h('span', { class: 'text-sm leading-6' }, String(option.label ?? '')),
|
||
h('span', { class: 'text-xs leading-5 text-muted' }, String(option.description ?? ''))
|
||
])
|
||
|
||
/**
|
||
* 选择框只显示简短名称,完整说明放在控件下方,避免撑高或截断控件。
|
||
* @param info 当前选择的信息
|
||
* @returns 当前表现方式的名称
|
||
*/
|
||
const renderPhysicsSelection: SelectRenderTag = info => String(info.option.label ?? '')
|
||
|
||
/** 动作许可选项共用同一种标题与说明布局,字段仍映射原有后端契约。 */
|
||
interface MotionPermissionOption {
|
||
/** 对应规则中的独立布尔字段。 */
|
||
field: 'allowExaggeratedMotion' | 'allowNonPhysicalActions' | 'allowSupernaturalAbilities'
|
||
/** 用户可见的动作许可名称。 */
|
||
label: string
|
||
/** 使用日常语言说明允许范围与例子。 */
|
||
description: string
|
||
}
|
||
/** 三类许可的区别直接展示,不依赖悬停或术语理解。 */
|
||
const motionPermissions: MotionPermissionOption[] = [
|
||
{
|
||
field: 'allowExaggeratedMotion',
|
||
label: '允许夸张动作',
|
||
description: '把正常动作做得更夸张,例如惊讶时大幅后仰、跑步时摆臂更大;不等于允许悬空或法术。'
|
||
},
|
||
{
|
||
field: 'allowNonPhysicalActions',
|
||
label: '允许非物理动作',
|
||
description: '允许现实中做不到的动作,例如身体像橡皮一样拉长、没有支撑也能悬空。'
|
||
},
|
||
{
|
||
field: 'allowSupernaturalAbilities',
|
||
label: '允许超自然能力',
|
||
description: '允许角色使用法术或特殊能力,例如瞬间移动、隔空移动物品、凭空变出火焰。'
|
||
}
|
||
]
|
||
/** 仅在用户点击保存时产生校验反馈,不在读取或新增空白例外时提前报错。 */
|
||
const issue = ref('')
|
||
/** 已读取策略且没有活动操作时允许点击保存,由保存入口校验完整性。 */
|
||
const canSave = computed(() => !!policy.value && !props.disabled && !loading.value && !saving.value)
|
||
/** 修改草稿后清除上一次校验反馈,下一次保存再检查完整策略。 */
|
||
watch(
|
||
policy,
|
||
() => {
|
||
issue.value = ''
|
||
},
|
||
{ deep: true, flush: 'sync' }
|
||
)
|
||
/** 规则文本仅做逐行编辑,空行不作为规则提交。 */
|
||
const rulesText = ref('')
|
||
/** 保留输入中的换行,仅在策略草稿中去除空白规则。 */
|
||
watch(rulesText, value => {
|
||
if (policy.value)
|
||
policy.value.customRules = value
|
||
.split('\n')
|
||
.map(item => item.trim())
|
||
.filter(Boolean)
|
||
})
|
||
/** 读取与保存使用递增版本隔离项目切换和组件卸载。 */
|
||
let version = 0
|
||
/** 当前读取的取消控制器。 */
|
||
let controller: AbortController | undefined
|
||
|
||
/** 从真实接口读取完整策略,失败后提供重试。 */
|
||
async function load() {
|
||
const current = ++version
|
||
const id = props.projectId
|
||
controller?.abort()
|
||
controller = new AbortController()
|
||
policy.value = null
|
||
loading.value = true
|
||
error.value = ''
|
||
notice.value = ''
|
||
issue.value = ''
|
||
try {
|
||
const result = props.initialPolicy ?? (await projectsApi.qualityPolicy(id, controller.signal))
|
||
if (current !== version) return
|
||
validateQualityPolicyReceipt(result, id)
|
||
savedGeneralSignature = qualityPolicySignature({ ...result.policy, actionExceptions: [] })
|
||
policy.value = policyDraft(result.policy)
|
||
rulesText.value = policy.value.customRules.join('\n')
|
||
} catch (cause) {
|
||
if (current === version) error.value = errorMessage(cause)
|
||
} finally {
|
||
if (current === version) loading.value = false
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 在用户明确提交时校验并返回独立草稿,不提前显示错误。
|
||
* @returns 完整草稿,未就绪或校验失败时为空
|
||
*/
|
||
function validatedDraft(): ProjectQualityPolicy | null {
|
||
if (!canSave.value || !policy.value) return null
|
||
issue.value = policyIssue(policy.value)
|
||
return issue.value ? null : policyDraft(policy.value)
|
||
}
|
||
|
||
/** 点击保存时校验完整策略,通过后再提交并核对项目归属。 */
|
||
async function save() {
|
||
const input = validatedDraft()
|
||
if (!input) return
|
||
const id = props.projectId
|
||
const current = version
|
||
loading.value = true
|
||
controller?.abort()
|
||
controller = new AbortController()
|
||
error.value = ''
|
||
notice.value = ''
|
||
try {
|
||
const latest = await projectsApi.qualityPolicy(id, controller.signal)
|
||
if (current !== version || props.disabled) return
|
||
validateQualityPolicyReceipt(latest, id)
|
||
if (qualityPolicySignature({ ...latest.policy, actionExceptions: [] }) !== savedGeneralSignature)
|
||
throw new Error('质量规则已在其他位置更新,请重新打开配置后再保存。')
|
||
// 例外由问题修订入口管理,基础配置永远合并服务端最新例外,不提交隐藏的旧副本。
|
||
if (latest.policy.actionExceptions)
|
||
input.actionExceptions = latest.policy.actionExceptions.map(item => ({ ...item }))
|
||
else delete input.actionExceptions
|
||
loading.value = false
|
||
saving.value = true
|
||
const result = await projectsApi.saveQualityPolicy(id, input, latest.revision)
|
||
if (current !== version) return
|
||
validateQualityPolicyReceipt(result, id)
|
||
savedGeneralSignature = qualityPolicySignature({ ...result.policy, actionExceptions: [] })
|
||
policy.value = policyDraft(result.policy)
|
||
rulesText.value = policy.value.customRules.join('\n')
|
||
notice.value = result.confirmed
|
||
? '质量规则已保存,当前版本已经确认。'
|
||
: '质量规则已保存。下次启动拆解前需要核对并确认。'
|
||
emit('saved')
|
||
} catch (cause) {
|
||
if (current === version) error.value = errorMessage(cause)
|
||
} finally {
|
||
if (current === version) {
|
||
saving.value = false
|
||
loading.value = false
|
||
}
|
||
}
|
||
}
|
||
|
||
watch(() => props.projectId, load, { immediate: true })
|
||
watch(saving, value => emit('busy', value), { flush: 'sync' })
|
||
onScopeDispose(() => {
|
||
version++
|
||
controller?.abort()
|
||
})
|
||
/** 弹窗底栏复用同一个保存入口。 */
|
||
defineExpose({ save, canSave, validatedDraft })
|
||
</script>
|
||
|
||
<template>
|
||
<!-- 以下是质量策略读取反馈模块 -->
|
||
<div class="quality-policy-settings">
|
||
<p v-if="loading" role="status" class="text-sm text-muted">正在读取项目质量策略……</p>
|
||
<div v-if="error" role="alert" class="flex flex-wrap items-center gap-3 text-sm text-danger">
|
||
<span>{{ error }}</span
|
||
><NButton v-if="!policy" text @click="load">重新读取</NButton>
|
||
</div>
|
||
<template v-if="policy">
|
||
<!-- 以下是物理表现与明确动作许可模块 -->
|
||
<section class="space-y-4" aria-label="动作与物理规则">
|
||
<WorkspaceSectionHeading
|
||
title="动作与物理规则"
|
||
description="选择表现方式和允许的动作。已有内容保留。"
|
||
/>
|
||
<NFormItem label="物理表现方式" :label-props="{ id: 'quality-physics-mode-label' }">
|
||
<div class="quality-policy-field">
|
||
<NSelect
|
||
v-model:value="policy.physicsMode"
|
||
aria-labelledby="quality-physics-mode-label"
|
||
aria-describedby="quality-physics-mode-help"
|
||
:options="physicsOptions"
|
||
:render-label="renderPhysicsLabel"
|
||
:render-tag="renderPhysicsSelection"
|
||
:virtual-scroll="false"
|
||
:disabled="disabled || saving"
|
||
/>
|
||
<p id="quality-physics-mode-help" class="text-xs leading-6 text-muted">
|
||
{{ physicsDescription }}
|
||
</p>
|
||
</div>
|
||
</NFormItem>
|
||
<!-- 以下是动作许可及其日常示例,桌面按列对齐,手机逐项展示 -->
|
||
<div class="grid gap-4 sm:grid-cols-3">
|
||
<div v-for="permission in motionPermissions" :key="permission.field" class="min-w-0 space-y-2">
|
||
<NCheckbox
|
||
v-model:checked="policy[permission.field]"
|
||
:disabled="disabled || saving"
|
||
:aria-describedby="`quality-${permission.field}-help`"
|
||
>{{ permission.label }}</NCheckbox
|
||
>
|
||
<p :id="`quality-${permission.field}-help`" class="pl-6 text-xs leading-6 text-muted">
|
||
{{ permission.description }}
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<!-- 以下是逐行声明的自定义规则模块 -->
|
||
<NFormItem label="自定义规则" :label-props="{ for: 'quality-custom-rules' }">
|
||
<div class="quality-policy-field">
|
||
<p id="quality-custom-rules-help" class="text-xs leading-6 text-muted">
|
||
填写例外或要求,每行一条,最多 30 条。
|
||
</p>
|
||
<NInput
|
||
v-model:value="rulesText"
|
||
type="textarea"
|
||
:autosize="{ minRows: 3, maxRows: 8 }"
|
||
:input-props="{ id: 'quality-custom-rules', 'aria-describedby': 'quality-custom-rules-help' }"
|
||
placeholder="例如:跳跃可以夸张,但落地后必须恢复正常姿势。"
|
||
:disabled="disabled || saving"
|
||
/>
|
||
</div>
|
||
</NFormItem>
|
||
<!-- 以下是已有例外的只读核对模块,首次拆解没有例外时完全隐藏 -->
|
||
<QualityPolicyExceptions :exceptions="policy.actionExceptions ?? []" />
|
||
<p v-if="issue" role="status" class="text-xs leading-6 text-danger">{{ issue }}</p>
|
||
</template>
|
||
<p v-if="notice" role="status" class="text-xs leading-6 text-muted">{{ notice }}</p>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.quality-policy-settings {
|
||
display: grid;
|
||
gap: 24px;
|
||
padding-top: 20px;
|
||
}
|
||
.quality-policy-field {
|
||
display: grid;
|
||
width: 100%;
|
||
min-width: 0;
|
||
gap: 8px;
|
||
}
|
||
</style>
|