diff --git a/example.html b/example.html index 2e90b4e..9b2fb48 100644 --- a/example.html +++ b/example.html @@ -7,12 +7,12 @@ +
+ -
- diff --git a/example.nim b/example.nim index d281cec..ab2e48d 100644 --- a/example.nim +++ b/example.nim @@ -1,5 +1,5 @@ -import vdom, karax, karaxdsl +import vdom, karax, karaxdsl, jdict when false: var plugins {.exportc.}: seq[(string, proc())] = @[] @@ -21,15 +21,24 @@ proc onTodoEnter(val: cstring) = proc onclickHandler(ev: Event; n: VNode) = let id = suffixAsInt(n.id, "remove:") - entries.delete(id) + #entries.delete(id) + entries[id] = nil + +var entryCache = newJDict[int, VNode]() proc createEntry(i: int; d: cstring): VNode = + # implement caching: + if entryCache.contains(i): + let old = entryCache[i] + return old + result = buildHtml(tr) do: td: text d td: span(id="remove:" & $i, onclick=onclickHandler): text "[remove]" + entryCache[i] = result proc createDom(): VNode = result = buildHtml(tdiv) do: @@ -42,7 +51,8 @@ proc createDom(): VNode = enterInput("todo-input", "", onTodoEnter) table(class = "wl"): for i, d in entries: - createEntry(i, d) + if d != nil: + createEntry(i, d) setRenderer createDom diff --git a/jdict.nim b/jdict.nim new file mode 100644 index 0000000..7d27183 --- /dev/null +++ b/jdict.nim @@ -0,0 +1,11 @@ + +type + JDict*[K, V] = ref object + +proc `[]`*[K, V](d: JDict[K, V], k: K): V {.importcpp: "#[#]".} +proc `[]=`*[K, V](d: JDict[K, V], k: K, v: V) {.importcpp: "#[#] = #".} + +proc newJDict*[K, V](): JDict[K, V] {.importcpp: "{@}".} + +proc contains*[K, V](d: JDict[K, V], k: K): bool {.importcpp: "#.hasOwnProperty(#)".} + diff --git a/karax.nim b/karax.nim index 690add0..060f95d 100644 --- a/karax.nim +++ b/karax.nim @@ -138,9 +138,12 @@ proc dodraw() = proc redraw*() = # we buffer redraw requests: - if drawTimeout != nil: - clearTimeout(drawTimeout) - drawTimeout = setTimeout(dodraw, 30) + when false: + if drawTimeout != nil: + clearTimeout(drawTimeout) + drawTimeout = setTimeout(dodraw, 30) + else: + dodraw() #proc prepend*(parent, kid: Element) = # parent.insertBefore(kid, parent.firstChild) diff --git a/vdom.nim b/vdom.nim index 6ca43e2..af5a95c 100644 --- a/vdom.nim +++ b/vdom.nim @@ -39,6 +39,21 @@ type proc value*(n: VNode): cstring = n.text proc `value=`*(n: VNode; v: cstring) = n.text = v +proc eq*(a, b: VNode): bool = + if a.kind != b.kind: return false + if a.id != b.id: return false + if a.class != b.class: return false + if a.kind != VNodeKind.text: + if a.kids.len != b.kids.len: return false + for i in 0..\L" + if n.kind == VNodeKind.text: + result.add n.text + else: + if n.text != nil: + result.add " value = " + result.add n.text + for child in items(n): + toString(child, result, indent+2) + for i in 1..indent: result.add ' ' + result.add "\L" + +proc `$`*(n: VNode): cstring = + var res = "" + toString(n, res, 0) + result = cstring(res)