Skip to content

Commit 655549f

Browse files
Re-use tap viewers and state for taps window, add window ns
1 parent 75e5743 commit 655549f

File tree

5 files changed

+53
-38
lines changed

5 files changed

+53
-38
lines changed

notebooks/tap_window.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
(:require [nextjournal.clerk :as clerk]
66
[nextjournal.clerk.viewer :as v]))
77

8-
(clerk/window! ::clerk/taps)
8+
#_(clerk/window! ::clerk/taps)
9+
(clerk/window! :my-window (clerk/html [:div.w-8.h-8.bg-green-500]))
910

1011
(comment
1112
(tap> (clerk/html [:div.w-8.h-8.bg-green-500]))

src/nextjournal/clerk.clj

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,6 @@
8080

8181
#_(recompute!)
8282

83-
(defonce !taps (atom ()))
84-
85-
(declare col)
86-
;; TODO: drop `col` in favour of a suitable viewer
87-
(defn window!
88-
([id] (case id ::taps (window! id {:title "🚰 Taps"} (col @!taps))))
89-
([id content] (window! id {} content))
90-
([id opts content]
91-
(webserver/update-window! id (merge opts {:nextjournal/presented (update (v/present content) :nextjournal/css-class #(or % ["px-0"]))
92-
:nextjournal/hash (gensym)
93-
:nextjournal/fetch-opts {:blob-id (str id)}
94-
:nextjournal/blob-id (str id)}))))
95-
96-
(defn destroy-window! [id] (webserver/destroy-window! id))
97-
98-
(defn tapped [x] (swap! !taps conj x) (window! ::taps))
99-
(defonce taps-setup (add-tap tapped))
100-
101-
#_(doseq [f @@(resolve 'clojure.core/tapset)] (remove-tap f))
102-
#_(reset! !taps ())
103-
#_(tap> (range 30))
104-
#_(window! ::taps)
105-
#_(destroy-window! ::taps)
106-
#_(tap> (html [:h1 "Ahoi"]))
107-
#_(tap> (table [[1 2] [3 4]]))
108-
#_(window! ::my-window {:title "🔭 Rear Window"} (table [[1 2] [3 4]]))
109-
#_(window! ::my-window {:title "🔭 Rear Window"} (range 30))
110-
#_(window! ::my-window {:title "🔭 Rear Window"} (plotly {:data [{:y [1 2 3]}]}))
111-
#_(window! ::my-window-2 {:title "🪟"} (range 100))
112-
#_(destroy-window! ::my-window)
113-
11483
(defn ^:private supported-file?
11584
"Returns whether `path` points to a file that should be shown."
11685
[path]

src/nextjournal/clerk/render/window.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124

125125
(defn show
126126
([content] (show content {}))
127-
([content opts]
127+
([content {:as opts :keys [css-class]}]
128128
(let [!panel-ref (hooks/use-ref nil)
129129
!dragging? (hooks/use-state nil)
130130
!dockable-at (hooks/use-state nil)
@@ -188,4 +188,4 @@
188188
(reset! !dockable-at nil)
189189
(reset! !docking-ref nil))}
190190
opts)]
191-
[:div.p-3.flex-auto.overflow-auto content]]])))
191+
[:div {:class (str "flex-auto " (or css-class "p-3 overflow-auto"))} content]]])))

src/nextjournal/clerk/tap.clj

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
^{::clerk/sync true ::clerk/viewer switch-view ::clerk/visibility {:result :show}}
2929
(defonce !view (atom :stream))
3030

31-
3231
(defonce !taps (atom ()))
3332

3433
(defn reset-taps! []
@@ -45,9 +44,9 @@
4544
(def tap-viewer
4645
{:pred (v/get-safe ::val)
4746
:render-fn '(fn [{::keys [val tapped-at]} opts]
48-
[:div.border-t.relative.py-3.mt-2
49-
[:span.absolute.rounded-full.px-2.bg-gray-300.font-mono.top-0
50-
{:class "left-1/2 -translate-x-1/2 -translate-y-1/2 py-[1px] text-[9px]"} (:nextjournal/value tapped-at)]
47+
[:div.w-full
48+
[:div.font-sans.bg-slate-50.px-2
49+
{:class "py-[2px] text-[9px]"} (:nextjournal/value tapped-at)]
5150
[:div.overflow-x-auto [nextjournal.clerk.render/inspect-presented val]]])
5251
:transform-fn (fn [{:as wrapped-value :nextjournal/keys [value]}]
5352
(-> wrapped-value

src/nextjournal/clerk/window.clj

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
(ns nextjournal.clerk.window
2+
(:require [nextjournal.clerk.tap :as tap]
3+
[nextjournal.clerk.viewer :as v]
4+
[nextjournal.clerk.webserver :as webserver]))
5+
6+
(def taps-viewer
7+
{:render-fn '(fn [taps opts]
8+
(into [:div]
9+
(nextjournal.clerk.viewer/inspect-children opts)
10+
taps))})
11+
12+
(defn window!
13+
([id]
14+
(case id
15+
::taps (window! id {:title "🚰 Taps" :css-class "p-0"}
16+
(v/with-viewers (v/add-viewers [tap/tap-viewer])
17+
(v/with-viewer taps-viewer @tap/!taps)))))
18+
([id content] (window! id {} content))
19+
([id opts content]
20+
(webserver/update-window! id (merge opts {:nextjournal/presented (update (v/present content) :nextjournal/css-class #(or % ["px-0"]))
21+
:nextjournal/hash (gensym)
22+
:nextjournal/fetch-opts {:blob-id (str id)}
23+
:nextjournal/blob-id (str id)}))))
24+
25+
(defn destroy-window! [id] (webserver/destroy-window! id))
26+
27+
(doseq [w (keys @webserver/!windows)]
28+
(destroy-window! w))
29+
30+
#_(window! ::taps)
31+
32+
#_(defn tapped [x] (swap! !taps conj x) (window! ::taps))
33+
#_(defonce taps-setup (add-tap tapped))
34+
35+
#_(doseq [f @@(resolve 'clojure.core/tapset)] (remove-tap f))
36+
#_(reset! !taps ())
37+
#_(tap> (range 30))
38+
#_(window! ::taps)
39+
#_(destroy-window! ::taps)
40+
#_(tap> (v/html [:h1 "Ahoi"]))
41+
#_(tap> (v/table [[1 2] [3 4]]))
42+
#_(window! ::my-window {:title "🔭 Rear Window"} (table [[1 2] [3 4]]))
43+
#_(window! ::my-window {:title "🔭 Rear Window"} (range 30))
44+
#_(window! ::my-window {:title "🔭 Rear Window"} (plotly {:data [{:y [1 2 3]}]}))
45+
#_(window! ::my-window-2 {:title "🪟"} (range 100))
46+
#_(destroy-window! ::my-window)

0 commit comments

Comments
 (0)