Files
Thing/src/modules/editor/components/BacklinksPanel.vue
T
2026-10-10 14:29:48 +08:00

219 lines
8.1 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">
/**
* 反链面板:当前笔记的**出链**与**入链**。
*
* # 数据从哪来
*
* 全部来自 SQLite 索引的 `links` 表(写入索引时已把 `[[名字]]` 按 Obsidian 语义
* 「全库按名解析」成库内相对路径)。因此这里不需要扫全库 —— 一次 `editor_backlinks`
* 调用就把两侧都取回。
*
* # 未解析链接为什么值得显示
*
* `[[还不存在的笔记]]` 在 Obsidian 里是一个**新建入口**,不是错误。面板把它单独列在
* 「未解析」分组下并允许一键创建 —— 这正是双链笔记「先写链接、后补内容」的用法。
*/
import { computed, ref, watch } from 'vue'
import { ArrowUpRight, Link2, Link2Off, Loader2, RefreshCw } from '@lucide/vue'
import { toast } from 'vue-sonner'
import { ScrollArea } from '@/components/ui/scroll-area'
import { StateBlock } from '@/components/common'
import { useEditorStore } from '@/stores/editorStore'
import { editorClient } from '../editorClient'
import type { NoteBacklinks, NoteLink } from '@/types/editor'
const props = defineProps<{
/** 当前笔记的库内相对路径(空串 = 没有笔记,面板显示空态) */
relPath: string
}>()
const emit = defineEmits<{
/** 打开某篇笔记 */
open: [relPath: string]
/** 已用某个名字新建了一篇(未解析链接的一键创建);上层据此展开树 */
create: [name: string]
}>()
const store = useEditorStore()
const data = ref<NoteBacklinks | null>(null)
const loading = ref(false)
/** 已解析的出链 */
const resolvedOutgoing = computed(() =>
(data.value?.outgoing ?? []).filter(item => item.resolved)
)
/** 未解析的出链(可一键新建) */
const unresolvedOutgoing = computed(() =>
(data.value?.outgoing ?? []).filter(item => !item.resolved && item.kind !== 'url')
)
/** 外部链接(`[](https://…)`):只展示不可跳转到库内 */
const externalOutgoing = computed(() =>
(data.value?.outgoing ?? []).filter(item => item.kind === 'url')
)
async function load(): Promise<void> {
if (!props.relPath) {
data.value = null
return
}
loading.value = true
try {
data.value = await editorClient.backlinks(props.relPath)
} catch (e) {
// 索引还没建立时这里会失败:不是错误,只是「暂时没有数据」
data.value = null
} finally {
loading.value = false
}
}
watch(
() => [props.relPath, store.indexStats?.noteCount],
() => void load(),
{ immediate: true }
)
/** 点击未解析链接:按名字新建一篇(`createNote` 会顺带把它打开)。 */
async function createMissing(link: NoteLink): Promise<void> {
try {
await store.createNote(currentDirOf(props.relPath), link.target)
emit('create', link.target)
await load()
} catch (e) {
toast.error(`新建失败:${String(e)}`)
}
}
/** 新笔记落在当前笔记所在目录(与 Obsidian「同目录优先」的习惯一致)。 */
function currentDirOf(relPath: string): string {
const parts = relPath.split('/')
parts.pop()
return parts.join('/')
}
function targetOf(link: NoteLink): string {
// URL 类型直接交给系统浏览器由上层处理;这里统一用 relPath(可能为空)
return link.relPath || link.target
}
</script>
<template>
<div class="flex h-full min-h-0 flex-col">
<div class="flex h-6 shrink-0 items-center gap-1.5 border-b border-border px-2 text-[11px] text-muted-foreground">
<Link2 class="size-3" />
<span>双链</span>
<span class="ml-auto tabular-nums">
被 {{ data?.incomingSources ?? 0 }} 篇引用 · 出链 {{ resolvedOutgoing.length + unresolvedOutgoing.length }}
</span>
<button
type="button"
class="rounded-sm p-0.5 hover:bg-active"
title="刷新"
@click="load"
>
<RefreshCw class="size-3" :class="loading && 'animate-spin'" />
</button>
</div>
<ScrollArea class="min-h-0 flex-1">
<div class="px-1.5 py-2">
<div v-if="loading && !data" class="flex items-center justify-center gap-2 p-4">
<Loader2 class="size-3.5 animate-spin text-muted-foreground" />
<span class="text-xs text-muted-foreground">正在查询链接…</span>
</div>
<StateBlock
v-else-if="!relPath"
variant="empty"
title="未打开笔记"
description="打开一篇笔记后这里会显示它的双链"
:min-height="120"
/>
<StateBlock
v-else-if="!data?.incoming.length && !data?.outgoing.length"
variant="empty"
title="还没有链接"
description="在正文里写 [[另一篇笔记]],或在别处引用这篇"
:min-height="120"
/>
<template v-else>
<!-- 入链 -->
<p v-if="data?.incoming.length" class="px-1 pb-1 text-[10px] font-medium tracking-wide text-fg-disabled uppercase">
被引用({{ data.incomingSources }})
</p>
<button
v-for="link in data?.incoming ?? []"
:key="`in-${link.relPath}-${link.target}`"
type="button"
class="flex w-full items-center gap-1.5 rounded-sm px-1 py-1 text-left text-xs text-fg-secondary transition-colors duration-150 hover:bg-row-hover hover:text-foreground"
@click="emit('open', link.relPath)"
>
<ArrowUpRight class="size-3 shrink-0 opacity-70" />
<span class="truncate">{{ link.title }}</span>
<span class="ml-auto shrink-0 truncate text-[10px] text-fg-disabled">{{ link.relPath }}</span>
</button>
<!-- 已解析出链 -->
<p
v-if="resolvedOutgoing.length"
class="px-1 pt-3 pb-1 text-[10px] font-medium tracking-wide text-fg-disabled uppercase"
>
指向({{ resolvedOutgoing.length }})
</p>
<button
v-for="link in resolvedOutgoing"
:key="`out-${link.relPath}`"
type="button"
class="flex w-full items-center gap-1.5 rounded-sm px-1 py-1 text-left text-xs text-fg-secondary transition-colors duration-150 hover:bg-row-hover hover:text-foreground"
@click="emit('open', targetOf(link))"
>
<Link2 class="size-3 shrink-0 opacity-70" />
<span class="truncate">{{ link.title }}</span>
</button>
<!-- 未解析出链(可一键新建) -->
<p
v-if="unresolvedOutgoing.length"
class="px-1 pt-3 pb-1 text-[10px] font-medium tracking-wide text-fg-disabled uppercase"
>
未解析({{ unresolvedOutgoing.length }})
</p>
<button
v-for="link in unresolvedOutgoing"
:key="`miss-${link.target}`"
type="button"
class="group flex w-full items-center gap-1.5 rounded-sm px-1 py-1 text-left text-xs text-muted-foreground transition-colors duration-150 hover:bg-row-hover hover:text-foreground"
title="点击新建这篇笔记"
@click="createMissing(link)"
>
<Link2Off class="size-3 shrink-0 opacity-70" />
<span class="truncate">{{ link.target }}</span>
<span class="ml-auto shrink-0 text-[10px] opacity-0 group-hover:opacity-100">新建</span>
</button>
<!-- 外部链接 -->
<p
v-if="externalOutgoing.length"
class="px-1 pt-3 pb-1 text-[10px] font-medium tracking-wide text-fg-disabled uppercase"
>
外部链接({{ externalOutgoing.length }})
</p>
<a
v-for="link in externalOutgoing"
:key="`url-${link.target}`"
class="flex w-full items-center gap-1.5 rounded-sm px-1 py-1 text-xs text-fg-secondary transition-colors duration-150 hover:bg-row-hover hover:text-foreground"
:href="link.target"
target="_blank"
rel="noreferrer noopener"
>
<ArrowUpRight class="size-3 shrink-0 opacity-70" />
<span class="truncate">{{ link.target }}</span>
</a>
</template>
</div>
</ScrollArea>
</div>
</template>