diff --git a/src/jdict.nim b/src/jdict.nim index b239af2..6e2aa9f 100644 --- a/src/jdict.nim +++ b/src/jdict.nim @@ -17,7 +17,7 @@ type proc `[]`*[T](s: JSeq[T], i: int): T {.importcpp: "#[#]", noSideEffect.} proc `[]=`*[T](s: JSeq[T], i: int, v: T) {.importcpp: "#[#] = #", noSideEffect.} -proc newJSeq*[T](): JSeq[T] {.importcpp: "[@]".} +proc newJSeq*[T](len: int = 0): JSeq[T] {.importcpp: "new Array(#)".} proc len*[T](s: JSeq[T]): int {.importcpp: "#.length", noSideEffect.} proc add*[T](s: JSeq[T]; x: T) {.importcpp: "#.push(#)", noSideEffect.} diff --git a/src/karax.nim b/src/karax.nim index 5ea7d62..ad8ddfc 100644 --- a/src/karax.nim +++ b/src/karax.nim @@ -14,6 +14,14 @@ proc key*(e: Node): VKey = else: result = -1 proc `key=`*(e: Node; x: VKey) {.importcpp: "#.karaxKey = #", nodecl.} +type + PatchKind = enum + pkReplace, pkRemove, pkAppend, pkInsertBefore + Patch = object + k: PatchKind + parent, current: Node + n: VNode + type KaraxInstance* = ref object ## underlying karax instance. Usually you don't have ## know about this. @@ -24,6 +32,9 @@ type toFocus: Node toFocusV: VNode renderId: int + patches: seq[Patch] # we reuse this to save allocations + patchLen: int + var kxi*: KaraxInstance ## The current Karax instance. This is always used @@ -33,8 +44,9 @@ var ## in your 'buildHtml' statement, it needs to be named ## 'kxi'. -proc setFocus*(n: VNode; kxi: KaraxInstance = kxi) = - kxi.toFocusV = n +proc setFocus*(n: VNode; enabled = true; kxi: KaraxInstance = kxi) = + if enabled: + kxi.toFocusV = n # ----------------- event wrapping --------------------------------------- @@ -199,7 +211,7 @@ proc updateStyles(newNode, oldNode: VNode) = else: oldNode.dom.style = Style() oldNode.style = newNode.style -proc mergeEvents(newNode, oldNode: VNode) = +proc mergeEvents(newNode, oldNode: VNode; kxi: KaraxInstance) = let d = oldNode.dom for i in 0..= kxi.patches.len: + # allocate more space: + kxi.patches.add(Patch(k: ka, parent: parenta, current: currenta, n: na)) + else: + kxi.patches[L].k = ka + kxi.patches[L].parent = parenta + kxi.patches[L].current = currenta + kxi.patches[L].n = na + inc kxi.patchLen -proc addPatch(patches: JSeq[Patch]; k: PatchKind; parent, current: Node; - n: VNode) = - patches.add(Patch(k: k, parent: parent, current: current, n: n)) - -proc apply(patches: JSeq[Patch]; kxi: KaraxInstance) = - for i in 0..