Files
Thing/src/modules/editor/components/EditorSettingsDialog.vue
T

420 lines
15 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">
/**
* 编辑器窗口内的设置对话框。
*
* # 为什么把主界面的设置搬到这里
*
* 「库」与「编辑器」两页的设置本来只在主模块页可达 —— 用户把编辑器窗口拖到副屏、
* 或在专注写作时,改个字号 / 换库都得先切回主界面。这里把这些**高频设置**就地暴露
* 出来(导入导出 / WebDAV 这类重面板不搬)。
*
* # 改完即全局生效
*
* 读写都走当前窗口的 `editorStore`:`patchSettings` 落盘后 Rust 广播 `editor-settings`
* 事件,主窗口与其它编辑器窗口据此重新读取设置;库根同理(`editor-library-changed`)。
* 因此这里没有「保存」按钮,也不需要与主窗口做额外同步。
*/
import { ref } from 'vue'
import { open as openDialog } from '@tauri-apps/plugin-dialog'
import { openPath } from '@tauri-apps/plugin-opener'
import {
Copy,
FolderOpen,
Library,
NotebookPen,
Palette,
RefreshCw,
Settings,
Tags
} from '@lucide/vue'
import { toast } from 'vue-sonner'
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle
} from '@/components/ui/dialog'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Switch } from '@/components/ui/switch'
import { ScrollArea } from '@/components/ui/scroll-area'
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from '@/components/ui/select'
import { ConfirmDialog, SegmentedNav, SettingGroup, SettingRow } from '@/components/common'
import { useEditorStore } from '@/stores/editorStore'
import { EDITOR_THEME_OPTIONS } from '../editor/themes'
import type { EditorSettings } from '@/types/editor'
defineProps<{ open: boolean }>()
const emit = defineEmits<{ 'update:open': [value: boolean] }>()
const store = useEditorStore()
/** 当前分区(默认「编辑器设置」—— 在编辑器窗口里调整外观 / 行为是最常见的诉求) */
const section = ref<'library' | 'editor'>('editor')
const sections = [
{ value: 'library', label: '库设置', icon: Library },
{ value: 'editor', label: '编辑器设置', icon: Palette }
]
// ===== 设置写入(即时生效) =====
function commit(patch: Partial<EditorSettings>): void {
void store.patchSettings(patch)
}
function onNumber(
patchKey: 'fontSize' | 'lineHeight' | 'tabSize' | 'autosaveMs' | 'maxFileSizeMb',
event: Event
): void {
const value = Number((event.target as HTMLInputElement).value)
if (!Number.isFinite(value)) return
commit({ [patchKey]: value } as Partial<EditorSettings>)
}
// ===== 库根 =====
const resetRootOpen = ref(false)
async function pickLibraryRoot(): Promise<void> {
try {
const selected = await openDialog({ directory: true, multiple: false, title: '选择笔记库目录' })
if (typeof selected !== 'string') return
await store.setLibraryRoot(selected)
toast.success('已切换笔记库')
} catch (e) {
toast.error(`切换失败:${String(e)}`)
}
}
async function resetLibraryRoot(): Promise<void> {
try {
await store.setLibraryRoot(null)
toast.success('已恢复默认库目录')
} catch (e) {
toast.error(`恢复失败:${String(e)}`)
} finally {
resetRootOpen.value = false
}
}
async function revealRoot(): Promise<void> {
if (!store.libraryRoot) return
try {
await openPath(store.libraryRoot)
} catch (e) {
toast.error(`打开目录失败:${String(e)}`)
}
}
async function copyRoot(): Promise<void> {
if (!store.libraryRoot) return
try {
await navigator.clipboard.writeText(store.libraryRoot)
toast.success('库路径已复制')
} catch {
toast.error('复制失败')
}
}
// ===== 索引 =====
const rebuilding = ref(false)
async function rebuildIndex(): Promise<void> {
rebuilding.value = true
try {
await store.rebuildIndex()
await store.refreshLibrary()
toast.success('索引已重建')
} catch (e) {
toast.error(`重建索引失败:${String(e)}`)
} finally {
rebuilding.value = false
}
}
</script>
<template>
<Dialog :open="open" @update:open="emit('update:open', $event)">
<DialogContent class="max-h-[85vh] sm:max-w-2xl">
<DialogHeader class="shrink-0">
<DialogTitle class="flex items-center gap-2 text-base">
<Settings class="size-4 text-primary" />
设置
</DialogTitle>
</DialogHeader>
<SegmentedNav v-model="section" :items="sections" block class="shrink-0" />
<ScrollArea class="max-h-[60vh] pr-3">
<!-- ===== 库设置 ===== -->
<div v-if="section === 'library'" class="flex flex-col gap-4 pb-1">
<SettingGroup
title="笔记库"
description="笔记以 Markdown 文件形式保存在该目录;切换库只改变指向,原目录内容不会被移动"
>
<SettingRow label="库目录" :description="store.libraryRoot || '—'">
<div class="flex flex-wrap items-center gap-2">
<Button variant="outline" size="sm" :disabled="!store.libraryRoot" @click="copyRoot">
<Copy class="size-3.5" />复制
</Button>
<Button variant="outline" size="sm" :disabled="!store.libraryRoot" @click="revealRoot">
<FolderOpen class="size-3.5" />浏览
</Button>
<Button variant="outline" size="sm" @click="pickLibraryRoot">
<NotebookPen class="size-3.5" />切换…
</Button>
<Button
v-if="store.info?.isCustom"
variant="ghost"
size="sm"
class="text-muted-foreground"
@click="resetRootOpen = true"
>
恢复默认
</Button>
</div>
</SettingRow>
<SettingRow label="重新扫描" description="在应用外新增或修改文件后,手动刷新库内容">
<Button variant="outline" size="sm" @click="store.refreshLibrary()">
<RefreshCw class="size-3.5" />刷新
</Button>
</SettingRow>
</SettingGroup>
<SettingGroup
title="索引与检索"
description="全文检索与标签依赖库内的 SQLite 索引;删掉也能从 Markdown 文件完整重建"
>
<SettingRow label="索引状态" :description="store.indexStats?.dbPath || '尚未建立索引'">
<div class="flex items-center gap-3 text-[13px] text-fg-secondary">
<span class="tabular-nums">笔记 {{ store.indexStats?.noteCount ?? 0 }}</span>
<span class="tabular-nums">文件 {{ store.indexStats?.fileCount ?? 0 }}</span>
<span class="flex items-center gap-1 tabular-nums">
<Tags class="size-3.5" />{{ store.indexStats?.tagCount ?? 0 }}
</span>
</div>
</SettingRow>
<SettingRow
label="重建索引"
description="按文件重新解析标题、标签与双链(库很大时需要一点时间)"
>
<Button
variant="outline"
size="sm"
:disabled="rebuilding || store.indexing"
@click="rebuildIndex"
>
<RefreshCw class="size-3.5" :class="rebuilding && 'animate-spin'" />重建
</Button>
</SettingRow>
<SettingRow
label="单文件大小上限"
description="超过该大小的文件会关闭所见即所得 / 折叠 / 补全,单位 MB"
control-width="wide"
>
<Input
type="number"
min="1"
max="100"
:model-value="store.settings.maxFileSizeMb"
@change="onNumber('maxFileSizeMb', $event)"
/>
</SettingRow>
</SettingGroup>
</div>
<!-- ===== 编辑器设置 ===== -->
<div v-else class="flex flex-col gap-4 pb-1">
<SettingGroup title="编辑器外观">
<SettingRow
label="配色方案"
description="「跟随主应用主题」关闭时,窗口按本方案取色与深浅"
control-width="wide"
>
<Select
:model-value="String(store.settings.editorTheme)"
@update:model-value="v => commit({ editorTheme: String(v) })"
>
<SelectTrigger class="w-full"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem v-for="opt in EDITOR_THEME_OPTIONS" :key="opt.value" :value="opt.value">
{{ opt.label }}
</SelectItem>
</SelectContent>
</Select>
</SettingRow>
<SettingRow label="跟随主应用主题" description="打开时编辑器窗口整体跟随主应用主题;关闭时由上方「配色方案」决定,窗口内外深浅一致">
<Switch
:model-value="store.settings.followAppTheme"
@update:model-value="v => commit({ followAppTheme: v === true })"
/>
</SettingRow>
<SettingRow label="字体族" description="留空使用内置等宽字体栈" control-width="wide">
<Input
:model-value="store.settings.fontFamily"
placeholder="Cascadia Code, Consolas, …"
@change="(e: Event) => commit({ fontFamily: (e.target as HTMLInputElement).value })"
/>
</SettingRow>
<SettingRow label="字号" description="单位 px" control-width="wide">
<Input
type="number"
min="10"
max="32"
:model-value="store.settings.fontSize"
@change="onNumber('fontSize', $event)"
/>
</SettingRow>
<SettingRow label="行高" description="倍数,建议 1.5 ~ 2.0" control-width="wide">
<Input
type="number"
step="0.1"
min="1"
max="3"
:model-value="store.settings.lineHeight"
@change="onNumber('lineHeight', $event)"
/>
</SettingRow>
<SettingRow label="字体连字" description="启用 ligatures(如 => 显示为箭头)">
<Switch
:model-value="store.settings.ligatures"
@update:model-value="v => commit({ ligatures: v === true })"
/>
</SettingRow>
<SettingRow label="显示行号">
<Switch
:model-value="store.settings.showLineNumbers"
@update:model-value="v => commit({ showLineNumbers: v === true })"
/>
</SettingRow>
<SettingRow
label="显示大纲"
description="在编辑区右侧抽屉显示标题树(Ctrl+Shift+O 可临时切换)"
>
<Switch
:model-value="store.settings.showOutline"
@update:model-value="v => commit({ showOutline: v === true })"
/>
</SettingRow>
</SettingGroup>
<SettingGroup title="编辑器行为">
<SettingRow
label="所见即所得"
description="Live Preview:语法符号在非光标行隐藏、光标行显形;关闭后为纯源码 + 高亮"
>
<Switch
:model-value="store.settings.livePreview"
@update:model-value="v => commit({ livePreview: v === true })"
/>
</SettingRow>
<SettingRow label="默认视图" description="新开编辑器窗口时的初始视图" control-width="wide">
<Select
:model-value="String(store.settings.defaultView)"
@update:model-value="v => commit({ defaultView: String(v) })"
>
<SelectTrigger class="w-full"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem value="wysiwyg">所见即所得</SelectItem>
<SelectItem value="source">源码</SelectItem>
</SelectContent>
</Select>
</SettingRow>
<SettingRow
label="自动保存"
description="停止输入后延迟保存,单位毫秒;0 = 关闭(仅 Ctrl+S)"
control-width="wide"
>
<Input
type="number"
min="0"
max="10000"
step="100"
:model-value="store.settings.autosaveMs"
@change="onNumber('autosaveMs', $event)"
/>
</SettingRow>
<SettingRow label="Tab 宽度" description="按空格数计" control-width="wide">
<Input
type="number"
min="1"
max="8"
:model-value="store.settings.tabSize"
@change="onNumber('tabSize', $event)"
/>
</SettingRow>
<SettingRow label="自动补全" description="括号自动闭合与代码补全(大文件会临时关闭)">
<Switch
:model-value="store.settings.autocomplete"
@update:model-value="v => commit({ autocomplete: v === true })"
/>
</SettingRow>
<SettingRow
label="识别 [[双链]]"
description="按 Obsidian 语义解析双链并写入索引(反链面板留待后续)"
>
<Switch
:model-value="store.settings.wikilink"
@update:model-value="v => commit({ wikilink: v === true })"
/>
</SettingRow>
<SettingRow
label="复用文档窗口"
description="同一篇笔记重复打开时聚焦已有窗口,而不是开出第二个"
>
<Switch
:model-value="store.settings.reuseWindow"
@update:model-value="v => commit({ reuseWindow: v === true })"
/>
</SettingRow>
<SettingRow
label="导出时高亮代码"
description="关闭后导出的 HTML 里代码块是纯黑白,适合黑白打印机"
>
<Switch
:model-value="store.settings.exportHighlight"
@update:model-value="v => commit({ exportHighlight: v === true })"
/>
</SettingRow>
<SettingRow label="新建时选模板" description="关闭后用上次选中的模板直接新建">
<Switch
:model-value="store.settings.showTemplatePicker"
@update:model-value="v => commit({ showTemplatePicker: v === true })"
/>
</SettingRow>
</SettingGroup>
</div>
</ScrollArea>
<ConfirmDialog
v-model:open="resetRootOpen"
title="恢复默认库目录"
description="笔记不会丢失:仅把库根指回应用数据目录下的默认位置,原目录内容保持不动。"
confirm-text="恢复默认"
@confirm="resetLibraryRoot"
/>
</DialogContent>
</Dialog>
</template>