diff --git a/src/lib.nim b/src/lib.nim index fb1c383..45584c5 100644 --- a/src/lib.nim +++ b/src/lib.nim @@ -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)() diff --git a/src/pages/package.nim b/src/pages/package.nim index ed0a186..a8eecf1 100644 --- a/src/pages/package.nim +++ b/src/pages/package.nim @@ -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 diff --git a/src/pages/search.nim b/src/pages/search.nim index e28922d..3825182 100644 --- a/src/pages/search.nim +++ b/src/pages/search.nim @@ -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(