Skip to content

Commit 7f77fb8

Browse files
authored
v3.10.2 (#262 from HapticX/dev)
fix rendering system, implement JS sugar for SPA
2 parents e5d23de + b0f73f6 commit 7f77fb8

File tree

17 files changed

+238
-81
lines changed

17 files changed

+238
-81
lines changed

examples/website/src/components/button.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ component Button:
2424
"flex justify-center items-center font-bold text-lg cursor-pointer select-none"
2525
):
2626
if self.flat:
27-
tDiv(class = "px-2 text-4xl lg:text-2xl xl:text-base md:px-4 xl:px-8 py-1 text-[{Foreground}] dark:text-[{ForegroundDark}] hover:opacity-80 active:opacity-60 duration-300"):
27+
tDiv(class = "px-2 text-4xl lg:text-2xl xl:text-base md:px-4 xl:px-6 py-1 text-[{Foreground}] dark:text-[{ForegroundDark}] hover:opacity-80 active:opacity-60 duration-300"):
2828
slot
2929
else:
30-
tDiv(class = "px-2 text-4xl lg:text-2xl xl:text-base md:px-4 xl:px-8 py-1 text-[{Background}] dark:text-[{BackgroundDark}] rounded-md bg-[{Orange}] dark:bg-[{Yellow}] hover:opacity-90 active:opacity-75 duration-300"):
30+
tDiv(class = "px-2 text-4xl lg:text-2xl xl:text-base md:px-4 xl:px-6 py-1 text-[{Background}] dark:text-[{BackgroundDark}] rounded-md bg-[{Orange}] dark:bg-[{Yellow}] hover:opacity-90 active:opacity-75 duration-300"):
3131
slot
3232
@click:
3333
self.action()

examples/website/src/components/code_block_guide.nim

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,29 @@ component CodeBlockGuide:
113113
source = self.CodeBlockGuide.sources.val[i]
114114
playButton = document.getElementById(fmt"{source.id}play_button{self.uniqCompId}")
115115
playResult = document.getElementById(fmt"{source.id}play_result{self.uniqCompId}")
116-
playStates: seq[tuple[text, html, lang: cstring, waitMs: int]] = source.playResult.states
117116
var idx = 0
118117

119118
playButton.classList.add("hidden")
120119
playResult.classList.remove("hidden")
121120
playResult.innerHTML = ""
122-
123-
proc hiddenProc(text, html, lang: cstring, waitMs: int) =
124-
if text.len == 0 and html.len == 0:
125-
playButton.classList.remove("hidden")
126-
playResult.classList.add("hidden")
127-
elif html.len != 0:
128-
playResult.innerHTML &= html
129-
else:
130-
playResult.innerHTML &= fmt"""<pre><code id="{self.uniqCompId}{lang}{idx}" language="{lang}" class="language-{lang}" style="padding-top: 0 !important; padding-bottom: 0 !important;">{text}</code></pre>"""
131-
let id: cstring = fmt"{self.uniqCompId}{lang}{idx}"
132-
inc idx
133-
buildJs:
134-
let codeBlock = document.getElementById(~id)
135-
hljs.highlightElement(codeBlock)
136121

137-
{.emit: """//js
138-
const playStates = [...`playStates`];
139-
let timeout = 0;
140-
141-
playStates.forEach(state => {
142-
timeout += state.Field3;
143-
setTimeout(
144-
() => `hiddenProc`(state.Field0, state.Field1, state.Field2, state.Field3),
145-
timeout
146-
);
147-
console.log(state.Field0, state.Field1, state.Field2, state.Field3);
148-
});
149-
""".}
122+
var timeout = 0
123+
for j in self.CodeBlockGuide.sources.val[i].playResult.states:
124+
timeout += j.waitMs
125+
withVariables j:
126+
withTimeout timeout, t:
127+
if j.text.len == 0 and j.html.len == 0:
128+
playButton.classList.remove("hidden")
129+
playResult.classList.add("hidden")
130+
elif j.html.len != 0:
131+
playResult.innerHTML &= j.html
132+
else:
133+
playResult.innerHTML &= fmt"""<pre><code id="{self.uniqCompId}{j.lang}{idx}" language="{j.lang}" class="language-{j.lang}" style="padding-top: 0 !important; padding-bottom: 0 !important;">{j.text}</code></pre>"""
134+
let id: cstring = fmt"{self.uniqCompId}{j.lang}{idx}"
135+
inc idx
136+
buildJs:
137+
let codeBlock = document.getElementById(~id)
138+
hljs.highlightElement(codeBlock)
150139
tDiv(id = "{source.id}play_result", class = "w-full pb-4")
151140
if not haslanguage(self.CodeBlockGuide, currentLanguage.val):
152141
tCode(

examples/website/src/components/code_block_slider.nim

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ component CodeBlockSlider:
8383
else:
8484
0
8585
)
86-
{.emit: """//js
87-
setTimeout(() => { `upd`() }, 5000);
88-
""".}
89-
{.emit: """//js
90-
setTimeout(() => { `upd`() }, 5000);
91-
""".}
86+
withTimeout 5000, t:
87+
upd()
88+
withTimeout 5000, t:
89+
upd()
9290

9391
proc updateIndex(idx: int) =
9492
if self.index.isNil():

examples/website/src/components/drawer.nim

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,21 +109,11 @@ component Drawer:
109109

110110
[methods]:
111111
proc toggle*() =
112-
enableRouting = false
113112
let
114113
drawerBack = document.getElementById("drawerBack" & self.uniqCompId)
115114
drawer = document.getElementById("drawer" & self.uniqCompId)
116-
self.isOpen.set(not self.isOpen)
117-
if self.isOpen:
118-
drawerBack.classList.remove("opacity-0")
119-
drawerBack.classList.add("opacity-100")
120-
drawerBack.classList.remove("pointer-events-none")
121-
drawer.classList.remove("translate-x-full")
122-
drawer.classList.add("translate-x-0")
123-
else:
124-
drawerBack.classList.remove("opacity-100")
125-
drawerBack.classList.add("opacity-0")
126-
drawerBack.classList.add("pointer-events-none")
127-
drawer.classList.remove("translate-x-0")
128-
drawer.classList.add("translate-x-full")
129-
enableRouting = true
115+
drawerBack.classList.toggle("opacity-0")
116+
drawerBack.classList.toggle("opacity-100")
117+
drawerBack.classList.toggle("pointer-events-none")
118+
drawer.classList.toggle("translate-x-full")
119+
drawer.classList.toggle("translate-x-0")

examples/website/src/components/guide_page.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ component GuidePage:
1313

1414
`template`:
1515
tDiv(
16-
class = "flex flex-col text-3xl lg:text-xl xl:text-base w-full h-full px-4 lg:px-12 xl:px-24 py-2 bg-[{BackgroundSecondary}] dark:bg-[{BackgroundSecondaryDark}] gap-8"
16+
class = "flex flex-col text-3xl lg:text-xl xl:text-base w-full h-full px-4 lg:px-12 xl:px-16 py-2 bg-[{BackgroundSecondary}] dark:bg-[{BackgroundSecondaryDark}] gap-8"
1717
):
1818
tDiv(class = "flex flex-col gap-4"):
1919
case currentGuidePage.val

