feat: 实现笔临 P0/P1 Electron 桌面应用 v0.1.0
交付写作核心(TipTap、自动保存、分卷分章)、Onboarding、7 主题与双语 i18n、快捷键设置;主进程使用 node:sqlite;补全 Vitest 与 Playwright E2E;统一 design/ 目录并移除 desigin 拼写错误路径。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { create } from 'zustand'
|
||||
|
||||
export type AppView = 'home' | 'editor' | 'settings'
|
||||
|
||||
interface AppState {
|
||||
view: AppView
|
||||
sidebarPanel: 'chapters' | 'outline' | 'setting' | 'inspiration'
|
||||
rightPanel: 'ai' | 'knowledge' | 'wordfreq' | 'book-settings'
|
||||
toast: string | null
|
||||
setView: (view: AppView) => void
|
||||
setSidebarPanel: (panel: AppState['sidebarPanel']) => void
|
||||
setRightPanel: (panel: AppState['rightPanel']) => void
|
||||
showToast: (message: string) => void
|
||||
clearToast: () => void
|
||||
}
|
||||
|
||||
export const useAppStore = create<AppState>((set) => ({
|
||||
view: 'home',
|
||||
sidebarPanel: 'chapters',
|
||||
rightPanel: 'ai',
|
||||
toast: null,
|
||||
setView: (view) => set({ view }),
|
||||
setSidebarPanel: (sidebarPanel) => set({ sidebarPanel }),
|
||||
setRightPanel: (rightPanel) => set({ rightPanel }),
|
||||
showToast: (toast) => {
|
||||
set({ toast })
|
||||
setTimeout(() => set({ toast: null }), 2500)
|
||||
},
|
||||
clearToast: () => set({ toast: null })
|
||||
}))
|
||||
@@ -0,0 +1,66 @@
|
||||
import { create } from 'zustand'
|
||||
import type { BookMeta, Volume, Chapter } from '@shared/types'
|
||||
import { ipcCall } from '@renderer/lib/ipc-client'
|
||||
|
||||
interface BookState {
|
||||
books: BookMeta[]
|
||||
currentBookId: string | null
|
||||
volumes: Volume[]
|
||||
chapters: Chapter[]
|
||||
selectedChapterId: string | null
|
||||
activeVolumeId: string | null
|
||||
loadBooks: () => Promise<void>
|
||||
openBook: (bookId: string) => Promise<void>
|
||||
setSelectedChapter: (chapterId: string) => void
|
||||
setActiveVolume: (volumeId: string) => void
|
||||
refreshChapters: () => Promise<void>
|
||||
updateChapterLocal: (chapter: Chapter) => void
|
||||
}
|
||||
|
||||
export const useBookStore = create<BookState>((set, get) => ({
|
||||
books: [],
|
||||
currentBookId: null,
|
||||
volumes: [],
|
||||
chapters: [],
|
||||
selectedChapterId: null,
|
||||
activeVolumeId: null,
|
||||
loadBooks: async () => {
|
||||
const books = await ipcCall(() => window.electronAPI.book.list())
|
||||
set({ books })
|
||||
},
|
||||
openBook: async (bookId) => {
|
||||
const result = await ipcCall(() => window.electronAPI.book.open(bookId))
|
||||
const firstVolume = result.volumes[0]?.id ?? null
|
||||
const lastChapter =
|
||||
result.meta.lastChapterId ??
|
||||
result.chapters.find((c) => c.volumeId === firstVolume)?.id ??
|
||||
result.chapters[0]?.id ??
|
||||
null
|
||||
set({
|
||||
currentBookId: bookId,
|
||||
volumes: result.volumes,
|
||||
chapters: result.chapters,
|
||||
activeVolumeId: result.chapters.find((c) => c.id === lastChapter)?.volumeId ?? firstVolume,
|
||||
selectedChapterId: lastChapter
|
||||
})
|
||||
},
|
||||
setSelectedChapter: (chapterId) => {
|
||||
const ch = get().chapters.find((c) => c.id === chapterId)
|
||||
set({
|
||||
selectedChapterId: chapterId,
|
||||
activeVolumeId: ch?.volumeId ?? get().activeVolumeId
|
||||
})
|
||||
},
|
||||
setActiveVolume: (volumeId) => set({ activeVolumeId: volumeId }),
|
||||
refreshChapters: async () => {
|
||||
const bookId = get().currentBookId
|
||||
if (!bookId) return
|
||||
const result = await ipcCall(() => window.electronAPI.book.open(bookId))
|
||||
set({ volumes: result.volumes, chapters: result.chapters })
|
||||
},
|
||||
updateChapterLocal: (chapter) => {
|
||||
set((s) => ({
|
||||
chapters: s.chapters.map((c) => (c.id === chapter.id ? chapter : c))
|
||||
}))
|
||||
}
|
||||
}))
|
||||
@@ -0,0 +1,33 @@
|
||||
import { create } from 'zustand'
|
||||
import type { ThemeId, Language, GlobalSettings, ActionId } from '@shared/types'
|
||||
import { ipcCall } from '@renderer/lib/ipc-client'
|
||||
import { applyTheme } from '@renderer/lib/theme'
|
||||
import i18n from '@renderer/i18n'
|
||||
|
||||
interface SettingsState extends GlobalSettings {
|
||||
loaded: boolean
|
||||
load: () => Promise<void>
|
||||
update: (partial: Partial<GlobalSettings>) => Promise<void>
|
||||
}
|
||||
|
||||
export const useSettingsStore = create<SettingsState>((set, get) => ({
|
||||
penName: '未命名作者',
|
||||
onboardingCompleted: false,
|
||||
theme: 'default',
|
||||
language: 'zh-CN',
|
||||
dailyWordGoal: 0,
|
||||
shortcuts: {} as Record<ActionId, string>,
|
||||
loaded: false,
|
||||
load: async () => {
|
||||
const data = await ipcCall(() => window.electronAPI.settings.get())
|
||||
set({ ...data, loaded: true })
|
||||
applyTheme(data.theme)
|
||||
await i18n.changeLanguage(data.language)
|
||||
},
|
||||
update: async (partial) => {
|
||||
const data = await ipcCall(() => window.electronAPI.settings.update(partial))
|
||||
set({ ...get(), ...data })
|
||||
if (partial.theme) applyTheme(data.theme)
|
||||
if (partial.language) await i18n.changeLanguage(data.language)
|
||||
}
|
||||
}))
|
||||
@@ -0,0 +1,58 @@
|
||||
import { create } from 'zustand'
|
||||
|
||||
export interface AppTab {
|
||||
id: string
|
||||
type: 'book' | 'settings'
|
||||
name: string
|
||||
bookId?: string
|
||||
}
|
||||
|
||||
interface TabState {
|
||||
openTabs: AppTab[]
|
||||
activeTabId: string | null
|
||||
openBookTab: (bookId: string, name: string) => void
|
||||
openSettingsTab: () => void
|
||||
switchTab: (tabId: string) => void
|
||||
closeTab: (tabId: string) => string | null
|
||||
}
|
||||
|
||||
export const useTabStore = create<TabState>((set, get) => ({
|
||||
openTabs: [],
|
||||
activeTabId: null,
|
||||
openBookTab: (bookId, name) => {
|
||||
const existing = get().openTabs.find((t) => t.type === 'book' && t.bookId === bookId)
|
||||
if (existing) {
|
||||
set({ activeTabId: existing.id })
|
||||
return
|
||||
}
|
||||
const id = `book-${bookId}`
|
||||
set((s) => ({
|
||||
openTabs: [...s.openTabs, { id, type: 'book', name, bookId }],
|
||||
activeTabId: id
|
||||
}))
|
||||
},
|
||||
openSettingsTab: () => {
|
||||
const existing = get().openTabs.find((t) => t.type === 'settings')
|
||||
if (existing) {
|
||||
set({ activeTabId: existing.id })
|
||||
return
|
||||
}
|
||||
set((s) => ({
|
||||
openTabs: [...s.openTabs, { id: 'settings', type: 'settings', name: '系统设置' }],
|
||||
activeTabId: 'settings'
|
||||
}))
|
||||
},
|
||||
switchTab: (tabId) => set({ activeTabId: tabId }),
|
||||
closeTab: (tabId) => {
|
||||
const { openTabs, activeTabId } = get()
|
||||
const idx = openTabs.findIndex((t) => t.id === tabId)
|
||||
if (idx === -1) return activeTabId
|
||||
const nextTabs = openTabs.filter((t) => t.id !== tabId)
|
||||
let nextActive = activeTabId
|
||||
if (activeTabId === tabId) {
|
||||
nextActive = nextTabs[Math.max(0, idx - 1)]?.id ?? null
|
||||
}
|
||||
set({ openTabs: nextTabs, activeTabId: nextActive })
|
||||
return nextActive
|
||||
}
|
||||
}))
|
||||
Reference in New Issue
Block a user