diff --git a/cmd/frontend/articles.go b/cmd/frontend/articles.go index 130016d..2c0ada6 100644 --- a/cmd/frontend/articles.go +++ b/cmd/frontend/articles.go @@ -16,10 +16,18 @@ import ( b "streifling.com/jason/cpolis/cmd/backend" ) -type ArticlePreviewHtmlData struct { +const ( + EditMode = iota + PreviewMode +) + +type EditorHTMLData struct { Title string Description string - Content template.HTML + Content string + HTMLContent template.HTML + Tags []*b.Tag + Mode int } func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { @@ -31,14 +39,36 @@ func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) } + session.Values["article"] = nil + if err = session.Save(r, w); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"].(int)) } } -func WriteArticle(c *b.Config, db *b.DB) http.HandlerFunc { +func WriteArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - tags, err := db.GetTagList() + session, err := s.Get(r, "cookie") + if err != nil { + tmpl, err := template.ParseFiles(c.WebDir + "/templates/login.html") + msg := "Session nicht mehr gültig. Bitte erneut anmelden." + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) + } + + var data EditorHTMLData + if session.Values["article"] == nil { + data = EditorHTMLData{} + } else { + data = session.Values["article"].(EditorHTMLData) + } + data.Mode = EditMode + + data.Tags, err = db.GetTagList() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -46,7 +76,7 @@ func WriteArticle(c *b.Config, db *b.DB) http.HandlerFunc { } tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") - template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags) + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data) } } @@ -59,6 +89,13 @@ func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) } + session.Values["article"] = nil + if err = session.Save(r, w); err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + article := &b.Article{ Title: r.PostFormValue("article-title"), Description: r.PostFormValue("article-description"), @@ -449,7 +486,7 @@ func UploadImage(c *b.Config) http.HandlerFunc { func PreviewArticle(c *b.Config, s *b.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error - data := new(ArticlePreviewHtmlData) + data := EditorHTMLData{Mode: PreviewMode} data.Title, err = b.ConvertToPlain(r.PostFormValue("article-title")) if err != nil { @@ -465,13 +502,14 @@ func PreviewArticle(c *b.Config, s *b.CookieStore) http.HandlerFunc { return } - content, err := b.ConvertToHTML(r.PostFormValue("article-content")) + data.Content = r.PostFormValue("article-content") + content, err := b.ConvertToHTML(data.Content) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } - data.Content = template.HTML(content) + data.HTMLContent = template.HTML(content) session, err := s.Get(r, "cookie") if err != nil { @@ -489,6 +527,6 @@ func PreviewArticle(c *b.Config, s *b.CookieStore) http.HandlerFunc { tmpl, err := template.ParseFiles(c.WebDir + "/templates/editor.html") tmpl = template.Must(tmpl, err) - tmpl.ExecuteTemplate(w, "preview", data) + tmpl.ExecuteTemplate(w, "page-content", data) } } diff --git a/cmd/main.go b/cmd/main.go index 8e8c667..220e945 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -12,7 +12,7 @@ import ( func init() { gob.Register(b.User{}) - gob.Register(f.ArticlePreviewHtmlData{}) + gob.Register(f.EditorHTMLData{}) } func main() { @@ -68,7 +68,7 @@ func main() { mux.HandleFunc("GET /show-all-users-delete", f.ShowAllUsers(config, db, store, "delete-user")) mux.HandleFunc("GET /this-issue", f.ShowCurrentArticles(config, db)) mux.HandleFunc("GET /unpublished-articles", f.ShowUnpublishedArticles(config, db)) - mux.HandleFunc("GET /write-article", f.WriteArticle(config, db)) + mux.HandleFunc("GET /write-article", f.WriteArticle(config, db, store)) mux.HandleFunc("POST /add-first-user", f.AddFirstUser(config, db, store)) mux.HandleFunc("POST /add-tag", f.AddTag(config, db, store)) diff --git a/web/templates/editor.html b/web/templates/editor.html index b197a62..35b095a 100644 --- a/web/templates/editor.html +++ b/web/templates/editor.html @@ -2,28 +2,26 @@

Editor

-
- - -
+ {{if eq .Mode 0}} +
- +
- +
- +
Tags
- {{range .}} + {{range .Tags}}
@@ -41,6 +39,26 @@
+ {{else}} + + + Titel +
+ {{.Title}} +
+ + Beschreibung +
+ {{.Description}} +
+ + Artikel +
+
+ {{.HTMLContent}} +
+
+ {{end}}