156 lines
5.9 KiB
Vue
156 lines
5.9 KiB
Vue
<script setup lang="ts">
|
|
import { NCheckbox, NFormItem, NInput, NInputNumber, NSelect } from 'naive-ui'
|
|
import FieldLabel from '../../../components/ui/FieldLabel.vue'
|
|
import type { GenerationOptionSchema, GenerationOptions } from '../types'
|
|
|
|
/** 动态模型参数字段,实际说明由后端 schema 提供。 */
|
|
defineProps<{
|
|
/** 当前模型开放的参数列表。 */
|
|
schemas: GenerationOptionSchema[]
|
|
/** 本次实际参数值。 */
|
|
modelValue: GenerationOptions
|
|
/** 上游操作期间禁止编辑。 */
|
|
disabled?: boolean
|
|
}>()
|
|
/** 字段编辑后通知上游更新参数。 */
|
|
const emit = defineEmits<{ 'update:modelValue': [value: GenerationOptions] }>()
|
|
|
|
function update(model: GenerationOptions, key: string, value: string | number | boolean | null) {
|
|
const next = { ...model }
|
|
if (value === null || value === '') delete next[key]
|
|
else next[key] = value
|
|
emit('update:modelValue', next)
|
|
}
|
|
|
|
function selectValue(value: string | number | boolean | undefined) {
|
|
return typeof value === 'string' || typeof value === 'number' ? value : null
|
|
}
|
|
|
|
function selectOptions(schema: GenerationOptionSchema) {
|
|
return [...(schema.options || []), ...(schema.specialValues || [])].flatMap(option =>
|
|
typeof option.value === 'string' || typeof option.value === 'number'
|
|
? [{ label: option.label, value: option.value }]
|
|
: []
|
|
)
|
|
}
|
|
|
|
function isDuration(schema: GenerationOptionSchema) {
|
|
return /duration|时长/i.test(`${schema.key} ${schema.label}`)
|
|
}
|
|
|
|
function optionKey(value: string | number) {
|
|
return `${typeof value}:${value}`
|
|
}
|
|
|
|
/** 尺寸和时长使用有限选项,避免用户输入 Provider 不支持的自由值。 */
|
|
function constrainedOptions(schema: GenerationOptionSchema, current: string | number | boolean | undefined) {
|
|
const options = selectOptions(schema)
|
|
if (schema.type === 'size') {
|
|
// 图片尺寸完全由当前 Provider Schema 决定,禁止混入其它模型的分辨率档位。
|
|
} else if (
|
|
isDuration(schema) &&
|
|
Number.isInteger(schema.min) &&
|
|
Number.isInteger(schema.max) &&
|
|
schema.max! >= schema.min! &&
|
|
schema.max! - schema.min! <= 60
|
|
) {
|
|
for (let value = schema.min!; value <= schema.max!; value++) {
|
|
options.push({ label: `${value} 秒`, value })
|
|
}
|
|
}
|
|
if (typeof schema.providerDefault === 'string' || typeof schema.providerDefault === 'number') {
|
|
options.push({ label: String(schema.providerDefault), value: schema.providerDefault })
|
|
}
|
|
if (typeof current === 'string' || typeof current === 'number') {
|
|
options.push({ label: String(current), value: current })
|
|
}
|
|
const seen = new Set<string>()
|
|
return options.filter(option => {
|
|
const key = optionKey(option.value)
|
|
if (seen.has(key)) return false
|
|
seen.add(key)
|
|
return true
|
|
})
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div v-if="schemas.length" class="generation-option-grid">
|
|
<NFormItem
|
|
v-for="schema in schemas"
|
|
:key="schema.key"
|
|
:label="`${schema.label}${schema.required ? ' *' : ''}`"
|
|
:show-feedback="false"
|
|
>
|
|
<!-- 以下是参数标签与按需说明模块 -->
|
|
<template #label>
|
|
<FieldLabel
|
|
:label="`${schema.label}${schema.required ? ' *' : ''}`"
|
|
:description="schema.description"
|
|
/>
|
|
</template>
|
|
<NCheckbox
|
|
v-if="schema.type === 'boolean'"
|
|
:checked="modelValue[schema.key] === true"
|
|
:disabled="disabled"
|
|
@update:checked="update(modelValue, schema.key, $event)"
|
|
>启用</NCheckbox
|
|
>
|
|
<NSelect
|
|
v-else-if="schema.type === 'select' || schema.type === 'size' || isDuration(schema)"
|
|
:value="selectValue(modelValue[schema.key])"
|
|
:options="constrainedOptions(schema, modelValue[schema.key])"
|
|
:disabled="disabled"
|
|
:clearable="!schema.required"
|
|
@update:value="update(modelValue, schema.key, $event)"
|
|
/>
|
|
<NInputNumber
|
|
v-else-if="schema.type === 'integer' || schema.type === 'number'"
|
|
:value="typeof modelValue[schema.key] === 'number' ? (modelValue[schema.key] as number) : null"
|
|
:min="schema.min"
|
|
:max="schema.max"
|
|
:precision="schema.type === 'integer' ? 0 : undefined"
|
|
:disabled="disabled"
|
|
clearable
|
|
@update:value="update(modelValue, schema.key, $event)"
|
|
/>
|
|
<NInput
|
|
v-else
|
|
:value="typeof modelValue[schema.key] === 'string' ? (modelValue[schema.key] as string) : ''"
|
|
:disabled="disabled"
|
|
placeholder="例如 2048x2048 或 auto"
|
|
clearable
|
|
@update:value="update(modelValue, schema.key, $event)"
|
|
/>
|
|
</NFormItem>
|
|
</div>
|
|
<p v-else class="text-xs text-muted">当前模型没有开放额外生成参数。</p>
|
|
</template>
|
|
|
|
<style scoped>
|
|
@reference "../../../styles/styles.css";
|
|
.generation-option-grid {
|
|
@apply grid grid-cols-[repeat(2,_minmax(0,_1fr))] items-start gap-x-4 gap-y-6;
|
|
}
|
|
.generation-option-grid :deep(.n-form-item) {
|
|
@apply min-w-0 mb-0 self-start;
|
|
}
|
|
/* Naive 默认使用 1fr 扩展控件行;同排说明文字不等高时会把较短字段的控件推到底部。 */
|
|
.generation-option-grid :deep(.n-form-item--top-labelled) {
|
|
grid-template-rows: minmax(var(--n-label-height), auto) auto auto;
|
|
}
|
|
.generation-option-grid :deep(.n-form-item-label) {
|
|
min-height: 30px;
|
|
padding: 0 0 8px;
|
|
}
|
|
.generation-option-grid :deep(.n-form-item-blank) {
|
|
min-height: 34px;
|
|
align-items: center;
|
|
}
|
|
@media (max-width: 760px) {
|
|
.generation-option-grid {
|
|
@apply grid-cols-[minmax(0,_1fr)];
|
|
}
|
|
}
|
|
</style>
|