Compare commits

...

2 commits

Author SHA1 Message Date
3de1b8b714 Load LastModified time on note 2025-09-30 11:14:29 +01:00
33cc62b3fc Auto save note 2025-09-30 11:07:54 +01:00
2 changed files with 28 additions and 6 deletions

View file

@ -37,7 +37,7 @@ var commandBar = new TinyMDE.CommandBar({
'|',
'h1', 'h2',
'|',
'ul', 'ol', {name: "checklist", title: "Check List", action: makeChecklist},
'ul', 'ol',
'|',
'blockquote', 'hr',
'|',
@ -45,9 +45,23 @@ var commandBar = new TinyMDE.CommandBar({
],
});
let editTimeout;
let autoSaveDelay = 2000;
tinyMDE.addEventListener("change", function() {
clearTimeout(editTimeout);
editTimeout = setTimeout(() => {
let form = document.querySelector("form");
let formData = new FormData(form);
fetch("{{.urlSave}}", {
method: "POST",
body: formData,
}).then((data) => {
console.log("Saved Note")
});
}, autoSaveDelay);
});
function makeChecklist(editor) {
editor.wrapSelection("* [ ] ", "")
};
</script>
{{end}}

View file

@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"strings"
"time"
"forgejo.gwairfelin.com/max/gonotes/internal/conf"
markdown "github.com/teekennedy/goldmark-markdown"
@ -36,6 +37,7 @@ type Note struct {
Owner string
Viewers map[string]struct{}
Uid string
LastModified time.Time
}
type NoteStore struct {
@ -63,7 +65,7 @@ func Init() error {
files, err := os.ReadDir(notesDir)
if err != nil {
if os.IsNotExist(err) {
os.MkdirAll(notesDir, os.FileMode(0750))
os.MkdirAll(notesDir, os.FileMode(0o750))
} else {
log.Print(err.Error())
return err
@ -165,7 +167,7 @@ func (n *Note) Save() error {
return os.WriteFile(
filename,
[]byte(fmt.Sprintf("---\n%s---\n%s", frontmatter, n.Body)),
0600,
0o600,
)
}
@ -190,6 +192,11 @@ func loadNote(uid string) (*Note, error) {
}
defer f.Close()
stat, err := os.Stat(filename)
if err != nil {
return nil, err
}
bodyScanner := bufio.NewScanner(f)
body := make([]byte, 0, 10)
fullBody := make([]byte, 0, 10)
@ -236,6 +243,7 @@ func loadNote(uid string) (*Note, error) {
note := NewNoteNoSave(title, owner)
note.Uid = uid
note.Body = body
note.LastModified = stat.ModTime()
viewers := metaData["viewers"].([]interface{})