diff --git a/karax/karax.nim b/karax/karax.nim index 142746c..31e83e0 100644 --- a/karax/karax.nim +++ b/karax/karax.nim @@ -829,6 +829,14 @@ proc addEventHandler*(n: VNode; k: EventKind; action: proc(); if not kxi.surpressRedraws: redraw(kxi) addEventListener(n, k, wrapper) +proc addEventHandlerNoRedraw*(n: VNode; k: EventKind; action: EventHandler) = + addEventListener(n, k, action) + +proc addEventHandlerNoRedraw*(n: VNode; k: EventKind; action: proc()) = + proc wrapper(ev: Event; n: VNode) = + action() + addEventListener(n, k, wrapper) + proc setOnHashChange*(action: proc (hashPart: cstring)) {.deprecated: "use setRenderer instead".} = ## Now deprecated, instead pass a callback to ``setRenderer`` that receives ## a ``data: RouterData`` parameter. diff --git a/karax/karaxdsl.nim b/karax/karaxdsl.nim index a73c3f5..8980aab 100644 --- a/karax/karaxdsl.nim +++ b/karax/karaxdsl.nim @@ -1,6 +1,6 @@ import macros, vdom, compact, kbase -from strutils import startsWith, toLowerAscii +from strutils import startsWith, toLowerAscii, cmpIgnoreStyle when defined(js): import karax @@ -44,6 +44,21 @@ proc toKstring(n: NimNode): NimNode = proc newDotAsgn(tmp: NimNode, key: string, x: NimNode): NimNode = result = newTree(nnkAsgn, newDotExpr(tmp, newIdentNode key), x) +proc handleNoRedrawPragma(call: NimNode, tmpContext, name, anon: NimNode): NimNode = + when defined(js): + if anon.pragma.kind == nnkPragma and len(anon.pragma) > 0: + var hasNoRedrawPragma = false + for i in 0 ..< len(anon.pragma): + # using anon because anon needs to get rid of the pragma + if anon.pragma[i].kind == nnkIdent and cmpIgnoreStyle(anon.pragma[i].strVal, "noredraw") == 0: + hasNoRedrawPragma = true + anon.pragma.del(i) + break + if hasNoRedrawPragma: + return newCall(ident"addEventHandlerNoRedraw", tmpContext, + newDotExpr(bindSym"EventKind", name), anon) + result = call + proc tcall2(n, tmpContext: NimNode): NimNode = # we need to distinguish statement and expression contexts: # every call statement 's' needs to be transformed to 'dest.add s'. @@ -94,8 +109,9 @@ proc tcall2(n, tmpContext: NimNode): NimNode = if tmpContext == nil: error "no VNode to attach the event handler to" else: - result = newCall(evHandler(), tmpContext, - newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi")) + let call = newCall(evHandler(), tmpContext, + newDotExpr(bindSym"EventKind", n[0]), anon, ident("kxi")) + result = handleNoRedrawPragma(call, tmpContext, n[0], anon) else: result = n of nnkVarSection, nnkLetSection, nnkConstSection: