Better Editor Forms #56

Merged
h4kor merged 6 commits from better_forms into main 2024-02-21 19:05:08 +00:00
13 changed files with 110 additions and 37 deletions
Showing only changes of commit 1fdcdff41d - Show all commits

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"owl-blogs/web/forms"
) )
type Note struct { type Note struct {
@ -13,11 +12,22 @@ type Note struct {
} }
type NoteMetaData struct { type NoteMetaData struct {
forms.DefaultForm
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
// Form implements model.EntryMetaData.
func (meta *NoteMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Note", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*NoteMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
return &NoteMetaData{
Content: data.FormValue("content"),
}, nil
}
func (e *Note) Title() string { func (e *Note) Title() string {
return "" return ""
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"owl-blogs/web/forms"
) )
type Page struct { type Page struct {
@ -13,12 +12,24 @@ type Page struct {
} }
type PageMetaData struct { type PageMetaData struct {
forms.DefaultForm
Title string `owl:"inputType=text"` Title string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
// Form implements model.EntryMetaData.
func (meta *PageMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Page", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*PageMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
return &PageMetaData{
Title: data.FormValue("title"),
Content: data.FormValue("content"),
}, nil
}
func (e *Page) Title() string { func (e *Page) Title() string {
return e.meta.Title return e.meta.Title
} }

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"owl-blogs/web/forms" "strings"
) )
type Recipe struct { type Recipe struct {
@ -13,8 +13,6 @@ type Recipe struct {
} }
type RecipeMetaData struct { type RecipeMetaData struct {
forms.DefaultForm
Title string `owl:"inputType=text"` Title string `owl:"inputType=text"`
Yield string `owl:"inputType=text"` Yield string `owl:"inputType=text"`
Duration string `owl:"inputType=text"` Duration string `owl:"inputType=text"`
@ -22,6 +20,30 @@ type RecipeMetaData struct {
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
// Form implements model.EntryMetaData.
func (meta *RecipeMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Recipe", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*RecipeMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
ings := strings.Split(data.FormValue("ingredients"), "\n")
clean := make([]string, 0)
for _, ing := range ings {
if strings.TrimSpace(ing) != "" {
clean = append(clean, strings.TrimSpace(ing))
}
}
return &RecipeMetaData{
Title: data.FormValue("title"),
Yield: data.FormValue("yield"),
Duration: data.FormValue("duration"),
Ingredients: clean,
Content: data.FormValue("content"),
}, nil
}
func (e *Recipe) Title() string { func (e *Recipe) Title() string {
return e.meta.Title return e.meta.Title
} }

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/render" "owl-blogs/render"
"owl-blogs/web/forms"
) )
type Reply struct { type Reply struct {
@ -13,13 +12,26 @@ type Reply struct {
} }
type ReplyMetaData struct { type ReplyMetaData struct {
forms.DefaultForm
Title string `owl:"inputType=text"` Title string `owl:"inputType=text"`
Url string `owl:"inputType=text"` Url string `owl:"inputType=text"`
Content string `owl:"inputType=text widget=textarea"` Content string `owl:"inputType=text widget=textarea"`
} }
// Form implements model.EntryMetaData.
func (meta *ReplyMetaData) Form(binSvc model.BinaryStorageInterface) string {
f, _ := render.RenderTemplateToString("forms/Reply", meta)
return f
}
// ParseFormData implements model.EntryMetaData.
func (*ReplyMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
return &ReplyMetaData{
Title: data.FormValue("title"),
Url: data.FormValue("url"),
Content: data.FormValue("content"),
}, nil
}
func (e *Reply) Title() string { func (e *Reply) Title() string {
return "Re: " + e.meta.Title return "Re: " + e.meta.Title
} }

View File

@ -2,6 +2,4 @@
<input type="text" name="title" value="{{.Title}}" /> <input type="text" name="title" value="{{.Title}}" />
<label for="content">Content</label> <label for="content">Content</label>
<textarea name="content" rows="16"> <textarea name="content" rows="16">{{.Content}}</textarea>
{{.Content}}
</textarea>

View File

@ -5,6 +5,4 @@
<input type="text" name="url" value="{{.Url}}" placeholder="https://..." /> <input type="text" name="url" value="{{.Url}}" placeholder="https://..." />
<label for="content">Content</label> <label for="content">Content</label>
<textarea name="content" rows="16"> <textarea name="content" rows="16">{{.Content}}</textarea></textarea>
{{.Content}}
</textarea>

View File

@ -5,6 +5,4 @@
<input type="text" name="title" value="{{.Title}}" /> <input type="text" name="title" value="{{.Title}}" />
<label for="content">Content</label> <label for="content">Content</label>
<textarea name="content" rows="16"> <textarea name="content" rows="16">{{.Content}}</textarea>
{{.Content}}
</textarea>

View File

@ -0,0 +1,2 @@
<label for="content">Content</label>
<textarea name="content" rows="8">{{.Content}}</textarea>

View File

@ -0,0 +1,5 @@
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="content">Content</label>
<textarea name="content" rows="16">{{.Content}}</textarea>

View File

@ -0,0 +1,15 @@
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="yield">Yield</label>
<input type="text" name="yield" value="{{.Yield}}" />
<label for="duration">Duration</label>
<input type="text" name="duration" value="{{.Duration}}" />
<label for="ingredients">Ingredients</label>
<textarea name="ingredients" rows="8">{{ range $i := .Ingredients }}
{{$i}}{{ end }}</textarea>
<label for="content">Content</label>
<textarea name="content" rows="16">{{.Content}}</textarea>

View File

@ -0,0 +1,8 @@
<label for="title">Title</label>
<input type="text" name="title" value="{{.Title}}" />
<label for="url">Reply To</label>
<input type="text" name="url" value="{{.Url}}" placeholder="https://..." />
<label for="content">Content</label>
<textarea name="content" rows="16">{{.Content}}</textarea></textarea>

View File

@ -2,19 +2,26 @@ package test
import ( import (
"owl-blogs/domain/model" "owl-blogs/domain/model"
"owl-blogs/web/forms"
"time" "time"
) )
type MockEntryMetaData struct { type MockEntryMetaData struct {
forms.DefaultForm
Str string Str string
Number int Number int
Date time.Time Date time.Time
Title string Title string
} }
// Form implements model.EntryMetaData.
func (*MockEntryMetaData) Form(binSvc model.BinaryStorageInterface) string {
panic("unimplemented")
}
// ParseFormData implements model.EntryMetaData.
func (*MockEntryMetaData) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
panic("unimplemented")
}
type MockEntry struct { type MockEntry struct {
model.EntryBase model.EntryBase
metaData *MockEntryMetaData metaData *MockEntryMetaData

View File

@ -7,19 +7,6 @@ import (
"strings" "strings"
) )
type DefaultForm struct{}
func (meta *DefaultForm) Form(binSvc model.BinaryStorageInterface) string {
form := NewForm(meta, nil)
htmlForm, _ := form.HtmlForm()
return htmlForm
}
func (meta *DefaultForm) ParseFormData(data model.HttpFormData, binSvc model.BinaryStorageInterface) (model.EntryMetaData, error) {
form := NewForm(meta, binSvc)
return form.Parse(data)
}
type Form[T interface{}] struct { type Form[T interface{}] struct {
data T data T
binSvc model.BinaryStorageInterface binSvc model.BinaryStorageInterface