1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

List works

This commit is contained in:
Zack Scholl 2017-03-22 11:19:39 -06:00
parent 420f56843f
commit 67d5d2f882
3 changed files with 121 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/gin-contrib/static" "github.com/gin-contrib/static"
@ -27,6 +28,7 @@ func serve(port string) {
router.POST("/prime", handlePrime) router.POST("/prime", handlePrime)
router.POST("/lock", handleLock) router.POST("/lock", handleLock)
router.POST("/encrypt", handleEncrypt) router.POST("/encrypt", handleEncrypt)
router.DELETE("/listitem", deleteListItem)
router.Run(":" + port) router.Run(":" + port)
} }
@ -85,6 +87,7 @@ func handlePageRequest(c *gin.Context) {
"VersionsText": versionsText, "VersionsText": versionsText,
"IsLocked": p.IsLocked, "IsLocked": p.IsLocked,
"IsEncrypted": p.IsEncrypted, "IsEncrypted": p.IsEncrypted,
"ListItems": renderList(rawText),
}) })
} }
@ -211,3 +214,38 @@ func handleEncrypt(c *gin.Context) {
q.Save() q.Save()
c.JSON(http.StatusOK, gin.H{"success": true, "message": message}) c.JSON(http.StatusOK, gin.H{"success": true, "message": message})
} }
func deleteListItem(c *gin.Context) {
lineNum, err := strconv.Atoi(c.DefaultQuery("lineNum", "None"))
page := c.Query("page") // shortcut for c.Request.URL.Query().Get("lastname")
if err == nil {
p := Open(page)
_, listItems := reorderList(p.Text.GetCurrent())
newText := p.Text.GetCurrent()
for i, lineString := range listItems {
// fmt.Println(i, lineString, lineNum)
if i+1 == lineNum {
// fmt.Println("MATCHED")
if strings.Contains(lineString, "~~") == false {
// fmt.Println(p.Text, "("+lineString[2:]+"\n"+")", "~~"+lineString[2:]+"~~"+"\n")
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", "~~"+strings.TrimSpace(lineString[2:])+"~~"+"\n", 1)
} else {
newText = strings.Replace(newText+"\n", lineString[2:]+"\n", lineString[4:len(lineString)-2]+"\n", 1)
}
p.Update(newText)
break
}
}
c.JSON(200, gin.H{
"success": true,
"message": "Done.",
})
} else {
c.JSON(200, gin.H{
"success": false,
"message": err.Error(),
})
}
}

62
listify.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"html/template"
"strconv"
"strings"
)
func reorderList(text string) ([]template.HTML, []string) {
listItemsString := ""
for _, lineString := range strings.Split(text, "\n") {
if len(lineString) > 1 {
if string(lineString[0]) != "-" {
listItemsString += "- " + lineString + "\n"
} else {
listItemsString += lineString + "\n"
}
}
}
// get ordering of template.HTML for rendering
renderedListString := MarkdownToHtml(listItemsString)
listItems := []template.HTML{}
endItems := []template.HTML{}
for _, lineString := range strings.Split(renderedListString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "<del>") || strings.Contains(lineString, "</ul>") {
endItems = append(endItems, template.HTML(lineString))
} else {
listItems = append(listItems, template.HTML(lineString))
}
}
}
// get ordering of strings for deleting
listItemsStringArray := []string{}
endItemsStringArray := []string{}
for _, lineString := range strings.Split(listItemsString, "\n") {
if len(lineString) > 1 {
if strings.Contains(lineString, "~~") {
endItemsStringArray = append(endItemsStringArray, lineString)
} else {
listItemsStringArray = append(listItemsStringArray, lineString)
}
}
}
return append(listItems, endItems...), append(listItemsStringArray, endItemsStringArray...)
}
func renderList(currentRawText string) []template.HTML {
listItems, _ := reorderList(currentRawText)
for i := range listItems {
newHTML := strings.Replace(string(listItems[i]), "</a>", "</a>"+`<span id="`+strconv.Itoa(i)+`" class="deletable">`, -1)
newHTML = strings.Replace(newHTML, "<a href=", "</span><a href=", -1)
newHTML = strings.Replace(newHTML, "<li>", "<li>"+`<span id="`+strconv.Itoa(i)+`" class="deletable">`, -1)
newHTML = strings.Replace(newHTML, "</li>", "</span></li>", -1)
newHTML = strings.Replace(newHTML, "<li>"+`<span id="`+strconv.Itoa(i)+`" class="deletable"><del>`, "<li><del>"+`<span id="`+strconv.Itoa(i)+`" class="deletable">`, -1)
newHTML = strings.Replace(newHTML, "</del></span></li>", "</span></del></li>", -1)
listItems[i] = template.HTML([]byte(newHTML))
}
return listItems
}

View File

@ -241,6 +241,22 @@
} }
}); });
$('.deletable').click(function(event) {
event.preventDefault();
var lineNum = $(this).attr('id')
$.ajax({
url: "/listitem" + '?' + $.param({
"lineNum": lineNum,
"page": "{{ .Page }}"
}),
type: 'DELETE',
success: function() {
window.location.reload(true);
}
});
});
}); //]]> }); //]]>
</script> </script>
@ -284,6 +300,11 @@
{{end}} {{end}}
</ul> </ul>
{{ end }} {{ end }}
{{ if .ListPage }}
{{ range $index, $element := .ListItems }}
{{ $element }}
{{ end }}
{{ end }}
</div> </div>
</article> </article>