examples/website/src/components/header.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ component Header:
3333
):
3434
tDiv(class = "flex items-center gap-4"):
3535
tSvg(
36-
width="98", height="96", viewBox = "0 0 98 96", xmlns="http://www.w3.org/2000/svg",
37-
class = "h-6 w-6 fill-[{Background}] dark:fill-[{BackgroundDark}]"
36+
class = "h-6 w-6 fill-[{Background}] dark:fill-[{BackgroundDark}]",
37+
width = "98", height = "96", viewBox = "0 0 98 96", xmlns = "http://www.w3.org/2000/svg"
3838
):
3939
tPath(
4040
d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z"

examples/website/src/docs/ssr_basics.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ component SsrBasics:
4040
tH2: {translate"Helpful Routes 🔌"}
4141
if currentLanguage == "Nim":
4242
tP:
43-
{translate"HappyX has additional helpful routes - setup, middleware, notfound, and staticDir"}
43+
{translate"HappyX has additional useful routes - setup, middleware, notfound, onException, and staticDir"}
4444
else:
4545
tP:
4646
{translate"HappyX has additional helpful routes - middleware, notfound and static directories"}

examples/website/src/pages/guide.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ mount UserGuide:
1919
# Header
2020
tDiv(class = "w-full sticky top-0 z-20"):
2121
Header(drawer = drawer_comp)
22-
tDiv(class = "flex w-full h-full gap-8 px-4"):
22+
tDiv(class = "flex w-full h-full gap-8"):
2323
# SideBar
24-
tDiv(class = "fixed top-0 pt-16 w-80"):
24+
tDiv(class = "fixed top-0 pt-16 w-96"):
2525
SideBar
26-
tDiv(class = "pl-0 xl:pl-80 w-full"):
26+
tDiv(class = "pl-0 xl:pl-96 w-full"):
2727
GuidePage(link)
2828
tStyle:
2929
{fmt"""

examples/website/src/ui/code/nim_ssr.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ serve "127.0.0.1", 5000:
155155
156156
notfound:
157157
return "Oops, seems like this route is not available"
158+
159+
onException:
160+
echo e.msg
161+
echo e.name
162+
statusCode = 500
163+
return "Oops, seems like some error was raised"
158164
159165
staticDir "/path/to/directory" -> "directory"
160166
"""

examples/website/src/ui/translations.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,12 +1154,12 @@ translatable:
11541154
"zh" -> "有用的路由 🔌"
11551155
"fr" -> "Itinéraires utiles 🔌"
11561156
"ko" -> "도움말 라우트 🔌"
1157-
"HappyX has additional helpful routes - setup, middleware, notfound, and staticDir":
1158-
"ru" -> "У HappyX есть дополнительные полезные маршруты - setup, middleware, notfound и staticDir"
1159-
"ja" -> "HappyXには追加の便利なルートがあります - setup、middleware、notfound、およびstaticDir"
1160-
"zh" -> "HappyX具有额外的有用路由 - setup、middleware、notfound和staticDir"
1161-
"fr" -> "HappyX dispose de routes supplémentaires utiles - setup, middleware, notfound et staticDir"
1162-
"ko" -> "HappyX에는 추가로 유용한 라우트가 있습니다 - setup, middleware, notfound 및 staticDir"
1157+
"HappyX has additional useful routes - setup, middleware, notfound, onException, and staticDir":
1158+
"ru" -> "У HappyX есть дополнительные полезные маршруты - setup, middleware, notfound, onException и staticDir"
1159+
"ja" -> "HappyXには追加の便利なルートがあります - setup、middleware、notfound、onException、およびstaticDir"
1160+
"zh" -> "HappyX还有其他有用的路由 - setup、middleware、notfound、onException和staticDir"
1161+
"fr" -> "HappyX dispose de routes supplémentaires utiles - setup, middleware, notfound, onException et staticDir"
1162+
"ko" -> "HappyX에는 추가로 유용한 경로가 있습니다 - setup, middleware, notfound, onException 및 staticDir"
11631163
"HappyX has additional helpful routes - middleware, notfound and static directories":
11641164
"ru" -> "У HappyX есть дополнительные полезные маршруты - middleware, notfound и статические каталоги"
11651165
"ja" -> "HappyXには追加の便利なルートがあります - ミドルウェア、notfound、および静的ディレクトリ"

happyx.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
description = "Macro-oriented asynchronous web-framework written with ♥"
44
author = "HapticX"
5-
version = "3.10.1"
5+
version = "3.10.2"
66
license = "MIT"
77
srcDir = "src"
88
installExt = @["nim"]

src/happyx.nim

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,15 @@ when not defined(js):
302302
import
303303
happyx/core/[exceptions, constants, queries],
304304
happyx/sugar/[use, sgr, js, style],
305-
happyx/spa/[renderer, state, components, translatable, tag, spa_utils],
305+
happyx/spa/[renderer, state, components, translatable, tag],
306306
happyx/tmpl_engine/[engine],
307307
happyx/routing/[mounting, routing, decorators],
308308
happyx/ssr/utils
309309

310+
when defined(js):
311+
import happyx/spa/spa_utils
312+
export spa_utils
313+
310314

311315
when enableApiDoc and not defined(js) and not exportPython and not exportJvm:
312316
import happyx/ssr/docs/open_api
@@ -329,8 +333,7 @@ export
329333
mounting,
330334
sgr,
331335
js,
332-
utils,
333-
spa_utils
336+
utils
334337

335338

336339
# Language bindings

src/happyx/core/constants.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ const
9595
# Framework version
9696
HpxMajor* = 3
9797
HpxMinor* = 10
98-
HpxPatch* = 1
98+
HpxPatch* = 2
9999
HpxVersion* = $HpxMajor & "." & $HpxMinor & "." & $HpxPatch
100100

101101

src/happyx/private/macro_utils.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,8 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
908908
)
909909
),
910910
newCall(
911-
"eventListener", ident("__el" & $elemEventId), newLit(event), procedure.body
911+
"eventListener", ident("__el" & $elemEventId), newLit(event),
912+
newNimNode(nnkBlockStmt).add(newEmptyNode(), procedure.body)
912913
),
913914
newNimNode(nnkPragma).add(
914915
newNimNode(nnkExprColonExpr).add(
@@ -1008,7 +1009,8 @@ proc buildHtmlProcedure*(root, body: NimNode, inComponent: bool = false,
10081009
)
10091010
),
10101011
newCall(
1011-
"eventListener", ident("__el" & $elemEventId), newLit(event), procedure.body
1012+
"eventListener", ident("__el" & $elemEventId), newLit(event),
1013+
newNimNode(nnkBlockStmt).add(newEmptyNode(), procedure.body)
10121014
),
10131015
newNimNode(nnkPragma).add(
10141016
newNimNode(nnkExprColonExpr).add(

src/happyx/spa/renderer.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ else:
262262
when defined(js):
263263
proc renderVdom*(app: App, tag: TagRef, force: bool = false) =
264264
## Rerender DOM with VDOM
265+
echo 1
265266
var realDom = document.getElementById(app.appId).Node
266267
realDom.innerHTML = ""
267268
realDom.appendChild(tag)

0 commit comments

Comments
 (0)