-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
127 lines (104 loc) · 2.84 KB
/
app.go
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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/wailsapp/wails/v3/pkg/application"
)
// App struct
type App struct{}
// GetMdContent
func (a *App) GetMdContent() map[string]interface{} {
if gFileName == "" {
// log.Println("### Error\nNo filename specified")
return map[string]interface{}{"value": "", "filename": ""}
}
content, err := os.ReadFile(gFileName)
localDir = getFilePath(gFileName)
// log.Println("localDir:", localDir)
if err != nil {
errMsg := fmt.Sprintf("### Error\nError reading file: %v", err)
log.Println(errMsg)
return map[string]interface{}{"value": "", "filename": ""}
}
// log.Println("-->Content: ", string(content))
return map[string]interface{}{"value": string(content), "filename": gFileName}
}
// GoNewFile
func (a *App) GoNewFile() string {
gFileName = ""
bChanged = false
return "OK"
}
// goOpenFile
func (a *App) GoOpenFile() map[string]interface{} {
dialog := application.OpenFileDialog().
SetTitle("Open Markdown File").
SetMessage("Select a markdown file to open").
AddFilter("Markdown file(*.md)", "*.md")
filename, _ := dialog.PromptForSingleSelection()
if filename == "" {
return map[string]interface{}{"value": "", "filename": ""}
}
content, err := os.ReadFile(filename)
if err != nil {
return map[string]interface{}{"value": "", "filename": ""}
}
gFileName = filename
localDir = getFilePath(gFileName)
bChanged = false
return map[string]interface{}{"value": string(content), "filename": filename}
}
func getNewFileName() string {
filename, _ := application.SaveFileDialog().
CanCreateDirectories(true).
SetMessage("Save to a markdow file").
SetFilename("untitle.md").
PromptForSingleSelection()
// log.Println("getNewFileName filename: ", filename)
return filename
}
// GoSaveFile
func (a *App) GoSaveFile(content string) string {
if gFileName == "" {
gFileName = getNewFileName()
}
if gFileName != "" {
localDir = getFilePath(gFileName)
bChanged = false
// log.Println("localDir:", localDir)
err := os.WriteFile(gFileName, []byte(content), 0644)
if err == nil {
return "OK"
}
}
return "Error"
}
// GoSaveAsFile resutl: {"value": "OK", "filename": "e:\\StXhCode\\go\\mdPad\\untitle.md"}
func (a *App) GoSaveAsFile(content string) map[string]interface{} {
gFileName = getNewFileName()
if gFileName != "" {
localDir = getFilePath(gFileName)
bChanged = false
// log.Println("localDir:", localDir)
err := os.WriteFile(gFileName, []byte(content), 0644)
if err == nil {
return map[string]interface{}{"value": "OK", "filename": gFileName}
}
}
return map[string]interface{}{"value": "Error", "filename": ""}
}
// GoFileChanged
func (a *App) GoFileChanged() bool {
bChanged = true
return bChanged
}
// get file path
func getFilePath(filename string) string {
if filename != "" {
return filepath.Dir(filename)
} else {
return ""
}
}