1
0
Fork 0
forked from jriou/coller

feat: Add text editor

Use Monaco Editor to create notes. Support syntax highlighting. Store the
language with the note in the database to later support syntax highlighting in
a note web view.

Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
Julien Riou 2025-08-27 22:42:12 +02:00
parent b45c3e3253
commit c54f32495b
Signed by: jriou
GPG key ID: 9A099EDA51316854
9 changed files with 170 additions and 24 deletions

View file

@ -11,6 +11,7 @@ import (
"log/slog"
"net/http"
"strconv"
"strings"
"git.riou.xyz/jriou/coller/internal"
"github.com/gorilla/mux"
@ -88,6 +89,7 @@ type CreateNotePayload struct {
Encrypted bool `json:"encrypted"`
Expiration int `json:"expiration"`
DeleteAfterRead bool `json:"delete_after_read"`
Language string `json:"language"`
}
type CreateNoteResponse struct {
@ -114,7 +116,7 @@ func (h *CreateNoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
note, err := h.db.Create(content, body.Password, body.Encrypted, body.Expiration, body.DeleteAfterRead)
note, err := h.db.Create(content, body.Password, body.Encrypted, body.Expiration, body.DeleteAfterRead, body.Language)
if err != nil {
WriteError(w, "could not create note", err)
return
@ -187,6 +189,7 @@ type PageData struct {
Title string
Version string
Expirations []int
Languages []string
Err error
URL string
}
@ -264,7 +267,7 @@ func (h *CreateNoteWithFormHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
}
h.logger.Debug("checking content")
if content == nil {
if content == nil || len(content) == 0 {
h.PageData.Err = fmt.Errorf("empty note")
h.Templates.ExecuteTemplate(w, templateName, h.PageData)
return
@ -275,6 +278,7 @@ func (h *CreateNoteWithFormHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
password := r.FormValue("password")
expiration := r.FormValue("expiration")
deleteAfterRead := r.FormValue("delete-after-read")
language := r.FormValue("language")
if password == "" && noPassword == "" {
h.logger.Debug("generating password")
@ -290,7 +294,7 @@ func (h *CreateNoteWithFormHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
}
h.logger.Debug("saving note to the database")
note, err := h.db.Create(content, password, password != "", expirationInt, deleteAfterRead != "")
note, err := h.db.Create(content, password, password != "", expirationInt, deleteAfterRead != "", language)
if err != nil {
h.PageData.Err = err
h.Templates.ExecuteTemplate(w, templateName, h.PageData)
@ -334,10 +338,12 @@ func (s *Server) Start() error {
// Web pages
funcs := template.FuncMap{
"HumanDuration": internal.HumanDuration,
"lower": strings.ToLower,
}
p := PageData{
Title: s.config.Title,
Expirations: s.config.Expirations,
Languages: s.config.Languages,
}
if s.config.ShowVersion {