simple page edit screen

This commit is contained in:
Iris Lightshard 2022-06-02 22:56:38 -06:00
parent 3f04afa16e
commit 358d4fb2af
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
5 changed files with 54 additions and 17 deletions

View file

@ -3,8 +3,8 @@ package archetype
type EditMode int
const (
EditModeLiteralTextArea EditMode = iota
EditModeEscapedContentEditable
EditModeLiteralTextArea EditMode = iota
EditModeEscapedContentEditable
)
type Adapter interface {

View file

@ -28,7 +28,7 @@ func (self *EurekaAdapter) Name() string {
}
func (self *EurekaAdapter) EditMode() EditMode {
return EditModeLiteralTextArea
return EditModeLiteralTextArea
}
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {

View file

@ -1,17 +1,28 @@
package lfo
import (
"context"
"net/http"
core "nilfm.cc/git/nirvash/archetype"
"context"
"net/http"
core "nilfm.cc/git/nirvash/archetype"
)
func WithAdapter(next http.Handler, adapter core.Adapter) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
*req = *req.WithContext(context.WithValue(req.Context(), "adapter", adapter))
next.ServeHTTP(w, req)
}
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
*req = *req.WithContext(context.WithValue(req.Context(), "adapter", adapter))
next.ServeHTTP(w, req)
}
return http.HandlerFunc(handlerFunc)
return http.HandlerFunc(handlerFunc)
}
func WithEditModes(next http.Handler) http.Handler {
handlerFunc := func(w http.ResponseWriter, req *http.Request) {
*req = *req.WithContext(context.WithValue(req.Context(), "edit-modes", map[string]core.EditMode{
"Literal": core.EditModeLiteralTextArea,
"Escaped": core.EditModeEscapedContentEditable,
}))
next.ServeHTTP(w, req)
}
return http.HandlerFunc(handlerFunc)
}

View file

@ -39,11 +39,18 @@ func main() {
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1"))
rtr.Get("/", middleware.Protected(
shell.WithAdapter(
renderer.Template(
"templates/cms_list.html",
"templates/header.html",
"templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login"))
shell.WithAdapter(
renderer.Template(
"templates/cms_list.html",
"templates/header.html",
"templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login"))
rtr.Get(`/edit/(?P<Slug>\S+)`, middleware.Protected(
shell.WithAdapter(
renderer.Template(
"templates/cms_edit.html",
"templates/header.html",
"templates/footer.html"), cfg.Adapter), http.MethodGet, udb, "/login"))
http.ListenAndServe(":8080", rtr)
}

19
templates/cms_edit.html Normal file
View file

@ -0,0 +1,19 @@
{{ $slug := ((.Context).Value "params").Slug }}
{{ $page := ((.Context).Value "adapter").GetPage $slug }}
{{ $editMode := ((.Context).Value "adapter").EditMode }}
{{ $editModes := (.Context).Value "edit-modes" }}
{{ template "header" . }}
<form method="POST" action="/save/{{$slug}}">
<textarea name="title">{{($page).Title}}</textarea>
<span>last edited {{($page).Edited}}</span>
{{ if eq $editMode 0 }}
<textarea name="content">{{($page).Content}}</textarea>
{{ else }}
<div contenteditable>{{($page).Content}}</div>
{{ end }}
<input type="submit" value="Save"/>
</form>
{{ template "footer" . }}