/
frisk.cljs
436 lines (395 loc) · 17.3 KB
/
frisk.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
(ns pinkgorilla.ui.frisk
(:require
[cljs.pprint :refer [pprint]]
[reagent.core :as r]
[pinkgorilla.ui.pinkie :refer-macros [register-component]]))
;; stolen from: https://github.com/Odinodin/data-frisk-reagent
(defn map-vals [f m]
(zipmap (keys m)
(map f (vals m))))
(declare DataFrisk)
(def styles
{:shell {:backgroundColor "#FAFAFA"
:fontFamily "Consolas,Monaco,Courier New,monospace"
:fontSize "12px"
:z-index 9999}
:strings {:color "#4Ebb4E"}
:keywords {:color "purple"}
:numbers {:color "blue"}
:nil {:color "red"}
:shell-visible-button {:backgroundColor "#4EE24E"}})
(defn ErrorIcon []
[:svg {:viewBox "0 0 30 42" :width "100%" :height "100%"}
[:path {:fill "darkorange"
:stroke "red"
:stroke-width "2"
:d "M15 3
Q16.5 6.8 25 18
A12.8 12.8 0 1 1 5 18
Q13.5 6.8 15 3z"}]
[:circle {:cx 15 :cy 32 :r 7 :fill "yellow"}]])
(defn ErrorText [text]
[:div {:style {:fontSize "0.7em"
:display "flex"
:align-items "center"
:color "red"}} text])
(defn ExpandButton [{:keys [expanded? path emit-fn]}]
[:button {:style {:border 0
:padding "5px 4px 5px 2px"
:textAlign "center"
:backgroundColor "transparent"
:width "20px"
:height "20px"
:cursor "pointer"}
:onClick #(emit-fn (if expanded? :contract :expand) path)}
[:svg {:viewBox "0 0 100 100"
:width "100%" :height "100%"
:style {:transition "all 0.2s ease"
:transform (when expanded? "rotate(90deg)")}}
[:polygon {:points "0,0 0,100 100,50" :stroke "black"}]]])
(def button-style {:padding "1px 3px"
:cursor "pointer"
:background-color "white"})
(defn ExpandAllButton [emit-fn data]
[:button {:onClick #(emit-fn :expand-all data)
:style (merge button-style
{:borderTopLeftRadius "2px"
:borderBottomLeftRadius "2px"
:border "1px solid darkgray"})}
"Expand"])
(defn CollapseAllButton [emit-fn data]
[:button {:onClick #(emit-fn :collapse-all)
:style
(merge button-style
{:borderTop "1px solid darkgray"
:borderBottom "1px solid darkgray"
:borderRight "1px solid darkgray"
:borderLeft "0"})}
"Collapse"])
(defn CopyButton [emit-fn data]
[:button {:onClick #(emit-fn :copy data)
:style (merge button-style
{:borderTopRightRadius "2px"
:borderBottomRightRadius "2px"
:borderTop "1px solid darkgray"
:borderBottom "1px solid darkgray"
:borderRight "1px solid darkgray"
:borderLeft "0"})}
"Copy"])
(defn NilText []
[:span {:style (:nil styles)} (pr-str nil)])
(defn StringText [data]
[:span {:style (:strings styles)} (pr-str data)])
(defn KeywordText [data]
[:span {:style (:keywords styles)} (str data)])
(defn NumberText [data]
[:span {:style (:numbers styles)} data])
(defn KeySet [keyset]
[:span
(->> keyset
(sort-by str)
(map-indexed
(fn [i data] ^{:key i} [:span
(cond (nil? data) [NilText]
(string? data) [StringText data]
(keyword? data) [KeywordText data]
(number? data) [NumberText data]
:else (str data))]))
(interpose " "))])
(defn Node [{:keys [data path emit-fn swappable metadata-paths]}]
[:div {:style {:display "flex"}} (cond
(nil? data)
[NilText]
(string? data)
(if swappable
[:input {:type "text"
:default-value (str data)
:on-change
(fn string-changed [e]
(emit-fn :changed path (.. e -target -value)))}]
[StringText data])
(keyword? data)
(if swappable
[:input {:type "text"
:default-value (name data)
:on-change
(fn keyword-changed [e]
(emit-fn :changed path (keyword (.. e -target -value))))}]
[KeywordText data])
(object? data)
(str data " " (.stringify js/JSON data))
(number? data)
(if swappable
[:input {:type "number"
:default-value data
:on-change
(fn number-changed [e]
(emit-fn :changed path (js/Number (.. e -target -value))))}]
[NumberText data])
:else
(str data))
(when-let [errors (:error (get metadata-paths path))]
[ErrorText (str "\u00A0 " errors)])])
(defn expandable? [v]
(or (map? v) (seq? v) (coll? v)))
(defn CollectionSummary [{:keys [data]}]
(cond (map? data) [:div {:style {:flex "0 1 auto"}}
[:span "{"]
[KeySet (keys data)]
[:span "}"]]
(set? data) [:div {:style {:flex "0 1 auto"}} [:span "#{"]
(str (count data) " items")
[:span "}"]]
(or (seq? data)
(vector? data)) [:div {:style {:flex 1}}
[:span (if (vector? data) "[" "(")]
(str (count data) " items")
[:span (if (vector? data) "]" ")")]]))
(defn KeyValNode [{[k v] :data :keys [path metadata-paths emit-fn swappable]}]
(let [path-to-here (conj path k)
expandable-node? (and (expandable? v)
(not (empty? v)))
metadata (get metadata-paths path-to-here)
expanded? (:expanded? metadata)]
[:div {:style {:display "flex"
:flex-flow "column"}}
[:div {:style {:display "flex"}}
[:div {:style {:flex "0 0 20px"}}
(when expandable-node?
[ExpandButton {:expanded? expanded?
:path path-to-here
:emit-fn emit-fn}])]
[:div {:style {:flex "0 1 auto"}}
[:div {:style {:display "flex"
:flex-flow "row"}}
[:div {:style {:flex "0 1 auto"}}
[Node {:data k}]]
[:div {:style {:flex "0 1 auto" :paddingLeft "4px"}}
(if (expandable? v)
[CollectionSummary {:data v}]
[Node {:data v
:swappable swappable
:path path-to-here
:metadata-paths metadata-paths
:emit-fn emit-fn}])]]]]
(when expanded?
[:div {:style {:flex "1"}}
[DataFrisk {:hide-header? true
:data v
:swappable swappable
:path path-to-here
:metadata-paths metadata-paths
:emit-fn emit-fn}]])]))
(defn ListVecNode [{:keys [data path metadata-paths emit-fn swappable hide-header?]}]
(let [metadata (get metadata-paths path)
expanded? (:expanded? metadata)]
[:div {:style {:display "flex"
:flex-flow "column"}}
(when-not hide-header?
[:div {:style {:display "flex"}}
(when (:error metadata)
[:div {:style {:margin-left "-1em"
:width "1em"
:height "1.2em"}}
[ErrorIcon]])
[ExpandButton {:expanded? expanded?
:path path
:emit-fn emit-fn}]
[:div {:style {:flex "0 1 auto"}}
[:span (if (vector? data) "[" "(")]
(str (count data) " items")
[:span (if (vector? data) "]" ")")]]])
(when expanded?
[:div {:style {:flex "0 1 auto" :padding "0 0 0 20px"}}
(when (:error metadata)
[:div {:style {:paddingBottom "4px"}}
[ErrorText (:error metadata)]])
(map-indexed (fn [i x] ^{:key i} [DataFrisk {:data x
:swappable swappable
:path (conj path i)
:metadata-paths metadata-paths
:emit-fn emit-fn}]) data)])]))
(defn SetNode [{:keys [data path metadata-paths emit-fn swappable hide-header?]}]
(let [metadata (get metadata-paths path)
expanded? (:expanded? metadata)]
[:div {:style {:display "flex"
:flex-flow "column"}}
(when-not hide-header?
[:div {:style {:display "flex"}}
(when (:error metadata)
[:div {:style {:margin-left "-1em"
:width "1em"
:height "1.2em"}}
[ErrorIcon]])
[ExpandButton {:expanded? expanded?
:path path
:emit-fn emit-fn}]
[:div {:style {:flex "0 1 auto"}}
[:span "#{"]
(str (count data) " items")
[:span "}"]]])
(when expanded?
[:div {:style {:flex "0 1 auto" :paddingLeft "20px"}}
(when (:error metadata)
[:div {:style {:paddingBottom "4px"}}
[ErrorText (:error metadata)]])
(map-indexed (fn [i x] ^{:key i} [DataFrisk {:data x
:swappable swappable
:path (conj path i)
:metadata-paths metadata-paths
:emit-fn emit-fn}]) data)])]))
(defn MapNode [{:keys [data path metadata-paths emit-fn hide-header?] :as all}]
(let [metadata (get metadata-paths path)
expanded? (:expanded? metadata)]
[:div {:style {:display "flex"
:flex-flow "column"}}
(when-not hide-header?
[:div {:style {:display "flex"}}
(when (:error metadata)
[:div {:style {:margin-left "-1em"
:width "1em"
:height "1.2em"}}
[ErrorIcon]])
[ExpandButton {:expanded? expanded?
:path path
:emit-fn emit-fn}]
[:div {:style {:flex "0 1 auto"}}
[:span (str "{")]
[KeySet (keys data)]
[:span "}"]]])
(when expanded?
[:div {:style {:flex "0 1 auto" :paddingLeft "20px"}}
(when (:error metadata)
[:div {:style {:paddingBottom "4px"}}
[ErrorText (:error metadata)]])
(->> data
(sort-by (fn [[k _]] (str k)))
(map-indexed (fn [i x] ^{:key i} [KeyValNode (assoc all :data x)])))])]))
(defn DataFrisk [{:keys [data] :as all}]
(cond (map? data) [MapNode all]
(set? data) [SetNode all]
(or (seq? data) (vector? data)) [ListVecNode all]
(satisfies? IDeref data) [DataFrisk (assoc all :data @data)]
:else [:div {:style {:paddingLeft "20px"}} [Node all]]))
(defn conj-to-set [coll x]
(conj (or coll #{}) x))
(defn expand-all-paths [root-value current-expanded-paths]
(loop [remaining [{:path [] :node root-value}]
expanded-paths current-expanded-paths]
(if (seq remaining)
(let [[current & rest] remaining
current-node (if (satisfies? IDeref (:node current)) @(:node current) (:node current))]
(cond (map? current-node)
(recur
(concat rest (map (fn [[k v]] {:path (conj (:path current) k)
:node v})
current-node))
(assoc-in expanded-paths [(:path current) :expanded?] true))
(or (seq? current-node)
(vector? current-node)
(set? current-node))
(recur
(concat rest (map-indexed (fn [i node] {:path (conj (:path current) i)
:node node})
current-node))
(assoc-in expanded-paths [(:path current) :expanded?] true))
:else
(recur
rest
(if (coll? current-node)
(assoc-in expanded-paths [(:path current) :expanded?] true)
expanded-paths))))
expanded-paths)))
(defn copy-to-clipboard [data]
(let [pretty (with-out-str (pprint data))
textArea (.createElement js/document "textarea")]
(doto textArea
;; Put in top left corner of screen
(aset "style" "position" "fixed")
(aset "style" "top" 0)
(aset "style" "left" 0)
;; Make it small
(aset "style" "width" "2em")
(aset "style" "height" "2em")
(aset "style" "padding" 0)
(aset "style" "border" "none")
(aset "style" "outline" "none")
(aset "style" "boxShadow" "none")
;; Avoid flash of white box
(aset "style" "background" "transparent")
(aset "value" pretty))
(.appendChild (.-body js/document) textArea)
(.select textArea)
(.execCommand js/document "copy")
(.removeChild (.-body js/document) textArea)))
(defn collapse-all [metadata-paths]
(map-vals #(assoc % :expanded? false) metadata-paths))
(defn emit-fn-factory [state-atom id swappable]
(fn [event & args]
(case event
:expand (swap! state-atom assoc-in [:data-frisk id :metadata-paths (first args) :expanded?] true)
:expand-all (swap! state-atom update-in [:data-frisk id :metadata-paths] (partial expand-all-paths (first args)))
:contract (swap! state-atom assoc-in [:data-frisk id :metadata-paths (first args) :expanded?] false)
:collapse-all (swap! state-atom update-in [:data-frisk id :metadata-paths] collapse-all)
:copy (copy-to-clipboard (first args))
:changed (let [[path value] args]
(if (seq path)
(swap! swappable assoc-in path value)
(reset! swappable value))))))
(defn Root [data id state-atom]
(let [data-frisk (:data-frisk @state-atom)
swappable (when (satisfies? IAtom data)
data)
emit-fn (emit-fn-factory state-atom id swappable)
metadata-paths (get-in data-frisk [id :metadata-paths])]
[:div
[:div {:style {:padding "4px 2px"}}
[ExpandAllButton emit-fn data]
[CollapseAllButton emit-fn]
[CopyButton emit-fn data]]
[:div {:style {:flex "0 1 auto"}}
[DataFrisk {:data data
:swappable swappable
:path []
:metadata-paths metadata-paths
:emit-fn emit-fn}]]]))
(defn VisibilityButton
[visible? update-fn]
[:button {:style {:border 0
:backgroundColor "transparent" :width "20px" :height "20px"}
:onClick update-fn}
[:svg {:viewBox "0 0 100 100"
:width "100%" :height "100%"
:style {:transition "all 0.2s ease"
:transform (when visible? "rotate(90deg)")}}
[:polygon {:points "0,0 0,100 100,50" :stroke "black"}]]])
(defn ^{:category :pinkie}
DataFriskView [data]
(let [expand-by-default (reduce #(assoc-in %1 [:data-frisk %2 :metadata-paths [] :expanded?] true) {} (range (count data)))
state-atom (r/atom expand-by-default)]
(fn [& data]
(let [data-frisk (:data-frisk @state-atom)
visible? (:visible? data-frisk)]
[:div {:style (merge {:flex-flow "row nowrap"
:transition "all 0.3s ease-out"
:z-index "5"}
(when-not visible?
{:overflow-x "hide"
:overflow-y "hide"
:max-height "30px"
:max-width "100px"})
(:shell styles))}
[VisibilityButton visible? (fn [_] (swap! state-atom assoc-in [:data-frisk :visible?] (not visible?)))]
[:span "Data frisk"]
(when visible?
[:div {:style {:padding "10px"
;; TODO Make the max height and width adjustable
;:max-height "400px"
;:max-width "800px"
:resize "both"
:box-sizing "border-box"
:overflow-x "auto"
:overflow-y "auto"}}
(map-indexed (fn [id x]
^{:key id} [Root x id state-atom]) data)])]))))
(register-component :p/frisk DataFriskView)