frontend: add the reference library, its tags, and a way in

参考文献 now sits in the header to the right of 论文 and 模板 — the library is
drawn *from* rather than worked in, so it is a button one step away. Its
own second-level menu marks out 文献列表 / 新增文献 / 标签管理 and then lists
the tags themselves, read from their store, so a label can be picked
straight from the rail.

A tag in the rail links to `/references?tag=<id>`, which makes the query
parameter part of the view rather than hidden filter state: the table's own
selector writes the same parameter, so the two cannot disagree and a
filtered view is a link someone can be sent.

The form keeps the order the feature intends — a tag is created before it
is applied, so no stored reference can point at a label that is not there —
and the citation count in a row opens the answer it exists to give (which
paper, which paragraph, which quoted passage).

Tag colours are painted from the row rather than a class map: the palette
is the writer's, and the chip has to be that hue in the table, in the rail
and in the picker.
This commit is contained in:
2026-09-22 17:55:20 +08:00
parent 6020310b3f
commit 872ad8cf3b
12 changed files with 2837 additions and 27 deletions
@@ -0,0 +1,568 @@
<script setup lang="ts">
/**
* 参考文献 — the library table.
*
* One row per document, with the whole CRUD surface around it. Two things here
* are the reason the table exists in this shape:
*
* * **The tag column is a filter as much as a column.** The rail lists the labels
* and links to `/references?tag=<id>`; the select in the toolbar writes the
* same query parameter back, so the URL is the single source of truth for the
* filter and a filtered view can be pasted to someone else.
* * **The citation count is a door, not a number.** "Which of my sentences use
* this?" is the question that decides whether an entry is safe to delete, and
* answering it is one click rather than a different page.
*/
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ChatLineSquare, Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
batchDeleteReferences,
deleteReference,
listReferences,
REFERENCE_TYPE_OPTIONS,
referenceTypeLabel,
referenceTypeMarker,
type ReferenceListItem,
type ReferenceType,
} from '@/api/references'
import ReferenceCitationsDialog from '@/components/references/ReferenceCitationsDialog.vue'
import ReferenceFormDialog from '@/components/references/ReferenceFormDialog.vue'
import TagChip from '@/components/references/TagChip.vue'
import { useReferenceTagsStore } from '@/stores/referenceTags'
import { formatDateTime } from '@/utils/format'
const route = useRoute()
const router = useRouter()
const tagsStore = useReferenceTagsStore()
const loading = ref(false)
const rows = ref<ReferenceListItem[]>([])
const total = ref(0)
const selection = ref<ReferenceListItem[]>([])
const query = reactive({
keyword: '',
type: null as ReferenceType | null,
year: undefined as number | undefined,
cited: null as boolean | null,
page: 1,
page_size: 20,
})
const dialogVisible = ref(false)
const editing = ref<ReferenceListItem | null>(null)
const citationsVisible = ref(false)
const citationsTarget = ref<ReferenceListItem | null>(null)
const CITED_FILTERS = [
{ value: null, label: '全部文献' },
{ value: true, label: '已被引用' },
{ value: false, label: '还没引用' },
]
const tags = computed(() => tagsStore.items)
/**
* The tag filter lives in the URL, not in this component.
*
* The rail links to a filtered library and the toolbar changes the same filter,
* so one of them has to own it — and the URL is the one both can read. That is
* also why the rail can highlight the label you are looking at.
*/
const tagFilter = computed<number | null>({
get: () => {
const raw = route.query.tag
const value = Array.isArray(raw) ? raw[0] : raw
const parsed = value ? Number(value) : null
return parsed != null && Number.isFinite(parsed) ? parsed : null
},
set: (value) => {
void router.replace({
path: '/references',
query: value == null ? {} : { tag: String(value) },
})
},
})
async function load(): Promise<void> {
loading.value = true
try {
const result = await listReferences({
keyword: query.keyword,
type: query.type,
year: query.year ?? null,
cited: query.cited,
tag_ids: tagFilter.value == null ? [] : [tagFilter.value],
page: query.page,
page_size: query.page_size,
})
rows.value = result.items
total.value = result.total
// A delete can empty the last page. Stepping back keeps the table from
// showing "no data" while the pager still claims a page beyond the end.
if (result.items.length === 0 && result.total > 0 && query.page > 1) {
query.page -= 1
await load()
}
} catch (error) {
ElMessage.error(errorMessage(error))
} finally {
loading.value = false
}
}
function search(): void {
query.page = 1
void load()
}
function resetFilters(): void {
query.keyword = ''
query.type = null
query.year = undefined
query.cited = null
query.page = 1
tagFilter.value = null
void load()
}
function onCreate(): void {
editing.value = null
dialogVisible.value = true
}
function onEdit(row: ReferenceListItem): void {
editing.value = row
dialogVisible.value = true
}
function onShowCitations(row: ReferenceListItem): void {
citationsTarget.value = row
citationsVisible.value = true
}
/** Refresh the table and the rail's tag counts, which this page can change. */
async function refreshAll(): Promise<void> {
await Promise.all([load(), tagsStore.reload()])
}
async function onDelete(row: ReferenceListItem): Promise<void> {
try {
await ElMessageBox.confirm(`确定从文献库删除「${row.title}」?`, '删除文献', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
})
} catch {
return // dismissed
}
try {
await deleteReference(row.id)
ElMessage.success('已删除')
await refreshAll()
} catch (error) {
// A cited entry is refused with the count, which is worth showing whole.
ElMessage.error(errorMessage(error))
}
}
async function onBatchDelete(): Promise<void> {
const ids = selection.value.map((row) => row.id)
if (ids.length === 0) return
try {
await ElMessageBox.confirm(
`确定删除选中的 ${ids.length} 篇文献?已被正文引用的会被拒绝。`,
'批量删除',
{ type: 'warning', confirmButtonText: '删除', cancelButtonText: '取消' },
)
} catch {
return // dismissed
}
try {
const result = await batchDeleteReferences(ids)
ElMessage.success(`已删除 ${result.deleted} 篇文献`)
selection.value = []
await refreshAll()
} catch (error) {
ElMessage.error(errorMessage(error))
}
}
/** The heading a row prints: the type's Chinese name, with its GB/T marker. */
function typeLabel(row: ReferenceListItem): string {
const marker = referenceTypeMarker(row.type)
const label = referenceTypeLabel(row.type)
return marker ? `${label} [${marker}]` : label
}
/** Where it was published, as one column: the container, else the publisher. */
function sourceLine(row: ReferenceListItem): string {
const parts = [row.container_title ?? row.publisher ?? ''].filter(Boolean)
if (row.volume) parts.push(`第 ${row.volume} 卷`)
if (row.issue) parts.push(`第 ${row.issue} 期`)
if (row.pages) parts.push(row.pages)
return parts.join(' · ')
}
/**
* Open the add dialog when the rail's 新建文献 entry sent us here.
*
* The query is cleared straight away, so a refresh or a back-navigation does
* not reopen the dialog.
*/
watch(
() => route.query.new,
(flag) => {
if (!flag) return
onCreate()
void router.replace({ path: '/references', query: route.query.tag ? { tag: route.query.tag } : {} })
},
{ immediate: true },
)
// The tag filter is read from the URL, so a change to it is a change of view.
watch(
() => route.query.tag,
() => {
query.page = 1
void load()
},
{ immediate: true },
)
onMounted(() => {
void tagsStore.ensureLoaded()
})
</script>
<template>
<div class="page page--fill">
<el-card shadow="never" class="card--fill">
<template #header>
<div class="card-header">
<div>
<span class="card-title">参考文献库</span>
<span class="card-subtitle">
文献是全局的,可以被任意多篇论文引用;标签用来给自己归档
</span>
</div>
<el-tag type="info" effect="plain">共 {{ total }} 篇</el-tag>
</div>
</template>
<div class="toolbar">
<el-input
v-model="query.keyword"
placeholder="搜索题名、刊名、作者、DOI、关键词"
clearable
class="search"
@keyup.enter="search"
@clear="search"
>
<template #prefix><el-icon><Search /></el-icon></template>
</el-input>
<el-select
v-model="query.type"
placeholder="全部类型"
clearable
class="type-filter"
@change="search"
>
<el-option
v-for="option in REFERENCE_TYPE_OPTIONS"
:key="option.value"
:value="option.value"
:label="`${option.label} [${option.marker}]`"
/>
</el-select>
<el-select
v-model="tagFilter"
placeholder="全部标签"
clearable
class="tag-filter"
@change="search"
>
<el-option v-for="tag in tags" :key="tag.id" :value="tag.id" :label="tag.name">
<span class="tag-option">
<span class="tag-dot" :style="{ backgroundColor: tag.color }" />
<span>{{ tag.name }}</span>
<span class="tag-option-count">{{ tag.reference_count }}</span>
</span>
</el-option>
</el-select>
<el-select
v-model="query.cited"
placeholder="引用情况"
clearable
class="cited-filter"
@change="search"
>
<el-option
v-for="option in CITED_FILTERS"
:key="String(option.value)"
:value="option.value"
:label="option.label"
/>
</el-select>
<el-input-number
v-model="query.year"
:min="1"
:max="2999"
:controls="false"
placeholder="年份"
class="year-filter"
@change="search"
/>
<el-button @click="search">查询</el-button>
<el-button text @click="resetFilters">重置</el-button>
<div class="toolbar-spacer" />
<el-button :icon="Refresh" :loading="loading" @click="load">刷新</el-button>
<el-button
type="danger"
plain
:icon="Delete"
:disabled="selection.length === 0"
@click="onBatchDelete"
>
批量删除{{ selection.length ? `(${selection.length})` : '' }}
</el-button>
<el-button type="primary" :icon="Plus" @click="onCreate">新增文献</el-button>
</div>
<el-table
v-loading="loading"
:data="rows"
row-key="id"
stripe
class="table table-fill"
height="100%"
@selection-change="(value: ReferenceListItem[]) => (selection = value)"
>
<el-table-column type="selection" width="46" reserve-selection />
<el-table-column label="题名" min-width="260">
<template #default="{ row }">
<el-button link type="primary" class="title-link" @click="onEdit(row)">
{{ row.title }}
</el-button>
<div v-if="row.subtitle" class="subtitle">{{ row.subtitle }}</div>
</template>
</el-table-column>
<el-table-column label="类型" width="120">
<template #default="{ row }">
<el-tag size="small" effect="plain">{{ typeLabel(row) }}</el-tag>
</template>
</el-table-column>
<el-table-column label="责任者" min-width="150" show-overflow-tooltip>
<template #default="{ row }">
<span v-if="row.authors_line">{{ row.authors_line }}</span>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="出版信息" min-width="180" show-overflow-tooltip>
<template #default="{ row }">
<span v-if="sourceLine(row)">{{ sourceLine(row) }}</span>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="年份" width="80" align="center">
<template #default="{ row }">
<span v-if="row.publication_year">{{ row.publication_year }}</span>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="标签" min-width="180">
<template #default="{ row }">
<template v-if="row.tags.length">
<TagChip
v-for="tag in row.tags.slice(0, 3)"
:key="tag.id"
:name="tag.name"
:color="tag.color"
class="row-tag"
@click="tagFilter = tag.id"
/>
<span v-if="row.tags.length > 3" class="muted">
+{{ row.tags.length - 3 }}
</span>
</template>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="引用" width="90" align="center">
<template #default="{ row }">
<el-button
v-if="row.citation_count"
link
type="primary"
@click="onShowCitations(row)"
>
{{ row.citation_count }} 处
</el-button>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="更新时间" width="150">
<template #default="{ row }">{{ formatDateTime(row.updated_at) }}</template>
</el-table-column>
<el-table-column label="操作" width="210" fixed="right">
<template #default="{ row }">
<el-button link type="primary" :icon="Edit" @click="onEdit(row)">编辑</el-button>
<el-button
link
type="primary"
:icon="ChatLineSquare"
@click="onShowCitations(row)"
>
引用
</el-button>
<el-button link type="danger" :icon="Delete" @click="onDelete(row)">删除</el-button>
</template>
</el-table-column>
<template #empty>
<el-empty
:description="
query.keyword || query.type || tagFilter != null
? '没有匹配的文献'
: '文献库还是空的,先加一篇'
"
>
<el-button type="primary" :icon="Plus" @click="onCreate">新增文献</el-button>
</el-empty>
</template>
</el-table>
<div class="table-pagination">
<el-pagination
v-model:current-page="query.page"
v-model:page-size="query.page_size"
:total="total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
background
@current-change="load"
@size-change="search"
/>
</div>
</el-card>
<ReferenceFormDialog
v-model="dialogVisible"
:reference="editing"
@saved="refreshAll"
/>
<ReferenceCitationsDialog v-model="citationsVisible" :reference="citationsTarget" />
</div>
</template>
<style scoped>
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.card-title {
font-weight: 600;
margin-right: 10px;
}
.card-subtitle {
color: var(--el-text-color-secondary);
font-size: 12px;
}
.search {
width: 240px;
}
.type-filter {
width: 150px;
}
.tag-filter {
width: 150px;
}
.cited-filter {
width: 130px;
}
.year-filter {
width: 100px;
}
.table {
margin-top: 16px;
}
.title-link {
font-weight: 600;
padding: 0;
height: auto;
text-align: left;
white-space: normal;
}
.subtitle {
font-size: 12px;
color: var(--el-text-color-secondary);
line-height: 1.5;
}
.row-tag {
margin: 0 4px 4px 0;
cursor: pointer;
}
.muted {
color: var(--el-text-color-placeholder);
}
.tag-option {
display: flex;
align-items: center;
gap: 6px;
}
.tag-dot {
width: 8px;
height: 8px;
border-radius: 50%;
flex: 0 0 auto;
}
.tag-option-count {
margin-left: auto;
padding-left: 12px;
font-size: 12px;
color: var(--el-text-color-placeholder);
}
</style>
@@ -0,0 +1,303 @@
<script setup lang="ts">
/**
* 标签管理 — the label library the reference module draws on.
*
* This page is the first half of the tagging flow: labels are **made here and
* applied in the reference form**, so a reference is never stored pointing at a
* label that does not exist. The library is global, so a label means the same
* thing in every paper.
*
* Deleting is not guarded the way deleting a library field is. A tag is the
* writer's own filing rather than something the document claims, so removing it
* removes labels and leaves the literature where it was — the confirmation says
* how many entries will lose the label, which is the honest form of that
* warning.
*/
import { onMounted, reactive, ref } from 'vue'
import { Delete, Edit, Plus, Refresh, Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
batchDeleteReferenceTags,
deleteReferenceTag,
listReferenceTags,
type ReferenceTag,
} from '@/api/referenceTags'
import TagChip from '@/components/references/TagChip.vue'
import TagFormDialog from '@/components/references/TagFormDialog.vue'
import { useReferenceTagsStore } from '@/stores/referenceTags'
import { formatDateTime } from '@/utils/format'
const tagsStore = useReferenceTagsStore()
const loading = ref(false)
const rows = ref<ReferenceTag[]>([])
const total = ref(0)
const selection = ref<ReferenceTag[]>([])
const query = reactive({
keyword: '',
page: 1,
page_size: 20,
})
const dialogVisible = ref(false)
const editing = ref<ReferenceTag | null>(null)
async function load(): Promise<void> {
loading.value = true
try {
const result = await listReferenceTags(query)
rows.value = result.items
total.value = result.total
if (result.items.length === 0 && result.total > 0 && query.page > 1) {
query.page -= 1
await load()
}
} catch (error) {
ElMessage.error(errorMessage(error))
} finally {
loading.value = false
}
}
function search(): void {
query.page = 1
void load()
}
function onCreate(): void {
editing.value = null
dialogVisible.value = true
}
function onEdit(row: ReferenceTag): void {
editing.value = row
dialogVisible.value = true
}
/**
* Refresh this table and the shared library together.
*
* The rail lists the same labels and the reference form picks from them, so a
* page that renames or deletes one has to reload the store as well or the two
* would disagree until the next navigation.
*/
async function refreshAll(): Promise<void> {
await Promise.all([load(), tagsStore.reload()])
}
async function onDelete(row: ReferenceTag): Promise<void> {
const warning = row.reference_count
? `「${row.name}」正被 ${row.reference_count} 篇文献使用,删除后这些文献会失去这个标签(文献本身不会删)。`
: `确定删除标签「${row.name}」?`
try {
await ElMessageBox.confirm(warning, '删除标签', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
})
} catch {
return // dismissed
}
try {
await deleteReferenceTag(row.id)
ElMessage.success('已删除标签')
await refreshAll()
} catch (error) {
ElMessage.error(errorMessage(error))
}
}
async function onBatchDelete(): Promise<void> {
const ids = selection.value.map((row) => row.id)
if (ids.length === 0) return
const labels = selection.value
.reduce((sum, row) => sum + row.reference_count, 0)
try {
await ElMessageBox.confirm(
`确定删除选中的 ${ids.length} 个标签?相关文献会失去这些标签(文献本身不会删)。` +
(labels ? `涉及 ${labels} 处标注。` : ''),
'批量删除',
{ type: 'warning', confirmButtonText: '删除', cancelButtonText: '取消' },
)
} catch {
return // dismissed
}
try {
const result = await batchDeleteReferenceTags(ids)
ElMessage.success(`已删除 ${result.deleted} 个标签`)
selection.value = []
await refreshAll()
} catch (error) {
ElMessage.error(errorMessage(error))
}
}
onMounted(async () => {
await load()
void tagsStore.reload()
})
</script>
<template>
<div class="page page--fill">
<el-card shadow="never" class="card--fill">
<template #header>
<div class="card-header">
<div>
<span class="card-title">标签库</span>
<span class="card-subtitle">
先在这里建好标签,再在文献里勾选;标签是给自己归档用的,不会出现在参考文献表里
</span>
</div>
<el-tag type="info" effect="plain">共 {{ total }} 个</el-tag>
</div>
</template>
<div class="toolbar">
<el-input
v-model="query.keyword"
placeholder="搜索标签名称或备注"
clearable
class="search"
@keyup.enter="search"
@clear="search"
>
<template #prefix><el-icon><Search /></el-icon></template>
</el-input>
<el-button @click="search">查询</el-button>
<div class="toolbar-spacer" />
<el-button :icon="Refresh" :loading="loading" @click="load">刷新</el-button>
<el-button
type="danger"
plain
:icon="Delete"
:disabled="selection.length === 0"
@click="onBatchDelete"
>
批量删除{{ selection.length ? `(${selection.length})` : '' }}
</el-button>
<el-button type="primary" :icon="Plus" @click="onCreate">新建标签</el-button>
</div>
<el-table
v-loading="loading"
:data="rows"
row-key="id"
stripe
class="table table-fill"
height="100%"
@selection-change="(value: ReferenceTag[]) => (selection = value)"
>
<el-table-column type="selection" width="46" reserve-selection />
<el-table-column label="标签" min-width="200">
<template #default="{ row }">
<TagChip :name="row.name" :color="row.color" size="default" />
</template>
</el-table-column>
<el-table-column label="备注" min-width="240" show-overflow-tooltip>
<template #default="{ row }">
<span v-if="row.note">{{ row.note }}</span>
<span v-else class="muted">—</span>
</template>
</el-table-column>
<el-table-column label="文献数" width="110" align="center">
<template #default="{ row }">
<RouterLink
v-if="row.reference_count"
:to="{ path: '/references', query: { tag: String(row.id) } }"
class="count-link"
>
{{ row.reference_count }} 篇
</RouterLink>
<span v-else class="muted">未使用</span>
</template>
</el-table-column>
<el-table-column label="更新时间" width="170">
<template #default="{ row }">{{ formatDateTime(row.updated_at) }}</template>
</el-table-column>
<el-table-column label="操作" width="150" fixed="right">
<template #default="{ row }">
<el-button link type="primary" :icon="Edit" @click="onEdit(row)">编辑</el-button>
<el-button link type="danger" :icon="Delete" @click="onDelete(row)">删除</el-button>
</template>
</el-table-column>
<template #empty>
<el-empty
:description="query.keyword ? '没有匹配的标签' : '还没有标签,先建一个再给文献打标'"
>
<el-button type="primary" :icon="Plus" @click="onCreate">新建标签</el-button>
</el-empty>
</template>
</el-table>
<div class="table-pagination">
<el-pagination
v-model:current-page="query.page"
v-model:page-size="query.page_size"
:total="total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
background
@current-change="load"
@size-change="search"
/>
</div>
</el-card>
<TagFormDialog v-model="dialogVisible" :tag="editing" @saved="refreshAll" />
</div>
</template>
<style scoped>
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.card-title {
font-weight: 600;
margin-right: 10px;
}
.card-subtitle {
color: var(--el-text-color-secondary);
font-size: 12px;
}
.search {
width: 240px;
}
.table {
margin-top: 16px;
}
.count-link {
color: var(--el-color-primary);
text-decoration: none;
}
.muted {
color: var(--el-text-color-placeholder);
}
</style>