1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
import { create } from 'zustand'
import type { Tab } from '@/types'
import { readFile, writeFile, saveDialog, pickFile } from '@/lib/tauri'
import { detectLanguage } from '@/lib/languageMap'
interface TabsState {
tabs: Tab[]
activeTabId: string | null
cursorLine: number
cursorCol: number
savingId: string | null
statusMessage: string | null
untitledCounter: number
openFile: (path: string) => Promise<void>
pickAndOpenFile: () => Promise<void>
createUntitledFile: () => void
closeTab: (id: string) => void
setActiveTab: (id: string) => void
updateContent: (id: string, content: string) => void
saveFile: (id: string) => Promise<void>
setCursorPosition: (line: number, col: number) => void
clearStatusMessage: () => void
}
function isUntitled(id: string) {
return id.startsWith('untitled://')
}
export const useTabsStore = create<TabsState>((set, get) => ({
tabs: [],
activeTabId: null,
cursorLine: 1,
cursorCol: 1,
savingId: null,
statusMessage: null,
untitledCounter: 0,
openFile: async (path: string) => {
const { tabs } = get()
const existing = tabs.find((t) => t.path === path)
if (existing) {
set({ activeTabId: existing.id })
return
}
try {
const content = await readFile(path)
const name = path.split(/[\\/]/).pop() ?? 'untitled'
const id = path
const tab: Tab = {
id,
path,
name,
language: detectLanguage(name),
content,
modified: false,
}
set({ tabs: [...tabs, tab], activeTabId: id })
} catch (err) {
console.error('Failed to open file:', err)
}
},
pickAndOpenFile: async () => {
const path = await pickFile()
if (path) get().openFile(path)
},
createUntitledFile: () => {
const { tabs, untitledCounter } = get()
const n = untitledCounter + 1
const name = `Untitled-${n}`
const id = `untitled://${n}`
const tab: Tab = {
id,
path: id,
name,
language: 'plaintext',
content: '',
modified: false,
}
set({ tabs: [...tabs, tab], activeTabId: id, untitledCounter: n })
},
closeTab: (id: string) => {
const { tabs, activeTabId } = get()
const newTabs = tabs.filter((t) => t.id !== id)
let newActive = activeTabId
if (activeTabId === id) {
const idx = tabs.findIndex((t) => t.id === id)
newActive = newTabs[Math.min(idx, newTabs.length - 1)]?.id ?? null
}
set({ tabs: newTabs, activeTabId: newActive })
},
setActiveTab: (id: string) => {
set({ activeTabId: id })
},
updateContent: (id: string, content: string) => {
set((state) => ({
tabs: state.tabs.map((t) =>
t.id === id ? { ...t, content, modified: true } : t
),
}))
},
saveFile: async (id: string) => {
const { tabs } = get()
const tab = tabs.find((t) => t.id === id)
if (!tab) return
set({ savingId: id, statusMessage: null })
try {
let targetPath = tab.path
if (isUntitled(targetPath)) {
const picked = await saveDialog(tab.name)
if (!picked) {
set({ savingId: null })
return
}
targetPath = picked
}
await writeFile(targetPath, tab.content)
const name = targetPath.split(/[\\/]/).pop() ?? tab.name
set({
tabs: tabs.map((t) =>
t.id === id
? { ...t, path: targetPath, name, language: detectLanguage(name), modified: false }
: t
),
savingId: null,
statusMessage: 'Saved',
})
} catch (err) {
set({ savingId: null, statusMessage: 'Save failed' })
console.error('Failed to save file:', err)
}
},
setCursorPosition: (line: number, col: number) => {
set({ cursorLine: line, cursorCol: col })
},
clearStatusMessage: () => {
set({ statusMessage: null })
},
}))
|