feat: 按标签复习页以已有导图列表为主入口

This commit is contained in:
2026-10-22 22:01:10 +08:00
parent 0d0b0c2c47
commit f8e1a970aa
2 changed files with 255 additions and 34 deletions
+89 -3
View File
@@ -1,15 +1,18 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import { createMemoryHistory, createRouter } from 'vue-router'
import TagReview from '@/components/TagReview.vue'
import { queryTagMindMap, listTagRecallRecords } from '@/api/tagMindMap'
import { queryTagMindMap, listTagRecallRecords, listTagMindMaps } from '@/api/tagMindMap'
import { listTagCandidates } from '@/api/markers'
import { pageOf } from '../support/page'
import { submitAiTask } from '@/api/aiTasks'
vi.mock('@/api/tagMindMap', () => ({
queryTagMindMap: vi.fn(),
updateTagMindMap: vi.fn(),
listTagRecallRecords: vi.fn(),
listTagMindMaps: vi.fn(),
}))
vi.mock('@/api/markers', () => ({
@@ -42,9 +45,27 @@ const tagMap = {
sourceFragmentCount: 3,
}
const mountTagReview = async () => {
const listSummary = {
id: 1,
scopeKey: 'SCOPE_1',
tags: ['英语'],
mode: 'UNION',
title: '英语',
generator: 'AI',
sourceFragmentCount: 3,
needsUpdate: false,
generatedTime: '2026-10-10T10:00:00',
}
const mountTagReview = async (query: Record<string, string | string[]> = {}) => {
const router = createRouter({
history: createMemoryHistory(),
routes: [{ path: '/review/by-tag', component: TagReview }],
})
await router.push({ path: '/review/by-tag', query })
await router.isReady()
const wrapper = mount(TagReview, {
global: { plugins: [ElementPlus], stubs: { MindMapViewer: mindMapStub } },
global: { plugins: [ElementPlus, router], stubs: { MindMapViewer: mindMapStub } },
})
await flushPromises()
return wrapper
@@ -60,6 +81,7 @@ beforeEach(() => {
vi.clearAllMocks()
recallOutline = '- 根主题'
vi.mocked(listTagCandidates).mockResolvedValue({ code: 200, data: ['英语', '数学'] } as never)
vi.mocked(listTagMindMaps).mockResolvedValue({ code: 200, data: pageOf([]) } as never)
vi.mocked(listTagRecallRecords).mockResolvedValue({ code: 200, data: [] } as never)
vi.mocked(submitAiTask).mockResolvedValue({ code: 201, data: { taskNum: 'AI1', status: 'PENDING' } } as never)
})
@@ -86,6 +108,51 @@ describe('TagReview 按标签复习(闭环-04)', () => {
wrapper.unmount()
})
it('置顶展示已生成过导图的标签集合,点卡片直接进入复习', async () => {
vi.mocked(listTagMindMaps).mockResolvedValue({
code: 200,
data: pageOf([{ ...listSummary, needsUpdate: true }]),
} as never)
vi.mocked(queryTagMindMap).mockResolvedValue({
code: 200,
data: { scopeKey: 'SCOPE_1', tags: ['英语'], mode: 'UNION', map: tagMap, latestRecallRecord: null },
} as never)
const wrapper = await mountTagReview()
expect(wrapper.text()).toContain('已生成的标签导图')
expect(wrapper.text()).toContain('有新碎片待整理')
const card = wrapper.find('.map-row')
expect(card.exists()).toBe(true)
await card.trigger('click')
await flushPromises()
expect(queryTagMindMap).toHaveBeenCalledWith(['英语'], 'UNION')
expect(wrapper.text()).toContain('回忆对比')
wrapper.unmount()
})
it('行内「补充新碎片」按该导图的标签与模式提交增量任务', async () => {
vi.mocked(listTagMindMaps).mockResolvedValue({
code: 200,
data: pageOf([{ ...listSummary, needsUpdate: true }]),
} as never)
const wrapper = await mountTagReview()
vi.mocked(submitAiTask).mockClear()
const supplement = wrapper.findAll('button').find((b) => b.text().includes('补充新碎片'))
await supplement!.trigger('click')
await flushPromises()
expect(submitAiTask).toHaveBeenCalledWith({
type: 'TAG_MIND_MAP',
targetNum: 'SCOPE_1',
params: { tags: ['英语'], combineMode: 'UNION', mode: 'incremental' },
})
wrapper.unmount()
})
it('生成导图走 TAG_MIND_MAP 异步任务,targetNum 用范围键', async () => {
vi.mocked(queryTagMindMap).mockResolvedValue({
code: 200,
@@ -188,6 +255,25 @@ describe('TagReview 按标签复习(闭环-04)', () => {
wrapper.unmount()
})
it('从复习总览深链进来:URL 里的标签集合与取数模式直接开始复习', async () => {
vi.mocked(queryTagMindMap).mockResolvedValue({
code: 200,
data: { scopeKey: 'SCOPE_1', tags: ['英语'], mode: 'UNION', map: tagMap, latestRecallRecord: null },
} as never)
const wrapper = await mountTagReview({ tag: '英语', mode: 'UNION' })
expect(queryTagMindMap).toHaveBeenCalledWith(['英语'], 'UNION')
expect(wrapper.text()).toContain('回忆对比')
wrapper.unmount()
})
it('没有 URL 范围时不自动查询', async () => {
const wrapper = await mountTagReview()
expect(queryTagMindMap).not.toHaveBeenCalled()
wrapper.unmount()
})
it('回忆内容只有一个节点时拒绝提交,补全后才提交对比任务', async () => {
vi.mocked(queryTagMindMap).mockResolvedValue({
code: 200,
+166 -31
View File
@@ -1,28 +1,37 @@
<script setup lang="ts">
import { computed, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import MindMapViewer, { type MindMapTreeNode } from "@/components/MindMapViewer.vue";
import MindMapRegenerateDialog, {
type MindMapRegenerateMode,
} from "@/components/MindMapRegenerateDialog.vue";
import TagMindMapList from "@/components/TagMindMapList.vue";
import {
listTagMindMaps,
listTagRecallRecords,
queryTagMindMap,
updateTagMindMap,
type TagCombineMode,
type TagMindMapSummary,
type TagMindMapView,
type TagRecallRecord,
} from "@/api/tagMindMap";
import { listTagCandidates } from "@/api/markers";
import { formatDateTime } from "@/utils/format";
import { isAiTaskCancelled, useAiTask } from "@/components/composables/useAiTask";
import { useReviewDuration } from "@/components/composables/useReviewDuration";
/**
* 按标签复习(闭环-04):选一个或多个标签 → 聚合成标签导图 → 走既有的回忆对比链路。
* 与按任务复习并存:任务导图仍在 ReviewRecall 里,这里只处理「标签集合 + 并集/交集」这一半。
*
* 主入口是「已生成的标签导图」列表:导图生成后标签集合一般不再变化,
* 用户通常先挑一张已有导图进去复习,或把新碎片补充进去;手动选标签新建范围是次要入口。
*/
const candidates = ref<string[]>([]);
const route = useRoute();
const selectedTags = ref<string[]>([]);
const combineMode = ref<TagCombineMode>("UNION");
const view = ref<TagMindMapView | null>(null);
@@ -30,6 +39,19 @@ const records = ref<TagRecallRecord[]>([]);
const lastResult = ref<any>(null);
const currentDurationSeconds = ref(0);
/** 已有导图列表:先让用户看到已经生成过导图的标签集合,不必先选标签再猜有没有导图 */
const maps = ref<TagMindMapSummary[]>([]);
const loadingMaps = ref(false);
const mapsLoadFailed = ref(false);
/** 列表已分页:导图多起来时不再一次拉全量 */
const MAP_PAGE_SIZE = 20;
const mapsPage = ref(1);
const mapsTotal = ref(0);
/** 行内「补充新碎片」正在生成的导图范围键 */
const supplementingKey = ref<string | null>(null);
/** 手动新建范围的展开态:还没有任何已有导图时默认展开 */
const newScopeOpen = ref(false);
const busy = ref(false);
const saving = ref(false);
const editing = ref(false);
@@ -115,6 +137,26 @@ const loadCandidates = async () => {
}
};
/** 拉取已有导图列表(分页):待更新优先、其次按生成时间倒序,由后端保证顺序 */
const loadMaps = async (page = mapsPage.value) => {
loadingMaps.value = true;
mapsLoadFailed.value = false;
try {
const res = await listTagMindMaps(page, MAP_PAGE_SIZE);
const data = res?.data;
mapsPage.value = data?.current || page;
mapsTotal.value = data?.total || 0;
maps.value = data?.records || [];
// 还没有任何已有导图时把手动新建范围展开,避免页面看起来无事可做
if (mapsTotal.value === 0) newScopeOpen.value = true;
} catch {
mapsLoadFailed.value = true;
newScopeOpen.value = true;
} finally {
loadingMaps.value = false;
}
};
const loadRecords = async () => {
if (selectedTags.value.length === 0) {
records.value = [];
@@ -181,6 +223,7 @@ const generateMap = async (mode: "full" | "incremental" | "ensure" = "ensure") =
mode,
});
await refreshMap();
await loadMaps();
recallTree.value = buildEmptyRecallTree();
ElMessage.success(mode === "incremental" ? "新碎片已整理进标签导图" : "标签导图已生成");
} catch (e) {
@@ -189,6 +232,41 @@ const generateMap = async (mode: "full" | "incremental" | "ensure" = "ensure") =
}
};
/** 点已有导图卡片:回填它的标签与取数模式后直接进入复习 */
const openMap = async (item: TagMindMapSummary) => {
selectedTags.value = [...item.tags];
combineMode.value = item.mode;
await startReview();
};
/**
* 行内「补充新碎片」:按该导图的标签与模式做增量生成,把新碎片整理进现有结构。
* 这是比「全量重建」更常用的操作,所以直接放在列表行内,不必先进入导图再点重新生成。
*/
const supplementMap = async (item: TagMindMapSummary) => {
if (supplementingKey.value) return;
supplementingKey.value = item.scopeKey;
try {
await mindMapTask.run("TAG_MIND_MAP", item.scopeKey, {
tags: item.tags,
combineMode: item.mode,
mode: "incremental",
});
ElMessage.success("新碎片已整理进标签导图");
await loadMaps();
// 正在看这张导图时同步刷新,避免列表与下方内容不一致
if (view.value?.scopeKey === item.scopeKey) {
await refreshMap();
recallTree.value = buildEmptyRecallTree();
}
} catch (e) {
if (isAiTaskCancelled(e)) return;
ElMessage.error(errorMessageOf(e, "补充新碎片失败,请稍后重试"));
} finally {
supplementingKey.value = null;
}
};
// 重新生成入口(防抖):导图已存在时先弹窗让用户选「整理进现有导图 / 全量重建」
const handleRegenerate = () => {
if (generating.value) return;
@@ -259,43 +337,96 @@ const resetRecall = () => {
recallTree.value = buildEmptyRecallTree();
};
onMounted(() => {
/** 从复习总览的「按标签复习」模块进来时,用 URL 里的标签集合与取数模式直接开始复习(深链) */
const applyRouteScope = async () => {
const rawTag = route.query.tag;
const tags = (Array.isArray(rawTag) ? rawTag : rawTag ? [rawTag] : [])
.map((item) => String(item))
.filter(Boolean);
if (tags.length === 0) return;
selectedTags.value = tags;
combineMode.value = route.query.mode === "INTERSECT" ? "INTERSECT" : "UNION";
await startReview();
};
onMounted(async () => {
void loadCandidates();
await loadMaps();
// 从复习总览点已有导图进来:URL 带范围(tag 可重复、mode),直接进入复习
await applyRouteScope();
});
</script>
<template>
<section class="tag-review-page">
<article class="surface-card scope-card" v-loading="busy">
<h3>按标签复习</h3>
<p class="scope-hint">
选一个或多个标签,把带这些标签的知识碎片聚合成一张导图,然后凭记忆重绘它。
标签导图与按任务复习并存,互不替代。
<article class="surface-card maps-card">
<div class="card-header">
<h3>已生成的标签导图</h3>
</div>
<p class="panel-hint">
直接选一张已有导图进入复习,或把新碎片补充进去。
</p>
<div class="scope-field">
<span class="scope-label">标签</span>
<el-select
v-model="selectedTags"
class="scope-select"
multiple
:multiple-limit="10"
filterable
allow-create
default-first-option
placeholder="选择或输入标签"
>
<el-option v-for="name in candidates" :key="name" :label="name" :value="name" />
</el-select>
<TagMindMapList
:maps="maps"
:loading="loadingMaps"
:failed="mapsLoadFailed"
empty-text="还没有生成过标签导图,先在下面新建一个标签范围"
:supplementing-key="supplementingKey"
:active-key="view?.scopeKey || ''"
@open="openMap"
@supplement="supplementMap"
@retry="loadMaps(mapsPage)"
/>
<el-pagination
v-if="mapsTotal > MAP_PAGE_SIZE"
class="list-pagination"
background
layout="prev, pager, next"
:total="mapsTotal"
:page-size="MAP_PAGE_SIZE"
:current-page="mapsPage"
@current-change="loadMaps"
/>
</article>
<article class="surface-card scope-card">
<div class="card-header">
<h3>新建标签范围</h3>
<div class="card-actions">
<el-button size="small" @click="newScopeOpen = !newScopeOpen">
{{ newScopeOpen ? "收起" : "展开" }}
</el-button>
</div>
</div>
<div class="scope-field">
<span class="scope-label">多个标签取</span>
<el-radio-group v-model="combineMode">
<el-radio value="UNION">并集(带任一标签)</el-radio>
<el-radio value="INTERSECT">交集(同时带全部标签)</el-radio>
</el-radio-group>
</div>
<div class="scope-actions">
<el-button type="success" :loading="busy" @click="startReview">开始复习</el-button>
<p class="panel-hint">
自定义一组标签(并集或交集取数)生成新的标签导图;已有导图直接在上方进入。
</p>
<div v-show="newScopeOpen" v-loading="busy" class="new-scope-body">
<div class="scope-field">
<span class="scope-label">标签</span>
<el-select
v-model="selectedTags"
class="scope-select"
multiple
:multiple-limit="10"
filterable
allow-create
default-first-option
placeholder="选择或输入标签"
>
<el-option v-for="name in candidates" :key="name" :label="name" :value="name" />
</el-select>
</div>
<div class="scope-field">
<span class="scope-label">多个标签取</span>
<el-radio-group v-model="combineMode">
<el-radio value="UNION">并集(带任一标签)</el-radio>
<el-radio value="INTERSECT">交集(同时带全部标签)</el-radio>
</el-radio-group>
</div>
<div class="scope-actions">
<el-button type="success" :loading="busy" @click="startReview">开始复习</el-button>
</div>
</div>
</article>
@@ -392,7 +523,7 @@ onMounted(() => {
<article class="surface-card history-card" v-if="records.length > 0">
<h3>复习历史</h3>
<div v-for="record in records" :key="record.id" class="history-row">
<span class="history-time">{{ record.createdTime?.replace("T", " ")?.substring(0, 16) }}</span>
<span class="history-time">{{ formatDateTime(record.createdTime) }}</span>
<span class="history-ratio">覆盖率 {{ ((record.recallRatio || 0) * 100).toFixed(1) }}%</span>
<span class="history-meta">
命中 {{ record.matchedCount || 0 }} · 遗漏 {{ record.missedCount || 0 }} · 额外 {{ record.extraCount || 0 }}
@@ -418,6 +549,7 @@ onMounted(() => {
margin: 0 auto;
}
.maps-card,
.scope-card,
.map-card,
.recall-card,
@@ -426,7 +558,6 @@ onMounted(() => {
padding: 18px;
}
.scope-hint,
.panel-hint {
margin: 0 0 12px;
font-size: 13px;
@@ -434,6 +565,10 @@ onMounted(() => {
line-height: 1.6;
}
.new-scope-body {
min-height: 40px;
}
.scope-field {
display: flex;
align-items: center;