-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
reagent.cljs
78 lines (66 loc) · 2.13 KB
/
reagent.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
(ns emmy.portal.reagent
"Portal viewer for rendering Reagent snippets.
Install the viewer like this:
```clojure
(emmy.portal/install! \"emmy/portal/reagent.cljs\")
```
The viewer is automatically installed by the functions in [[emmy.portal]]."
(:require [clojure.walk :refer [postwalk]]
[portal.ui.api :as p]
[portal.ui.rpc :as rpc]
["react" :as react]))
(def viewer-name :emmy.portal/reagent)
(def xform-key
"Currently transforms are stored under this key for compatibility with the
Emmy-Viewers Clerk code."
:nextjournal.clerk/viewer)
(defn ^:no-doc strip-meta
"Given an unevaluated Reagent body, returns an identical form with all metadata
stripped off."
[form]
(postwalk
(fn [x]
(if (meta x)
(vary-meta x dissoc xform-key)
x))
form))
(defn ->f
"Given a quoted Reagent body (which might use functions like
`reagent.core/with-let` or third-party components, not just Hiccup syntax),
returns a no-argument-function component that renders an `eval`-ed version of
`body`."
[body]
(eval
(list 'fn [] (strip-meta body))))
(defn expand
"Renders the supplied Reagent fragment by expanding it
with [[emmy.viewer/expand]] and then evaluating it via [[->f]]."
[v]
(let [[form set-form!] (react/useState nil)]
(react/useEffect
(fn []
(-> (rpc/call 'emmy.viewer/expand v)
(.then (comp set-form! ->f)))
js/undefined)
#js [])
form))
(defn show-reagent
"Renders `v`, an unevaluated Reagent fragment, by either using [[->f]] directly
or first expanding it via [[expand]].
The latter case comes up with a fragment that needs some wrapping context,
like `mafs.plot/OfX`. This component can't render alone but needs a wrapping
`mafs.core/Mafs`, provided by `expand`."
[v]
(let [xform (xform-key (meta v))]
(if (or (not xform) (map? xform))
[(->f v)]
[:f> expand v])))
(defn reagent? [v]
(when-let [m (meta v)]
(or (:portal.viewer/reagent? m)
(= viewer-name
(:portal.viewer/default m)))))
(p/register-viewer!
{:name viewer-name
:component show-reagent
:predicate reagent?})