diff --git a/public/style.css b/public/style.css index b8c8820..95c3d13 100644 --- a/public/style.css +++ b/public/style.css @@ -253,9 +253,12 @@ summary { border-top: 1px dotted gray; font-size: 12px; } -.comment form.savecomment { +form.savecomment { margin: 0px 0px 10px 10px; } +.comment > .children > form.savecomment { + margin: 0px 0px 10px 20px; +} .savecomment textarea { width: 500px; height: 100px; @@ -370,7 +373,6 @@ form.nsfw div { } .pager { margin: 10px; - display: none; } .pager a { padding: 1px 4px; @@ -381,10 +383,18 @@ form.nsfw div { text-decoration: none; color: #369; } +.pager.hidden { + display: none; +} +#loadmore { + display: none; +} #loadmore, #end { - visibility: hidden; margin: 10px 0px; } +#loadmore.show { + display: block; +} #loadmore[disabled] { visibility: visible; } @@ -475,6 +485,23 @@ form.nsfw div { position: relative; color: #000; } +#settings { + background-color: white; + border: 1px solid #888; + display: none; + position: absolute; + right: 10px; + top: 45px; +} +#settings form { + margin: 0px; +} +.dark #settings { + background-color: #262626; +} +#settings.open { + display: inline-block; +} .expando.open{ display: block; } @@ -980,7 +1007,7 @@ form.create input[type=file], form.create select { } .preferences label{ display: inline-block; - width: 120px; + width: 100px; text-align: right; } diff --git a/public/utils.js b/public/utils.js index 458610a..5af1ba4 100644 --- a/public/utils.js +++ b/public/utils.js @@ -1,8 +1,10 @@ -function request(url, params, callback, errorcallback) { +function request(url, params, callback, errorcallback = function(){}) { var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() { - if (xmlHttp.readyState == 4 && xmlHttp.status == 200) + if (xmlHttp.readyState != 4 ) { return } + if (xmlHttp.status == 200) { return callback(xmlHttp.responseText); + } errorcallback(xmlHttp.responseText); } var method = "GET" @@ -41,7 +43,11 @@ function commentClick(e) { var form = e.target.parentNode if (form) { data = new FormData(form) - if (("c"+data.get("commentid")) != targ.id) { return } + if (("c"+data.get("commentid")) == targ.id) { + + } else if (("c"+data.get("parentid")) == targ.id) { + targ = form + } else { return } params = new URLSearchParams(data).toString() params += "&" + e.target.name + "=" + e.target.value params += "&xhr=1" @@ -114,6 +120,8 @@ function loadMore(e) { toggle_images(true) } } + var loadmore = document.getElementById("loadmore") + if (loadmore) loadmore.className = "show" } else { e.target.outerHTML = '' @@ -167,6 +175,47 @@ function formSubmit(e) { return false } +function open_settings(e) { + e.preventDefault() + var settings = document.getElementById("settings") + settings.className = "open" + request(e.target.href + "?xhr=1", "", function(res) { + settings.innerHTML = res + var options = document.getElementsByClassName("scripting") + for (var i = 0; i < options.length; i++) { + var input = options[i].getElementsByTagName('input') + if (!input.length) { continue } + if (localStorage.getItem(input[0].name) == "true") { + input[0].checked = "checked" + } + } + }) + return false +} + +function close_settings(e) { + e.preventDefault() + var settings = document.getElementById("settings") + settings.className = "" + return false +} + +function save_settings(e) { + e = e || window.event; + var targ = e.currentTarget || e.srcElement || e; + var data = new FormData(targ) + console.log(data) + e.preventDefault() + var params = new URLSearchParams(data).toString() + request(targ.target, params, function(res) { + ["endlessScrolling", "autoLoad"].map(function(x) { + localStorage.setItem(x, data.get(x)=="on") + }) + window.location.reload() + }) + return false; +} + function parse_youtube(url){ if (url.indexOf("youtu") == -1) return false var regExp = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/; @@ -227,12 +276,22 @@ for (var i = 0; i < posts.length; i++) { } } -window.onscroll = function(ev) { - if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) { - var loadmore = document.getElementById("loadmore") - if (loadmore) { - loadmore.click() - } - } -}; +if (localStorage.getItem("endlessScrolling") == "true") { + var pager = document.getElementsByClassName("pager") + if (pager.length) pager[0].className = "pager hidden" + var loadmore = document.getElementById("loadmore") + if (loadmore) loadmore.className = "show" + + if (localStorage.getItem("autoLoad") == "true") { + window.onscroll = function(e) { + if ((window.innerHeight + Math.round(window.scrollY)) >= document.body.offsetHeight) { + var loadmore = document.getElementById("loadmore") + if (loadmore) { + loadmore.click() + } + } + }; + } +} + diff --git a/routes.go b/routes.go index 71c388e..c32c9a4 100644 --- a/routes.go +++ b/routes.go @@ -147,7 +147,7 @@ var funcMap = template.FuncMap{ converted = re.ReplaceAllString(converted, `href="/`+host+`/$1`) re = regexp.MustCompile(` !([a-zA-Z0-9]+)@([a-zA-Z0-9\.\-]+) `) converted = re.ReplaceAllString(converted, ` !$1@$2 `) - re = regexp.MustCompile(`::: spoiler (.*?)\n(.*?)\s:::`) + re = regexp.MustCompile(`::: spoiler (.*?)\n([\S\s]*?):::`) converted = re.ReplaceAllString(converted, "
$1$2
") return template.HTML(converted) }, @@ -999,33 +999,59 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { parentid, _ := strconv.Atoi(r.FormValue("parentid")) state.GetComment(parentid) } - createComment := types.CreateComment{ - Content: r.FormValue("content"), - PostID: state.PostID, + if r.FormValue("submit") == "save" { + createComment := types.CreateComment{ + Content: r.FormValue("content"), + PostID: state.PostID, + } + if state.CommentID > 0 { + createComment.ParentID = types.NewOptional(state.CommentID) + } + resp, err := state.Client.CreateComment(context.Background(), createComment) + if err == nil { + if r.FormValue("xhr") != "" { + state.XHR = true + state.Comments = nil + state.GetComment(resp.CommentView.Comment.ID) + Render(w, "index.html", state) + return + } + postid := strconv.Itoa(state.PostID) + commentid := strconv.Itoa(resp.CommentView.Comment.ID) + r.URL.Path = "/" + state.Host + "/post/" + postid + r.URL.Fragment = "c" + commentid + } else { + fmt.Println(err) + } + } else if r.FormValue("xhr") != "" { + w.Write([]byte{}) + return } - if state.CommentID > 0 { - createComment.ParentID = types.NewOptional(state.CommentID) - } - resp, err := state.Client.CreateComment(context.Background(), createComment) - if err == nil { - postid := strconv.Itoa(state.PostID) - commentid := strconv.Itoa(resp.CommentView.Comment.ID) - r.URL.Path = "/" + state.Host + "/post/" + postid - r.URL.Fragment = "c" + commentid - } else { - fmt.Println(err) + if r.FormValue("submit") == "cancel" { + r.URL.RawQuery = "" } case "edit_comment": commentid, _ := strconv.Atoi(r.FormValue("commentid")) - resp, err := state.Client.EditComment(context.Background(), types.EditComment{ - CommentID: commentid, - Content: types.NewOptional(r.FormValue("content")), - }) - if err != nil { - fmt.Println(err) - } else { - commentid := strconv.Itoa(resp.CommentView.Comment.ID) - r.URL.Fragment = "c" + commentid + if r.FormValue("submit") == "save" { + resp, err := state.Client.EditComment(context.Background(), types.EditComment{ + CommentID: commentid, + Content: types.NewOptional(r.FormValue("content")), + }) + if err != nil { + fmt.Println(err) + } else { + commentid := strconv.Itoa(resp.CommentView.Comment.ID) + r.URL.Fragment = "c" + commentid + r.URL.RawQuery = "" + } + } + if r.FormValue("xhr") != "" { + state.XHR = true + state.GetComment(commentid) + Render(w, "index.html", state) + return + } + if r.FormValue("submit") == "cancel" { r.URL.RawQuery = "" } case "delete_comment": diff --git a/templates/comment.html b/templates/comment.html index cdd0e9b..3533ac2 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -28,17 +28,25 @@ {{ end }} {{ if eq .Op "edit" }} +
- + +
{{ else }} -
- {{if .P.Comment.Deleted}}[removed]{{else}}{{ markdown .State.Host .P.Comment.Content }}{{end}} +
+ {{if .P.Comment.Deleted}} + [removed] + {{else}} +
+ {{ markdown .State.Host .P.Comment.Content }} +
+ {{end}} {{ if eq .Op "source" }}
{{end}} @@ -73,13 +81,11 @@ {{ end }} - {{ if ne .Op "reply" }}
  • reply
  • - {{ end }} {{ end }} {{ if gt .ChildCount 0 }}
  • hideshow {{ .ChildCount }} child comments
  • @@ -90,11 +96,12 @@ {{ if and (eq .State.Op "reply") (eq .State.CommentID .P.Comment.ID)}}
    - +
    - + +
    {{ end}} {{ range $ci, $child := .C }}{{ template "comment.html" $child }}{{end}} diff --git a/templates/frontpage.html b/templates/frontpage.html index 76154ac..806e152 100644 --- a/templates/frontpage.html +++ b/templates/frontpage.html @@ -2,7 +2,7 @@ {{ if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - + @@ -56,6 +56,6 @@ {{ template "sidebar.html" . }} {{ end }} - + diff --git a/templates/main.html b/templates/main.html index 7caa5bf..6cc7a1e 100644 --- a/templates/main.html +++ b/templates/main.html @@ -3,21 +3,18 @@ {{if and .Posts .PostID }}{{ (index .Posts 0).Post.Name}} : {{.CommunityName}}{{else if and .Community (ne .Community.CommunityView.Community.Title "")}}{{.Community.CommunityView.Community.Title}}{{else if ne .CommunityName ""}}/c/{{.CommunityName}}{{ else if .User}}overview for {{.User.PersonView.Person.Name}}{{else}}{{ host .Host }}{{end}} - +