diff --git a/lib/hyperscript/compiler.sx b/lib/hyperscript/compiler.sx index e25a60f3..e685be17 100644 --- a/lib/hyperscript/compiler.sx +++ b/lib/hyperscript/compiler.sx @@ -34,7 +34,8 @@ (cond ((= th dot-sym) (let - ((base-ast (nth target 1)) (prop (nth target 2))) + ((base-ast (nth target 1)) + (prop (nth target 2))) (cond ((and (list? base-ast) (= (first base-ast) (quote query)) (let ((s (nth base-ast 1))) (and (string? s) (> (len s) 0) (= (substring s 0 1) ".")))) (list @@ -85,7 +86,10 @@ (nth target 1) value)) ((= th (quote ref)) - (list (quote set!) (make-symbol (nth target 1)) value)) + (list + (quote set!) + (make-symbol (nth target 1)) + value)) ((= th (quote local)) (list (quote hs-scoped-set!) @@ -113,7 +117,8 @@ (list (quote hs-set-inner-html!) (hs-to-sx target) value)) ((= th (quote of)) (let - ((prop-ast (nth target 1)) (obj-ast (nth target 2))) + ((prop-ast (nth target 1)) + (obj-ast (nth target 2))) (if (and (list? prop-ast) (= (first prop-ast) dot-sym)) (let @@ -377,7 +382,8 @@ (fn (ast) (let - ((mode (nth ast 1)) (body (hs-to-sx (nth ast 2)))) + ((mode (nth ast 1)) + (body (hs-to-sx (nth ast 2)))) (cond ((and (list? mode) (= (first mode) (quote forever))) (list @@ -414,13 +420,23 @@ (list (quote fn) (list) body))))))) (define hs-reserved-var? - (fn (name) (or (= name "meta") (= name "event") (= name "it") (= name "result"))) + (fn + (name) + (or + (= name "meta") + (= name "event") + (= name "it") + (= name "result"))) emit-for (fn (ast) (let ((var-name (nth ast 1)) - (safe-param (if (hs-reserved-var? var-name) (str "_hs_lv_" var-name) var-name)) + (safe-param + (if + (hs-reserved-var? var-name) + (str "_hs_lv_" var-name) + var-name)) (raw-coll-ast (nth ast 2)) (where-cond (if @@ -455,7 +471,9 @@ (quote map-indexed) (list (quote fn) - (list (make-symbol (nth ast 5)) (make-symbol safe-param)) + (list + (make-symbol (nth ast 5)) + (make-symbol safe-param)) body) collection) (list @@ -468,13 +486,15 @@ (ast) (let ((event-name (nth ast 1)) - (has-from (and (> (len ast) 2) (= (nth ast 2) :from))) + (has-from + (and (> (len ast) 2) (= (nth ast 2) :from))) (has-from-or (and (> (len ast) 4) (= (nth ast 2) :from) (= (nth ast 4) :or))) - (has-or (and (> (len ast) 2) (= (nth ast 2) :or)))) + (has-or + (and (> (len ast) 2) (= (nth ast 2) :or)))) (cond (has-from-or (list @@ -483,7 +503,10 @@ event-name (nth ast 5))) (has-from - (list (quote hs-wait-for) (hs-to-sx (nth ast 3)) event-name)) + (list + (quote hs-wait-for) + (hs-to-sx (nth ast 3)) + event-name)) (has-or (list (quote hs-wait-for-or) @@ -512,9 +535,14 @@ (ast) (let ((type-name (nth ast 1)) - (called (if (>= (len ast) 3) (nth ast 2) nil)) + (called + (if (>= (len ast) 3) (nth ast 2) nil)) (args (if (>= (len ast) 4) (nth ast 3) nil)) - (kind (if (>= (len ast) 5) (nth ast 4) (quote auto)))) + (kind + (if + (>= (len ast) 5) + (nth ast 4) + (quote auto)))) (let ((make-call (cond ((nil? args) (list (quote hs-make) type-name)) (true (cons (quote hs-make) (cons type-name (map hs-to-sx args))))))) (cond @@ -571,7 +599,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) dot-sym)) (let - ((obj (hs-to-sx (nth expr 1))) (prop (nth expr 2))) + ((obj (hs-to-sx (nth expr 1))) + (prop (nth expr 2))) (list (quote let) (list @@ -608,7 +637,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) (quote dom-ref))) (let - ((el (hs-to-sx (nth expr 2))) (name (nth expr 1))) + ((el (hs-to-sx (nth expr 2))) + (name (nth expr 1))) (list (quote let) (list @@ -687,7 +717,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) dot-sym)) (let - ((obj (hs-to-sx (nth expr 1))) (prop (nth expr 2))) + ((obj (hs-to-sx (nth expr 1))) + (prop (nth expr 2))) (list (quote let) (list @@ -724,7 +755,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) (quote dom-ref))) (let - ((el (hs-to-sx (nth expr 2))) (name (nth expr 1))) + ((el (hs-to-sx (nth expr 2))) + (name (nth expr 1))) (list (quote let) (list @@ -782,14 +814,28 @@ (fn (ast) (let - ((name (nth ast 1)) (params (nth ast 2)) (body (nth ast 3))) + ((name (nth ast 1)) + (params (nth ast 2)) + (body (nth ast 3))) (list (quote define) (make-symbol name) (list (quote fn) - (cons (quote me) (map make-symbol params)) - (cons (quote do) (map hs-to-sx body))))))) + (cons + (quote me) + (map + (fn + (p) + (if + (list? p) + (make-symbol (nth p 1)) + (make-symbol p))) + params)) + (list + (quote let) + (list (list (quote beingTold) (quote me))) + (cons (quote do) (map hs-to-sx body)))))))) (fn (ast) (let @@ -846,7 +892,10 @@ (let ((raw (nth ast 1))) (let - ((parts (list)) (buf "") (i 0) (n (len raw))) + ((parts (list)) + (buf "") + (i 0) + (n (len raw))) (define tpl-flush (fn @@ -884,10 +933,14 @@ (if (= depth 1) j - (tpl-find-close (+ j 1) (- depth 1))) + (tpl-find-close + (+ j 1) + (- depth 1))) (if (= (nth raw j) "{") - (tpl-find-close (+ j 1) (+ depth 1)) + (tpl-find-close + (+ j 1) + (+ depth 1)) (tpl-find-close (+ j 1) depth)))))) (define tpl-collect @@ -898,7 +951,10 @@ (let ((ch (nth raw i))) (if - (and (= ch "\\") (< (+ i 1) n) (= (nth raw (+ i 1)) "$")) + (and + (= ch "\\") + (< (+ i 1) n) + (= (nth raw (+ i 1)) "$")) (do (set! buf (str buf "$")) (set! i (+ i 2)) @@ -907,39 +963,40 @@ (and (= ch "$") (< (+ i 1) n)) (if (= (nth raw (+ i 1)) "{") - (let - ((start (+ i 2))) (let - ((close (tpl-find-close start 1))) + ((start (+ i 2))) (let - ((expr-src (slice raw start close))) - (do - (tpl-flush) - (set! - parts - (append + ((close (tpl-find-close start 1))) + (let + ((expr-src (slice raw start close))) + (do + (tpl-flush) + (set! parts - (list - (hs-to-sx - (hs-compile expr-src))))) - (set! i (+ close 1)) - (tpl-collect))))) - (let - ((start (+ i 1))) + (append + parts + (list + (hs-to-sx + (hs-compile expr-src))))) + (set! i (+ close 1)) + (tpl-collect))))) (let - ((end (tpl-read-id start))) + ((start (+ i 1))) (let - ((ident (slice raw start end))) - (do - (tpl-flush) - (set! - parts - (append + ((end (tpl-read-id start))) + (let + ((ident (slice raw start end))) + (do + (tpl-flush) + (set! parts - (list - (hs-to-sx (hs-compile ident))))) - (set! i end) - (tpl-collect)))))) + (append + parts + (list + (hs-to-sx + (hs-compile ident))))) + (set! i end) + (tpl-collect)))))) (do (set! buf (str buf ch)) (set! i (+ i 1)) @@ -983,7 +1040,10 @@ (list (quote hs-pick-random) (hs-to-sx (nth ast 1)) - (if (nil? (nth ast 2)) nil (hs-to-sx (nth ast 2)))))) + (if + (nil? (nth ast 2)) + nil + (hs-to-sx (nth ast 2)))))) ((= head (quote pick-items)) (list (quote set!) @@ -1082,7 +1142,10 @@ (quote hs-method-call) (cons (hs-to-sx dot-node) args)))))) ((= head (quote string-postfix)) - (list (quote str) (hs-to-sx (nth ast 1)) (nth ast 2))) + (list + (quote str) + (hs-to-sx (nth ast 1)) + (nth ast 2))) ((= head (quote block-literal)) (let ((params (map make-symbol (nth ast 1))) @@ -1101,10 +1164,11 @@ ((= prop "last") (list (quote hs-last) target)) (true (list (quote host-get) target prop))))) ((= head (quote ref)) - (if - (= (nth ast 1) "selection") - (list (quote hs-get-selection)) - (make-symbol (nth ast 1)))) + (cond + ((= (nth ast 1) "selection") + (list (quote hs-get-selection))) + ((= (nth ast 1) "element") (make-symbol "me")) + (else (make-symbol (nth ast 1))))) ((= head (quote query)) (list (quote hs-query-first) (nth ast 1))) ((= head (quote query-scoped)) @@ -1133,7 +1197,10 @@ (hs-to-sx (nth ast 1)) (nth ast 2))) ((= head (quote local)) - (list (quote hs-scoped-get) (quote me) (nth ast 1))) + (list + (quote hs-scoped-get) + (quote me) + (nth ast 1))) ((= head (quote array)) (cons (quote list) (map hs-to-sx (rest ast)))) ((= head (quote not)) @@ -1196,7 +1263,8 @@ (list (quote nil?) (hs-to-sx (nth ast 1))))) ((= head (quote matches?)) (let - ((left (nth ast 1)) (right (nth ast 2))) + ((left (nth ast 1)) + (right (nth ast 2))) (if (and (list? right) (= (first right) (quote query))) (list @@ -1316,7 +1384,10 @@ "parentElement") (nth ast 1))) ((= head (quote next)) - (list (quote hs-next) (hs-to-sx (nth ast 2)) (nth ast 1))) + (list + (quote hs-next) + (hs-to-sx (nth ast 2)) + (nth ast 1))) ((= head (quote previous)) (list (quote hs-previous) @@ -1367,7 +1438,8 @@ (nth ast 2))) ((= head (quote set-styles)) (let - ((pairs (nth ast 1)) (tgt (hs-to-sx (nth ast 2)))) + ((pairs (nth ast 1)) + (tgt (hs-to-sx (nth ast 2)))) (cons (quote do) (map @@ -1512,7 +1584,8 @@ (list (quote hs-splice-at!) (hs-to-sx coll) idx)))) ((and (list? tgt) (= (first tgt) dot-sym)) (let - ((obj (nth tgt 1)) (prop (nth tgt 2))) + ((obj (nth tgt 1)) + (prop (nth tgt 2))) (emit-set obj (list @@ -1521,7 +1594,8 @@ prop)))) ((and (list? tgt) (= (first tgt) (quote of))) (let - ((prop-ast (nth tgt 1)) (obj-ast (nth tgt 2))) + ((prop-ast (nth tgt 1)) + (obj-ast (nth tgt 2))) (let ((prop (cond ((string? prop-ast) prop-ast) ((and (list? prop-ast) (= (first prop-ast) (quote ref))) (nth prop-ast 1)) (true (hs-to-sx prop-ast))))) (emit-set @@ -1533,7 +1607,8 @@ (true (list (quote dom-remove) (hs-to-sx tgt)))))) ((= head (quote add-value)) (let - ((val (hs-to-sx (nth ast 1))) (tgt (nth ast 2))) + ((val (hs-to-sx (nth ast 1))) + (tgt (nth ast 2))) (emit-set tgt (list (quote hs-add-to!) val (hs-to-sx tgt))))) @@ -1547,7 +1622,8 @@ (hs-to-sx (nth ast 2))))) ((= head (quote remove-value)) (let - ((val (hs-to-sx (nth ast 1))) (tgt (nth ast 2))) + ((val (hs-to-sx (nth ast 1))) + (tgt (nth ast 2))) (emit-set tgt (list (quote hs-remove-from!) val (hs-to-sx tgt))))) @@ -1562,7 +1638,10 @@ ((and (list? tgt) (= (first tgt) (quote query))) (list (quote for-each) - (list (quote fn) (list (quote _el)) (list (quote hs-empty-target!) (quote _el))) + (list + (quote fn) + (list (quote _el)) + (list (quote hs-empty-target!) (quote _el))) (list (quote hs-query-all) (nth tgt 1)))) (true (list (quote hs-empty-target!) (hs-to-sx tgt)))))) ((= head (quote open-element)) @@ -1710,7 +1789,9 @@ (nth ast 3) (hs-to-sx (nth ast 4)))) ((= head (quote set!)) - (emit-set (nth ast 1) (hs-to-sx (nth ast 2)))) + (emit-set + (nth ast 1) + (hs-to-sx (nth ast 2)))) ((= head (quote put!)) (let ((val (hs-to-sx (nth ast 1))) @@ -1800,7 +1881,8 @@ (= (first c) (quote define))))) compiled))) (cons (quote do) (append defs non-defs))))))) - ((= head (quote wait)) (list (quote hs-wait) (nth ast 1))) + ((= head (quote wait)) + (list (quote hs-wait) (nth ast 1))) ((= head (quote wait-for)) (emit-wait-for ast)) ((= head (quote log)) (list (quote console-log) (hs-to-sx (nth ast 1)))) @@ -1813,8 +1895,16 @@ (= (len ast) 4) (list? (nth ast 2)) (= (first (nth ast 2)) (quote dict)))) - (tgt (if (= (len ast) 4) (nth ast 3) (nth ast 2))) - (detail (if (= (len ast) 4) (nth ast 2) nil))) + (tgt + (if + (= (len ast) 4) + (nth ast 3) + (nth ast 2))) + (detail + (if + (= (len ast) 4) + (nth ast 2) + nil))) (list (quote dom-dispatch) (hs-to-sx tgt) @@ -1823,8 +1913,16 @@ ((= head (quote hide)) (let ((tgt (let ((raw-tgt (nth ast 1))) (if (and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-query-all) (nth raw-tgt 1)) (hs-to-sx raw-tgt)))) - (strategy (if (> (len ast) 2) (nth ast 2) "display")) - (when-cond (if (> (len ast) 3) (nth ast 3) nil))) + (strategy + (if + (> (len ast) 2) + (nth ast 2) + "display")) + (when-cond + (if + (> (len ast) 3) + (nth ast 3) + nil))) (if (nil? when-cond) (list (quote hs-hide!) tgt strategy) @@ -1839,8 +1937,16 @@ ((= head (quote show)) (let ((tgt (let ((raw-tgt (nth ast 1))) (if (and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-query-all) (nth raw-tgt 1)) (hs-to-sx raw-tgt)))) - (strategy (if (> (len ast) 2) (nth ast 2) "display")) - (when-cond (if (> (len ast) 3) (nth ast 3) nil))) + (strategy + (if + (> (len ast) 2) + (nth ast 2) + "display")) + (when-cond + (if + (> (len ast) 3) + (nth ast 3) + nil))) (if (nil? when-cond) (list (quote hs-show!) tgt strategy) @@ -1884,23 +1990,41 @@ ((= head (quote repeat-until)) (list (quote hs-repeat-until) - (list (quote fn) (list) (hs-to-sx (nth ast 1))) - (list (quote fn) (list) (hs-to-sx (nth ast 2))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 2))))) ((= head (quote repeat-while)) (list (quote hs-repeat-while) - (list (quote fn) (list) (hs-to-sx (nth ast 1))) - (list (quote fn) (list) (hs-to-sx (nth ast 2))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 2))))) ((= head (quote fetch)) (list - (if (nth ast 3) (quote hs-fetch-no-throw) (quote hs-fetch)) + (if + (nth ast 3) + (quote hs-fetch-no-throw) + (quote hs-fetch)) (hs-to-sx (nth ast 1)) (nth ast 2))) ((= head (quote fetch-gql)) (list (quote hs-fetch-gql) (nth ast 1) - (if (nth ast 2) (hs-to-sx (nth ast 2)) nil))) + (if + (nth ast 2) + (hs-to-sx (nth ast 2)) + nil))) ((= head (quote call)) (let ((raw-fn (nth ast 1)) @@ -1911,14 +2035,7 @@ (hs-to-sx raw-fn))) (args (map hs-to-sx (rest (rest ast))))) (let - ((call-expr - (if - (and (list? raw-fn) (= (first raw-fn) (quote ref))) - (list - (quote hs-win-call) - (nth raw-fn 1) - (cons (quote list) args)) - (cons fn-expr args)))) + ((call-expr (if (and (list? raw-fn) (= (first raw-fn) (quote ref))) (list (quote hs-win-call) (nth raw-fn 1) (cons (quote list) args)) (cons fn-expr args)))) (emit-set (quote the-result) call-expr)))) ((= head (quote return)) (let @@ -1936,7 +2053,9 @@ ((= head (quote settle)) (list (quote hs-settle) (quote me))) ((= head (quote go)) - (list (quote hs-navigate!) (hs-to-sx (nth ast 1)))) + (list + (quote hs-navigate!) + (hs-to-sx (nth ast 1)))) ((= head (quote ask)) (let ((val (list (quote hs-ask) (hs-to-sx (nth ast 1))))) @@ -2022,17 +2141,35 @@ (let ((kind (nth ast 1)) (name (nth ast 2)) - (from-sel (if (> (len ast) 3) (nth ast 3) nil)) - (for-tgt (if (> (len ast) 4) (nth ast 4) nil)) - (attr-val (if (> (len ast) 5) (nth ast 5) nil)) - (with-val (if (> (len ast) 6) (nth ast 6) nil))) + (from-sel + (if + (> (len ast) 3) + (nth ast 3) + nil)) + (for-tgt + (if + (> (len ast) 4) + (nth ast 4) + nil)) + (attr-val + (if + (> (len ast) 5) + (nth ast 5) + nil)) + (with-val + (if + (> (len ast) 6) + (nth ast 6) + nil))) (let ((target (if for-tgt (hs-to-sx for-tgt) (quote me))) (scope (cond ((nil? from-sel) nil) ((and (list? from-sel) (= (first from-sel) (quote query))) - (list (quote hs-query-all) (nth from-sel 1))) + (list + (quote hs-query-all) + (nth from-sel 1))) (true (hs-to-sx from-sel)))) (with-sx (if @@ -2064,13 +2201,31 @@ (true (list (quote hs-take!) target kind name scope)))))) ((= head (quote make)) (emit-make ast)) ((= head (quote install)) - (cons (quote hs-install) (map hs-to-sx (rest ast)))) + (let + ((bname (nth ast 1))) + (cons + (make-symbol bname) + (cons + (quote me) + (map + (fn + (arg) + (if + (and + (list? arg) + (= (first arg) (quote type-assert))) + (+ (nth arg 2) 0) + (hs-to-sx arg))) + (rest (rest ast))))))) ((= head (quote measure)) (list (quote hs-measure) (hs-to-sx (nth ast 1)))) ((= head (quote increment!)) (if (= (len ast) 3) - (emit-inc (nth ast 1) 1 (nth ast 2)) + (emit-inc + (nth ast 1) + 1 + (nth ast 2)) (emit-inc (nth ast 1) (nth ast 2) @@ -2078,7 +2233,10 @@ ((= head (quote decrement!)) (if (= (len ast) 3) - (emit-dec (nth ast 1) 1 (nth ast 2)) + (emit-dec + (nth ast 1) + 1 + (nth ast 2)) (emit-dec (nth ast 1) (nth ast 2) @@ -2092,7 +2250,8 @@ ((= head (quote on)) (emit-on ast)) ((= head (quote when-changes)) (let - ((expr (nth ast 1)) (body (nth ast 2))) + ((expr (nth ast 1)) + (body (nth ast 2))) (if (and (list? expr) (= (first expr) (quote dom-ref))) (list @@ -2104,7 +2263,10 @@ ((= head (quote init)) (list (quote hs-init) - (list (quote fn) (list) (hs-to-sx (nth ast 1))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))))) ((= head (quote def)) (let ((body (hs-to-sx (nth ast 3))) @@ -2143,7 +2305,10 @@ (quote =) (list (quote first) (quote _e)) "hs-return")) - (list (quote nth) (quote _e) 1) + (list + (quote nth) + (quote _e) + 1) (list (quote raise) (quote _e))))) body)))) (list @@ -2162,14 +2327,22 @@ (string? src) (first (sx-parse src)) (list (quote cek-eval) (hs-to-sx src))))) - ((= head (quote component)) (make-symbol (nth ast 1))) + ((= head (quote component)) + (make-symbol (nth ast 1))) ((= head (quote render)) (let ((comp-raw (nth ast 1)) (kwargs (nth ast 2)) - (pos (if (> (len ast) 3) (nth ast 3) nil)) + (pos + (if + (> (len ast) 3) + (nth ast 3) + nil)) (target - (if (> (len ast) 4) (hs-to-sx (nth ast 4)) nil))) + (if + (> (len ast) 4) + (hs-to-sx (nth ast 4)) + nil))) (let ((comp (if (string? comp-raw) (make-symbol comp-raw) (hs-to-sx comp-raw)))) (define @@ -2266,7 +2439,9 @@ ((and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-reset!) - (list (quote hs-query-all) (nth raw-tgt 1)))) + (list + (quote hs-query-all) + (nth raw-tgt 1)))) (true (list (quote hs-reset!) (hs-to-sx raw-tgt)))))) ((= head (quote default!)) (let @@ -2292,7 +2467,8 @@ (list (quote dom-focus) (hs-to-sx (nth ast 1)))) ((= head (quote js-block)) (let - ((params (nth ast 1)) (js-src (nth ast 2))) + ((params (nth ast 1)) + (js-src (nth ast 2))) (let ((bound-syms (map (fn (p) (make-symbol p)) params))) (list diff --git a/lib/hyperscript/integration.sx b/lib/hyperscript/integration.sx index cade049f..823a899d 100644 --- a/lib/hyperscript/integration.sx +++ b/lib/hyperscript/integration.sx @@ -77,26 +77,51 @@ ;; Marks the element to avoid double-activation. (define - hs-activate! + hs-register-scripts! (fn - (el) - (let - ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) - (when - (and src (not (= src prev))) + () + (for-each + (fn + (script) (when - (dom-dispatch el "hyperscript:before:init" nil) - (hs-log-event! "hyperscript:init") - (dom-set-data el "hs-script" src) - (dom-set-data el "hs-active" true) - (dom-set-attr el "data-hyperscript-powered" "true") - (let ((handler (hs-handler src))) (handler el)) - (dom-dispatch el "hyperscript:after:init" nil)))))) + (not (dom-get-data script "hs-script-loaded")) + (let + ((src (host-get script "innerHTML"))) + (when + (and src (not (= src ""))) + (guard + (_e (true nil)) + (eval-expr-cek (hs-to-sx-from-source src))) + (dom-set-data script "hs-script-loaded" true))))) + (hs-query-all "script[type=text/hyperscript]")))) ;; ── Boot: scan entire document ────────────────────────────────── ;; Called once at page load. Finds all elements with _ attribute, ;; compiles their hyperscript, and activates them. +(define + hs-activate! + (fn + (el) + (do + (hs-register-scripts!) + (let + ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) + (when + (and src (not (= src prev))) + (when + (dom-dispatch el "hyperscript:before:init" nil) + (hs-log-event! "hyperscript:init") + (dom-set-data el "hs-script" src) + (dom-set-data el "hs-active" true) + (dom-set-attr el "data-hyperscript-powered" "true") + (let ((handler (hs-handler src))) (handler el)) + (dom-dispatch el "hyperscript:after:init" nil))))))) + +;; ── Boot subtree: for dynamic content ─────────────────────────── +;; Called after HTMX swaps or dynamic DOM insertion. +;; Only activates elements within the given root. + (define hs-deactivate! (fn @@ -108,10 +133,6 @@ (dom-set-data el "hs-active" false) (dom-set-data el "hs-script" nil)))) -;; ── Boot subtree: for dynamic content ─────────────────────────── -;; Called after HTMX swaps or dynamic DOM insertion. -;; Only activates elements within the given root. - (define hs-boot! (fn diff --git a/shared/static/wasm/sx/hs-compiler.sx b/shared/static/wasm/sx/hs-compiler.sx index e25a60f3..e685be17 100644 --- a/shared/static/wasm/sx/hs-compiler.sx +++ b/shared/static/wasm/sx/hs-compiler.sx @@ -34,7 +34,8 @@ (cond ((= th dot-sym) (let - ((base-ast (nth target 1)) (prop (nth target 2))) + ((base-ast (nth target 1)) + (prop (nth target 2))) (cond ((and (list? base-ast) (= (first base-ast) (quote query)) (let ((s (nth base-ast 1))) (and (string? s) (> (len s) 0) (= (substring s 0 1) ".")))) (list @@ -85,7 +86,10 @@ (nth target 1) value)) ((= th (quote ref)) - (list (quote set!) (make-symbol (nth target 1)) value)) + (list + (quote set!) + (make-symbol (nth target 1)) + value)) ((= th (quote local)) (list (quote hs-scoped-set!) @@ -113,7 +117,8 @@ (list (quote hs-set-inner-html!) (hs-to-sx target) value)) ((= th (quote of)) (let - ((prop-ast (nth target 1)) (obj-ast (nth target 2))) + ((prop-ast (nth target 1)) + (obj-ast (nth target 2))) (if (and (list? prop-ast) (= (first prop-ast) dot-sym)) (let @@ -377,7 +382,8 @@ (fn (ast) (let - ((mode (nth ast 1)) (body (hs-to-sx (nth ast 2)))) + ((mode (nth ast 1)) + (body (hs-to-sx (nth ast 2)))) (cond ((and (list? mode) (= (first mode) (quote forever))) (list @@ -414,13 +420,23 @@ (list (quote fn) (list) body))))))) (define hs-reserved-var? - (fn (name) (or (= name "meta") (= name "event") (= name "it") (= name "result"))) + (fn + (name) + (or + (= name "meta") + (= name "event") + (= name "it") + (= name "result"))) emit-for (fn (ast) (let ((var-name (nth ast 1)) - (safe-param (if (hs-reserved-var? var-name) (str "_hs_lv_" var-name) var-name)) + (safe-param + (if + (hs-reserved-var? var-name) + (str "_hs_lv_" var-name) + var-name)) (raw-coll-ast (nth ast 2)) (where-cond (if @@ -455,7 +471,9 @@ (quote map-indexed) (list (quote fn) - (list (make-symbol (nth ast 5)) (make-symbol safe-param)) + (list + (make-symbol (nth ast 5)) + (make-symbol safe-param)) body) collection) (list @@ -468,13 +486,15 @@ (ast) (let ((event-name (nth ast 1)) - (has-from (and (> (len ast) 2) (= (nth ast 2) :from))) + (has-from + (and (> (len ast) 2) (= (nth ast 2) :from))) (has-from-or (and (> (len ast) 4) (= (nth ast 2) :from) (= (nth ast 4) :or))) - (has-or (and (> (len ast) 2) (= (nth ast 2) :or)))) + (has-or + (and (> (len ast) 2) (= (nth ast 2) :or)))) (cond (has-from-or (list @@ -483,7 +503,10 @@ event-name (nth ast 5))) (has-from - (list (quote hs-wait-for) (hs-to-sx (nth ast 3)) event-name)) + (list + (quote hs-wait-for) + (hs-to-sx (nth ast 3)) + event-name)) (has-or (list (quote hs-wait-for-or) @@ -512,9 +535,14 @@ (ast) (let ((type-name (nth ast 1)) - (called (if (>= (len ast) 3) (nth ast 2) nil)) + (called + (if (>= (len ast) 3) (nth ast 2) nil)) (args (if (>= (len ast) 4) (nth ast 3) nil)) - (kind (if (>= (len ast) 5) (nth ast 4) (quote auto)))) + (kind + (if + (>= (len ast) 5) + (nth ast 4) + (quote auto)))) (let ((make-call (cond ((nil? args) (list (quote hs-make) type-name)) (true (cons (quote hs-make) (cons type-name (map hs-to-sx args))))))) (cond @@ -571,7 +599,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) dot-sym)) (let - ((obj (hs-to-sx (nth expr 1))) (prop (nth expr 2))) + ((obj (hs-to-sx (nth expr 1))) + (prop (nth expr 2))) (list (quote let) (list @@ -608,7 +637,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) (quote dom-ref))) (let - ((el (hs-to-sx (nth expr 2))) (name (nth expr 1))) + ((el (hs-to-sx (nth expr 2))) + (name (nth expr 1))) (list (quote let) (list @@ -687,7 +717,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) dot-sym)) (let - ((obj (hs-to-sx (nth expr 1))) (prop (nth expr 2))) + ((obj (hs-to-sx (nth expr 1))) + (prop (nth expr 2))) (list (quote let) (list @@ -724,7 +755,8 @@ (list (quote set!) (quote it) (quote __hs-new)))))) ((and (list? expr) (= (first expr) (quote dom-ref))) (let - ((el (hs-to-sx (nth expr 2))) (name (nth expr 1))) + ((el (hs-to-sx (nth expr 2))) + (name (nth expr 1))) (list (quote let) (list @@ -782,14 +814,28 @@ (fn (ast) (let - ((name (nth ast 1)) (params (nth ast 2)) (body (nth ast 3))) + ((name (nth ast 1)) + (params (nth ast 2)) + (body (nth ast 3))) (list (quote define) (make-symbol name) (list (quote fn) - (cons (quote me) (map make-symbol params)) - (cons (quote do) (map hs-to-sx body))))))) + (cons + (quote me) + (map + (fn + (p) + (if + (list? p) + (make-symbol (nth p 1)) + (make-symbol p))) + params)) + (list + (quote let) + (list (list (quote beingTold) (quote me))) + (cons (quote do) (map hs-to-sx body)))))))) (fn (ast) (let @@ -846,7 +892,10 @@ (let ((raw (nth ast 1))) (let - ((parts (list)) (buf "") (i 0) (n (len raw))) + ((parts (list)) + (buf "") + (i 0) + (n (len raw))) (define tpl-flush (fn @@ -884,10 +933,14 @@ (if (= depth 1) j - (tpl-find-close (+ j 1) (- depth 1))) + (tpl-find-close + (+ j 1) + (- depth 1))) (if (= (nth raw j) "{") - (tpl-find-close (+ j 1) (+ depth 1)) + (tpl-find-close + (+ j 1) + (+ depth 1)) (tpl-find-close (+ j 1) depth)))))) (define tpl-collect @@ -898,7 +951,10 @@ (let ((ch (nth raw i))) (if - (and (= ch "\\") (< (+ i 1) n) (= (nth raw (+ i 1)) "$")) + (and + (= ch "\\") + (< (+ i 1) n) + (= (nth raw (+ i 1)) "$")) (do (set! buf (str buf "$")) (set! i (+ i 2)) @@ -907,39 +963,40 @@ (and (= ch "$") (< (+ i 1) n)) (if (= (nth raw (+ i 1)) "{") - (let - ((start (+ i 2))) (let - ((close (tpl-find-close start 1))) + ((start (+ i 2))) (let - ((expr-src (slice raw start close))) - (do - (tpl-flush) - (set! - parts - (append + ((close (tpl-find-close start 1))) + (let + ((expr-src (slice raw start close))) + (do + (tpl-flush) + (set! parts - (list - (hs-to-sx - (hs-compile expr-src))))) - (set! i (+ close 1)) - (tpl-collect))))) - (let - ((start (+ i 1))) + (append + parts + (list + (hs-to-sx + (hs-compile expr-src))))) + (set! i (+ close 1)) + (tpl-collect))))) (let - ((end (tpl-read-id start))) + ((start (+ i 1))) (let - ((ident (slice raw start end))) - (do - (tpl-flush) - (set! - parts - (append + ((end (tpl-read-id start))) + (let + ((ident (slice raw start end))) + (do + (tpl-flush) + (set! parts - (list - (hs-to-sx (hs-compile ident))))) - (set! i end) - (tpl-collect)))))) + (append + parts + (list + (hs-to-sx + (hs-compile ident))))) + (set! i end) + (tpl-collect)))))) (do (set! buf (str buf ch)) (set! i (+ i 1)) @@ -983,7 +1040,10 @@ (list (quote hs-pick-random) (hs-to-sx (nth ast 1)) - (if (nil? (nth ast 2)) nil (hs-to-sx (nth ast 2)))))) + (if + (nil? (nth ast 2)) + nil + (hs-to-sx (nth ast 2)))))) ((= head (quote pick-items)) (list (quote set!) @@ -1082,7 +1142,10 @@ (quote hs-method-call) (cons (hs-to-sx dot-node) args)))))) ((= head (quote string-postfix)) - (list (quote str) (hs-to-sx (nth ast 1)) (nth ast 2))) + (list + (quote str) + (hs-to-sx (nth ast 1)) + (nth ast 2))) ((= head (quote block-literal)) (let ((params (map make-symbol (nth ast 1))) @@ -1101,10 +1164,11 @@ ((= prop "last") (list (quote hs-last) target)) (true (list (quote host-get) target prop))))) ((= head (quote ref)) - (if - (= (nth ast 1) "selection") - (list (quote hs-get-selection)) - (make-symbol (nth ast 1)))) + (cond + ((= (nth ast 1) "selection") + (list (quote hs-get-selection))) + ((= (nth ast 1) "element") (make-symbol "me")) + (else (make-symbol (nth ast 1))))) ((= head (quote query)) (list (quote hs-query-first) (nth ast 1))) ((= head (quote query-scoped)) @@ -1133,7 +1197,10 @@ (hs-to-sx (nth ast 1)) (nth ast 2))) ((= head (quote local)) - (list (quote hs-scoped-get) (quote me) (nth ast 1))) + (list + (quote hs-scoped-get) + (quote me) + (nth ast 1))) ((= head (quote array)) (cons (quote list) (map hs-to-sx (rest ast)))) ((= head (quote not)) @@ -1196,7 +1263,8 @@ (list (quote nil?) (hs-to-sx (nth ast 1))))) ((= head (quote matches?)) (let - ((left (nth ast 1)) (right (nth ast 2))) + ((left (nth ast 1)) + (right (nth ast 2))) (if (and (list? right) (= (first right) (quote query))) (list @@ -1316,7 +1384,10 @@ "parentElement") (nth ast 1))) ((= head (quote next)) - (list (quote hs-next) (hs-to-sx (nth ast 2)) (nth ast 1))) + (list + (quote hs-next) + (hs-to-sx (nth ast 2)) + (nth ast 1))) ((= head (quote previous)) (list (quote hs-previous) @@ -1367,7 +1438,8 @@ (nth ast 2))) ((= head (quote set-styles)) (let - ((pairs (nth ast 1)) (tgt (hs-to-sx (nth ast 2)))) + ((pairs (nth ast 1)) + (tgt (hs-to-sx (nth ast 2)))) (cons (quote do) (map @@ -1512,7 +1584,8 @@ (list (quote hs-splice-at!) (hs-to-sx coll) idx)))) ((and (list? tgt) (= (first tgt) dot-sym)) (let - ((obj (nth tgt 1)) (prop (nth tgt 2))) + ((obj (nth tgt 1)) + (prop (nth tgt 2))) (emit-set obj (list @@ -1521,7 +1594,8 @@ prop)))) ((and (list? tgt) (= (first tgt) (quote of))) (let - ((prop-ast (nth tgt 1)) (obj-ast (nth tgt 2))) + ((prop-ast (nth tgt 1)) + (obj-ast (nth tgt 2))) (let ((prop (cond ((string? prop-ast) prop-ast) ((and (list? prop-ast) (= (first prop-ast) (quote ref))) (nth prop-ast 1)) (true (hs-to-sx prop-ast))))) (emit-set @@ -1533,7 +1607,8 @@ (true (list (quote dom-remove) (hs-to-sx tgt)))))) ((= head (quote add-value)) (let - ((val (hs-to-sx (nth ast 1))) (tgt (nth ast 2))) + ((val (hs-to-sx (nth ast 1))) + (tgt (nth ast 2))) (emit-set tgt (list (quote hs-add-to!) val (hs-to-sx tgt))))) @@ -1547,7 +1622,8 @@ (hs-to-sx (nth ast 2))))) ((= head (quote remove-value)) (let - ((val (hs-to-sx (nth ast 1))) (tgt (nth ast 2))) + ((val (hs-to-sx (nth ast 1))) + (tgt (nth ast 2))) (emit-set tgt (list (quote hs-remove-from!) val (hs-to-sx tgt))))) @@ -1562,7 +1638,10 @@ ((and (list? tgt) (= (first tgt) (quote query))) (list (quote for-each) - (list (quote fn) (list (quote _el)) (list (quote hs-empty-target!) (quote _el))) + (list + (quote fn) + (list (quote _el)) + (list (quote hs-empty-target!) (quote _el))) (list (quote hs-query-all) (nth tgt 1)))) (true (list (quote hs-empty-target!) (hs-to-sx tgt)))))) ((= head (quote open-element)) @@ -1710,7 +1789,9 @@ (nth ast 3) (hs-to-sx (nth ast 4)))) ((= head (quote set!)) - (emit-set (nth ast 1) (hs-to-sx (nth ast 2)))) + (emit-set + (nth ast 1) + (hs-to-sx (nth ast 2)))) ((= head (quote put!)) (let ((val (hs-to-sx (nth ast 1))) @@ -1800,7 +1881,8 @@ (= (first c) (quote define))))) compiled))) (cons (quote do) (append defs non-defs))))))) - ((= head (quote wait)) (list (quote hs-wait) (nth ast 1))) + ((= head (quote wait)) + (list (quote hs-wait) (nth ast 1))) ((= head (quote wait-for)) (emit-wait-for ast)) ((= head (quote log)) (list (quote console-log) (hs-to-sx (nth ast 1)))) @@ -1813,8 +1895,16 @@ (= (len ast) 4) (list? (nth ast 2)) (= (first (nth ast 2)) (quote dict)))) - (tgt (if (= (len ast) 4) (nth ast 3) (nth ast 2))) - (detail (if (= (len ast) 4) (nth ast 2) nil))) + (tgt + (if + (= (len ast) 4) + (nth ast 3) + (nth ast 2))) + (detail + (if + (= (len ast) 4) + (nth ast 2) + nil))) (list (quote dom-dispatch) (hs-to-sx tgt) @@ -1823,8 +1913,16 @@ ((= head (quote hide)) (let ((tgt (let ((raw-tgt (nth ast 1))) (if (and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-query-all) (nth raw-tgt 1)) (hs-to-sx raw-tgt)))) - (strategy (if (> (len ast) 2) (nth ast 2) "display")) - (when-cond (if (> (len ast) 3) (nth ast 3) nil))) + (strategy + (if + (> (len ast) 2) + (nth ast 2) + "display")) + (when-cond + (if + (> (len ast) 3) + (nth ast 3) + nil))) (if (nil? when-cond) (list (quote hs-hide!) tgt strategy) @@ -1839,8 +1937,16 @@ ((= head (quote show)) (let ((tgt (let ((raw-tgt (nth ast 1))) (if (and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-query-all) (nth raw-tgt 1)) (hs-to-sx raw-tgt)))) - (strategy (if (> (len ast) 2) (nth ast 2) "display")) - (when-cond (if (> (len ast) 3) (nth ast 3) nil))) + (strategy + (if + (> (len ast) 2) + (nth ast 2) + "display")) + (when-cond + (if + (> (len ast) 3) + (nth ast 3) + nil))) (if (nil? when-cond) (list (quote hs-show!) tgt strategy) @@ -1884,23 +1990,41 @@ ((= head (quote repeat-until)) (list (quote hs-repeat-until) - (list (quote fn) (list) (hs-to-sx (nth ast 1))) - (list (quote fn) (list) (hs-to-sx (nth ast 2))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 2))))) ((= head (quote repeat-while)) (list (quote hs-repeat-while) - (list (quote fn) (list) (hs-to-sx (nth ast 1))) - (list (quote fn) (list) (hs-to-sx (nth ast 2))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 2))))) ((= head (quote fetch)) (list - (if (nth ast 3) (quote hs-fetch-no-throw) (quote hs-fetch)) + (if + (nth ast 3) + (quote hs-fetch-no-throw) + (quote hs-fetch)) (hs-to-sx (nth ast 1)) (nth ast 2))) ((= head (quote fetch-gql)) (list (quote hs-fetch-gql) (nth ast 1) - (if (nth ast 2) (hs-to-sx (nth ast 2)) nil))) + (if + (nth ast 2) + (hs-to-sx (nth ast 2)) + nil))) ((= head (quote call)) (let ((raw-fn (nth ast 1)) @@ -1911,14 +2035,7 @@ (hs-to-sx raw-fn))) (args (map hs-to-sx (rest (rest ast))))) (let - ((call-expr - (if - (and (list? raw-fn) (= (first raw-fn) (quote ref))) - (list - (quote hs-win-call) - (nth raw-fn 1) - (cons (quote list) args)) - (cons fn-expr args)))) + ((call-expr (if (and (list? raw-fn) (= (first raw-fn) (quote ref))) (list (quote hs-win-call) (nth raw-fn 1) (cons (quote list) args)) (cons fn-expr args)))) (emit-set (quote the-result) call-expr)))) ((= head (quote return)) (let @@ -1936,7 +2053,9 @@ ((= head (quote settle)) (list (quote hs-settle) (quote me))) ((= head (quote go)) - (list (quote hs-navigate!) (hs-to-sx (nth ast 1)))) + (list + (quote hs-navigate!) + (hs-to-sx (nth ast 1)))) ((= head (quote ask)) (let ((val (list (quote hs-ask) (hs-to-sx (nth ast 1))))) @@ -2022,17 +2141,35 @@ (let ((kind (nth ast 1)) (name (nth ast 2)) - (from-sel (if (> (len ast) 3) (nth ast 3) nil)) - (for-tgt (if (> (len ast) 4) (nth ast 4) nil)) - (attr-val (if (> (len ast) 5) (nth ast 5) nil)) - (with-val (if (> (len ast) 6) (nth ast 6) nil))) + (from-sel + (if + (> (len ast) 3) + (nth ast 3) + nil)) + (for-tgt + (if + (> (len ast) 4) + (nth ast 4) + nil)) + (attr-val + (if + (> (len ast) 5) + (nth ast 5) + nil)) + (with-val + (if + (> (len ast) 6) + (nth ast 6) + nil))) (let ((target (if for-tgt (hs-to-sx for-tgt) (quote me))) (scope (cond ((nil? from-sel) nil) ((and (list? from-sel) (= (first from-sel) (quote query))) - (list (quote hs-query-all) (nth from-sel 1))) + (list + (quote hs-query-all) + (nth from-sel 1))) (true (hs-to-sx from-sel)))) (with-sx (if @@ -2064,13 +2201,31 @@ (true (list (quote hs-take!) target kind name scope)))))) ((= head (quote make)) (emit-make ast)) ((= head (quote install)) - (cons (quote hs-install) (map hs-to-sx (rest ast)))) + (let + ((bname (nth ast 1))) + (cons + (make-symbol bname) + (cons + (quote me) + (map + (fn + (arg) + (if + (and + (list? arg) + (= (first arg) (quote type-assert))) + (+ (nth arg 2) 0) + (hs-to-sx arg))) + (rest (rest ast))))))) ((= head (quote measure)) (list (quote hs-measure) (hs-to-sx (nth ast 1)))) ((= head (quote increment!)) (if (= (len ast) 3) - (emit-inc (nth ast 1) 1 (nth ast 2)) + (emit-inc + (nth ast 1) + 1 + (nth ast 2)) (emit-inc (nth ast 1) (nth ast 2) @@ -2078,7 +2233,10 @@ ((= head (quote decrement!)) (if (= (len ast) 3) - (emit-dec (nth ast 1) 1 (nth ast 2)) + (emit-dec + (nth ast 1) + 1 + (nth ast 2)) (emit-dec (nth ast 1) (nth ast 2) @@ -2092,7 +2250,8 @@ ((= head (quote on)) (emit-on ast)) ((= head (quote when-changes)) (let - ((expr (nth ast 1)) (body (nth ast 2))) + ((expr (nth ast 1)) + (body (nth ast 2))) (if (and (list? expr) (= (first expr) (quote dom-ref))) (list @@ -2104,7 +2263,10 @@ ((= head (quote init)) (list (quote hs-init) - (list (quote fn) (list) (hs-to-sx (nth ast 1))))) + (list + (quote fn) + (list) + (hs-to-sx (nth ast 1))))) ((= head (quote def)) (let ((body (hs-to-sx (nth ast 3))) @@ -2143,7 +2305,10 @@ (quote =) (list (quote first) (quote _e)) "hs-return")) - (list (quote nth) (quote _e) 1) + (list + (quote nth) + (quote _e) + 1) (list (quote raise) (quote _e))))) body)))) (list @@ -2162,14 +2327,22 @@ (string? src) (first (sx-parse src)) (list (quote cek-eval) (hs-to-sx src))))) - ((= head (quote component)) (make-symbol (nth ast 1))) + ((= head (quote component)) + (make-symbol (nth ast 1))) ((= head (quote render)) (let ((comp-raw (nth ast 1)) (kwargs (nth ast 2)) - (pos (if (> (len ast) 3) (nth ast 3) nil)) + (pos + (if + (> (len ast) 3) + (nth ast 3) + nil)) (target - (if (> (len ast) 4) (hs-to-sx (nth ast 4)) nil))) + (if + (> (len ast) 4) + (hs-to-sx (nth ast 4)) + nil))) (let ((comp (if (string? comp-raw) (make-symbol comp-raw) (hs-to-sx comp-raw)))) (define @@ -2266,7 +2439,9 @@ ((and (list? raw-tgt) (= (first raw-tgt) (quote query))) (list (quote hs-reset!) - (list (quote hs-query-all) (nth raw-tgt 1)))) + (list + (quote hs-query-all) + (nth raw-tgt 1)))) (true (list (quote hs-reset!) (hs-to-sx raw-tgt)))))) ((= head (quote default!)) (let @@ -2292,7 +2467,8 @@ (list (quote dom-focus) (hs-to-sx (nth ast 1)))) ((= head (quote js-block)) (let - ((params (nth ast 1)) (js-src (nth ast 2))) + ((params (nth ast 1)) + (js-src (nth ast 2))) (let ((bound-syms (map (fn (p) (make-symbol p)) params))) (list diff --git a/shared/static/wasm/sx/hs-integration.sx b/shared/static/wasm/sx/hs-integration.sx index cade049f..823a899d 100644 --- a/shared/static/wasm/sx/hs-integration.sx +++ b/shared/static/wasm/sx/hs-integration.sx @@ -77,26 +77,51 @@ ;; Marks the element to avoid double-activation. (define - hs-activate! + hs-register-scripts! (fn - (el) - (let - ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) - (when - (and src (not (= src prev))) + () + (for-each + (fn + (script) (when - (dom-dispatch el "hyperscript:before:init" nil) - (hs-log-event! "hyperscript:init") - (dom-set-data el "hs-script" src) - (dom-set-data el "hs-active" true) - (dom-set-attr el "data-hyperscript-powered" "true") - (let ((handler (hs-handler src))) (handler el)) - (dom-dispatch el "hyperscript:after:init" nil)))))) + (not (dom-get-data script "hs-script-loaded")) + (let + ((src (host-get script "innerHTML"))) + (when + (and src (not (= src ""))) + (guard + (_e (true nil)) + (eval-expr-cek (hs-to-sx-from-source src))) + (dom-set-data script "hs-script-loaded" true))))) + (hs-query-all "script[type=text/hyperscript]")))) ;; ── Boot: scan entire document ────────────────────────────────── ;; Called once at page load. Finds all elements with _ attribute, ;; compiles their hyperscript, and activates them. +(define + hs-activate! + (fn + (el) + (do + (hs-register-scripts!) + (let + ((src (dom-get-attr el "_")) (prev (dom-get-data el "hs-script"))) + (when + (and src (not (= src prev))) + (when + (dom-dispatch el "hyperscript:before:init" nil) + (hs-log-event! "hyperscript:init") + (dom-set-data el "hs-script" src) + (dom-set-data el "hs-active" true) + (dom-set-attr el "data-hyperscript-powered" "true") + (let ((handler (hs-handler src))) (handler el)) + (dom-dispatch el "hyperscript:after:init" nil))))))) + +;; ── Boot subtree: for dynamic content ─────────────────────────── +;; Called after HTMX swaps or dynamic DOM insertion. +;; Only activates elements within the given root. + (define hs-deactivate! (fn @@ -108,10 +133,6 @@ (dom-set-data el "hs-active" false) (dom-set-data el "hs-script" nil)))) -;; ── Boot subtree: for dynamic content ─────────────────────────── -;; Called after HTMX swaps or dynamic DOM insertion. -;; Only activates elements within the given root. - (define hs-boot! (fn