-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.go
More file actions
275 lines (238 loc) · 6.47 KB
/
Copy pathapp.go
File metadata and controls
275 lines (238 loc) · 6.47 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"context"
"encoding/json"
"net/http"
"os"
"strings"
"time"
"github.com/fsnotify/fsnotify"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
startupFile string
watcher *fsnotify.Watcher
watchMap map[string]bool
}
// GitHubRelease represents the GitHub API response for a release
type GitHubRelease struct {
TagName string `json:"tag_name"`
HtmlUrl string `json:"html_url"`
Body string `json:"body"`
}
// UpdateCheckResult represents the result of an update check
type UpdateCheckResult struct {
HasUpdate bool `json:"hasUpdate"`
Version string `json:"version"`
Url string `json:"url"`
Description string `json:"description"`
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{
watchMap: make(map[string]bool),
}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// shutdown is called when the app closes
func (a *App) shutdown(ctx context.Context) {
// close watcher cleanly when app exits
if a.watcher != nil {
a.watcher.Close()
a.watcher = nil
}
}
// StartWatching starts watching a file for changes
func (a *App) StartWatching(path string) error {
if a.watchMap[path] {
return nil
}
if a.watcher == nil {
w, err := fsnotify.NewWatcher()
if err != nil {
return err
}
a.watcher = w
debounceMap := make(map[string]*time.Timer)
// start background goroutine to listen for events
go func() {
for {
select {
case event, ok := <-a.watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
// debounce — wait 300ms after last event
// reset timer if another event fires
if timer, exists := debounceMap[event.Name]; exists {
timer.Stop()
}
path := event.Name
debounceMap[path] = time.AfterFunc(300*time.Millisecond, func() {
runtime.EventsEmit(a.ctx, "fileChanged", path)
delete(debounceMap, path)
})
}
case err, ok := <-a.watcher.Errors:
if !ok {
return
}
_ = err
}
}
}()
}
// add file to watcher
err := a.watcher.Add(path)
if err != nil {
return err
}
// mark as watched
a.watchMap[path] = true
return nil
}
// StopWatching stops watching a file for changes
func (a *App) StopWatching(path string) error {
// not watching this file? skip
if !a.watchMap[path] {
return nil
}
// remove from watcher
if a.watcher != nil {
err := a.watcher.Remove(path)
if err != nil {
return err
}
}
// remove from watchMap
delete(a.watchMap, path)
// if no more files watched → close watcher entirely
if len(a.watchMap) == 0 && a.watcher != nil {
a.watcher.Close()
a.watcher = nil
}
return nil
}
func (a *App) GetStartupFile() string {
return a.startupFile
}
// OpenFile opens a native file dialog and returns the selected file path
func (a *App) OpenFile() string {
path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Open Markdown File",
Filters: []runtime.FileFilter{
{
DisplayName: "Markdown Files",
Pattern: "*.md;*.markdown;*.txt;*.mdx",
},
},
})
if err != nil {
return ""
}
return path
}
// OpenImportFile opens a native file dialog for importing documents
func (a *App) OpenImportFile() string {
path, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Import Document",
Filters: []runtime.FileFilter{
{
DisplayName: "PDF Files",
Pattern: "*.pdf",
},
},
})
if err != nil {
return ""
}
return path
}
// ReadFile reads a file from disk and returns its content as text
func (a *App) ReadFile(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(content), nil
}
// ReadFileBytes reads a file and returns raw bytes (for binary files like PDF)
func (a *App) ReadFileBytes(path string) ([]byte, error) {
return os.ReadFile(path)
}
// SaveFile saves content to an existing file path
func (a *App) SaveFile(path string, content string) error {
return os.WriteFile(path, []byte(content), 0644)
}
// SaveFileAs opens a native save dialog and saves content to chosen path
func (a *App) SaveFileAs(content string) (string, error) {
path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
Title: "Save Markdown File",
DefaultFilename: "untitled.md",
Filters: []runtime.FileFilter{
{
DisplayName: "Markdown Files",
Pattern: "*.md;*.markdown",
},
},
})
if err != nil || path == "" {
return "", err
}
err = os.WriteFile(path, []byte(content), 0644)
if err != nil {
return "", err
}
return path, nil
}
// CheckForUpdates checks the GitHub API for the latest release
func (a *App) CheckForUpdates(currentVersion string) UpdateCheckResult {
const repoURL = "https://api.github.com/repositories/1281269179/releases/latest"
println("Checking for updates. Current version:", currentVersion)
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(repoURL)
if err != nil {
println("Error fetching GitHub API:", err.Error())
return UpdateCheckResult{HasUpdate: false}
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
println("GitHub API returned status:", resp.StatusCode)
return UpdateCheckResult{HasUpdate: false}
}
var release GitHubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
println("Error decoding GitHub response:", err.Error())
return UpdateCheckResult{HasUpdate: false}
}
println("Latest version from GitHub:", release.TagName)
// Compare versions (strip 'v' prefix if present)
current := strings.TrimPrefix(currentVersion, "v")
latest := strings.TrimPrefix(release.TagName, "v")
if current != latest {
println("Update available! Current:", current, "Latest:", latest)
return UpdateCheckResult{
HasUpdate: true,
Version: release.TagName,
Url: release.HtmlUrl,
Description: release.Body,
}
}
println("No update available. Current:", current, "Latest:", latest)
return UpdateCheckResult{HasUpdate: false}
}
// ConvertToMarkdownFile converts a file on disk to markdown
func (a *App) ConvertToMarkdownFile(filePath string) ConversionResult {
return ConvertToMarkdown(filePath)
}
// ConvertFileContent converts uploaded file bytes to markdown
// Called from the frontend when the user imports a file
func (a *App) ConvertFileContent(fileName string, content []byte) ConversionResult {
return ConvertFileContent(fileName, content)
}