Skip to content

Commit 804d6cd

Browse files
authored
Feature: updating to RC7, patch elements namespaces (#15)
- added the new namespace option to patch elements functions - examples of morphing using namespaces
1 parent 49954ab commit 804d6cd

11 files changed

Lines changed: 285 additions & 35 deletions

File tree

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Release notes for the Clojure SDK
22

3+
## 2025-12-22 - RC6
4+
### Added
5+
- Patch elements functions in the SDK now provide the namespace option introduced in
6+
[Datastar RC7](https://github.com/starfederation/datastar/releases/tag/v1.0.0-RC.7).
7+
8+
### Changes
9+
- Urls for the Datastar CDN provided in the API now point to
10+
[Datastar RC7](https://github.com/starfederation/datastar/releases/tag/v1.0.0-RC.7).
11+
312
## 2025-12-14 - RC5
413

514
### Fixed

libraries/sdk/src/main/starfederation/datastar/clojure/api.clj

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ These function take options map whose keys are:
1616
- [[selector]]
1717
- [[patch-mode]]
1818
- [[use-view-transition]]
19+
- [[element-ns]]
1920
- [[only-if-missing]]
2021
- [[auto-remove]]
2122
- [[attributes]]
@@ -54,13 +55,13 @@ Some scripts are provided:
5455
;; -----------------------------------------------------------------------------
5556
(def CDN-url
5657
"URL for the Datastar js bundle tracking the latest Datastar, currently
57-
v1.0.0-RC6."
58+
v1.0.0-RC7."
5859

59-
"https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.6/bundles/datastar.js")
60+
"https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.7/bundles/datastar.js")
6061

6162
(def CDN-map-url
6263
"URL for the Datastar source map going with [[CDN-url]]."
63-
"https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.6/bundles/datastar.js.map")
64+
"https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.7/bundles/datastar.js.map")
6465

6566
;; -----------------------------------------------------------------------------
6667
;; SSE generator management
@@ -180,6 +181,17 @@ Some scripts are provided:
180181
Datastar client side will default to false."
181182
common/use-view-transition)
182183

184+
185+
(def element-ns
186+
"[[patch-elements!]] & [[patch-elements-seq!]] option, boolean:
187+
188+
Use a namespace when patching elements.
189+
Possible values are:
190+
- [[ns-html]] default
191+
- [[ns-svg]]
192+
- [[ns-mathml]]"
193+
common/element-namespace)
194+
183195
;;Signals opts
184196
(def only-if-missing
185197
"[[patch-signals!]] option, boolean:
@@ -240,6 +252,17 @@ Some scripts are provided:
240252
related state."
241253
consts/element-patch-mode-replace)
242254

255+
(def ns-html
256+
"element namespace: default html namespace"
257+
consts/element-namespace-html)
258+
259+
(def ns-svg
260+
"element namespace: svg namespace"
261+
consts/element-namespace-svg)
262+
263+
(def ns-mathml
264+
"element namespace: mathMl namespace"
265+
consts/element-namespace-mathml)
243266

244267
(defn patch-elements!
245268
"Send HTML elements to the browser to be patchd into the DOM.
@@ -255,6 +278,7 @@ Some scripts are provided:
255278
- [[selector]]
256279
- [[patch-mode]]
257280
- [[use-view-transition]]
281+
- [[element-ns]]
258282
259283
Return value:
260284
- `false` if the connection is closed
@@ -378,7 +402,7 @@ Some scripts are provided:
378402
(scripts/execute-script! sse-gen script-text opts)))
379403

380404

381-
405+
382406
;; -----------------------------------------------------------------------------
383407
;; SSE helpers
384408
;; -----------------------------------------------------------------------------
@@ -483,4 +507,3 @@ Some scripts are provided:
483507
of the header means the request is issued from a datastar action."
484508
[request]
485509
(= "true" (get-in request [:headers "datastar-request"])))
486-

libraries/sdk/src/main/starfederation/datastar/clojure/api/common.clj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
(ns starfederation.datastar.clojure.api.common
2-
(:require
3-
[clojure.string :as string]))
1+
(ns starfederation.datastar.clojure.api.common)
42

53
;; -----------------------------------------------------------------------------
64
;; Option names
@@ -14,6 +12,7 @@
1412
(def selector :d*.elements/selector)
1513
(def patch-mode :d*.elements/patch-mode)
1614
(def use-view-transition :d*.elements/use-view-transition)
15+
(def element-namespace :d*.elements/namespace)
1716

1817
;;Signals opts
1918
(def only-if-missing :d*.signals/only-if-missing)

libraries/sdk/src/main/starfederation/datastar/clojure/api/elements.clj

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@
1212
;; -----------------------------------------------------------------------------
1313
(def ^:private valid-selector? u/not-empty-string?)
1414

15-
(defn- add-epm? [fmm]
16-
(and fmm (not= fmm consts/default-element-patch-mode)))
15+
(defn- add-epm? [patch-mode]
16+
(not= patch-mode consts/default-element-patch-mode))
1717

1818
(defn- add-view-transition? [v]
1919
(common/add-boolean-option? consts/default-elements-use-view-transitions v))
2020

21+
(defn add-namespace? [namespace]
22+
(not= consts/default-element-namespace namespace))
2123

2224
(defn conj-patch-element-opts!
2325
"Conj the optional data-lines to the transient `data-lines` vector.
2426
vector."
2527
[data-lines! opts]
2628
(let [sel (common/selector opts)
2729
patch-mode (common/patch-mode opts)
28-
use-vt (common/use-view-transition opts)]
30+
use-vt (common/use-view-transition opts)
31+
namespace (common/element-namespace opts)]
2932

3033
(cond-> data-lines!
3134
(and sel (valid-selector? sel))
@@ -35,7 +38,10 @@
3538
(common/add-opt-line! consts/mode-dataline-literal patch-mode)
3639

3740
(and use-vt (add-view-transition? use-vt))
38-
(common/add-opt-line! consts/use-view-transition-dataline-literal use-vt))))
41+
(common/add-opt-line! consts/use-view-transition-dataline-literal use-vt)
42+
43+
(and namespace (add-namespace? namespace))
44+
(common/add-opt-line! consts/namespace-dataline-literal namespace))))
3945

4046

4147

@@ -76,10 +82,12 @@
7682
(= (->patch-elements "<div>hello</div> \n<div>world!!!</div>"
7783
{common/selector "#toto"
7884
common/patch-mode consts/element-patch-mode-after
79-
common/use-view-transition true})
85+
common/use-view-transition true
86+
common/element-namespace "svg"})
8087
["selector #toto"
8188
"mode after"
8289
"useViewTransition true"
90+
"namespace svg"
8391
"elements <div>hello</div> "
8492
"elements <div>world!!!</div>"]))
8593

libraries/sdk/src/main/starfederation/datastar/clojure/consts.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
(def use-view-transition-dataline-literal "useViewTransition ")
2323
(def signals-dataline-literal "signals ")
2424
(def only-if-missing-dataline-literal "onlyIfMissing ")
25+
(def namespace-dataline-literal "namespace ")
2526

2627

2728
;; -----------------------------------------------------------------------------
@@ -80,6 +81,12 @@
8081
Morphs the element into the existing element."
8182
element-patch-mode-outer)
8283

84+
;; Namespaces
85+
(def element-namespace-html "html")
86+
(def element-namespace-svg "svg")
87+
(def element-namespace-mathml "mathml")
88+
(def default-element-namespace element-namespace-html)
89+
8390

8491
;; EventType
8592

src/dev/examples/form_behavior/core.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@
125125
(def router
126126
(rr/router
127127
[["/" {:handler form}]
128-
["/endpoint" {:handler process-form}]
128+
["/endpoint" {:handler process-form}]]))
129129
;:parameters {:multipart true} :middleware [mpparams/multipart-middleware]}]
130-
c/datastar-route]))
131130

132131

133132
(def handler
@@ -137,4 +136,5 @@
137136

138137

139138
(comment
140-
(u/reboot-hk-server! #'handler))
139+
(u/reboot-hk-server! #'handler)
140+
(user/clear-terminal!))

src/dev/examples/forms/core.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
(rr/router
2626
[["/" {:handler (constantly (rur/response home))}]
2727
["" efh/routes]
28-
["" efd*/routes]
29-
c/datastar-route]))
28+
["" efd*/routes]]))
3029

3130

3231

src/dev/examples/namespaces.clj

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
(ns examples.namespaces
2+
(:require
3+
[clojure.java.io :as io]
4+
[examples.common :as c]
5+
[dev.onionpancakes.chassis.core :as h]
6+
[dev.onionpancakes.chassis.compiler :as hc]
7+
[examples.utils :as u]
8+
[ring.util.response :as rur]
9+
[reitit.ring :as rr]
10+
[reitit.ring.middleware.parameters :as params]
11+
[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response on-open]]
12+
[starfederation.datastar.clojure.api :as d*]))
13+
14+
;; example taken from https://github.com/zigster64/datastar.http.zig/blob/d142e841620769fde0647fd032025b71622e70f4/examples/01_basic.zig
15+
;; Here we patch svg/math elements using the d*/element-ns options
16+
(defn r-int [low high]
17+
(+ low (rand-int (- high low))))
18+
19+
20+
(defn random-rect []
21+
[:rect {:id :svg-rect
22+
:x (r-int 10 100)
23+
:y (r-int 10 100)
24+
:width (r-int 10 80)
25+
:height 80
26+
:fill "red" :stroke "black" :stroke-width 4
27+
:class "animate"}])
28+
29+
30+
(defn random-circle []
31+
[:circle {:id :svg-circle
32+
:cx (r-int 10 100)
33+
:cy (r-int 10 100)
34+
:r (r-int 10 80)
35+
:fill "green" :stroke "black" :stroke-width 4
36+
:class "animate"}])
37+
38+
39+
(defn random-polygon []
40+
(let [v #(r-int 50 300)]
41+
[:polygon {:id :svg-triangle
42+
:points (str (v) "," (v) " "
43+
(v) "," (v) " "
44+
(v) "," (v) " ")
45+
:fill "blue"
46+
:stroke "black"
47+
:stroke-width 4
48+
:class "animate"}]))
49+
50+
(defn random-img []
51+
[:svg {:width 500
52+
:height 300
53+
:viewBox "0 0 500 300"
54+
:xmlns "http://www.w3.org/2000/svg"}
55+
(random-rect)
56+
(random-polygon)
57+
(random-circle)])
58+
59+
(def math
60+
[:math {:id "math-ml" :display "block"}
61+
[:mrow
62+
[:mi "E"]
63+
[:mo "="]
64+
[:mi "m"]
65+
[:msup
66+
[:mi "c"]
67+
[:mn {:id "math-factor"} "2"]]]])
68+
69+
(def page
70+
(h/html
71+
(c/page-scaffold
72+
[:div
73+
[:style ".animate {transition: all 0.4s ease;}"]
74+
[:h1 "Namespaces tests"]
75+
[:div
76+
[:h2 "svg"]
77+
[:button {:data-on:click (d*/sse-post "/rand")} "random!!!"]
78+
(random-img)]
79+
[:div
80+
[:h2 "math"]
81+
[:button {:data-on:click (d*/sse-post "/rand-math")} "random!!!"]
82+
math]])))
83+
84+
85+
86+
(defn home
87+
([_]
88+
(rur/response page))
89+
([req respond _]
90+
(respond (home req))))
91+
92+
93+
(defn merge-svg [req]
94+
(->sse-response req
95+
{on-open (fn [sse]
96+
(d*/with-open-sse sse
97+
(d*/patch-elements-seq! sse
98+
[(h/html (random-rect))
99+
(h/html (random-circle))
100+
(h/html (random-polygon))]
101+
{d*/element-ns d*/ns-svg})))}))
102+
103+
104+
105+
(defn merge-math [req]
106+
(->sse-response req
107+
{on-open (fn [sse]
108+
(d*/with-open-sse sse
109+
(d*/patch-elements! sse
110+
(h/html [:mn {:id "math-factor"} (rand-int 5)])
111+
{d*/element-ns d*/ns-svg})))}))
112+
113+
114+
(def router
115+
(rr/router
116+
[["/" {:handler #'home}]
117+
["/rand" {:handler #'merge-svg}]
118+
["/rand-math" {:handler #'merge-math}]]))
119+
120+
121+
(def handler
122+
(rr/ring-handler router
123+
(rr/create-default-handler)
124+
{:middleware [params/parameters-middleware]}))
125+
126+
(comment
127+
(u/reboot-hk-server! #'handler)
128+
(user/clear-terminal!))

src/dev/examples/utils.clj

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns examples.utils
22
(:require
33
[charred.api :as charred]
4+
[clojure.java.io :as io]
45
[fireworks.core :refer [?]]
56
[puget.printer :as pp]
67
[starfederation.datastar.clojure.api :as d*]))
@@ -9,17 +10,39 @@
910
;; -----------------------------------------------------------------------------
1011
;; Misc utils
1112
;; -----------------------------------------------------------------------------
12-
(defn clear-terminal! []
13-
(binding [*out* (java.io.PrintWriter. System/out)]
14-
(print "\033c")
15-
(flush)))
13+
(defn- safe-requiring-resolve [sym]
14+
(try
15+
(deref (requiring-resolve sym))
16+
(catch Exception _ nil)))
17+
18+
19+
(def original-out
20+
(or
21+
(:out (safe-requiring-resolve 'cider.nrepl.middleware.out/original-output))
22+
System/out))
23+
1624

25+
(def original-err
26+
(or
27+
(:err (safe-requiring-resolve 'cider.nrepl.middleware.out/original-output))
28+
System/err))
1729

18-
(defmacro force-out [& body]
19-
`(binding [*out* (java.io.OutputStreamWriter. System/out)]
30+
31+
(defmacro force-out
32+
"Binds [[*out*]] to the original system's out."
33+
[& body]
34+
`(binding [*out* (io/writer original-out)]
2035
~@body))
2136

2237

38+
(defn clear-terminal!
39+
"Clear the terminal of all text"
40+
[]
41+
(force-out
42+
(print "\033c")
43+
(flush)))
44+
45+
2346
(defn pp-request [req]
2447
(-> req
2548
(dissoc :reitit.core/match :reitit.core/router)

0 commit comments

Comments
 (0)