mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
List works
Former-commit-id: d123891bd72ab552a24bbccfd614270de340dac5 [formerly ce7c2d1b9dbdcaf5ef2d665e647833f065c18246] [formerly fc2e4872180a9f264e57e348e2ec4e5896781e74 [formerly 67d5d2f882
]]
Former-commit-id: 1d9e245d81cab6cfe197bc52b516154c7429421b [formerly 1f22520a0e2509d0a509a592d1fed50002eccebc]
Former-commit-id: b60b97411ea8c9b54b26b8058aeda0f1f0466b64
This commit is contained in:
parent
522c4c6283
commit
fb5ee8d09e
38
handlers.go
38
handlers.go
@ -4,6 +4,7 @@ import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/static"
|
||||
@ -27,6 +28,7 @@ func serve(port string) {
|
||||
router.POST("/prime", handlePrime)
|
||||
router.POST("/lock", handleLock)
|
||||
router.POST("/encrypt", handleEncrypt)
|
||||
router.DELETE("/listitem", deleteListItem)
|
||||
|
||||
router.Run(":" + port)
|
||||
}
|
||||
@ -85,6 +87,7 @@ func handlePageRequest(c *gin.Context) {
|
||||
"VersionsText": versionsText,
|
||||
"IsLocked": p.IsLocked,
|
||||
"IsEncrypted": p.IsEncrypted,
|
||||
"ListItems": renderList(rawText),
|
||||
})
|
||||
}
|
||||
|
||||
@ -211,3 +214,38 @@ func handleEncrypt(c *gin.Context) {
|
||||
q.Save()
|
||||
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
62
listify.go
Normal 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
|
||||
}
|
@ -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>
|
||||
@ -284,6 +300,11 @@
|
||||
{{end}}
|
||||
</ul>
|
||||
{{ end }}
|
||||
{{ if .ListPage }}
|
||||
{{ range $index, $element := .ListItems }}
|
||||
{{ $element }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
</div>
|
||||
</article>
|
||||
|
Loading…
Reference in New Issue
Block a user