Files
paper-doc/frontend/src/views/papers/PaperDetailView.vue
T
govin 340e0777ce frontend: print the 参考文献 block as entries, not quotes
The card listed the quoted passage where the bibliography entry belongs, under
a `引用 #53` tag that named a database id rather than the work. It now prints
the server's `references` verbatim — one line per work, already formatted in
Springer's basic style — with the fields a complete entry still lacks shown as
「缺 责任者、出版年」, and the paragraphs that cite it underneath:

    1 引言 / Introduction
    2.1 技术主题识别 / Technology Topic Identification ×2

Each of those links carries the quoted passages for that paragraph in its
tooltip, which is where the quote belongs: it is evidence for a citation, not
the citation itself. The client formats nothing and groups nothing but the
paragraph labels — a second implementation of "which work is number 7" would
eventually disagree with the first.

The marker tooltip in the text stops saying `引用 #53` too and shows the entry
it points at, so hovering a `[7]` answers the question the number raises.
2026-09-22 21:50:39 +08:00

585 lines
17 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">
/**
* One paper, read as a document.
*
* The page renders exactly what the server assembled — the template's
* paragraphs in position order, each carrying the sentences stored at its
* position — and refuses to do any of the assembling itself. That is the whole
* point of the feature: the shape of a paper is decided once, by its template,
* and cannot be re-derived differently on the client.
*
* Three states are visible and none of them is an error:
*
* * a **written paragraph** — heading, prose, numbered citation markers;
* * an **empty paragraph** — its heading and nothing else. Some positions in an
* outline are meant to stay empty, so a blank paragraph is printed blank
* rather than annotated;
* * an **unmatched paragraph** (未设定) — content whose position the current
* template does not define, kept in order rather than hidden, and recoverable
* by editing the paragraph or switching back.
*
* Every paragraph carries its own edit button, written or not: the editor is
* the only way content enters a paper, so it may never be the thing that is
* missing.
*/
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
ArrowLeft,
Delete,
Edit,
Refresh,
Switch,
Warning,
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { errorMessage } from '@/api/client'
import {
deletePaper,
getPaperDocument,
PAPER_STATUS_LABELS,
PAPER_STATUS_TAG,
referenceStyleLabel,
type PaperDocument,
type PaperParagraph,
type PaperReference,
} from '@/api/papers'
import PaperFormDialog from '@/components/papers/PaperFormDialog.vue'
import ParagraphEditDialog from '@/components/papers/ParagraphEditDialog.vue'
import TemplateSwitchDialog from '@/components/papers/TemplateSwitchDialog.vue'
import PaperParagraphView from '@/components/papers/PaperParagraph.vue'
import { usePapersStore } from '@/stores/papers'
import { PARAGRAPH_UNSET_LABEL, formatDateTime, splitKeywords } from '@/utils/format'
const route = useRoute()
const router = useRouter()
const papersStore = usePapersStore()
const loading = ref(false)
const paperDoc = ref<PaperDocument | null>(null)
const paragraphDialog = ref(false)
const editingSort = ref<number | null>(null)
const switchDialog = ref(false)
const formDialog = ref(false)
const paperId = computed(() => Number(route.params.id))
const paper = computed(() => paperDoc.value?.paper ?? null)
/** Citation id -> the number shown in the text, in reading order. */
const citationIndex = computed<Record<number, number>>(() => {
const map: Record<number, number> = {}
for (const citation of paperDoc.value?.citations ?? []) {
map[citation.id] = citation.index
}
return map
})
/**
* Reference id -> the formatted entry, for the marker tooltips.
*
* The tooltip on a `[7]` in the text should say what `[7]` *is*, and the entry
* is assembled once by the server — this is a lookup of that same string, not a
* second attempt at formatting it.
*/
const referenceText = computed<Record<number, string>>(() => {
const map: Record<number, string> = {}
for (const entry of paperDoc.value?.references ?? []) {
if (entry.reference_id != null && entry.text) {
map[entry.reference_id] = entry.text
}
}
return map
})
/** How the entries are printed, named rather than hard-coded in the view. */
const styleLabel = computed(() =>
referenceStyleLabel(paperDoc.value?.reference_style ?? 'springer'),
)
/**
* Where an entry is cited, one row per paragraph.
*
* The same work cited twice in one paragraph is one place with a count, not two
* identical links: what the reader needs from the list is where to look, and
* the quotes for that paragraph travel in the tooltip.
*/
function places(entry: PaperReference): { sort: number; name: string; quotes: string[] }[] {
const byParagraph = new Map<number, { sort: number; name: string; quotes: string[] }>()
for (const citation of entry.citations) {
const found = byParagraph.get(citation.paper_template_filed_sort)
if (found) {
found.quotes.push(citation.quote)
continue
}
byParagraph.set(citation.paper_template_filed_sort, {
sort: citation.paper_template_filed_sort,
name: citation.paragraph_name ?? PARAGRAPH_UNSET_LABEL,
quotes: [citation.quote],
})
}
return [...byParagraph.values()]
}
/** How much of the structure has been written, for the header read-out. */
const progress = computed(() => {
if (!paperDoc.value) return { written: 0, total: 0, unmatched: 0 }
const paragraphs = paperDoc.value.paragraphs
return {
written: paragraphs.filter((paragraph) => paragraph.sentences.length > 0).length,
total: paragraphs.filter((paragraph) => paragraph.matched).length,
unmatched: paragraphs.filter((paragraph) => !paragraph.matched).length,
}
})
/** Any 未设定 paragraph means content lost its heading in a template switch. */
const hasUnmatched = computed(() => progress.value.unmatched > 0)
async function load(): Promise<void> {
if (!Number.isFinite(paperId.value)) {
void router.replace('/papers')
return
}
loading.value = true
try {
paperDoc.value = await getPaperDocument(paperId.value)
} catch (error) {
ElMessage.error(errorMessage(error))
paperDoc.value = null
} finally {
loading.value = false
}
}
function openParagraph(paragraph: PaperParagraph): void {
editingSort.value = paragraph.paper_template_filed_sort
paragraphDialog.value = true
}
/** A paragraph write returns the refreshed document, so state is replaced. */
function onParagraphSaved(updated: PaperDocument): void {
paperDoc.value = updated
void papersStore.reload()
}
async function onPaperSaved(): Promise<void> {
await load()
await papersStore.reload()
}
async function onTemplateSwitched(): Promise<void> {
await onPaperSaved()
if (hasUnmatched.value) {
ElMessage.warning(
`有 ${progress.value.unmatched} 段内容在新模板里没有对应标题,已按顺序保留并显示为「${PARAGRAPH_UNSET_LABEL}」`,
)
}
}
async function onDelete(): Promise<void> {
const current = paper.value
if (!current) return
try {
await ElMessageBox.confirm(
`确定删除论文「${current.title}」?其中的 ${current.sentence_count} 句正文会一并删除。`,
'删除论文',
{ type: 'warning', confirmButtonText: '删除', cancelButtonText: '取消' },
)
} catch {
return // dismissed
}
try {
await deletePaper(current.id)
await papersStore.reload()
ElMessage.success('已删除')
void router.push('/papers')
} catch (error) {
ElMessage.error(errorMessage(error))
}
}
/** Scroll to a paragraph, so a citation can point back at its source. */
function scrollToParagraph(sort: number): void {
const element = document.getElementById(`paragraph-${sort}`)
element?.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
/** Reload when the route id changes, without remounting the view. */
watch(paperId, () => {
editingSort.value = null
void load()
})
onMounted(load)
</script>
<template>
<div v-loading="loading" class="page">
<el-empty v-if="!loading && !paper" description="论文不存在或已被删除">
<el-button type="primary" :icon="ArrowLeft" @click="router.push('/papers')">
返回论文列表
</el-button>
</el-empty>
<template v-else-if="paper">
<el-card shadow="never" class="head-card">
<div class="head">
<div class="head-main">
<div class="head-top">
<el-button link :icon="ArrowLeft" @click="router.push('/papers')">论文列表</el-button>
<el-divider direction="vertical" />
<el-tag size="small" :type="PAPER_STATUS_TAG[paper.status]" effect="plain">
{{ PAPER_STATUS_LABELS[paper.status] }}
</el-tag>
<el-tag v-if="paper.template_name" size="small" effect="plain" type="success">
模板:{{ paper.template_name }}
</el-tag>
<el-tag v-else size="small" effect="plain" type="warning">未选择模板</el-tag>
<span v-if="hasUnmatched" class="unmatched-hint">
<el-icon><Warning /></el-icon>
{{ progress.unmatched }} 段没有对应标题
</span>
</div>
<h1 class="head-title">{{ paper.title }}</h1>
<div class="head-meta">
<span v-if="paper.author">作者:{{ paper.author }}</span>
<span v-if="paper.target_journal">目标期刊:{{ paper.target_journal }}</span>
<span>
进度:{{ progress.written }} / {{ progress.total }} 段 ·
{{ paper.sentence_count }} 句
</span>
<span>更新于 {{ formatDateTime(paper.updated_at) }}</span>
</div>
<div v-if="splitKeywords(paper.keywords).length" class="head-keywords">
<el-tag
v-for="word in splitKeywords(paper.keywords)"
:key="word"
size="small"
type="info"
effect="plain"
>
{{ word }}
</el-tag>
</div>
</div>
<div class="head-actions">
<el-button :icon="Refresh" :loading="loading" @click="load">刷新</el-button>
<el-button :icon="Switch" @click="switchDialog = true">切换模板</el-button>
<el-button :icon="Edit" @click="formDialog = true">编辑信息</el-button>
<el-button type="danger" plain :icon="Delete" @click="onDelete">删除</el-button>
</div>
</div>
</el-card>
<el-alert
v-for="warning in paperDoc?.warnings ?? []"
:key="warning"
type="warning"
:closable="false"
show-icon
:title="warning"
/>
<el-card shadow="never" class="paper-card">
<template #header>
<div class="paper-head">
<span class="paper-title">正文</span>
<span class="paper-hint">
段落顺序完全来自模板的 sort;每个段落右侧都可以单独编辑
</span>
</div>
</template>
<el-empty
v-if="!paperDoc?.paragraphs.length"
description="这篇论文还没有结构,先选择一个模板"
>
<el-button type="primary" :icon="Switch" @click="switchDialog = true">
选择模板
</el-button>
</el-empty>
<div v-else class="sheet">
<PaperParagraphView
v-for="paragraph in paperDoc.paragraphs"
:key="paragraph.paper_template_filed_sort"
:paragraph="paragraph"
:citation-index="citationIndex"
:reference-text="referenceText"
@edit="openParagraph"
/>
</div>
</el-card>
<el-card v-if="paperDoc?.references.length" shadow="never">
<template #header>
<div class="paper-head">
<span class="paper-title">参考文献</span>
<span class="paper-hint">
按正文首次出现顺序编号,同一篇文献在多处引用共用同一个序号;条目按 {{ styleLabel }} 排版
</span>
</div>
</template>
<ol class="references">
<li v-for="entry in paperDoc.references" :key="entry.index" class="reference">
<span class="reference-index">[{{ entry.index }}]</span>
<div class="reference-body">
<!-- The entry as it prints. Whatever the library is missing shows
up as a shorter entry, so the gap is named underneath rather
than quietly filled in. -->
<p v-if="entry.text" class="reference-text">{{ entry.text }}</p>
<p v-else class="reference-text reference-text--unlinked">
这处引用还没有关联文献库条目,只有引用内容。
</p>
<div class="reference-meta">
<el-tag
v-if="entry.missing.length"
type="warning"
size="small"
effect="plain"
>
缺 {{ entry.missing.join('、') }}
</el-tag>
<el-tag v-if="!entry.reference_id" type="warning" size="small" effect="plain">
未关联文献
</el-tag>
<span class="reference-places">
<span class="reference-places-label">引用位置</span>
<el-tooltip
v-for="place in places(entry)"
:key="place.sort"
placement="top"
:show-after="200"
>
<template #content>
<div class="place-tip">
<div v-for="(quote, index) in place.quotes" :key="index">
「{{ quote }}」
</div>
</div>
</template>
<el-button link type="primary" @click="scrollToParagraph(place.sort)">
{{ place.name }}
<span v-if="place.quotes.length > 1" class="place-count">
×{{ place.quotes.length }}
</span>
</el-button>
</el-tooltip>
</span>
</div>
</div>
</li>
</ol>
</el-card>
<ParagraphEditDialog
v-model="paragraphDialog"
:paper-id="paper.id"
:field-sort="editingSort"
:paragraphs="paperDoc?.paragraphs ?? []"
@saved="onParagraphSaved"
/>
<TemplateSwitchDialog
v-if="paperDoc"
v-model="switchDialog"
:paper="paper"
:paragraphs="paperDoc.paragraphs"
@saved="onTemplateSwitched"
/>
<PaperFormDialog
v-model="formDialog"
:paper="paper"
@saved="onPaperSaved"
/>
</template>
</div>
</template>
<style scoped>
.head-card :deep(.el-card__body) {
padding-bottom: 16px;
}
.head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
flex-wrap: wrap;
}
.head-main {
min-width: 0;
flex: 1 1 420px;
}
.head-top {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
}
.unmatched-hint {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 12px;
color: var(--el-color-warning);
}
.head-title {
margin: 8px 0 6px;
font-size: 22px;
font-weight: 650;
line-height: 1.4;
overflow-wrap: anywhere;
}
.head-meta {
display: flex;
align-items: center;
gap: 16px;
flex-wrap: wrap;
font-size: 13px;
color: var(--el-text-color-secondary);
}
.head-keywords {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 8px;
}
.head-actions {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
flex: 0 0 auto;
}
.paper-head {
display: flex;
align-items: baseline;
gap: 10px;
flex-wrap: wrap;
}
.paper-title {
font-weight: 600;
}
.paper-hint {
font-size: 12px;
color: var(--el-text-color-secondary);
}
/* The writing surface: a sheet, not a table — the paper's own typography is
what the template describes, so the container stays out of the way.
*
* Full width, deliberately. A centred, capped text column reads well in a
* document viewer, but this page is also the editing surface: every paragraph
* carries its own heading and its edit button, and those belong on the panel's
* edges. Capped at 900px they floated in the middle of a wide screen with
* several hundred pixels of nothing on either side, which looks like a broken
* layout rather than a considered measure. */
.sheet {
display: flex;
flex-direction: column;
min-width: 0;
}
.references {
margin: 0;
padding: 0;
list-style: none;
display: flex;
flex-direction: column;
gap: 10px;
}
.reference {
display: flex;
gap: 10px;
align-items: flex-start;
}
.reference-index {
flex: 0 0 auto;
min-width: 34px;
font-variant-numeric: tabular-nums;
color: var(--el-color-primary);
}
.reference-body {
min-width: 0;
flex: 1 1 auto;
}
/* The entry, printed as the style wrote it: one line of running text, hanging
under its number like a bibliography. */
.reference-text {
margin: 0;
font-size: 13px;
line-height: 1.8;
color: var(--el-text-color-regular);
overflow-wrap: anywhere;
}
.reference-text--unlinked {
color: var(--el-color-warning);
}
.reference-meta {
display: flex;
align-items: center;
gap: 8px;
margin-top: 4px;
flex-wrap: wrap;
}
.reference-places {
display: inline-flex;
align-items: center;
gap: 2px;
flex-wrap: wrap;
font-size: 12px;
color: var(--el-text-color-secondary);
}
.reference-places-label {
margin-right: 2px;
color: var(--el-text-color-placeholder);
}
.place-count {
margin-left: 2px;
color: var(--el-text-color-placeholder);
font-variant-numeric: tabular-nums;
}
.place-tip {
display: flex;
flex-direction: column;
gap: 4px;
max-width: 420px;
line-height: 1.7;
}
</style>