Files
MySpace/frontend/src/NoteGraph.vue
T

78 lines
3.3 KiB
Vue

<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { api } from "./api";
const open = defineModel<boolean>({ required: true });
const emit = defineEmits<{ select: [id: string] }>();
const query = ref("");
const category = ref("");
const categories = ref<string[]>([]);
const nodes = ref<{ id: string; title: string; category: string }[]>([]);
const edges = ref<{ source: string; target: string }[]>([]);
const error = ref("");
const loading = ref(false);
let request = 0;
const positions = computed(() => nodes.value.map((note, index) => {
const angle = 2 * Math.PI * index / nodes.value.length - Math.PI / 2;
return { ...note, x: 50 + 38 * Math.cos(angle), y: 50 + 38 * Math.sin(angle) };
}));
const lines = computed(() => {
const index = new Map(positions.value.map(note => [note.id, note]));
return edges.value.flatMap(edge => {
const a = index.get(edge.source), b = index.get(edge.target);
return a && b ? [{ a, b }] : [];
});
});
async function load() {
const current = ++request;
loading.value = true;
error.value = "";
try {
const result = await api<{ nodes: typeof nodes.value; edges: typeof edges.value }>(
"/notes/graph?" + new URLSearchParams({ q: query.value.trim(), category: category.value }),
);
if (request === current) {
nodes.value = result.nodes;
edges.value = result.edges;
}
} catch (failure) { if (request === current) error.value = (failure as Error).message; }
finally { if (request === current) loading.value = false; }
}
watch(open, async value => {
if (!value) return;
try { categories.value = await api("/notes/categories"); }
catch (failure) { error.value = (failure as Error).message; return; }
await load();
});
function select(id: string) {
open.value = false;
emit("select", id);
}
</script>
<template>
<el-dialog v-model="open" title="知识图谱" width="min(800px, 94vw)">
<form class="library-actions" @submit.prevent="load">
<input class="field library-select" v-model="query" type="search" aria-label="搜索知识节点" placeholder="搜索标题或标签" />
<select class="field library-select" v-model="category" aria-label="分类" @change="load">
<option value="">全部分类</option><option v-for="name in categories" :key="name">{{ name }}</option>
</select>
<button class="btn">搜索</button>
<button type="button" class="btn" @click="query = ''; category = ''; load()">重置</button>
</form>
<p v-if="error" role="alert">{{ error }}</p>
<p v-else-if="loading" class="empty">正在加载…</p>
<p v-else-if="!nodes.length" class="empty">没有匹配的知识节点。</p>
<template v-else>
<div class="library-graph">
<svg viewBox="0 0 100 100" aria-hidden="true" preserveAspectRatio="none">
<line v-for="{a, b} in lines" :key="a.id + b.id" :x1="a.x" :x2="b.x" :y1="a.y" :y2="b.y" />
</svg>
<button v-for="node in positions" :key="node.id" :style="{ left: node.x + '%', top: node.y + '%' }"
:title="node.title" :aria-label="node.title" @click="select(node.id)" />
</div>
<div class="library-actions">
<button v-for="node in nodes" :key="node.id" class="btn" @click="select(node.id)">{{ node.title }}</button>
</div>
</template>
</el-dialog>
</template>