Merge branch 'master' of github.com:pragmagic/karax
This commit is contained in:
commit
451ee92dd0
7 changed files with 39 additions and 8 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,5 +1,5 @@
|
|||
nimcache/
|
||||
/tests/tester
|
||||
/tools/karun
|
||||
karax/tools/karun
|
||||
*.code-workspace
|
||||
*.exe
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@ license = "MIT"
|
|||
|
||||
requires "nim >= 0.16.1"
|
||||
|
||||
skipDirs = @["examples", "experiments"]
|
||||
skipDirs = @["examples", "experiments", "tests"]
|
||||
|
||||
bin = @["tools/karun"]
|
||||
bin = @["karax/tools/karun"]
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ proc ajax*(meth, url: cstring; headers: openarray[(cstring, cstring)];
|
|||
if this.status == 200:
|
||||
contWrapper(this.status, this.responseText)
|
||||
else:
|
||||
contWrapper(this.status, this.statusText)
|
||||
contWrapper(this.status, this.responseText)
|
||||
if useBinary:
|
||||
ajax.send(blob)
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -4,6 +4,9 @@ import kdom, vdom, jstrutils, compact, jdict, vstyles
|
|||
|
||||
export kdom.Event, kdom.Blob
|
||||
|
||||
when defined(nimNoNil):
|
||||
{.experimental: "notnil".}
|
||||
|
||||
proc kout*[T](x: T) {.importc: "console.log", varargs, deprecated.}
|
||||
## the preferred way of debugging karax applications. Now deprecated,
|
||||
## you can now use ``system.echo`` instead.
|
||||
|
|
@ -135,6 +138,11 @@ proc vnodeToDom*(n: VNode; kxi: KaraxInstance): Node =
|
|||
if n.kind == VNodeKind.text:
|
||||
result = document.createTextNode(n.text)
|
||||
attach n
|
||||
elif n.kind == VNodeKind.verbatim:
|
||||
result = document.createElement("div")
|
||||
result.innerHTML = n.text
|
||||
attach n
|
||||
return result
|
||||
elif n.kind == VNodeKind.vthunk:
|
||||
let x = callThunk(vcomponents[n.text], n)
|
||||
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 n.kind == VNodeKind.component:
|
||||
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:
|
||||
# we don't check these for now:
|
||||
result = true
|
||||
elif toTag[n.kind] == e.nodename:
|
||||
result = true
|
||||
if n.kind != VNodeKind.text:
|
||||
if e.len != n.len:
|
||||
echo "expected ", e.len, " real ", n.len, toTag[n.kind], " nesting ", nesting
|
||||
# BUGFIX: Microsoft's Edge gives the textarea a child containing the text node!
|
||||
if e.len != n.len and n.kind != VNodeKind.textarea:
|
||||
echo "expected ", n.len, " real ", e.len, " ", toTag[n.kind], " nesting ", nesting
|
||||
return false
|
||||
for i in 0 ..< n.len:
|
||||
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
|
||||
for i in 0..<a.len:
|
||||
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:
|
||||
# different component names mean different components:
|
||||
if a.text != b.text:
|
||||
|
|
@ -632,6 +646,11 @@ proc setRenderer*(renderer: proc (data: RouterData): VNode,
|
|||
proc (data: RouterData) = nil): KaraxInstance {.
|
||||
discardable.} =
|
||||
## 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,
|
||||
postRenderCallback: clientPostRenderCallback,
|
||||
patches: newSeq[Patch](60),
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ proc main =
|
|||
rest = rest.replace("--run ")
|
||||
of "css":
|
||||
selectedCss = css
|
||||
rest = rest.replace("--css ")
|
||||
else: discard
|
||||
of cmdShortOption:
|
||||
if op.key == "r":
|
||||
|
|
@ -14,7 +14,7 @@ type
|
|||
VNodeKind* {.pure.} = enum
|
||||
text = "#text", int = "#int", bool = "#bool",
|
||||
vthunk = "#vthunk", dthunk = "#dthunk",
|
||||
component = "#component",
|
||||
component = "#component", verbatim="#verbatim",
|
||||
|
||||
html, head, title, base, link, meta, style,
|
||||
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: 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 =
|
||||
for i in 0..<n.kids.len: yield n.kids[i]
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ to change.
|
|||
|
||||
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
|
||||
nim js todoapp.nim
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue