编辑器模块(笔记)初版
This commit is contained in:
1 parent
7e65825d3e
commit
ad987111a3
66 files changed
+17470
-283
No files matched your search
@@ -0,0 +1,314 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 导入 / 导出面板(主模块页「库」区)。
|
||||
*
|
||||
* # 三条链路,各自的风险点
|
||||
*
|
||||
* - **导入**:批量写库,唯一硬规则是「绝不覆盖」(重名加序号),选择即授权;
|
||||
* - **导出 HTML**:渲染在前端(`markdownExport`),落盘在后端 —— 后端不碰渲染、
|
||||
* 前端不碰文件系统,各自只做自己擅长且可审计的那一半;
|
||||
* - **导出 zip**:整库或某个目录打包,方便分享/迁移。
|
||||
*
|
||||
* # 为什么导出 HTML 的路径要用户选两次(先选笔记、再选保存位置)
|
||||
*
|
||||
* 批量导出(一次选目录、每篇一个 HTML)看起来更省事,但「一篇笔记配一个 assets 目录」
|
||||
* 的结构很难在浏览器里可靠实现(`<base>` 与相对路径在不同浏览器行为不一致)。
|
||||
* 这里只做**单篇 → 单文件**,路径语义清晰:导出文件和笔记在库内同级时相对路径依然有效。
|
||||
*/
|
||||
import { computed, ref } from 'vue'
|
||||
import { open } from '@tauri-apps/plugin-dialog'
|
||||
import { openPath } from '@tauri-apps/plugin-opener'
|
||||
import {
|
||||
FileDown,
|
||||
FileUp,
|
||||
FolderInput,
|
||||
FolderOutput,
|
||||
Loader2,
|
||||
NotebookPen
|
||||
} from '@lucide/vue'
|
||||
import { toast } from 'vue-sonner'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue
|
||||
} from '@/components/ui/select'
|
||||
import { SettingRow } from '@/components/common'
|
||||
import { useNotesStore } from '@/stores/notesStore'
|
||||
import { notesClient } from '../notesClient'
|
||||
|
||||
const store = useNotesStore()
|
||||
|
||||
const busy = ref<'import' | 'export' | 'zip' | null>(null)
|
||||
|
||||
/**
|
||||
* 「当前打开的笔记」这个选项的哨兵值。
|
||||
*
|
||||
* **不能用空串**:reka-ui 的 `Select` 把空串保留给「清空选择、显示 placeholder」,
|
||||
* 放一个 `value=""` 的 `SelectItem` 会直接抛
|
||||
* 「A <SelectItem /> must have a value prop that is not an empty string」。
|
||||
* 用哨兵值 + 调用点翻译,比「把空串当合法值」更清楚:哨兵名本身就说明了语义。
|
||||
*/
|
||||
const CURRENT_NOTE = '__current__'
|
||||
/** 「整个库」这个选项的哨兵值(同上,空串留给 placeholder) */
|
||||
const WHOLE_LIBRARY = '__all__'
|
||||
|
||||
/** 导出 HTML 的目标笔记(`CURRENT_NOTE` = 当前打开的笔记) */
|
||||
const exportTarget = ref(CURRENT_NOTE)
|
||||
|
||||
/** 库内目录选项(导出 zip 时限定范围) */
|
||||
const dirOptions = computed(() => {
|
||||
const dirs = store.entries.filter(e => e.isDir).map(e => e.relPath)
|
||||
return [{ value: WHOLE_LIBRARY, label: '整个库' }, ...dirs.map(d => ({ value: d, label: d }))]
|
||||
})
|
||||
/** 导出 zip 的范围(`WHOLE_LIBRARY` = 整库) */
|
||||
const zipScope = ref(WHOLE_LIBRARY)
|
||||
|
||||
/**
|
||||
* 可导出的笔记。
|
||||
*
|
||||
* 当前笔记只作为**第一个选项**出现(而不是「空串代表当前」)—— 这样列表里每一项
|
||||
* 都有明确的值,用户也能在「当前笔记」与具体路径之间切换。
|
||||
*/
|
||||
const noteOptions = computed(() => {
|
||||
const files = store.entries.filter(e => !e.isDir)
|
||||
const options: Array<{ value: string; label: string }> = []
|
||||
if (store.currentRelPath) {
|
||||
options.push({ value: CURRENT_NOTE, label: `当前笔记(${store.currentName})` })
|
||||
}
|
||||
options.push(...files.map(f => ({ value: f.relPath, label: f.relPath })))
|
||||
return options
|
||||
})
|
||||
|
||||
/** 导入:文件 / 目录 / zip 都走同一个入口(后端按扩展名判别)。 */
|
||||
async function doImport(kind: 'file' | 'dir'): Promise<void> {
|
||||
try {
|
||||
const selected = await open(
|
||||
kind === 'dir'
|
||||
? { directory: true, multiple: false, title: '选择要导入的文件夹' }
|
||||
: {
|
||||
directory: false,
|
||||
multiple: false,
|
||||
title: '选择要导入的文件或 zip',
|
||||
filters: [
|
||||
{ name: 'Markdown / Zip', extensions: ['md', 'markdown', 'zip'] },
|
||||
{ name: '全部文件', extensions: ['*'] }
|
||||
]
|
||||
}
|
||||
)
|
||||
if (typeof selected !== 'string') return
|
||||
busy.value = 'import'
|
||||
const report = await notesClient.import(selected, '')
|
||||
await store.refreshLibrary()
|
||||
await store.syncIndex()
|
||||
const renamed = report.renamed ? `,${report.renamed} 篇重名已加序号` : ''
|
||||
const skipped = report.skipped ? `,跳过 ${report.skipped} 个非笔记文件` : ''
|
||||
toast.success(`导入 ${report.imported} 个文件${renamed}${skipped}`)
|
||||
if (report.failures.length) {
|
||||
toast.warning(`${report.failures.length} 条失败:${report.failures[0]}`)
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error(`导入失败:${String(e)}`)
|
||||
} finally {
|
||||
busy.value = null
|
||||
}
|
||||
}
|
||||
|
||||
/** 导出单篇 HTML:渲染在本地做,落盘交给后端。 */
|
||||
async function doExportHtml(): Promise<void> {
|
||||
// 哨兵值翻译成真实路径(见 `CURRENT_NOTE` 的说明)
|
||||
const relPath = exportTarget.value === CURRENT_NOTE ? store.currentRelPath : exportTarget.value
|
||||
if (!relPath) {
|
||||
toast.error(store.currentRelPath ? '请先选择要导出的笔记' : '请先在编辑器里打开一篇笔记')
|
||||
return
|
||||
}
|
||||
try {
|
||||
const doc = await notesClient.readNote(relPath)
|
||||
// **动态引入**:`markdownExport` 会拉进 markdown-it + DOMPurify + highlight.js
|
||||
// (实测约 1MB),而导出是低频动作。静态引入会让「笔记」模块页一打开就下载
|
||||
// 这三个包(chunk 从 ~130kB 涨到 1.1MB),把首屏拖垮。
|
||||
const { renderNoteHtml } = await import('../editor/markdownExport')
|
||||
const html = renderNoteHtml({
|
||||
// 标题用笔记自己的显示名,而不是「当前笔记」那个下拉标签
|
||||
title: doc.name.replace(/\.md$/i, ''),
|
||||
fileName: doc.name,
|
||||
markdown: doc.content,
|
||||
highlight: store.settings.exportHighlight
|
||||
})
|
||||
const suggested = doc.name.replace(/\.md$/i, '') + '.html'
|
||||
const dest = await open({
|
||||
title: '导出为 HTML',
|
||||
defaultPath: suggested,
|
||||
filters: [{ name: 'HTML 文件', extensions: ['html'] }]
|
||||
})
|
||||
if (typeof dest !== 'string') return
|
||||
busy.value = 'export'
|
||||
const saved = await notesClient.exportNote(relPath, html, dest)
|
||||
toast.success(`已导出:${saved}`, {
|
||||
action: { label: '打开', onClick: () => void openPath(saved) }
|
||||
})
|
||||
} catch (e) {
|
||||
toast.error(`导出失败:${String(e)}`)
|
||||
} finally {
|
||||
busy.value = null
|
||||
}
|
||||
}
|
||||
|
||||
/** 导出 zip(整库或某个目录)。 */
|
||||
async function doExportZip(): Promise<void> {
|
||||
try {
|
||||
// 哨兵值翻译:`WHOLE_LIBRARY` → 不传范围(后端按「整库」处理)
|
||||
const scope = zipScope.value === WHOLE_LIBRARY ? '' : zipScope.value
|
||||
const scopeLabel = scope ? scope.replace(/\//g, '-') : 'thing-notes'
|
||||
const dest = await open({
|
||||
title: '导出 zip',
|
||||
defaultPath: `${scopeLabel}.zip`,
|
||||
filters: [{ name: 'Zip 压缩包', extensions: ['zip'] }]
|
||||
})
|
||||
if (typeof dest !== 'string') return
|
||||
busy.value = 'zip'
|
||||
const saved = await notesClient.exportZip(dest, scope || undefined)
|
||||
toast.success(`已导出:${saved}`, {
|
||||
action: { label: '打开目录', onClick: () => void openPath(saved.replace(/[\\/][^\\/]+$/, '')) }
|
||||
})
|
||||
} catch (e) {
|
||||
toast.error(`导出失败:${String(e)}`)
|
||||
} finally {
|
||||
busy.value = null
|
||||
}
|
||||
}
|
||||
|
||||
/** 把内置模板写到库内 `templates/`,方便用户直接改。 */
|
||||
async function exportTemplates(): Promise<void> {
|
||||
try {
|
||||
const written = await store.exportBuiltinTemplates()
|
||||
if (written.length) {
|
||||
toast.success(`已写入 ${written.length} 个内置模板到 templates/`)
|
||||
} else {
|
||||
toast.info('templates/ 里已有这些模板,未覆盖')
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error(`写入模板失败:${String(e)}`)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="flex flex-col gap-4">
|
||||
<!-- ===== 导入 ===== -->
|
||||
<Card class="!gap-2 !py-4">
|
||||
<CardHeader class="!px-4">
|
||||
<CardTitle class="flex items-center gap-2 text-base">
|
||||
<FileUp class="size-4 text-primary" />
|
||||
导入
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent class="!px-4">
|
||||
<p class="pb-2 text-xs text-muted-foreground">
|
||||
导入 Markdown 目录 / 单文件 / zip 包。**同名笔记不会被覆盖**,会自动加序号;
|
||||
图片等附件进 <code class="font-mono">assets/</code>,其它类型文件忽略。
|
||||
</p>
|
||||
<div class="flex flex-col divide-y divide-divider">
|
||||
<SettingRow label="导入文件夹" description="保留子目录结构,跳过隐藏目录与 __MACOSX">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
:disabled="!!busy"
|
||||
@click="doImport('dir')"
|
||||
>
|
||||
<Loader2 v-if="busy === 'import'" class="size-3.5 animate-spin" />
|
||||
<FolderInput v-else class="size-3.5" />
|
||||
选择文件夹
|
||||
</Button>
|
||||
</SettingRow>
|
||||
<SettingRow label="导入文件 / zip" description="单个 .md 或一个压缩包">
|
||||
<Button variant="outline" size="sm" :disabled="!!busy" @click="doImport('file')">
|
||||
<FileUp class="size-3.5" />选择文件
|
||||
</Button>
|
||||
</SettingRow>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- ===== 导出 ===== -->
|
||||
<Card class="!gap-2 !py-4">
|
||||
<CardHeader class="!px-4">
|
||||
<CardTitle class="flex items-center gap-2 text-base">
|
||||
<FileDown class="size-4 text-primary" />
|
||||
导出
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent class="!px-4">
|
||||
<p class="pb-2 text-xs text-muted-foreground">
|
||||
HTML 为**单文件、零外链**(样式内联),可离线双击打开或直接用系统打印成 PDF;
|
||||
正文经过消毒,笔记里嵌的脚本不会被执行。
|
||||
</p>
|
||||
<div class="flex flex-col divide-y divide-divider">
|
||||
<SettingRow label="导出笔记" description="Markdown → 独立 HTML" control-width="wide">
|
||||
<div class="flex w-full flex-col gap-2">
|
||||
<Select v-model="exportTarget">
|
||||
<SelectTrigger class="w-full"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem v-for="opt in noteOptions" :key="opt.value" :value="opt.value">
|
||||
{{ opt.label }}
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Button
|
||||
size="sm"
|
||||
class="self-start"
|
||||
:disabled="!!busy"
|
||||
@click="doExportHtml"
|
||||
>
|
||||
<Loader2 v-if="busy === 'export'" class="size-3.5 animate-spin" />
|
||||
<FileDown v-else class="size-3.5" />
|
||||
导出 HTML
|
||||
</Button>
|
||||
</div>
|
||||
</SettingRow>
|
||||
|
||||
<SettingRow label="导出 zip" description="整库或某个目录(含 assets/,不含 .notes/)" control-width="wide">
|
||||
<div class="flex w-full flex-col gap-2">
|
||||
<Select v-model="zipScope">
|
||||
<SelectTrigger class="w-full"><SelectValue /></SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem v-for="opt in dirOptions" :key="opt.value" :value="opt.value">
|
||||
{{ opt.label }}
|
||||
</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
class="self-start"
|
||||
:disabled="!!busy"
|
||||
@click="doExportZip"
|
||||
>
|
||||
<Loader2 v-if="busy === 'zip'" class="size-3.5 animate-spin" />
|
||||
<FolderOutput v-else class="size-3.5" />
|
||||
导出 zip
|
||||
</Button>
|
||||
</div>
|
||||
</SettingRow>
|
||||
|
||||
<SettingRow
|
||||
label="内置模板"
|
||||
description="把日记 / 会议纪要等内置模板写到库内 templates/,改完即刻生效"
|
||||
>
|
||||
<Button variant="outline" size="sm" @click="exportTemplates">
|
||||
<NotebookPen class="size-3.5" />写入 templates/
|
||||
</Button>
|
||||
</SettingRow>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<!-- 说明:导入目标目录固定为库根(保持与 zip 内结构一致),因此这里只给提示 -->
|
||||
<p class="px-1 text-[11px] text-muted-foreground">
|
||||
提示:导入的笔记都落在库根下(保留源目录结构)。想放到别处,导入后拖到目标目录即可。
|
||||
</p>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user