~roselandgoose: 1 Create new hyphae from /edit 6 files changed, 44 insertions(+), 16 deletions(-)
Copy & paste the following snippet into your terminal to import this patchset into git:
curl -s https://lists.sr.ht/~bouncepaw/mycorrhiza-devel/patches/37054/mbox | git am -3Learn more about email & git
From: Rosie K Languet <rkl@rosiesworkshop.net> This modifies handlerEdit to include a Name input field when one is not given in the url. It starts implementing the first part of bouncepaw's first bullet point: https://github.com/bouncepaw/mycorrhiza/issues/124#issuecomment-1219453093 It also extends util.HyphaNameFromReq just a little to conditionally accept an empty hypha name. This new behavior is only enabled in the two locations required for this feature. --- backlinks/web.go | 2 +- history/histweb/histview.go | 2 +- hypview/view_edit.html | 17 +++++++++++++++-- util/util.go | 5 ++++- web/mutators.go | 26 +++++++++++++++++++------- web/readers.go | 8 ++++---- 6 files changed, 44 insertions(+), 16 deletions(-) diff --git a/backlinks/web.go b/backlinks/web.go index ff5cb75..41bfa06 100644 --- a/backlinks/web.go @@ -20,7 +20,7 @@ func InitHandlers(rtr *mux.Router) { // handlerBacklinks lists all backlinks to a hypha. func handlerBacklinks(w http.ResponseWriter, rq *http.Request) { var ( - hyphaName = util.HyphaNameFromRq(rq, "backlinks") + hyphaName = util.HyphaNameFromRq(rq, false, "backlinks") backlinks []string ) for b := range yieldHyphaBacklinks(hyphaName) { diff --git a/history/histweb/histview.go b/history/histweb/histview.go index 1f59d77..fd2b62d 100644 --- a/history/histweb/histview.go +++ b/history/histweb/histview.go @@ -73,7 +73,7 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) { // handlerHistory lists all revisions of a hypha. func handlerHistory(w http.ResponseWriter, rq *http.Request) { - hyphaName := util.HyphaNameFromRq(rq, "history") + hyphaName := util.HyphaNameFromRq(rq, false, "history") var list string // History can be found for files that do not exist anymore. diff --git a/hypview/view_edit.html b/hypview/view_edit.html index dd6bdc9..2dcd6e1 100644 --- a/hypview/view_edit.html +++ b/hypview/view_edit.html @@ -56,8 +56,21 @@ <form method="post" class="edit-form" action="/upload-text/{{.HyphaName}}"> <h1 class="edit__title"> {{if .IsNew}} - {{block "creating [[hypha]]" .HyphaName}} - Create <a href="/hypha/{{.}}">{{beautifulName .}}</a> + Create + {{if ne .HyphaName ""}} + {{block "creating [[hypha]]" .HyphaName}} + <a href="/hypha/{{.}}">{{beautifulName .}}</a> + {{end}} + {{else}} + <p class="edit-form__message-zone"> + <input + id="name" + type="text" + name="name" + class="edit-form__message" + placeholder="{{block "new hypha name" .}}New Hypha Name{{end}}" + aria-label="{{template "new hypha name" .}}"> + </p> {{end}} {{else}} {{block "editing [[hypha]]" .HyphaName}} diff --git a/util/util.go b/util/util.go index a00230c..27a6f2d 100644 --- a/util/util.go +++ b/util/util.go @@ -72,12 +72,15 @@ func IsProfileName(hyphaName string) bool { } // HyphaNameFromRq extracts hypha name from http request. You have to also pass the action which is embedded in the url or several actions. For url /hypha/hypha, the action would be "hypha". -func HyphaNameFromRq(rq *http.Request, actions ...string) string { +func HyphaNameFromRq(rq *http.Request, allowEmpty bool, actions ...string) string { p := rq.URL.Path for _, action := range actions { if strings.HasPrefix(p, "/"+action+"/") { return CanonicalName(strings.TrimPrefix(p, "/"+action+"/")) } + if allowEmpty && p == "/"+action { + return CanonicalName("") + } } log.Println("HyphaNameFromRq: this request is invalid, fall back to home hypha") return cfg.HomeHypha diff --git a/web/mutators.go b/web/mutators.go index e85ddda..1c24c17 100644 --- a/web/mutators.go +++ b/web/mutators.go @@ -19,10 +19,11 @@ import ( "github.com/bouncepaw/mycorrhiza/shroom" "github.com/bouncepaw/mycorrhiza/user" "github.com/bouncepaw/mycorrhiza/util" + "github.com/bouncepaw/mycorrhiza/cfg" ) func initMutators(r *mux.Router) { - r.PathPrefix("/edit/").HandlerFunc(handlerEdit) + r.PathPrefix("/edit").HandlerFunc(handlerEdit) r.PathPrefix("/rename/").HandlerFunc(handlerRename).Methods("GET", "POST") r.PathPrefix("/delete/").HandlerFunc(handlerDelete).Methods("GET", "POST") r.PathPrefix("/remove-media/").HandlerFunc(handlerRemoveMedia).Methods("GET", "POST") @@ -36,7 +37,7 @@ func handlerRemoveMedia(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( u = user.FromRequest(rq) - h = hyphae.ByName(util.HyphaNameFromRq(rq, "remove-media")) + h = hyphae.ByName(util.HyphaNameFromRq(rq, false, "remove-media")) meta = viewutil.MetaFrom(w, rq) ) if !u.CanProceed("remove-media") { @@ -64,7 +65,7 @@ func handlerDelete(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( u = user.FromRequest(rq) - h = hyphae.ByName(util.HyphaNameFromRq(rq, "delete")) + h = hyphae.ByName(util.HyphaNameFromRq(rq, false, "delete")) meta = viewutil.MetaFrom(w, rq) ) @@ -100,7 +101,7 @@ func handlerRename(w http.ResponseWriter, rq *http.Request) { var ( u = user.FromRequest(rq) lc = l18n.FromRequest(rq) - h = hyphae.ByName(util.HyphaNameFromRq(rq, "rename")) + h = hyphae.ByName(util.HyphaNameFromRq(rq, false, "rename")) meta = viewutil.MetaFrom(w, rq) ) @@ -145,7 +146,7 @@ func handlerEdit(w http.ResponseWriter, rq *http.Request) { lc = l18n.FromRequest(rq) meta = viewutil.MetaFrom(w, rq) - hyphaName = util.HyphaNameFromRq(rq, "edit") + hyphaName = util.HyphaNameFromRq(rq, true, "edit") h = hyphae.ByName(hyphaName) isNew bool @@ -179,7 +180,18 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) { u = user.FromRequest(rq) meta = viewutil.MetaFrom(w, rq) - hyphaName = util.HyphaNameFromRq(rq, "upload-text") + hyphaName = util.HyphaNameFromRq(rq, true, "upload-text") + ) + + if hyphaName == "" { + hyphaName = util.CanonicalName(rq.PostFormValue("name")) + if hyphaName == "" { + log.Println("handlerUploadText: this request is invalid, fall back to home hypha") + hyphaName = cfg.HomeHypha + } + } + + var ( h = hyphae.ByName(hyphaName) _, isNew = h.(*hyphae.EmptyHypha) @@ -207,7 +219,7 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) rq.ParseMultipartForm(10 << 20) // Set upload limit var ( - hyphaName = util.HyphaNameFromRq(rq, "upload-binary") + hyphaName = util.HyphaNameFromRq(rq, false, "upload-binary") h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) lc = l18n.FromRequest(rq) diff --git a/web/readers.go b/web/readers.go index 53f7576..845065c 100644 --- a/web/readers.go +++ b/web/readers.go @@ -40,7 +40,7 @@ func initReaders(r *mux.Router) { func handlerMedia(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( - hyphaName = util.HyphaNameFromRq(rq, "media") + hyphaName = util.HyphaNameFromRq(rq, false, "media") h = hyphae.ByName(hyphaName) u = user.FromRequest(rq) lc = l18n.FromRequest(rq) @@ -155,7 +155,7 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) { // handlerText serves raw source text of the hypha. func handlerText(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - hyphaName := util.HyphaNameFromRq(rq, "text") + hyphaName := util.HyphaNameFromRq(rq, false, "text") switch h := hyphae.ByName(hyphaName).(type) { case hyphae.ExistingHypha: log.Println("Serving", h.TextFilePath()) @@ -167,7 +167,7 @@ func handlerText(w http.ResponseWriter, rq *http.Request) { // handlerBinary serves attachment of the hypha. func handlerBinary(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) - hyphaName := util.HyphaNameFromRq(rq, "binary") + hyphaName := util.HyphaNameFromRq(rq, false, "binary") switch h := hyphae.ByName(hyphaName).(type) { case *hyphae.EmptyHypha: case *hyphae.TextualHypha: @@ -184,7 +184,7 @@ func handlerBinary(w http.ResponseWriter, rq *http.Request) { func handlerHypha(w http.ResponseWriter, rq *http.Request) { util.PrepareRq(rq) var ( - hyphaName = util.HyphaNameFromRq(rq, "page", "hypha") + hyphaName = util.HyphaNameFromRq(rq, false, "page", "hypha") h = hyphae.ByName(hyphaName) contents string openGraph string -- 2.34.5
Thanks a lot for your contribution! I have found the following issues: 1. The New Hypha Name field looks weird. We don't Title Case anything. We don't have this bold font anywhere in the text fields. I think you should make it more similar to the edit description field on the same page (/edit). 2. If I don't enter anything in the New Hypha Name field, the home hypha gets edited (not necessarily created). I expect the field to be required for submitting the form. 3. Changing HyphaNameFromRq's signature was not a good idea. I believe it is better to introduce a separate function called like OptionalHyphaNameFromRq or something like that. 4. When using the Russian locale, the title on /edit/h, where h is a name of a hypha that does not exist, is broken. Expected: Создание h Got: Create Создание h Other than that, the new feature works fine. Looking forward to a new version of the patch! ---- Extra thoughts, not really applied to your patch, I am just stating them here for the record: 1. There should be a way to show a link to /edit in the top bar for authorized users only. On anon wikis, that would be everyone. For now, maybe the link should be simply included for everyone? 2. Media hypha creation should be done someday in a similar fashion.