From d8fef42c64dafbc835d7f645de3b1051df45ec9d Mon Sep 17 00:00:00 2001
From: Michiel Borkent SCI script tag
@@ -20,16 +35,16 @@
bound to happen. Feedback is welcome on Github.
You can get a copy - of sci_script_tag.js here. If - you want a pinned version of sci_script_tag.js, your best bet is to + of sci-script-tag.js here. If + you want a pinned version of sci-script-tag.js, your best bet is to download your own copy and host it yourself.
-
<head>
- <script src="https://borkdude.github.io/sci-script-tag/js/sci_script_tag.js" type="application/javascript"></script>
+ <script src="https://borkdude.github.io/sci-script-tag/js/sci-script-tag.js" type="application/javascript"></script>
<script type="application/x-sci">
(defn my-alert []
(js/alert "alert!"))
@@ -46,5 +61,30 @@
+
+ Reagent plugin
+
+ To enable reagent,
+ in addition to sci-script-tag.js, you need to include sci-script-tag-plugin-reagent.js.
+
+
+ <script src="https://borkdude.github.io/sci-script-tag/js/sci-script-tag-plugin-reagent.js" type="application/javascript"></script>
+ <script type="application/x-sci">
+ (require '[reagent.core :as r]
+ '[reagent.dom :as rdom])
+
+ (def state (r/atom {:clicks 0}))
+
+ (defn my-component []
+ [:div
+ [:p "Clicks: " (:clicks @state)]
+ [:p [:button {:on-click #(swap! state update :clicks inc)}
+ "Click me!"]]])
+
+ (rdom/render [my-component] (.getElementById js/document "app"))
+ </script>
+
+
+
diff --git a/script/release.clj b/script/release.clj
index 6010e64..6086fe9 100755
--- a/script/release.clj
+++ b/script/release.clj
@@ -1,18 +1,23 @@
#!/usr/bin/env bb
(require '[babashka.fs :as fs]
- '[babashka.tasks :refer [shell]]
- '[clojure.string :as str])
+ '[babashka.tasks :refer [shell]])
(fs/copy "resources/public/index.html" "gh-pages"
{:replace-existing true})
(shell "clojure -M:dev -m shadow.cljs.devtools.cli release main")
(def index-file (fs/file "gh-pages" "index.html"))
-(spit index-file (str/replace (slurp index-file) "main.js" "sci_script_tag.js"))
-(fs/create-dirs (fs/file "gh-pages" "js"))
-(fs/copy (fs/file "resources" "public" "js" "main.js")
- (fs/file "gh-pages" "js" "sci_script_tag.js")
- {:replace-existing true})
+
+(def js-source-dir (fs/file "resources" "public" "js"))
+(def js-target-dir (fs/file "gh-pages" "js"))
+(fs/create-dirs js-target-dir)
+
+(run! (fn [f]
+ (println "Copying" (str f))
+ (fs/copy f
+ js-target-dir
+ {:replace-existing true}))
+ (fs/glob js-source-dir "sci-script-tag*.js"))
(def with-gh-pages (partial shell {:dir "gh-pages"}))
(with-gh-pages "git add .")
diff --git a/shadow-cljs.edn b/shadow-cljs.edn
index 729240b..03ab8ba 100644
--- a/shadow-cljs.edn
+++ b/shadow-cljs.edn
@@ -7,6 +7,9 @@
:builds
{:main
{:target :browser
- :modules {:main {:entries [sci.script-tag]}}
+ :modules
+ {:sci-script-tag {:entries [sci.script-tag]}
+ :sci-script-tag-plugin-reagent {:entries [sci.script-tag.plugin-reagent]
+ :depends-on #{:sci-script-tag}}}
:output-dir "resources/public/js"
:devtools {:repl-pprint true}}}}
diff --git a/src/sci/script_tag.cljs b/src/sci/script_tag.cljs
index 55e63ba..c421aa9 100644
--- a/src/sci/script_tag.cljs
+++ b/src/sci/script_tag.cljs
@@ -9,35 +9,41 @@
(fn [s]
(str/upper-case (.charAt s 1)))))
-(defn defn-macro [_ _ fn-name & args]
+(defn- defn-macro [_ _ fn-name & args]
`(let [ns# (ns-name *ns*)]
(clojure.core/defn ~fn-name ~@args)
(sci.script-tag/-export ~fn-name (str ns# "." '~fn-name))))
-(def ctx (sci/init {:namespaces {'sci.script-tag
- {'defn (with-meta defn-macro
- {:sci/macro true})
- '-export (fn [f k]
- (let [parts (str/split k #"\.")]
- (loop [parts parts
- prev js/window]
- (let [fpart (first parts)
- fpart (kebab->camel fpart)]
- (if (= 1 (count parts))
- (gobject/set prev fpart f)
- (do (gobject/set prev fpart #js {})
- (recur (rest parts)
- (gobject/get prev fpart))))))
- (gobject/set js/window k f)))}
- 'clojure.core {'println println}}
- :classes {'js js/window
- :allow :all}}))
+(def ctx (atom (sci/init {:namespaces {'sci.script-tag
+ {'defn (with-meta defn-macro
+ {:sci/macro true})
+ '-export (fn [f k]
+ (let [parts (str/split k #"\.")]
+ (loop [parts parts
+ prev js/window]
+ (let [fpart (first parts)
+ fpart (kebab->camel fpart)]
+ (if (= 1 (count parts))
+ (gobject/set prev fpart f)
+ (if-let [obj (gobject/get prev fpart)]
+ (recur (rest parts) obj)
+ (let [obj #js {}]
+ (gobject/set prev fpart obj)
+ (recur (rest parts)
+ obj))))))
+ (gobject/set js/window k f)))}
+ 'clojure.core {'println println}}
+ :classes {'js js/window
+ :allow :all}})))
(defn eval-string [s]
- (sci/eval-string* ctx
+ (sci/eval-string* @ctx
(str "(require '[sci.script-tag :refer :all])"
s)))
+(defn merge-ctx [opts]
+ (swap! ctx sci/merge-opts opts))
+
(js/document.addEventListener
"DOMContentLoaded"
(fn []
diff --git a/src/sci/script_tag/plugin_reagent.cljs b/src/sci/script_tag/plugin_reagent.cljs
new file mode 100644
index 0000000..24eb83a
--- /dev/null
+++ b/src/sci/script_tag/plugin_reagent.cljs
@@ -0,0 +1,20 @@
+(ns sci.script-tag.plugin-reagent
+ (:require [reagent.core :as r]
+ [reagent.dom :as rdom]
+ [sci.core :as sci]
+ [sci.script-tag :as st]))
+
+(def rns (sci/create-ns 'reagent.core nil))
+
+(def reagent-namespace
+ {'atom (sci/copy-var r/atom rns)})
+
+(def rdns (sci/create-ns 'reagent.dom nil))
+
+(def reagent-dom-namespace
+ {'render (sci/copy-var rdom/render rdns)})
+
+(println :merging)
+(st/merge-ctx {:namespaces {'reagent.core reagent-namespace
+ 'reagent.dom reagent-dom-namespace}})
+(println :done-merging)