mirror of
				https://github.com/osmarks/mycorrhiza.git
				synced 2025-10-31 15:43:00 +00:00 
			
		
		
		
	Make edit hypha page
This commit is contained in:
		
							
								
								
									
										28
									
								
								hypha.go
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								hypha.go
									
									
									
									
									
								
							| @@ -2,7 +2,10 @@ package main | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io/ioutil" | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| type Hypha struct { | ||||
| @@ -50,3 +53,28 @@ func (h *Hypha) NewestRevision() string { | ||||
| func (h *Hypha) ParentName() string { | ||||
| 	return h.parentName | ||||
| } | ||||
|  | ||||
| func ActionEdit(hyphaName string, w http.ResponseWriter) { | ||||
| 	w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||||
| 	var initContents, initTextMime, initBinaryMime, initTags string | ||||
| 	hypha, ok := hyphae[hyphaName] | ||||
| 	if !ok { | ||||
| 		initContents = "Describe " + hyphaName + "here." | ||||
| 		initTextMime = "text/markdown" | ||||
| 	} else { | ||||
| 		newestRev := hypha.Revisions[hypha.NewestRevision()] | ||||
| 		contents, err := ioutil.ReadFile(newestRev.TextPath) | ||||
| 		if err != nil { | ||||
| 			w.WriteHeader(http.StatusInternalServerError) | ||||
| 			w.Write([]byte("<b>Sorry, something went wrong</b>")) | ||||
| 			return | ||||
| 		} | ||||
| 		initContents = string(contents) | ||||
| 		initTextMime = newestRev.TextMime | ||||
| 		initBinaryMime = newestRev.BinaryMime | ||||
| 		initTags = strings.Join(newestRev.Tags, ",") | ||||
| 	} | ||||
|  | ||||
| 	w.WriteHeader(http.StatusOK) | ||||
| 	w.Write([]byte(EditHyphaPage(hyphaName, initTextMime, initBinaryMime, initContents, initTags))) | ||||
| } | ||||
|   | ||||
							
								
								
									
										4
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								main.go
									
									
									
									
									
								
							| @@ -118,8 +118,8 @@ func HandlerHistory(w http.ResponseWriter, r *http.Request) { | ||||
| } | ||||
|  | ||||
| func HandlerEdit(w http.ResponseWriter, r *http.Request) { | ||||
| 	w.WriteHeader(http.StatusNotImplemented) | ||||
| 	log.Println("Attempt to access an unimplemented thing") | ||||
| 	vars := mux.Vars(r) | ||||
| 	ActionEdit(vars["hypha"], w) | ||||
| } | ||||
|  | ||||
| func HandlerRewind(w http.ResponseWriter, r *http.Request) { | ||||
|   | ||||
							
								
								
									
										51
									
								
								render.go
									
									
									
									
									
								
							
							
						
						
									
										51
									
								
								render.go
									
									
									
									
									
								
							| @@ -24,6 +24,57 @@ func Layout(f map[string]string) string { | ||||
| 	return fmt.Sprintf(template, f["title"], f["head"], f["header"], f["main"], f["sidebar"], FooterText, f["bodyBottom"]) | ||||
| } | ||||
|  | ||||
| func EditHyphaPage(name, text_mime, binary_mime, content, tags string) string { | ||||
| 	template := ` | ||||
| <div class="naviwrapper"> | ||||
| 	<form class="naviwrapper__edit edit-box"> | ||||
| 		<div class="naviwrapper__buttons"> | ||||
| 			<input type="submit" name="action" value="update"/> | ||||
| 		</div> | ||||
|  | ||||
| 		<div class="edit-box__left"> | ||||
| 			<h4>Edit box</h4> | ||||
| 			<textarea class="edit-box__text" name="text" cols="80" rows="25"> | ||||
| %s | ||||
| 			</textarea> | ||||
|  | ||||
| 			<h4>Upload file</h4> | ||||
| 			<p>If this hypha has a file like that, the text above is meant to be a description of it</p> | ||||
| 			<input type="file" name="binary"/> | ||||
| 		</div> | ||||
|  | ||||
| 		<div class="edit-box__right"> | ||||
| 			<h4>Text MIME-type</h4> | ||||
| 			<p>Good types are <code>text/markdown</code> and <code>text/plain</code></p> | ||||
| 			<input type="text" name="text_mime" value="%s"/> | ||||
|  | ||||
| 			<h4>Media MIME-type</h4> | ||||
| 			<p>For now, only image formats are supported. Choose any, but <code>image/jpeg</code> and <code>image/png</code> are recommended</p> | ||||
| 			<input type="text" name="binary_mime" value="%s"/> | ||||
|  | ||||
| 			<h4>Revision comment</h4> | ||||
| 			<p>Please make your comment helpful</p> | ||||
| 			<input type="text" name="comment" value="%s"/> | ||||
|  | ||||
| 			<h4>Edit tags</h4> | ||||
| 			<p>Tags are separated by commas, whitespace is ignored</p> | ||||
| 			<input type="text" name="comment" value="%s"/> | ||||
| 		</div> | ||||
| 	</form> | ||||
| </div> | ||||
| ` | ||||
| 	args := map[string]string{ | ||||
| 		"title":   fmt.Sprintf(TitleTemplate, "Edit "+name), | ||||
| 		"head":    DefaultStyles, | ||||
| 		"header":  `<h1 class="header__edit-title">Edit ` + name + `</h1>`, | ||||
| 		"main":    fmt.Sprintf(template, content, text_mime, binary_mime, "Update "+name, tags), | ||||
| 		"sidebar": "", | ||||
| 		"footer":  FooterText, | ||||
| 	} | ||||
|  | ||||
| 	return Layout(args) | ||||
| } | ||||
|  | ||||
| func HyphaPage(hyphae map[string]*Hypha, rev Revision, content string) string { | ||||
| 	template := ` | ||||
| <div class="naviwrapper"> | ||||
|   | ||||
| @@ -1,2 +1,7 @@ | ||||
| b { color: red; } | ||||
| article { border: 1px black solid; } | ||||
| .edit-box { display: grid; grid-template-columns: 7fr 5fr; } | ||||
| .edit-box .naviwrapper__buttons { grid-column: 1; grid-row: 2 } | ||||
| .edit-box__left { grid-column: 1; grid-row: 2 } | ||||
| .edit-box__right { grid-column: 2; grid-row: 1 / span 2 } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Timur Ismagilov
					Timur Ismagilov