frontend: add the paper list, document view, and paragraph editor
The 论文 menu now carries the papers themselves, read from a pinia store, so a paper opens straight from the rail; every page that changes the list reloads that store, and a filter box appears once there are more than six. PapersView is the library — search, create, edit, open, single and batch delete. PaperDetailView is the writing surface: it renders the server's document verbatim, with an edit button beside every paragraph (written or not, because content only ever enters a paper through the editor), citation markers numbered in reading order to match the 参考文献 list, and a 切换模板 dialog that previews the impact before re-shaping the document. The paragraph editor shows one paragraph as the paper will read it — every sentence on its own line, in sort order, each editable, each able to carry citations — and writes the paragraph back whole. Whitespace-only lines are dropped on save; a citation with an empty 引用内容 is refused; and 所属段落 moves the paragraph to another position, appended after what is already there.
This commit is contained in:
@@ -0,0 +1,576 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* The paragraph editor — the writer's unit of work.
|
||||
*
|
||||
* It shows one paragraph the way the paper will read it: every sentence of the
|
||||
* paragraph on its own line, in `sort` order, each one editable, each one able
|
||||
* to carry citations. Saving writes the paragraph back whole, so what is on
|
||||
* screen and what is stored cannot drift apart.
|
||||
*
|
||||
* Three rules are stated in the UI because they are the ones a writer would
|
||||
* otherwise have to guess:
|
||||
*
|
||||
* * **order comes from `sort`, not from the screen.** Rows are displayed in
|
||||
* ascending `sort`, so editing a number moves the line — which is the point:
|
||||
* the paragraph is reassembled from these numbers, and nothing else.
|
||||
* * **a citation must quote something.** A citation with an empty 引用内容 is
|
||||
* refused here (and by the API), because a bare id is not usable in a text.
|
||||
* * **whitespace-only lines are dropped on save.** Adding a line and leaving it
|
||||
* blank is a normal way to type, not a request for an empty sentence.
|
||||
*
|
||||
* The optional 「所属段落」 select moves the whole paragraph to another position,
|
||||
* appending after whatever that paragraph already holds. That is the manual
|
||||
* form of what switching the paper's template does by itself.
|
||||
*/
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { ArrowDown, ArrowUp, Delete, Plus, Refresh } from '@element-plus/icons-vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
import { errorMessage } from '@/api/client'
|
||||
import {
|
||||
getParagraph,
|
||||
replaceParagraph,
|
||||
type PaperDocument,
|
||||
type PaperParagraph,
|
||||
type SentenceInput,
|
||||
} from '@/api/papers'
|
||||
import { PARAGRAPH_UNSET_LABEL, foldWhitespace } from '@/utils/format'
|
||||
|
||||
const props = defineProps<{
|
||||
/** Visibility, used with `v-model`. */
|
||||
modelValue: boolean
|
||||
paperId: number
|
||||
/** The paragraph being edited, identified by the position it occupies. */
|
||||
fieldSort: number | null
|
||||
/** Every paragraph of the document, for the 「所属段落」 select. */
|
||||
paragraphs: PaperParagraph[]
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
'update:modelValue': [value: boolean]
|
||||
/** Emitted with the refreshed document after a successful write. */
|
||||
saved: [document: PaperDocument]
|
||||
}>()
|
||||
|
||||
/** One citation line in the editor. */
|
||||
interface CitationDraft {
|
||||
/** Local identity for `v-for`; not sent to the server. */
|
||||
key: number
|
||||
reference_id: number | null
|
||||
quote: string
|
||||
}
|
||||
|
||||
/** One sentence line in the editor. `sort` is always set — see the template. */
|
||||
interface SentenceDraft {
|
||||
key: number
|
||||
sort: number
|
||||
content: string
|
||||
citations: CitationDraft[]
|
||||
}
|
||||
|
||||
const visible = computed({
|
||||
get: () => props.modelValue,
|
||||
set: (value: boolean) => emit('update:modelValue', value),
|
||||
})
|
||||
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const heading = ref<PaperParagraph | null>(null)
|
||||
const rows = ref<SentenceDraft[]>([])
|
||||
const targetSort = ref<number | null>(null)
|
||||
|
||||
/** Keys only ever count up, so reopening the dialog cannot collide with itself. */
|
||||
let nextKey = 1
|
||||
|
||||
/** The paragraph as the reader will meet it: ascending `sort`, ties by insertion. */
|
||||
const orderedRows = computed(() =>
|
||||
[...rows.value].sort((a, b) => a.sort - b.sort || a.key - b.key),
|
||||
)
|
||||
|
||||
/** Where the paragraph sits now, and where it could go. */
|
||||
const currentSort = computed(() => props.fieldSort ?? 0)
|
||||
|
||||
const moveOptions = computed(() =>
|
||||
[...props.paragraphs]
|
||||
.sort((a, b) => a.paper_template_filed_sort - b.paper_template_filed_sort)
|
||||
.map((paragraph) => ({
|
||||
value: paragraph.paper_template_filed_sort,
|
||||
label: `#${paragraph.paper_template_filed_sort} · ${
|
||||
paragraph.name ?? PARAGRAPH_UNSET_LABEL
|
||||
}`,
|
||||
count: paragraph.sentences.length,
|
||||
})),
|
||||
)
|
||||
|
||||
/** The target paragraph, when the writer has chosen a different one. */
|
||||
const moveTarget = computed(() => {
|
||||
if (targetSort.value == null || targetSort.value === currentSort.value) return null
|
||||
return (
|
||||
props.paragraphs.find(
|
||||
(paragraph) => paragraph.paper_template_filed_sort === targetSort.value,
|
||||
) ?? null
|
||||
)
|
||||
})
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
async (open) => {
|
||||
if (!open || props.fieldSort == null) return
|
||||
|
||||
rows.value = []
|
||||
heading.value = null
|
||||
targetSort.value = props.fieldSort
|
||||
loading.value = true
|
||||
|
||||
try {
|
||||
// Read the paragraph from the server rather than from the caller's copy:
|
||||
// the editor must never write back a stale version of the paragraph.
|
||||
const detail = await getParagraph(props.paperId, props.fieldSort)
|
||||
heading.value = detail.paragraph
|
||||
rows.value = detail.paragraph.sentences.map((sentence) => ({
|
||||
key: nextKey++,
|
||||
sort: sentence.sort,
|
||||
content: sentence.content,
|
||||
citations: sentence.citations.map((citation) => ({
|
||||
key: nextKey++,
|
||||
reference_id: citation.reference_id,
|
||||
quote: citation.quote,
|
||||
})),
|
||||
}))
|
||||
} catch (error) {
|
||||
ElMessage.error(errorMessage(error))
|
||||
visible.value = false
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
/** The next free sort, so an added line lands at the end. */
|
||||
function nextSort(): number {
|
||||
return rows.value.reduce((max, row) => Math.max(max, row.sort), 0) + 1
|
||||
}
|
||||
|
||||
function addSentence(): void {
|
||||
rows.value.push({ key: nextKey++, sort: nextSort(), content: '', citations: [] })
|
||||
}
|
||||
|
||||
function removeSentence(key: number): void {
|
||||
rows.value = rows.value.filter((row) => row.key !== key)
|
||||
}
|
||||
|
||||
function addCitation(row: SentenceDraft): void {
|
||||
row.citations.push({ key: nextKey++, reference_id: null, quote: '' })
|
||||
}
|
||||
|
||||
function removeCitation(row: SentenceDraft, key: number): void {
|
||||
row.citations = row.citations.filter((citation) => citation.key !== key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a line one step, by swapping `sort` with its neighbour.
|
||||
*
|
||||
* Only the two values involved change, so a deliberately sparse numbering
|
||||
* (10, 20, 30) survives a nudge. Equal values would sit still after a plain
|
||||
* swap, so in that case the moving row is nudged past its neighbour instead.
|
||||
*/
|
||||
function swapOrder(index: number, delta: number): void {
|
||||
const ordered = orderedRows.value
|
||||
const target = index + delta
|
||||
if (target < 0 || target >= ordered.length) return
|
||||
|
||||
const moving = ordered[index]!
|
||||
const other = ordered[target]!
|
||||
const movingSort = moving.sort
|
||||
|
||||
if (movingSort === other.sort) {
|
||||
moving.sort = delta < 0 ? other.sort - 1 : other.sort + 1
|
||||
return
|
||||
}
|
||||
moving.sort = other.sort
|
||||
other.sort = movingSort
|
||||
}
|
||||
|
||||
/** Renumber the lines 1..N in the order they are displayed. */
|
||||
function autoNumber(): void {
|
||||
orderedRows.value.forEach((row, index) => {
|
||||
row.sort = index + 1
|
||||
})
|
||||
}
|
||||
|
||||
/** The first reason the paragraph cannot be saved, or `null`. */
|
||||
function firstProblem(): string | null {
|
||||
for (const [index, row] of orderedRows.value.entries()) {
|
||||
if (row.content.trim() && !Number.isFinite(row.sort)) {
|
||||
return `第 ${index + 1} 句的 sort 无效`
|
||||
}
|
||||
for (const citation of row.citations) {
|
||||
if (citation.reference_id != null && citation.reference_id < 1) {
|
||||
return `第 ${index + 1} 句的引用 id 必须是正整数`
|
||||
}
|
||||
// The one hard rule: a citation must say what it quotes.
|
||||
if (!citation.quote.trim()) {
|
||||
return `第 ${index + 1} 句的引用内容不能为空(引用必须写明引用的内容)`
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
if (props.fieldSort == null) return
|
||||
|
||||
const problem = firstProblem()
|
||||
if (problem) {
|
||||
ElMessage.warning(problem)
|
||||
return
|
||||
}
|
||||
|
||||
// Whitespace-only lines are dropped: a line the writer added and left blank
|
||||
// carries nothing, and keeping it would put an empty sentence in the paper.
|
||||
const sentences: SentenceInput[] = orderedRows.value
|
||||
.filter(
|
||||
(row) =>
|
||||
row.content.trim().length > 0 ||
|
||||
row.citations.some((citation) => citation.quote.trim().length > 0),
|
||||
)
|
||||
.map((row) => ({
|
||||
sort: row.sort,
|
||||
content: foldWhitespace(row.content),
|
||||
citations: row.citations
|
||||
.filter((citation) => citation.quote.trim().length > 0)
|
||||
.map((citation) => ({
|
||||
reference_id: citation.reference_id ?? null,
|
||||
quote: citation.quote.trim(),
|
||||
})),
|
||||
}))
|
||||
|
||||
saving.value = true
|
||||
try {
|
||||
const document_ = await replaceParagraph(props.paperId, props.fieldSort, {
|
||||
sentences,
|
||||
target_sort:
|
||||
targetSort.value != null && targetSort.value !== props.fieldSort
|
||||
? targetSort.value
|
||||
: null,
|
||||
})
|
||||
ElMessage.success(
|
||||
targetSort.value !== props.fieldSort ? '段落已保存并移动到新位置' : '段落已保存',
|
||||
)
|
||||
emit('saved', document_)
|
||||
visible.value = false
|
||||
} catch (error) {
|
||||
ElMessage.error(errorMessage(error))
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-dialog
|
||||
v-model="visible"
|
||||
width="min(860px, 94vw)"
|
||||
top="6vh"
|
||||
:close-on-click-modal="false"
|
||||
append-to-body
|
||||
destroy-on-close
|
||||
>
|
||||
<template #header>
|
||||
<div class="dialog-header">
|
||||
<span class="dialog-title">编辑段落</span>
|
||||
<span v-if="heading" class="dialog-subject">
|
||||
<span v-if="heading.name">{{ heading.name }}</span>
|
||||
<span v-else class="unset">{{ PARAGRAPH_UNSET_LABEL }}</span>
|
||||
<el-tag size="small" effect="plain" type="info">
|
||||
sort = {{ heading.paper_template_filed_sort }}
|
||||
</el-tag>
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div v-loading="loading" class="editor">
|
||||
<el-alert
|
||||
v-if="heading && !heading.matched"
|
||||
type="warning"
|
||||
:closable="false"
|
||||
show-icon
|
||||
title="当前模板在这个位置上没有段落"
|
||||
description="内容会照常保存和显示,标题显示为「未设定」。切回原来的模板,标题就会恢复。"
|
||||
/>
|
||||
|
||||
<div class="editor-meta">
|
||||
<span class="meta-label">所属段落</span>
|
||||
<el-select v-model="targetSort" size="small" class="meta-select">
|
||||
<el-option
|
||||
v-for="option in moveOptions"
|
||||
:key="option.value"
|
||||
:value="option.value"
|
||||
:label="option.label"
|
||||
>
|
||||
<span>{{ option.label }}</span>
|
||||
<span class="option-hint">
|
||||
{{ option.value === currentSort ? '当前' : `${option.count} 句` }}
|
||||
</span>
|
||||
</el-option>
|
||||
</el-select>
|
||||
|
||||
<span class="meta-hint">
|
||||
换段落会把这一段整体搬过去,追加到目标段落已有句子之后。
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<el-alert
|
||||
v-if="moveTarget"
|
||||
type="warning"
|
||||
:closable="false"
|
||||
show-icon
|
||||
:title="`目标段落「${moveTarget.name ?? PARAGRAPH_UNSET_LABEL}」已有 ${moveTarget.sentences.length} 句`"
|
||||
description="本段的句子会追加在它们之后。"
|
||||
/>
|
||||
|
||||
<div class="editor-toolbar">
|
||||
<span class="toolbar-title">共 {{ orderedRows.length }} 句</span>
|
||||
<span class="toolbar-hint">按 sort 升序拼成这个段落</span>
|
||||
<div class="toolbar-spacer" />
|
||||
<el-button size="small" :icon="Refresh" :disabled="!orderedRows.length" @click="autoNumber">
|
||||
自动编号
|
||||
</el-button>
|
||||
<el-button size="small" type="primary" plain :icon="Plus" @click="addSentence">
|
||||
新增一句
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<div class="rows">
|
||||
<el-empty
|
||||
v-if="orderedRows.length === 0"
|
||||
:image-size="60"
|
||||
description="这一段还没有内容,点「新增一句」开始写"
|
||||
/>
|
||||
|
||||
<article v-for="(row, index) in orderedRows" :key="row.key" class="row">
|
||||
<header class="row-head">
|
||||
<span class="row-index">第 {{ index + 1 }} 句</span>
|
||||
<el-input-number
|
||||
v-model="row.sort"
|
||||
size="small"
|
||||
controls-position="right"
|
||||
:min="-9999"
|
||||
:max="9999"
|
||||
class="sort-input"
|
||||
/>
|
||||
<div class="toolbar-spacer" />
|
||||
<el-button
|
||||
link
|
||||
:icon="ArrowUp"
|
||||
title="上移"
|
||||
:disabled="index === 0"
|
||||
@click="swapOrder(index, -1)"
|
||||
/>
|
||||
<el-button
|
||||
link
|
||||
:icon="ArrowDown"
|
||||
title="下移"
|
||||
:disabled="index === orderedRows.length - 1"
|
||||
@click="swapOrder(index, 1)"
|
||||
/>
|
||||
<el-button link type="danger" :icon="Delete" title="删除这一句" @click="removeSentence(row.key)" />
|
||||
</header>
|
||||
|
||||
<el-input
|
||||
v-model="row.content"
|
||||
type="textarea"
|
||||
:autosize="{ minRows: 1, maxRows: 6 }"
|
||||
placeholder="这一句的内容(只留空白的句子保存时会删除)"
|
||||
/>
|
||||
|
||||
<div class="citations">
|
||||
<div v-for="citation in row.citations" :key="citation.key" class="citation">
|
||||
<el-input-number
|
||||
v-model="citation.reference_id"
|
||||
size="small"
|
||||
controls-position="right"
|
||||
:min="1"
|
||||
:max="999999"
|
||||
placeholder="引用 id"
|
||||
class="cite-id"
|
||||
/>
|
||||
<el-input
|
||||
v-model="citation.quote"
|
||||
size="small"
|
||||
type="textarea"
|
||||
:autosize="{ minRows: 1, maxRows: 4 }"
|
||||
placeholder="引用内容(必填)"
|
||||
class="cite-quote"
|
||||
/>
|
||||
<el-button
|
||||
link
|
||||
type="danger"
|
||||
:icon="Delete"
|
||||
title="删除这条引用"
|
||||
@click="removeCitation(row, citation.key)"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<el-button link type="primary" :icon="Plus" @click="addCitation(row)">
|
||||
添加引用
|
||||
</el-button>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
<span class="footer-hint">空段落会保留结构,在正文里显示为空白。</span>
|
||||
<el-button @click="visible = false">取消</el-button>
|
||||
<el-button type="primary" :loading="saving" @click="submit">保存段落</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.dialog-header {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 12px;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.dialog-title {
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.dialog-subject {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
color: var(--el-text-color-regular);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.unset {
|
||||
color: var(--el-color-warning);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.editor {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
min-height: 200px;
|
||||
}
|
||||
|
||||
.editor-meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.meta-label {
|
||||
font-size: 13px;
|
||||
color: var(--el-text-color-regular);
|
||||
}
|
||||
|
||||
.meta-select {
|
||||
width: 240px;
|
||||
}
|
||||
|
||||
.meta-hint {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.option-hint {
|
||||
float: right;
|
||||
color: var(--el-text-color-placeholder);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.editor-toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.toolbar-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.toolbar-hint {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.toolbar-spacer {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.rows {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
max-height: 52vh;
|
||||
overflow-y: auto;
|
||||
scrollbar-width: thin;
|
||||
padding-right: 4px;
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--el-border-color);
|
||||
border-radius: 8px;
|
||||
background-color: var(--el-fill-color-blank);
|
||||
}
|
||||
|
||||
.row-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.row-index {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
}
|
||||
|
||||
.sort-input {
|
||||
width: 104px;
|
||||
}
|
||||
|
||||
.citations {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
padding-left: 12px;
|
||||
border-left: 2px solid var(--el-border-color-lighter);
|
||||
}
|
||||
|
||||
.citation {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.cite-id {
|
||||
width: 130px;
|
||||
flex: 0 0 auto;
|
||||
}
|
||||
|
||||
.cite-quote {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.footer-hint {
|
||||
float: left;
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
line-height: 32px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user