This commit is contained in:
Daylin Morgan 2024-11-25 14:05:16 -06:00
parent 77dbe8af05
commit 00e38fe2a5
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 63 additions and 33 deletions

View file

@ -1,4 +1,4 @@
import std/[strutils, uri]
import std/[strutils, uri, macros]
import std/jsffi except `&`
import jsconsole
export jsconsole
@ -20,26 +20,60 @@ proc currentUri*(): Uri {.inline.} =
func replace*(c: kstring, sub: string, by = " "): kstring =
($c).replace(sub, by).jss
macro kcall*(p: typed) =
## make procedure return another procedure that takes no arguments
##
## used for generating more succint callbacks compatible with karax
runnableExamples:
proc example(a: string) {.kcall.} =
echo a
example("hello world")()
expectKind p, nnkProcDef
if p.params[0].kind != nnkEmpty:
error "proc must return void"
var updated = copy(p)
let returnType = nnkProcTy.newTree(nnkFormalParams.newTree(newEmptyNode()), newEmptyNode())
updated.params = nnkFormalParams
.newTree(returnType)
.add(p.params[1 ..^ 1])
updated.body = nnkStmtList.newTree(
nnkLambda.newTree(
newEmptyNode(),
newEmptyNode(),
newEmptyNode(),
nnkFormalParams.newTree(newEmptyNode()),
newEmptyNode(),
newEmptyNode(),
p.body,
)
)
result = nnkStmtList.newTree()
result.add updated
type
SortMethod* = enum
smAlphabetical = "smAlphabetical", smCommitAge = "commit", smVersionAge = "version"
proc setSearchUrl*(searchQuery: kstring, sortMethod = smAlphabetical): proc() =
proc() =
var
url = currentUri()
params: seq[(string, string)]
if searchQuery != "":
params.add ("query", $searchQuery)
if sortMethod != smAlphabetical:
params.add ("sort", $sortMethod)
url.anchor = "/search"
url = url ? params
window.history.pushState(js{}, "".jss, url.jss)
let d = getVNodeById("search")
let node = d.dom
scrollIntoView(node)
redraw()
proc setSearchUrl*(
searchQuery: kstring,
sortMethod = smAlphabetical
) {.kcall.} =
var
url = currentUri()
params: seq[(string, string)]
if searchQuery != "":
params.add ("query", $searchQuery)
if sortMethod != smAlphabetical:
params.add ("sort", $sortMethod)
url.anchor = "/search"
url = url ? params
window.history.pushState(js{}, "".jss, url.jss)
let d = getVNodeById("search")
let node = d.dom
scrollIntoView(node)
redraw()
proc getSearchInput*() =
let searchInput = getVNodeById("search").getInputText
@ -50,15 +84,13 @@ proc getSearchInput*() =
)
setSearchUrl(searchInput, sortMethod)()
proc setSearchInput*(q: kstring): proc() =
result = proc() =
let sortNode = getVNodeById("sort-select")
let sortMethod = SortMethod(
if sortNode != nil: parseInt($sortNode.getInputText)
else: 0
)
setSearchUrl(q, sortMethod)()
proc setSearchInput*(q: kstring) {.kcall.} =
let sortNode = getVNodeById("sort-select")
let sortMethod = SortMethod(
if sortNode != nil: parseInt($sortNode.getInputText)
else: 0
)
setSearchUrl(q, sortMethod)()

View file

@ -6,9 +6,8 @@ import ../components/[tag, package]
import ../lib
import notfound
proc openLink(link: kstring): proc() =
result = proc() =
discard open(window, link, "_self")
proc openLink(link: kstring) {.kcall.} =
discard open(window, link, "_self")
proc versionTable(pkg: NimPackage): VNode =
var versions = pkg.versions

View file

@ -17,10 +17,9 @@ type
var pgCtx = PageContext()
proc scrollToAnchor(a: string): proc() =
result = proc() =
let d = getVNodeById(a)
scrollIntoView(d.dom)
proc scrollToAnchor(a: string){.kcall.} =
let d = getVNodeById(a)
scrollIntoView(d.dom)
proc letterlink(activeLinks: seq[char]): VNode = buildHtml:
tdiv(