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 std/jsffi except `&`
import jsconsole import jsconsole
export jsconsole export jsconsole
@ -20,12 +20,46 @@ proc currentUri*(): Uri {.inline.} =
func replace*(c: kstring, sub: string, by = " "): kstring = func replace*(c: kstring, sub: string, by = " "): kstring =
($c).replace(sub, by).jss ($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 type
SortMethod* = enum SortMethod* = enum
smAlphabetical = "smAlphabetical", smCommitAge = "commit", smVersionAge = "version" smAlphabetical = "smAlphabetical", smCommitAge = "commit", smVersionAge = "version"
proc setSearchUrl*(searchQuery: kstring, sortMethod = smAlphabetical): proc() = proc setSearchUrl*(
proc() = searchQuery: kstring,
sortMethod = smAlphabetical
) {.kcall.} =
var var
url = currentUri() url = currentUri()
params: seq[(string, string)] params: seq[(string, string)]
@ -50,9 +84,7 @@ proc getSearchInput*() =
) )
setSearchUrl(searchInput, sortMethod)() setSearchUrl(searchInput, sortMethod)()
proc setSearchInput*(q: kstring) {.kcall.} =
proc setSearchInput*(q: kstring): proc() =
result = proc() =
let sortNode = getVNodeById("sort-select") let sortNode = getVNodeById("sort-select")
let sortMethod = SortMethod( let sortMethod = SortMethod(
if sortNode != nil: parseInt($sortNode.getInputText) if sortNode != nil: parseInt($sortNode.getInputText)

View file

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

View file

@ -17,8 +17,7 @@ type
var pgCtx = PageContext() var pgCtx = PageContext()
proc scrollToAnchor(a: string): proc() = proc scrollToAnchor(a: string){.kcall.} =
result = proc() =
let d = getVNodeById(a) let d = getVNodeById(a)
scrollIntoView(d.dom) scrollIntoView(d.dom)