frontend: add the template configuration UI
The shell: the app mark at both ends of the header, the two top-level
entries between them, and the second-level menu on the right. That menu is
driven entirely by `route.meta.section`, so a route declares which menu it
belongs to and a deep link renders the right one on first paint; routes with
no section (the welcome page) show none.
Screens:
/ welcome, with live template and field counts so the
page doubles as a connectivity check
/papers 论文, content still to be decided
/templates/list the template table: search, create, edit, preview,
delete, batch delete, pagination
/templates/fields the field library: the same CRUD, plus level filter
The template form is the centre of it. Fields are chosen from the flat
library by clicking — any subset, any order, the same field more than once —
and the selection is always rendered sorted by `sort`, so editing a number
reorders the outline immediately. Repeats and duplicate sorts are surfaced as
warnings rather than blocked, because a repeat is often deliberate and a tie
is legal; a "自动排序" button renumbers the selection 1..N. The panel on the
right of the picker opens 字段管理 in a second tab, so a half-filled form is
never lost to a navigation.
The preview drawer renders the outline exactly as it will read, at each
field's own size and colour, with the sort value shown alongside — the
quickest way to confirm the ordering before writing against a template.
Also in this change:
- ApiError now extends Error. It was a plain object, so the common
`error instanceof Error ? error.message : ...` idiom fell through to a
generic message and threw away what the server actually said — such as
which template name is already taken. Server messages now reach the user.
- font colours are normalised client-side on blur, matching the backend,
so a hand-typed rgb(255, 0, 0) is tidied up rather than rejected later.
- api/health.ts is removed: the rewritten welcome page was its only
consumer, and the counts already prove connectivity.
This commit is contained in:
@@ -0,0 +1,242 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* Create / edit dialog for one library field.
|
||||
*
|
||||
* The live preview underneath the form is the point of the dialog: font size
|
||||
* and colour are the two settings whose effect is impossible to judge from the
|
||||
* value alone, so the preview renders the name exactly as the outline will.
|
||||
*/
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
|
||||
|
||||
import { errorMessage } from '@/api/client'
|
||||
import {
|
||||
createSectionField,
|
||||
updateSectionField,
|
||||
type SectionField,
|
||||
type SectionFieldPayload,
|
||||
} from '@/api/sectionFields'
|
||||
import { normalizeHexColor, typographyStyle } from '@/utils/format'
|
||||
|
||||
const props = defineProps<{
|
||||
/** Visibility, used with `v-model`. */
|
||||
modelValue: boolean
|
||||
/** The field being edited, or `null` to create a new one. */
|
||||
field: SectionField | null
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: boolean]
|
||||
/** Emitted after a successful write, so the list can refresh. */
|
||||
saved: []
|
||||
}>()
|
||||
|
||||
const visible = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (value: boolean) => emit('update:modelValue', value),
|
||||
})
|
||||
|
||||
const formRef = ref<FormInstance>()
|
||||
const saving = ref(false)
|
||||
|
||||
const form = reactive<SectionFieldPayload>({
|
||||
name: '',
|
||||
level: 1,
|
||||
// 小四, the conventional body size for a Chinese thesis.
|
||||
font_size: 12,
|
||||
font_color: '#000000',
|
||||
})
|
||||
|
||||
const rules: FormRules<SectionFieldPayload> = {
|
||||
name: [
|
||||
{ required: true, message: '请填写字段名称', trigger: 'blur' },
|
||||
{ max: 255, message: '字段名称最多 255 个字符', trigger: 'blur' },
|
||||
],
|
||||
}
|
||||
|
||||
const LEVEL_OPTIONS = [
|
||||
{ value: 1, label: '1 级 · 如 1. / 2.' },
|
||||
{ value: 2, label: '2 级 · 如 1.1 / 1.2' },
|
||||
{ value: 3, label: '3 级 · 如 1.1.1' },
|
||||
{ value: 4, label: '4 级' },
|
||||
]
|
||||
|
||||
/** Swatches offered by the picker — the greys match the seeded field colours. */
|
||||
const PRESET_COLORS = [
|
||||
'#000000',
|
||||
'#1F1F1F',
|
||||
'#404040',
|
||||
'#595959',
|
||||
'#8C8C8C',
|
||||
'#409EFF',
|
||||
'#67C23A',
|
||||
'#E6A23C',
|
||||
'#F56C6C',
|
||||
]
|
||||
|
||||
const previewStyle = computed(() => typographyStyle(form))
|
||||
|
||||
/** The title states which mode the dialog is in, so it is never ambiguous. */
|
||||
const title = computed(() => (props.field ? '编辑字段' : '新增字段'))
|
||||
|
||||
// Re-seed the form each time the dialog opens, so a cancelled edit never leaks
|
||||
// into the next one.
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(open) => {
|
||||
if (!open) return
|
||||
formRef.value?.clearValidate()
|
||||
|
||||
const source = props.field
|
||||
form.name = source?.name ?? ''
|
||||
form.level = source?.level ?? 1
|
||||
form.font_size = source?.font_size ?? 12
|
||||
form.font_color = source?.font_color ?? '#000000'
|
||||
},
|
||||
)
|
||||
|
||||
/**
|
||||
* Tidy a hand-typed colour when the input loses focus.
|
||||
*
|
||||
* The picker only ever produces `#RRGGBB`, but the text box next to it accepts
|
||||
* `rgb(...)` and shorthand too, matching what the API takes.
|
||||
*/
|
||||
function commitColor(): void {
|
||||
const normalized = normalizeHexColor(form.font_color)
|
||||
if (normalized) {
|
||||
form.font_color = normalized
|
||||
return
|
||||
}
|
||||
ElMessage.warning('颜色格式无效,请使用 #RRGGBB 或 rgb(r,g,b)')
|
||||
form.font_color = props.field?.font_color ?? '#000000'
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
const valid = await formRef.value?.validate().catch(() => false)
|
||||
if (!valid) return
|
||||
|
||||
saving.value = true
|
||||
try {
|
||||
if (props.field) {
|
||||
await updateSectionField(props.field.id, { ...form })
|
||||
ElMessage.success('字段已更新,使用它的模板会同步生效')
|
||||
} else {
|
||||
await createSectionField({ ...form })
|
||||
ElMessage.success('字段已创建')
|
||||
}
|
||||
emit('saved')
|
||||
visible.value = false
|
||||
} catch (error) {
|
||||
ElMessage.error(errorMessage(error))
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-dialog v-model="visible" :title="title" width="520px" append-to-body>
|
||||
<el-form ref="formRef" :model="form" :rules="rules" label-width="88px">
|
||||
<el-form-item label="字段名称" prop="name">
|
||||
<el-input
|
||||
v-model="form.name"
|
||||
placeholder="如 1. Introduction,编号自己写,系统不会改"
|
||||
maxlength="255"
|
||||
show-word-limit
|
||||
clearable
|
||||
/>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="字段等级">
|
||||
<el-select v-model="form.level" class="full">
|
||||
<el-option
|
||||
v-for="option in LEVEL_OPTIONS"
|
||||
:key="option.value"
|
||||
:label="option.label"
|
||||
:value="option.value"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="字体大小">
|
||||
<el-input-number
|
||||
v-model="form.font_size"
|
||||
:min="6"
|
||||
:max="48"
|
||||
:step="0.5"
|
||||
:precision="1"
|
||||
controls-position="right"
|
||||
/>
|
||||
<span class="unit">pt</span>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="字体颜色">
|
||||
<div class="color-row">
|
||||
<el-color-picker v-model="form.font_color" :predefine="PRESET_COLORS" />
|
||||
<el-input
|
||||
v-model="form.font_color"
|
||||
class="color-input"
|
||||
placeholder="#RRGGBB 或 rgb(r,g,b)"
|
||||
@blur="commitColor"
|
||||
/>
|
||||
</div>
|
||||
</el-form-item>
|
||||
|
||||
<el-form-item label="预览">
|
||||
<div class="preview">
|
||||
<span :style="previewStyle">{{ form.name || '(未命名字段)' }}</span>
|
||||
<span class="preview-meta">
|
||||
{{ form.level }} 级 · {{ form.font_size }}pt · {{ form.font_color }}
|
||||
</span>
|
||||
</div>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
|
||||
<template #footer>
|
||||
<el-button @click="visible = false">取消</el-button>
|
||||
<el-button type="primary" :loading="saving" @click="submit">保存</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.full {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.unit {
|
||||
margin-left: 8px;
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.color-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.color-input {
|
||||
width: 180px;
|
||||
}
|
||||
|
||||
.preview {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
border: 1px dashed var(--el-border-color);
|
||||
border-radius: 6px;
|
||||
background-color: var(--el-fill-color-lighter);
|
||||
/* Long names wrap rather than widening the dialog. */
|
||||
min-width: 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.preview-meta {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user