Merge branch 'master' of github.com:pragmagic/karax

This commit is contained in:
Andreas Rumpf 2018-06-10 12:09:36 +02:00
commit 451ee92dd0
7 changed files with 39 additions and 8 deletions

2
.gitignore vendored
View file

@ -1,5 +1,5 @@
nimcache/ nimcache/
/tests/tester /tests/tester
/tools/karun karax/tools/karun
*.code-workspace *.code-workspace
*.exe *.exe

View file

@ -9,6 +9,6 @@ license = "MIT"
requires "nim >= 0.16.1" requires "nim >= 0.16.1"
skipDirs = @["examples", "experiments"] skipDirs = @["examples", "experiments", "tests"]
bin = @["tools/karun"] bin = @["karax/tools/karun"]

View file

@ -38,7 +38,7 @@ proc ajax*(meth, url: cstring; headers: openarray[(cstring, cstring)];
if this.status == 200: if this.status == 200:
contWrapper(this.status, this.responseText) contWrapper(this.status, this.responseText)
else: else:
contWrapper(this.status, this.statusText) contWrapper(this.status, this.responseText)
if useBinary: if useBinary:
ajax.send(blob) ajax.send(blob)
else: else:

View file

@ -4,6 +4,9 @@ import kdom, vdom, jstrutils, compact, jdict, vstyles
export kdom.Event, kdom.Blob export kdom.Event, kdom.Blob
when defined(nimNoNil):
{.experimental: "notnil".}
proc kout*[T](x: T) {.importc: "console.log", varargs, deprecated.} proc kout*[T](x: T) {.importc: "console.log", varargs, deprecated.}
## the preferred way of debugging karax applications. Now deprecated, ## the preferred way of debugging karax applications. Now deprecated,
## you can now use ``system.echo`` instead. ## you can now use ``system.echo`` instead.
@ -135,6 +138,11 @@ proc vnodeToDom*(n: VNode; kxi: KaraxInstance): Node =
if n.kind == VNodeKind.text: if n.kind == VNodeKind.text:
result = document.createTextNode(n.text) result = document.createTextNode(n.text)
attach n attach n
elif n.kind == VNodeKind.verbatim:
result = document.createElement("div")
result.innerHTML = n.text
attach n
return result
elif n.kind == VNodeKind.vthunk: elif n.kind == VNodeKind.vthunk:
let x = callThunk(vcomponents[n.text], n) let x = callThunk(vcomponents[n.text], n)
result = vnodeToDom(x, kxi) result = vnodeToDom(x, kxi)
@ -183,14 +191,17 @@ proc same(n: VNode, e: Node; nesting = 0): bool =
if kxi.orphans.contains(n.id): return true if kxi.orphans.contains(n.id): return true
if n.kind == VNodeKind.component: if n.kind == VNodeKind.component:
result = same(VComponent(n).expanded, e, nesting+1) result = same(VComponent(n).expanded, e, nesting+1)
elif n.kind == VNodeKind.verbatim:
result = true
elif n.kind == VNodeKind.vthunk or n.kind == VNodeKind.dthunk: elif n.kind == VNodeKind.vthunk or n.kind == VNodeKind.dthunk:
# we don't check these for now: # we don't check these for now:
result = true result = true
elif toTag[n.kind] == e.nodename: elif toTag[n.kind] == e.nodename:
result = true result = true
if n.kind != VNodeKind.text: if n.kind != VNodeKind.text:
if e.len != n.len: # BUGFIX: Microsoft's Edge gives the textarea a child containing the text node!
echo "expected ", e.len, " real ", n.len, toTag[n.kind], " nesting ", nesting if e.len != n.len and n.kind != VNodeKind.textarea:
echo "expected ", n.len, " real ", e.len, " ", toTag[n.kind], " nesting ", nesting
return false return false
for i in 0 ..< n.len: for i in 0 ..< n.len:
if not same(n[i], e[i], nesting+1): return false if not same(n[i], e[i], nesting+1): return false
@ -239,6 +250,9 @@ proc eq(a, b: VNode): EqResult =
if a.len != b.len: return different if a.len != b.len: return different
for i in 0..<a.len: for i in 0..<a.len:
if eq(a[i], b[i]) == different: return different if eq(a[i], b[i]) == different: return different
elif a.kind == VNodeKind.verbatim:
if a.text != b.text:
return different
elif b.kind == VNodeKind.component: elif b.kind == VNodeKind.component:
# different component names mean different components: # different component names mean different components:
if a.text != b.text: if a.text != b.text:
@ -632,6 +646,11 @@ proc setRenderer*(renderer: proc (data: RouterData): VNode,
proc (data: RouterData) = nil): KaraxInstance {. proc (data: RouterData) = nil): KaraxInstance {.
discardable.} = discardable.} =
## Setup Karax. Usually the return value can be ignored. ## Setup Karax. Usually the return value can be ignored.
if document.getElementById(root).isNil:
let msg = "Could not find a <div> with id=" & root &
". Karax needs it as its rendering target."
raise newException(Exception, $msg)
result = KaraxInstance(rootId: root, renderer: renderer, result = KaraxInstance(rootId: root, renderer: renderer,
postRenderCallback: clientPostRenderCallback, postRenderCallback: clientPostRenderCallback,
patches: newSeq[Patch](60), patches: newSeq[Patch](60),

View file

@ -42,6 +42,7 @@ proc main =
rest = rest.replace("--run ") rest = rest.replace("--run ")
of "css": of "css":
selectedCss = css selectedCss = css
rest = rest.replace("--css ")
else: discard else: discard
of cmdShortOption: of cmdShortOption:
if op.key == "r": if op.key == "r":

View file

@ -14,7 +14,7 @@ type
VNodeKind* {.pure.} = enum VNodeKind* {.pure.} = enum
text = "#text", int = "#int", bool = "#bool", text = "#text", int = "#int", bool = "#bool",
vthunk = "#vthunk", dthunk = "#dthunk", vthunk = "#vthunk", dthunk = "#dthunk",
component = "#component", component = "#component", verbatim="#verbatim",
html, head, title, base, link, meta, style, html, head, title, base, link, meta, style,
script, noscript, script, noscript,
@ -267,6 +267,13 @@ when defined(js):
proc text*(s: string): VNode = VNode(kind: VNodeKind.text, text: kstring(s), index: -1) proc text*(s: string): VNode = VNode(kind: VNodeKind.text, text: kstring(s), index: -1)
proc text*(s: kstring): VNode = VNode(kind: VNodeKind.text, text: s, index: -1) proc text*(s: kstring): VNode = VNode(kind: VNodeKind.text, text: s, index: -1)
when defined(js):
proc verbatim*(s: string): VNode =
VNode(kind: VNodeKind.verbatim, text: kstring(s), index: -1)
proc verbatim*(s: kstring): VNode =
VNode(kind: VNodeKind.verbatim, text: s, index: -1)
iterator items*(n: VNode): VNode = iterator items*(n: VNode): VNode =
for i in 0..<n.kids.len: yield n.kids[i] for i in 0..<n.kids.len: yield n.kids[i]

View file

@ -7,7 +7,11 @@ to change.
To try it out, run:: To try it out, run::
nimble install # For first time cd ~/projects # Insert your favourite directory for projects
nimble develop karax # This will clone Karax and create a link to it in ~/.nimble
cd karax
cd examples/todoapp cd examples/todoapp
nim js todoapp.nim nim js todoapp.nim