@kairotic/data 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -2
- package/dist/edn.mjs +9 -0
- package/dist/fetch.mjs +9 -0
- package/dist/index.mjs +22 -0
- package/package.json +37 -4
- package/src/edn.cljs +176 -0
- package/src/fetch.cljs +123 -0
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kairotic/data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
*Official package of [Kairotic](https://github.com/bonuoq/kairotic) · [all packages](../README.md)*
|
|
4
|
+
|
|
5
|
+
Data from outside the work, and a way to read any value. `fetch` brings in what is at a URL without
|
|
6
|
+
leaving the pure logic of a box (a **task**: the engine makes the request and gives the outcome back to
|
|
7
|
+
the box); `edn` shows any value as a tree, after [Portal](https://github.com/djblue/portal).
|
|
8
|
+
|
|
9
|
+
| Class | What it does |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `fetch [json\|text\|edn] [live]` | a URL (a msg with it, a string, `[:fetch url]`, or `github:owner/repo@ref/path` through jsDelivr) brings in what is there: JSON read as EDN (keys that can be keywords become keywords), text or EDN. It goes out on the left and is **kept as the data of the box**: one gesture of the history, the same for everyone in a room, and going back in the history does not fetch again (a URL it already keeps is sent again, not fetched; `refresh` fetches it again). With `live` it is only held in this tab, for answers that change. `[:get k …]` sends a part of it; on the right, `:loading`, `:ready` or `[:error "…"]` |
|
|
12
|
+
| `edn [width height]` | any value as a tree: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part (left) and its path (right) — the path `fetch` takes as `[:get …]`. Dragging scrolls; `depth n` opens it n levels down |
|
|
13
|
+
|
|
14
|
+
```clojure
|
|
15
|
+
(box :b1 "msg github:bonuoq/kairotic@main/packages/data/package.json" 40 30)
|
|
16
|
+
(box :b2 "fetch" 40 80)
|
|
17
|
+
(box :b3 "edn 340 260" 40 150)
|
|
18
|
+
(box :b4 "print" 400 150)
|
|
19
|
+
(connect :b1 0 :b2 0) (connect :b2 0 :b3 0) (connect :b3 0 :b4 0)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Most sites refuse to be read by a page of another site (CORS): public APIs that allow it, files of a
|
|
23
|
+
public GitHub repository (`github:…`) and anything served by jsDelivr work. Inside claude.ai a page can
|
|
24
|
+
only reach jsDelivr, so there `github:…` is the way. What is kept travels in the history and in links:
|
|
25
|
+
at most 64 KB of it; beyond that it is still sent, and held only in that tab.
|
|
26
|
+
|
|
27
|
+
To write a component of your own that asks the world for something, see *From outside: tasks* in
|
|
28
|
+
[Writing a component](../../docs/writing-a-component.md).
|
|
29
|
+
|
|
30
|
+
Writing one of its classes in a work brings the package into it. Licence: [MPL-2.0](../../LICENSE).
|
package/dist/edn.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/edn.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "edn",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). depth n opens it n levels down; bang sends the picked part again (the whole value if none); clear empties it.",
|
|
8
|
+
"source": ";; edn · any value as a tree to open, read and pick from\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; After Portal (djblue/portal): what arrives is shown as a tree whose collections open and close, each\n;; value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part\n;; of the value (left) and its path (right), the path a fetch box takes as [:get …]. Dragging scrolls.\n;; What happens in the view (open, pick, scroll) is a message to the box itself, like any gesture, so\n;; the same value sent again changes nothing.\n(let [row-h 14 top-y 22 foot-h 16 cw 5.6 ; a line, where lines start, the footer, a character (size 9)\n max-children 200 max-rows 3000\n coll-kind (fn [v] (cond (map? v) :map (vector? v) :vector (set? v) :set (sequential? v) :list :else nil))\n opener {:map \"{\" :vector \"[\" :set \"#{\" :list \"(\"}\n plural (fn [n one many] (str n \" \" (if (= 1 n) one many)))\n size-text (fn [v] (case (coll-kind v)\n :map (plural (count v) \"key\" \"keys\")\n (:vector :list) (plural (count v) \"item\" \"items\")\n :set (plural (count v) \"element\" \"elements\")\n nil))\n ;; the colour of a value, by its kind (the palette of the patch, light or dark)\n ink-of (fn [v] (cond (string? v) :ink (number? v) :kairos (keyword? v) :accent\n (or (nil? v) (boolean? v)) :soft (coll-kind v) :soft :else :ink))\n ;; one level down: the parts of a collection, in a steady order, at most max-children of them\n entries (fn [c]\n (let [es (case (coll-kind c)\n :map (for [[k v] (sort-by (fn [[k _]] (pr-str k)) c)] {:k k :label (pr-str k) :key? true :v v})\n :set (for [v (sort-by pr-str c)] {:k v :label \"\" :v v})\n (map-indexed (fn [i v] {:k i :label (str i) :v v}) c))\n shown (vec (take max-children es))]\n (if (> (count c) max-children)\n (conj shown {:more (- (count c) max-children)})\n shown)))\n ;; a part of the value by its path: get-in, also through lists and sets\n part (fn [root path]\n (reduce (fn [c k] (case (coll-kind c)\n (:map :set) (get c k)\n (:vector :list) (if (number? k) (nth (seq c) k nil) nil)\n nil))\n root path))\n ;; the lines of the tree as it is open: [{:path :depth :label :key? :v :open? :more}]\n rows-of (fn [{:keys [root open]}]\n (if-not (coll-kind root)\n []\n (loop [todo (into () (reverse (map (fn [e] (assoc e :path [(:k e)] :depth 0)) (entries root))))\n out (transient [])]\n (if (or (empty? todo) (>= (count out) max-rows))\n (persistent! out)\n (let [r (first todo) more (rest todo)\n open? (and (coll-kind (:v r)) (contains? open (:path r)) (not (:more r)))\n kids (when open? (map (fn [e] (assoc e :path (conj (:path r) (:k e)) :depth (inc (:depth r))))\n (entries (:v r))))]\n (recur (if open? (into more (reverse kids)) more)\n (conj! out (assoc r :open? open?))))))))\n visible (fn [h] (max 1 (js/Math.floor (/ (- h top-y foot-h) row-h))))\n size-of (fn [args] (api/padded (filter number? args) [320 300]))\n ;; every collection down to depth n, open (at most max-rows of them)\n open-to (fn [root n]\n (loop [todo (list [[] root 0]) acc #{}]\n (if (or (empty? todo) (> (count acc) max-rows))\n acc\n (let [[p v d] (first todo)\n kids (when (and (coll-kind v) (< d n))\n (for [e (entries v) :when (and (not (:more e)) (coll-kind (:v e)))] [(conj p (:k e)) (:v e) (inc d)]))]\n (recur (into (rest todo) kids) (if (and (seq p) (coll-kind v) (<= d n)) (conj acc p) acc))))))\n shown-of (fn [st] {:top (:top st 0) :sel (:sel st) :open (count (:open st))})\n with-top (fn [st top h] (let [n (count (rows-of st))] (assoc st :top (max 0 (min (js/Math.round top) (- n (visible h)))))))]\n {;; ── about ──\n :class \"edn\"\n :license \"MPL-2.0\"\n :doc \"Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). depth n opens it n levels down; bang sends the picked part again (the whole value if none); clear empties it.\"\n :usage \"edn [width height]\"\n :io [1 2]\n :size (fn [box] (size-of (:args box)))\n\n ;; ── logic ──\n :receive (fn [{:keys [value state args]}]\n (let [st (or state {:open #{} :top 0})\n [_ h] (size-of args)\n msg? (fn [k] (and (vector? value) (= k (first value))))\n view (fn [st2] {:state st2 :show (shown-of st2)})]\n (cond\n ;; what happens in the view\n (msg? :edn-open)\n (let [[_ p on] value]\n (view (with-top (update st :open (if on conj disj) p) (:top st 0) h)))\n (msg? :edn-top) (view (with-top st (second value) h))\n (msg? :edn-pick)\n (let [p (second value)]\n (if (or (= p (:sel st)) (not (contains? st :root)))\n {:state st}\n {:state (assoc st :sel p) :show (shown-of (assoc st :sel p))\n :out [[1 p] [0 (part (:root st) p)]]}))\n ;; messages\n (and (msg? :depth) (number? (second value)))\n (view (with-top (assoc st :open (open-to (:root st) (max 0 (dec (second value))))) 0 h))\n (#{:bang \"bang\"} value)\n (if (contains? st :root)\n (let [p (or (:sel st) [])] {:out [[1 p] [0 (part (:root st) p)]]})\n {})\n (#{:clear \"clear\"} value) (view {:open #{} :top 0})\n ;; a value: the same again changes nothing; another keeps what was open and picked\n (and (contains? st :root) (= value (:root st))) {:state st}\n :else\n (let [st2 (assoc st :root value)]\n (view (with-top st2 (:top st 0) h))))))\n\n ;; a line pressed (on its arrow: open or close it; elsewhere: pick it), or a drag that scrolls\n :gesture (fn [{:keys [up hit dy start state h]}]\n (let [st (or state {}) rows (rows-of st) n (count rows) vis (visible h)\n t0 (:top start (:top st 0))]\n (cond\n (>= (js/Math.abs dy) 3)\n [:edn-top (if (= hit :bar)\n (+ t0 (* dy (/ n (* vis row-h))))\n (- t0 (/ dy row-h)))]\n (not up) nil\n ;; a line is its index; its arrow, -1 - its index\n (and (number? hit) (neg? hit))\n (when-let [r (get rows (- -1 hit))] (when (coll-kind (:v r)) [:edn-open (:path r) (not (:open? r))]))\n (number? hit) (when-let [r (get rows hit)] (when-not (:more r) [:edn-pick (:path r)]))\n :else nil)))\n\n ;; ── view ──\n :draw (fn [{:keys [state w h]}]\n (let [st (or state {}) root (:root st) rows (rows-of st) n (count rows)\n vis (visible h) top (max 0 (min (:top st 0) (- n vis))) bar? (> n vis)\n right (- w (if bar? 14 6))\n chars (fn [x] (max 0 (js/Math.floor (/ (- right x) cw))))\n cut (fn [s x] (let [m (chars x)] (if (> (count s) m) (str (subs s 0 (max 0 (dec m))) \"…\") s)))\n line (fn [j r i]\n (let [y (+ top-y (* j row-h)) base (+ y 10)\n x (+ 8 (* 12 (:depth r))) v (:v r) kind (coll-kind v)\n lbl (:label r) vx (+ x 12 (if (seq lbl) (* cw (inc (count lbl))) 0))\n sel? (= (:sel st) (:path r))]\n (if (:more r)\n [[:text {:x (+ x 12) :y base :size 9 :fill :soft} (str \"… \" (:more r) \" more\")]]\n (concat\n [[:rect {:x 2 :y y :w (- right 2) :h row-h :fill (if sel? :accent :bg) :opacity (if sel? 0.14 0) :hit i}]]\n (when kind\n [[:rect {:x (- x 2) :y y :w 12 :h row-h :fill :bg :opacity 0 :hit (- -1 i)}]\n [:text {:x x :y base :size 9 :fill :soft} (if (:open? r) \"▾\" \"▸\")]])\n (when (seq lbl)\n [[:text {:x (+ x 12) :y base :size 9 :fill (if (:key? r) :accent :soft)} (cut lbl (+ x 12))]])\n [[:text {:x vx :y base :size 9 :fill (ink-of v)}\n (cut (cond (and kind (:open? r)) (str (opener kind) \" \" (size-text v))\n kind (pr-str v)\n :else (pr-str v))\n vx)]]))))]\n (concat\n [[:rect {:x 1 :y 1 :w (- w 2) :h (- h 2) :r 4 :stroke :grid :fill :bg}]\n [:text {:x 8 :y 15 :size 9 :fill :soft}\n (cond (not (contains? st :root)) \"edn · send it a value\"\n (coll-kind root) (str (name (coll-kind root)) \" · \" (size-text root))\n :else (str (cond (string? root) \"string\" (number? root) \"number\" (keyword? root) \"keyword\"\n (nil? root) \"nil\" (boolean? root) \"boolean\" :else \"value\")))]\n [:line {:x1 1 :y1 19 :x2 (- w 1) :y2 19 :stroke :grid}]]\n ;; a value that is not a collection: itself, on one line\n (when (and (contains? st :root) (not (coll-kind root)))\n [[:text {:x 8 :y (+ top-y 10) :size 9 :fill (ink-of root)} (cut (pr-str root) 8)]])\n (mapcat (fn [j] (line j (nth rows (+ top j)) (+ top j))) (range (min vis (- n top))))\n (when bar?\n (let [track (* vis row-h) th (max 14 (* track (/ vis n))) ty (+ top-y (* (- track th) (/ top (max 1 (- n vis)))))]\n [[:rect {:x (- w 10) :y top-y :w 6 :h track :r 3 :fill :grid :hit :bar}]\n [:rect {:x (- w 10) :y ty :w 6 :h th :r 3 :fill :soft :hit :bar}]]))\n [[:line {:x1 1 :y1 (- h foot-h) :x2 (- w 1) :y2 (- h foot-h) :stroke :grid}]\n [:text {:x 8 :y (- h 5) :size 8 :fill :soft}\n (cut (cond (:sel st) (str \"→ \" (pr-str (:sel st)))\n (seq rows) (str n (if (= 1 n) \" line\" \" lines\") (when bar? (str \" · \" (inc top) \"–\" (min n (+ top vis)))) \" · pick a line\")\n :else \"\")\n 8)]])))\n\n ;; ── help ──\n :help {:inlets [\"any value: a map, a vector, a set, a list, or a single value; depth n · bang · clear\"]\n :outlets [\"the part picked\" \"its path, for [:get …] of a fetch box\"]\n :messages [[\"depth 2\" \"opens it two levels down\"]\n [\"bang\" \"sends the part picked again (the whole value if none)\"]\n [\"clear\" \"empties it\"]]\n :try \"Send it a fetch's answer and open the branches: pick a line and its path comes out on the right.\"}})\n"
|
|
9
|
+
};
|
package/dist/fetch.mjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Generated by packages/build.mjs from src/fetch.cljs
|
|
2
|
+
export default {
|
|
3
|
+
"class": "fetch",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"doc": "Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \"…\"].",
|
|
8
|
+
"source": ";; fetch · what is at a URL (JSON, text or EDN), brought in as a task\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; Its logic never touches the network: a URL asks for the task :get, the engine runs it (api/http-get)\n;; and what it finds comes back to this box on inlet :task. By default the box keeps it as its data\n;; ([:keep …]): one gesture of the history, the same for everyone in a room, and going back in the\n;; history does not fetch it again. With `live` it is only held in this tab, for answers that change.\n(let [formats #{:json :text :edn}\n fmt-of (fn [args] (if (formats (:as args)) (:as args) :json))\n ;; what the box holds: kept in the work, or (live) only here\n sane (fn [d] (if (and (map? d) (string? (:url d))) (select-keys d [:url :value :date]) {}))\n ;; kept in the work (its data) or, live or before it could be kept (inside a sub-patch, too long, played\n ;; by someone else in a jam), only here (its state)\n held-of (fn [args data state] (if (and (not (:live args)) (contains? data :value)) data (:got state)))\n kept? (fn [args data] (and (not (:live args)) (contains? data :value)))\n summary (fn [v] (cond (map? v) (str \"{} \" (count v) (if (= 1 (count v)) \" key\" \" keys\"))\n (sequential? v) (str \"[] \" (count v) (if (= 1 (count v)) \" item\" \" items\"))\n (string? v) (str \"\\\"\\\" \" (count v) \" characters\")\n (nil? v) \"nil\"\n :else (pr-str v)))\n url-in (fn [value]\n (cond (api/url-of value) (api/url-of value)\n (and (sequential? value) (#{:fetch :url \"fetch\" \"url\"} (first value))) (api/url-of (second value))\n :else nil))\n path-in (fn [value] (let [p (rest value)] (if (and (= 1 (count p)) (sequential? (first p))) (vec (first p)) (vec p))))]\n {;; ── about ──\n :class \"fetch\"\n :license \"MPL-2.0\"\n :doc \"Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \\\"…\\\"].\"\n :usage \"fetch [json|text|edn] [live]\"\n :io [1 2]\n :size [230 42]\n\n ;; ── arguments ──\n :args [[:as nil] [:live false]]\n\n ;; ── data: what it keeps, in the history ──\n :model {:empty {}\n :step (fn [d [op x]] (case op :set (sane x) :clear {} d))\n :load sane\n :code (fn [id d] (when (contains? d :value) [(str \"(keep! \" id \" \" (pr-str d) \")\")]))}\n\n ;; ── tasks: outside its pure logic ──\n :tasks {:get (fn [{:keys [url as]} _]\n (.then (api/http-get url {:as as})\n (fn [v] {:body v :date (.toISOString (js/Date.))})))}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state data args]}]\n (let [st (or state {})\n held (held-of args data st)\n ask (fn [u] (let [req {:url u :as (fmt-of args) :n (inc (:n st 0))}]\n {:state (assoc st :waiting req :n (:n req))\n :show {:status :loading :url u}\n :out [[1 :loading]]\n :effects [[:get req]]}))]\n (cond\n ;; what the task found\n (= inlet :task)\n (cond\n (not= (:arg value) (:waiting st)) {} ; the answer to an older request: dropped\n (:error value)\n {:state (dissoc st :waiting)\n :show {:status :error :url (:url (:arg value)) :error (:error value)}\n :out [[1 [:error (:error value)]]]}\n :else\n (let [got {:url (:url (:arg value)) :value (:body (:value value)) :date (:date (:value value))}\n same? (and (kept? args data) (= (:url data) (:url got)) (= (:value data) (:value got)))]\n {:state (-> st (dissoc :waiting) (assoc :got got))\n :show {:status :ready :url (:url got) :summary (summary (:value got))}\n :out [[1 :ready] [0 (:value got)]]\n :effects (if (or (:live args) same?) [] [[:keep got]])}))\n\n ;; a URL: fetched, unless it is the one it already keeps (sent again, then)\n (url-in value)\n (let [u (url-in value)]\n (cond (= u (:url (:waiting st))) {}\n (and (kept? args data) (= u (:url held))) {:out [[1 :ready] [0 (:value held)]]}\n :else (ask u)))\n (#{:refresh \"refresh\"} value)\n (if-let [u (or (:url held) (:url (:waiting st)))] (ask u) {})\n\n (#{:bang \"bang\"} value) (if held {:out [[1 :ready] [0 (:value held)]]} {})\n (and (sequential? value) (#{:get \"get\"} (first value)))\n (if held\n (let [p (path-in value) x (get-in (:value held) p ::none)]\n (if (= x ::none)\n {:out [[1 [:error (str \"nothing at \" (pr-str p))]]]}\n {:out [[0 x]]}))\n {})\n (#{:clear \"clear\"} value)\n {:state {:n (:n st 0)} :show {:status :idle}\n :effects (if (kept? args data) [[:keep nil]] [])}\n :else {})))\n\n :derive (fn [{:keys [shown data state args]}]\n (let [held (held-of args data state)\n status (or (:status shown) (if held :held :idle))\n short (fn [s n] (if (> (count s) n) (str (subs s 0 (dec n)) \"…\") s))]\n {:color (case status (:ready :held) :accent :loading :soft :error :kairos :grid)\n :label (short (case status\n :loading \"loading…\"\n :error (str \"✕ \" (:error shown))\n (:ready :held) (str (cond (kept? args data) \"kept · \" (:live args) \"live · \" :else \"here · \")\n (summary (:value held)))\n (str \"send a URL\" (when (:live args) \" · live\")))\n 38)\n :url (short (str (or (:url shown) (:url held) \"\")) 46)}))\n\n ;; ── view ──\n :view [[:rect {:x 1 :y 1 :w 228 :h 40 :r 4 :stroke :grid :fill :bg}]\n [:circle {:cx 12 :cy 14 :r 4 :fill :$color}]\n [:text {:x 22 :y 17 :size 10 :fill :ink} :$label]\n [:text {:x 8 :y 33 :size 8 :fill :soft} :$url]]\n\n ;; ── help ──\n :help {:inlets [\"a URL (msg https://… · a string · [:fetch url]); refresh; bang; [:get key …]; clear\"]\n :outlets [\"what it found (or the part a [:get …] asks for)\" \":loading, :ready or [:error \\\"…\\\"]\"]\n :messages [[\"a URL\" \"brings it in (if it already keeps that URL, sends it again)\"]\n [\"refresh\" \"fetches its URL again\"]\n [\"bang\" \"sends what it holds\"]\n [\"get items 0 name · [:get [:items 0 :name]]\" \"sends one part of it\"]\n [\"clear\" \"forgets it\"]]\n :try \"Click the message with the URL: the answer comes out on the left, into an edn box. Go back in the history: it is still there, without fetching.\"}})\n"
|
|
9
|
+
};
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated by packages/build.mjs
|
|
2
|
+
export default {
|
|
3
|
+
"package": "@kairotic/data",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"api": 1,
|
|
6
|
+
"author": "bonuoq",
|
|
7
|
+
"license": "MPL-2.0",
|
|
8
|
+
"components": [
|
|
9
|
+
{
|
|
10
|
+
"class": "edn",
|
|
11
|
+
"license": "MPL-2.0",
|
|
12
|
+
"doc": "Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). depth n opens it n levels down; bang sends the picked part again (the whole value if none); clear empties it.",
|
|
13
|
+
"source": ";; edn · any value as a tree to open, read and pick from\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; After Portal (djblue/portal): what arrives is shown as a tree whose collections open and close, each\n;; value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part\n;; of the value (left) and its path (right), the path a fetch box takes as [:get …]. Dragging scrolls.\n;; What happens in the view (open, pick, scroll) is a message to the box itself, like any gesture, so\n;; the same value sent again changes nothing.\n(let [row-h 14 top-y 22 foot-h 16 cw 5.6 ; a line, where lines start, the footer, a character (size 9)\n max-children 200 max-rows 3000\n coll-kind (fn [v] (cond (map? v) :map (vector? v) :vector (set? v) :set (sequential? v) :list :else nil))\n opener {:map \"{\" :vector \"[\" :set \"#{\" :list \"(\"}\n plural (fn [n one many] (str n \" \" (if (= 1 n) one many)))\n size-text (fn [v] (case (coll-kind v)\n :map (plural (count v) \"key\" \"keys\")\n (:vector :list) (plural (count v) \"item\" \"items\")\n :set (plural (count v) \"element\" \"elements\")\n nil))\n ;; the colour of a value, by its kind (the palette of the patch, light or dark)\n ink-of (fn [v] (cond (string? v) :ink (number? v) :kairos (keyword? v) :accent\n (or (nil? v) (boolean? v)) :soft (coll-kind v) :soft :else :ink))\n ;; one level down: the parts of a collection, in a steady order, at most max-children of them\n entries (fn [c]\n (let [es (case (coll-kind c)\n :map (for [[k v] (sort-by (fn [[k _]] (pr-str k)) c)] {:k k :label (pr-str k) :key? true :v v})\n :set (for [v (sort-by pr-str c)] {:k v :label \"\" :v v})\n (map-indexed (fn [i v] {:k i :label (str i) :v v}) c))\n shown (vec (take max-children es))]\n (if (> (count c) max-children)\n (conj shown {:more (- (count c) max-children)})\n shown)))\n ;; a part of the value by its path: get-in, also through lists and sets\n part (fn [root path]\n (reduce (fn [c k] (case (coll-kind c)\n (:map :set) (get c k)\n (:vector :list) (if (number? k) (nth (seq c) k nil) nil)\n nil))\n root path))\n ;; the lines of the tree as it is open: [{:path :depth :label :key? :v :open? :more}]\n rows-of (fn [{:keys [root open]}]\n (if-not (coll-kind root)\n []\n (loop [todo (into () (reverse (map (fn [e] (assoc e :path [(:k e)] :depth 0)) (entries root))))\n out (transient [])]\n (if (or (empty? todo) (>= (count out) max-rows))\n (persistent! out)\n (let [r (first todo) more (rest todo)\n open? (and (coll-kind (:v r)) (contains? open (:path r)) (not (:more r)))\n kids (when open? (map (fn [e] (assoc e :path (conj (:path r) (:k e)) :depth (inc (:depth r))))\n (entries (:v r))))]\n (recur (if open? (into more (reverse kids)) more)\n (conj! out (assoc r :open? open?))))))))\n visible (fn [h] (max 1 (js/Math.floor (/ (- h top-y foot-h) row-h))))\n size-of (fn [args] (api/padded (filter number? args) [320 300]))\n ;; every collection down to depth n, open (at most max-rows of them)\n open-to (fn [root n]\n (loop [todo (list [[] root 0]) acc #{}]\n (if (or (empty? todo) (> (count acc) max-rows))\n acc\n (let [[p v d] (first todo)\n kids (when (and (coll-kind v) (< d n))\n (for [e (entries v) :when (and (not (:more e)) (coll-kind (:v e)))] [(conj p (:k e)) (:v e) (inc d)]))]\n (recur (into (rest todo) kids) (if (and (seq p) (coll-kind v) (<= d n)) (conj acc p) acc))))))\n shown-of (fn [st] {:top (:top st 0) :sel (:sel st) :open (count (:open st))})\n with-top (fn [st top h] (let [n (count (rows-of st))] (assoc st :top (max 0 (min (js/Math.round top) (- n (visible h)))))))]\n {;; ── about ──\n :class \"edn\"\n :license \"MPL-2.0\"\n :doc \"Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). depth n opens it n levels down; bang sends the picked part again (the whole value if none); clear empties it.\"\n :usage \"edn [width height]\"\n :io [1 2]\n :size (fn [box] (size-of (:args box)))\n\n ;; ── logic ──\n :receive (fn [{:keys [value state args]}]\n (let [st (or state {:open #{} :top 0})\n [_ h] (size-of args)\n msg? (fn [k] (and (vector? value) (= k (first value))))\n view (fn [st2] {:state st2 :show (shown-of st2)})]\n (cond\n ;; what happens in the view\n (msg? :edn-open)\n (let [[_ p on] value]\n (view (with-top (update st :open (if on conj disj) p) (:top st 0) h)))\n (msg? :edn-top) (view (with-top st (second value) h))\n (msg? :edn-pick)\n (let [p (second value)]\n (if (or (= p (:sel st)) (not (contains? st :root)))\n {:state st}\n {:state (assoc st :sel p) :show (shown-of (assoc st :sel p))\n :out [[1 p] [0 (part (:root st) p)]]}))\n ;; messages\n (and (msg? :depth) (number? (second value)))\n (view (with-top (assoc st :open (open-to (:root st) (max 0 (dec (second value))))) 0 h))\n (#{:bang \"bang\"} value)\n (if (contains? st :root)\n (let [p (or (:sel st) [])] {:out [[1 p] [0 (part (:root st) p)]]})\n {})\n (#{:clear \"clear\"} value) (view {:open #{} :top 0})\n ;; a value: the same again changes nothing; another keeps what was open and picked\n (and (contains? st :root) (= value (:root st))) {:state st}\n :else\n (let [st2 (assoc st :root value)]\n (view (with-top st2 (:top st 0) h))))))\n\n ;; a line pressed (on its arrow: open or close it; elsewhere: pick it), or a drag that scrolls\n :gesture (fn [{:keys [up hit dy start state h]}]\n (let [st (or state {}) rows (rows-of st) n (count rows) vis (visible h)\n t0 (:top start (:top st 0))]\n (cond\n (>= (js/Math.abs dy) 3)\n [:edn-top (if (= hit :bar)\n (+ t0 (* dy (/ n (* vis row-h))))\n (- t0 (/ dy row-h)))]\n (not up) nil\n ;; a line is its index; its arrow, -1 - its index\n (and (number? hit) (neg? hit))\n (when-let [r (get rows (- -1 hit))] (when (coll-kind (:v r)) [:edn-open (:path r) (not (:open? r))]))\n (number? hit) (when-let [r (get rows hit)] (when-not (:more r) [:edn-pick (:path r)]))\n :else nil)))\n\n ;; ── view ──\n :draw (fn [{:keys [state w h]}]\n (let [st (or state {}) root (:root st) rows (rows-of st) n (count rows)\n vis (visible h) top (max 0 (min (:top st 0) (- n vis))) bar? (> n vis)\n right (- w (if bar? 14 6))\n chars (fn [x] (max 0 (js/Math.floor (/ (- right x) cw))))\n cut (fn [s x] (let [m (chars x)] (if (> (count s) m) (str (subs s 0 (max 0 (dec m))) \"…\") s)))\n line (fn [j r i]\n (let [y (+ top-y (* j row-h)) base (+ y 10)\n x (+ 8 (* 12 (:depth r))) v (:v r) kind (coll-kind v)\n lbl (:label r) vx (+ x 12 (if (seq lbl) (* cw (inc (count lbl))) 0))\n sel? (= (:sel st) (:path r))]\n (if (:more r)\n [[:text {:x (+ x 12) :y base :size 9 :fill :soft} (str \"… \" (:more r) \" more\")]]\n (concat\n [[:rect {:x 2 :y y :w (- right 2) :h row-h :fill (if sel? :accent :bg) :opacity (if sel? 0.14 0) :hit i}]]\n (when kind\n [[:rect {:x (- x 2) :y y :w 12 :h row-h :fill :bg :opacity 0 :hit (- -1 i)}]\n [:text {:x x :y base :size 9 :fill :soft} (if (:open? r) \"▾\" \"▸\")]])\n (when (seq lbl)\n [[:text {:x (+ x 12) :y base :size 9 :fill (if (:key? r) :accent :soft)} (cut lbl (+ x 12))]])\n [[:text {:x vx :y base :size 9 :fill (ink-of v)}\n (cut (cond (and kind (:open? r)) (str (opener kind) \" \" (size-text v))\n kind (pr-str v)\n :else (pr-str v))\n vx)]]))))]\n (concat\n [[:rect {:x 1 :y 1 :w (- w 2) :h (- h 2) :r 4 :stroke :grid :fill :bg}]\n [:text {:x 8 :y 15 :size 9 :fill :soft}\n (cond (not (contains? st :root)) \"edn · send it a value\"\n (coll-kind root) (str (name (coll-kind root)) \" · \" (size-text root))\n :else (str (cond (string? root) \"string\" (number? root) \"number\" (keyword? root) \"keyword\"\n (nil? root) \"nil\" (boolean? root) \"boolean\" :else \"value\")))]\n [:line {:x1 1 :y1 19 :x2 (- w 1) :y2 19 :stroke :grid}]]\n ;; a value that is not a collection: itself, on one line\n (when (and (contains? st :root) (not (coll-kind root)))\n [[:text {:x 8 :y (+ top-y 10) :size 9 :fill (ink-of root)} (cut (pr-str root) 8)]])\n (mapcat (fn [j] (line j (nth rows (+ top j)) (+ top j))) (range (min vis (- n top))))\n (when bar?\n (let [track (* vis row-h) th (max 14 (* track (/ vis n))) ty (+ top-y (* (- track th) (/ top (max 1 (- n vis)))))]\n [[:rect {:x (- w 10) :y top-y :w 6 :h track :r 3 :fill :grid :hit :bar}]\n [:rect {:x (- w 10) :y ty :w 6 :h th :r 3 :fill :soft :hit :bar}]]))\n [[:line {:x1 1 :y1 (- h foot-h) :x2 (- w 1) :y2 (- h foot-h) :stroke :grid}]\n [:text {:x 8 :y (- h 5) :size 8 :fill :soft}\n (cut (cond (:sel st) (str \"→ \" (pr-str (:sel st)))\n (seq rows) (str n (if (= 1 n) \" line\" \" lines\") (when bar? (str \" · \" (inc top) \"–\" (min n (+ top vis)))) \" · pick a line\")\n :else \"\")\n 8)]])))\n\n ;; ── help ──\n :help {:inlets [\"any value: a map, a vector, a set, a list, or a single value; depth n · bang · clear\"]\n :outlets [\"the part picked\" \"its path, for [:get …] of a fetch box\"]\n :messages [[\"depth 2\" \"opens it two levels down\"]\n [\"bang\" \"sends the part picked again (the whole value if none)\"]\n [\"clear\" \"empties it\"]]\n :try \"Send it a fetch's answer and open the branches: pick a line and its path comes out on the right.\"}})\n"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"class": "fetch",
|
|
17
|
+
"license": "MPL-2.0",
|
|
18
|
+
"doc": "Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \"…\"].",
|
|
19
|
+
"source": ";; fetch · what is at a URL (JSON, text or EDN), brought in as a task\n;; Part of @kairotic/data. Written against the Kairotic API (api/…).\n;; Its logic never touches the network: a URL asks for the task :get, the engine runs it (api/http-get)\n;; and what it finds comes back to this box on inlet :task. By default the box keeps it as its data\n;; ([:keep …]): one gesture of the history, the same for everyone in a room, and going back in the\n;; history does not fetch it again. With `live` it is only held in this tab, for answers that change.\n(let [formats #{:json :text :edn}\n fmt-of (fn [args] (if (formats (:as args)) (:as args) :json))\n ;; what the box holds: kept in the work, or (live) only here\n sane (fn [d] (if (and (map? d) (string? (:url d))) (select-keys d [:url :value :date]) {}))\n ;; kept in the work (its data) or, live or before it could be kept (inside a sub-patch, too long, played\n ;; by someone else in a jam), only here (its state)\n held-of (fn [args data state] (if (and (not (:live args)) (contains? data :value)) data (:got state)))\n kept? (fn [args data] (and (not (:live args)) (contains? data :value)))\n summary (fn [v] (cond (map? v) (str \"{} \" (count v) (if (= 1 (count v)) \" key\" \" keys\"))\n (sequential? v) (str \"[] \" (count v) (if (= 1 (count v)) \" item\" \" items\"))\n (string? v) (str \"\\\"\\\" \" (count v) \" characters\")\n (nil? v) \"nil\"\n :else (pr-str v)))\n url-in (fn [value]\n (cond (api/url-of value) (api/url-of value)\n (and (sequential? value) (#{:fetch :url \"fetch\" \"url\"} (first value))) (api/url-of (second value))\n :else nil))\n path-in (fn [value] (let [p (rest value)] (if (and (= 1 (count p)) (sequential? (first p))) (vec (first p)) (vec p))))]\n {;; ── about ──\n :class \"fetch\"\n :license \"MPL-2.0\"\n :doc \"Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \\\"…\\\"].\"\n :usage \"fetch [json|text|edn] [live]\"\n :io [1 2]\n :size [230 42]\n\n ;; ── arguments ──\n :args [[:as nil] [:live false]]\n\n ;; ── data: what it keeps, in the history ──\n :model {:empty {}\n :step (fn [d [op x]] (case op :set (sane x) :clear {} d))\n :load sane\n :code (fn [id d] (when (contains? d :value) [(str \"(keep! \" id \" \" (pr-str d) \")\")]))}\n\n ;; ── tasks: outside its pure logic ──\n :tasks {:get (fn [{:keys [url as]} _]\n (.then (api/http-get url {:as as})\n (fn [v] {:body v :date (.toISOString (js/Date.))})))}\n\n ;; ── logic ──\n :receive (fn [{:keys [inlet value state data args]}]\n (let [st (or state {})\n held (held-of args data st)\n ask (fn [u] (let [req {:url u :as (fmt-of args) :n (inc (:n st 0))}]\n {:state (assoc st :waiting req :n (:n req))\n :show {:status :loading :url u}\n :out [[1 :loading]]\n :effects [[:get req]]}))]\n (cond\n ;; what the task found\n (= inlet :task)\n (cond\n (not= (:arg value) (:waiting st)) {} ; the answer to an older request: dropped\n (:error value)\n {:state (dissoc st :waiting)\n :show {:status :error :url (:url (:arg value)) :error (:error value)}\n :out [[1 [:error (:error value)]]]}\n :else\n (let [got {:url (:url (:arg value)) :value (:body (:value value)) :date (:date (:value value))}\n same? (and (kept? args data) (= (:url data) (:url got)) (= (:value data) (:value got)))]\n {:state (-> st (dissoc :waiting) (assoc :got got))\n :show {:status :ready :url (:url got) :summary (summary (:value got))}\n :out [[1 :ready] [0 (:value got)]]\n :effects (if (or (:live args) same?) [] [[:keep got]])}))\n\n ;; a URL: fetched, unless it is the one it already keeps (sent again, then)\n (url-in value)\n (let [u (url-in value)]\n (cond (= u (:url (:waiting st))) {}\n (and (kept? args data) (= u (:url held))) {:out [[1 :ready] [0 (:value held)]]}\n :else (ask u)))\n (#{:refresh \"refresh\"} value)\n (if-let [u (or (:url held) (:url (:waiting st)))] (ask u) {})\n\n (#{:bang \"bang\"} value) (if held {:out [[1 :ready] [0 (:value held)]]} {})\n (and (sequential? value) (#{:get \"get\"} (first value)))\n (if held\n (let [p (path-in value) x (get-in (:value held) p ::none)]\n (if (= x ::none)\n {:out [[1 [:error (str \"nothing at \" (pr-str p))]]]}\n {:out [[0 x]]}))\n {})\n (#{:clear \"clear\"} value)\n {:state {:n (:n st 0)} :show {:status :idle}\n :effects (if (kept? args data) [[:keep nil]] [])}\n :else {})))\n\n :derive (fn [{:keys [shown data state args]}]\n (let [held (held-of args data state)\n status (or (:status shown) (if held :held :idle))\n short (fn [s n] (if (> (count s) n) (str (subs s 0 (dec n)) \"…\") s))]\n {:color (case status (:ready :held) :accent :loading :soft :error :kairos :grid)\n :label (short (case status\n :loading \"loading…\"\n :error (str \"✕ \" (:error shown))\n (:ready :held) (str (cond (kept? args data) \"kept · \" (:live args) \"live · \" :else \"here · \")\n (summary (:value held)))\n (str \"send a URL\" (when (:live args) \" · live\")))\n 38)\n :url (short (str (or (:url shown) (:url held) \"\")) 46)}))\n\n ;; ── view ──\n :view [[:rect {:x 1 :y 1 :w 228 :h 40 :r 4 :stroke :grid :fill :bg}]\n [:circle {:cx 12 :cy 14 :r 4 :fill :$color}]\n [:text {:x 22 :y 17 :size 10 :fill :ink} :$label]\n [:text {:x 8 :y 33 :size 8 :fill :soft} :$url]]\n\n ;; ── help ──\n :help {:inlets [\"a URL (msg https://… · a string · [:fetch url]); refresh; bang; [:get key …]; clear\"]\n :outlets [\"what it found (or the part a [:get …] asks for)\" \":loading, :ready or [:error \\\"…\\\"]\"]\n :messages [[\"a URL\" \"brings it in (if it already keeps that URL, sends it again)\"]\n [\"refresh\" \"fetches its URL again\"]\n [\"bang\" \"sends what it holds\"]\n [\"get items 0 name · [:get [:items 0 :name]]\" \"sends one part of it\"]\n [\"clear\" \"forgets it\"]]\n :try \"Click the message with the URL: the answer comes out on the left, into an edn box. Go back in the history: it is still there, without fetching.\"}})\n"
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairotic/data",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Kairotic data components: fetch (what is at a URL, brought in as a task and kept in the work) and edn (any value as a tree to open, read and pick from, after Portal)",
|
|
5
|
+
"author": "bonuoq",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/bonuoq/kairotic.git",
|
|
10
|
+
"directory": "packages/data"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/bonuoq/kairotic/tree/main/packages#readme",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "dist/index.mjs",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"kairotic": {
|
|
24
|
+
"api": 1
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "node ../build.mjs data",
|
|
28
|
+
"prepublishOnly": "node ../build.mjs data"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"kairotic",
|
|
32
|
+
"data",
|
|
33
|
+
"fetch",
|
|
34
|
+
"json",
|
|
35
|
+
"edn",
|
|
36
|
+
"inspector",
|
|
37
|
+
"patcher"
|
|
38
|
+
]
|
|
39
|
+
}
|
package/src/edn.cljs
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
;; edn · any value as a tree to open, read and pick from
|
|
2
|
+
;; Part of @kairotic/data. Written against the Kairotic API (api/…).
|
|
3
|
+
;; After Portal (djblue/portal): what arrives is shown as a tree whose collections open and close, each
|
|
4
|
+
;; value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part
|
|
5
|
+
;; of the value (left) and its path (right), the path a fetch box takes as [:get …]. Dragging scrolls.
|
|
6
|
+
;; What happens in the view (open, pick, scroll) is a message to the box itself, like any gesture, so
|
|
7
|
+
;; the same value sent again changes nothing.
|
|
8
|
+
(let [row-h 14 top-y 22 foot-h 16 cw 5.6 ; a line, where lines start, the footer, a character (size 9)
|
|
9
|
+
max-children 200 max-rows 3000
|
|
10
|
+
coll-kind (fn [v] (cond (map? v) :map (vector? v) :vector (set? v) :set (sequential? v) :list :else nil))
|
|
11
|
+
opener {:map "{" :vector "[" :set "#{" :list "("}
|
|
12
|
+
plural (fn [n one many] (str n " " (if (= 1 n) one many)))
|
|
13
|
+
size-text (fn [v] (case (coll-kind v)
|
|
14
|
+
:map (plural (count v) "key" "keys")
|
|
15
|
+
(:vector :list) (plural (count v) "item" "items")
|
|
16
|
+
:set (plural (count v) "element" "elements")
|
|
17
|
+
nil))
|
|
18
|
+
;; the colour of a value, by its kind (the palette of the patch, light or dark)
|
|
19
|
+
ink-of (fn [v] (cond (string? v) :ink (number? v) :kairos (keyword? v) :accent
|
|
20
|
+
(or (nil? v) (boolean? v)) :soft (coll-kind v) :soft :else :ink))
|
|
21
|
+
;; one level down: the parts of a collection, in a steady order, at most max-children of them
|
|
22
|
+
entries (fn [c]
|
|
23
|
+
(let [es (case (coll-kind c)
|
|
24
|
+
:map (for [[k v] (sort-by (fn [[k _]] (pr-str k)) c)] {:k k :label (pr-str k) :key? true :v v})
|
|
25
|
+
:set (for [v (sort-by pr-str c)] {:k v :label "" :v v})
|
|
26
|
+
(map-indexed (fn [i v] {:k i :label (str i) :v v}) c))
|
|
27
|
+
shown (vec (take max-children es))]
|
|
28
|
+
(if (> (count c) max-children)
|
|
29
|
+
(conj shown {:more (- (count c) max-children)})
|
|
30
|
+
shown)))
|
|
31
|
+
;; a part of the value by its path: get-in, also through lists and sets
|
|
32
|
+
part (fn [root path]
|
|
33
|
+
(reduce (fn [c k] (case (coll-kind c)
|
|
34
|
+
(:map :set) (get c k)
|
|
35
|
+
(:vector :list) (if (number? k) (nth (seq c) k nil) nil)
|
|
36
|
+
nil))
|
|
37
|
+
root path))
|
|
38
|
+
;; the lines of the tree as it is open: [{:path :depth :label :key? :v :open? :more}]
|
|
39
|
+
rows-of (fn [{:keys [root open]}]
|
|
40
|
+
(if-not (coll-kind root)
|
|
41
|
+
[]
|
|
42
|
+
(loop [todo (into () (reverse (map (fn [e] (assoc e :path [(:k e)] :depth 0)) (entries root))))
|
|
43
|
+
out (transient [])]
|
|
44
|
+
(if (or (empty? todo) (>= (count out) max-rows))
|
|
45
|
+
(persistent! out)
|
|
46
|
+
(let [r (first todo) more (rest todo)
|
|
47
|
+
open? (and (coll-kind (:v r)) (contains? open (:path r)) (not (:more r)))
|
|
48
|
+
kids (when open? (map (fn [e] (assoc e :path (conj (:path r) (:k e)) :depth (inc (:depth r))))
|
|
49
|
+
(entries (:v r))))]
|
|
50
|
+
(recur (if open? (into more (reverse kids)) more)
|
|
51
|
+
(conj! out (assoc r :open? open?))))))))
|
|
52
|
+
visible (fn [h] (max 1 (js/Math.floor (/ (- h top-y foot-h) row-h))))
|
|
53
|
+
size-of (fn [args] (api/padded (filter number? args) [320 300]))
|
|
54
|
+
;; every collection down to depth n, open (at most max-rows of them)
|
|
55
|
+
open-to (fn [root n]
|
|
56
|
+
(loop [todo (list [[] root 0]) acc #{}]
|
|
57
|
+
(if (or (empty? todo) (> (count acc) max-rows))
|
|
58
|
+
acc
|
|
59
|
+
(let [[p v d] (first todo)
|
|
60
|
+
kids (when (and (coll-kind v) (< d n))
|
|
61
|
+
(for [e (entries v) :when (and (not (:more e)) (coll-kind (:v e)))] [(conj p (:k e)) (:v e) (inc d)]))]
|
|
62
|
+
(recur (into (rest todo) kids) (if (and (seq p) (coll-kind v) (<= d n)) (conj acc p) acc))))))
|
|
63
|
+
shown-of (fn [st] {:top (:top st 0) :sel (:sel st) :open (count (:open st))})
|
|
64
|
+
with-top (fn [st top h] (let [n (count (rows-of st))] (assoc st :top (max 0 (min (js/Math.round top) (- n (visible h)))))))]
|
|
65
|
+
{;; ── about ──
|
|
66
|
+
:class "edn"
|
|
67
|
+
:license "MPL-2.0"
|
|
68
|
+
:doc "Shows any value as a tree, like Portal: maps, vectors, sets and lists open and close (▸ ▾), each value coloured by its kind, a closed collection previewed on its line. Picking a line sends that part of the value (left) and its path (right): the path a fetch box takes as [:get …]. Dragging scrolls (or the bar on the right). depth n opens it n levels down; bang sends the picked part again (the whole value if none); clear empties it."
|
|
69
|
+
:usage "edn [width height]"
|
|
70
|
+
:io [1 2]
|
|
71
|
+
:size (fn [box] (size-of (:args box)))
|
|
72
|
+
|
|
73
|
+
;; ── logic ──
|
|
74
|
+
:receive (fn [{:keys [value state args]}]
|
|
75
|
+
(let [st (or state {:open #{} :top 0})
|
|
76
|
+
[_ h] (size-of args)
|
|
77
|
+
msg? (fn [k] (and (vector? value) (= k (first value))))
|
|
78
|
+
view (fn [st2] {:state st2 :show (shown-of st2)})]
|
|
79
|
+
(cond
|
|
80
|
+
;; what happens in the view
|
|
81
|
+
(msg? :edn-open)
|
|
82
|
+
(let [[_ p on] value]
|
|
83
|
+
(view (with-top (update st :open (if on conj disj) p) (:top st 0) h)))
|
|
84
|
+
(msg? :edn-top) (view (with-top st (second value) h))
|
|
85
|
+
(msg? :edn-pick)
|
|
86
|
+
(let [p (second value)]
|
|
87
|
+
(if (or (= p (:sel st)) (not (contains? st :root)))
|
|
88
|
+
{:state st}
|
|
89
|
+
{:state (assoc st :sel p) :show (shown-of (assoc st :sel p))
|
|
90
|
+
:out [[1 p] [0 (part (:root st) p)]]}))
|
|
91
|
+
;; messages
|
|
92
|
+
(and (msg? :depth) (number? (second value)))
|
|
93
|
+
(view (with-top (assoc st :open (open-to (:root st) (max 0 (dec (second value))))) 0 h))
|
|
94
|
+
(#{:bang "bang"} value)
|
|
95
|
+
(if (contains? st :root)
|
|
96
|
+
(let [p (or (:sel st) [])] {:out [[1 p] [0 (part (:root st) p)]]})
|
|
97
|
+
{})
|
|
98
|
+
(#{:clear "clear"} value) (view {:open #{} :top 0})
|
|
99
|
+
;; a value: the same again changes nothing; another keeps what was open and picked
|
|
100
|
+
(and (contains? st :root) (= value (:root st))) {:state st}
|
|
101
|
+
:else
|
|
102
|
+
(let [st2 (assoc st :root value)]
|
|
103
|
+
(view (with-top st2 (:top st 0) h))))))
|
|
104
|
+
|
|
105
|
+
;; a line pressed (on its arrow: open or close it; elsewhere: pick it), or a drag that scrolls
|
|
106
|
+
:gesture (fn [{:keys [up hit dy start state h]}]
|
|
107
|
+
(let [st (or state {}) rows (rows-of st) n (count rows) vis (visible h)
|
|
108
|
+
t0 (:top start (:top st 0))]
|
|
109
|
+
(cond
|
|
110
|
+
(>= (js/Math.abs dy) 3)
|
|
111
|
+
[:edn-top (if (= hit :bar)
|
|
112
|
+
(+ t0 (* dy (/ n (* vis row-h))))
|
|
113
|
+
(- t0 (/ dy row-h)))]
|
|
114
|
+
(not up) nil
|
|
115
|
+
;; a line is its index; its arrow, -1 - its index
|
|
116
|
+
(and (number? hit) (neg? hit))
|
|
117
|
+
(when-let [r (get rows (- -1 hit))] (when (coll-kind (:v r)) [:edn-open (:path r) (not (:open? r))]))
|
|
118
|
+
(number? hit) (when-let [r (get rows hit)] (when-not (:more r) [:edn-pick (:path r)]))
|
|
119
|
+
:else nil)))
|
|
120
|
+
|
|
121
|
+
;; ── view ──
|
|
122
|
+
:draw (fn [{:keys [state w h]}]
|
|
123
|
+
(let [st (or state {}) root (:root st) rows (rows-of st) n (count rows)
|
|
124
|
+
vis (visible h) top (max 0 (min (:top st 0) (- n vis))) bar? (> n vis)
|
|
125
|
+
right (- w (if bar? 14 6))
|
|
126
|
+
chars (fn [x] (max 0 (js/Math.floor (/ (- right x) cw))))
|
|
127
|
+
cut (fn [s x] (let [m (chars x)] (if (> (count s) m) (str (subs s 0 (max 0 (dec m))) "…") s)))
|
|
128
|
+
line (fn [j r i]
|
|
129
|
+
(let [y (+ top-y (* j row-h)) base (+ y 10)
|
|
130
|
+
x (+ 8 (* 12 (:depth r))) v (:v r) kind (coll-kind v)
|
|
131
|
+
lbl (:label r) vx (+ x 12 (if (seq lbl) (* cw (inc (count lbl))) 0))
|
|
132
|
+
sel? (= (:sel st) (:path r))]
|
|
133
|
+
(if (:more r)
|
|
134
|
+
[[:text {:x (+ x 12) :y base :size 9 :fill :soft} (str "… " (:more r) " more")]]
|
|
135
|
+
(concat
|
|
136
|
+
[[:rect {:x 2 :y y :w (- right 2) :h row-h :fill (if sel? :accent :bg) :opacity (if sel? 0.14 0) :hit i}]]
|
|
137
|
+
(when kind
|
|
138
|
+
[[:rect {:x (- x 2) :y y :w 12 :h row-h :fill :bg :opacity 0 :hit (- -1 i)}]
|
|
139
|
+
[:text {:x x :y base :size 9 :fill :soft} (if (:open? r) "▾" "▸")]])
|
|
140
|
+
(when (seq lbl)
|
|
141
|
+
[[:text {:x (+ x 12) :y base :size 9 :fill (if (:key? r) :accent :soft)} (cut lbl (+ x 12))]])
|
|
142
|
+
[[:text {:x vx :y base :size 9 :fill (ink-of v)}
|
|
143
|
+
(cut (cond (and kind (:open? r)) (str (opener kind) " " (size-text v))
|
|
144
|
+
kind (pr-str v)
|
|
145
|
+
:else (pr-str v))
|
|
146
|
+
vx)]]))))]
|
|
147
|
+
(concat
|
|
148
|
+
[[:rect {:x 1 :y 1 :w (- w 2) :h (- h 2) :r 4 :stroke :grid :fill :bg}]
|
|
149
|
+
[:text {:x 8 :y 15 :size 9 :fill :soft}
|
|
150
|
+
(cond (not (contains? st :root)) "edn · send it a value"
|
|
151
|
+
(coll-kind root) (str (name (coll-kind root)) " · " (size-text root))
|
|
152
|
+
:else (str (cond (string? root) "string" (number? root) "number" (keyword? root) "keyword"
|
|
153
|
+
(nil? root) "nil" (boolean? root) "boolean" :else "value")))]
|
|
154
|
+
[:line {:x1 1 :y1 19 :x2 (- w 1) :y2 19 :stroke :grid}]]
|
|
155
|
+
;; a value that is not a collection: itself, on one line
|
|
156
|
+
(when (and (contains? st :root) (not (coll-kind root)))
|
|
157
|
+
[[:text {:x 8 :y (+ top-y 10) :size 9 :fill (ink-of root)} (cut (pr-str root) 8)]])
|
|
158
|
+
(mapcat (fn [j] (line j (nth rows (+ top j)) (+ top j))) (range (min vis (- n top))))
|
|
159
|
+
(when bar?
|
|
160
|
+
(let [track (* vis row-h) th (max 14 (* track (/ vis n))) ty (+ top-y (* (- track th) (/ top (max 1 (- n vis)))))]
|
|
161
|
+
[[:rect {:x (- w 10) :y top-y :w 6 :h track :r 3 :fill :grid :hit :bar}]
|
|
162
|
+
[:rect {:x (- w 10) :y ty :w 6 :h th :r 3 :fill :soft :hit :bar}]]))
|
|
163
|
+
[[:line {:x1 1 :y1 (- h foot-h) :x2 (- w 1) :y2 (- h foot-h) :stroke :grid}]
|
|
164
|
+
[:text {:x 8 :y (- h 5) :size 8 :fill :soft}
|
|
165
|
+
(cut (cond (:sel st) (str "→ " (pr-str (:sel st)))
|
|
166
|
+
(seq rows) (str n (if (= 1 n) " line" " lines") (when bar? (str " · " (inc top) "–" (min n (+ top vis)))) " · pick a line")
|
|
167
|
+
:else "")
|
|
168
|
+
8)]])))
|
|
169
|
+
|
|
170
|
+
;; ── help ──
|
|
171
|
+
:help {:inlets ["any value: a map, a vector, a set, a list, or a single value; depth n · bang · clear"]
|
|
172
|
+
:outlets ["the part picked" "its path, for [:get …] of a fetch box"]
|
|
173
|
+
:messages [["depth 2" "opens it two levels down"]
|
|
174
|
+
["bang" "sends the part picked again (the whole value if none)"]
|
|
175
|
+
["clear" "empties it"]]
|
|
176
|
+
:try "Send it a fetch's answer and open the branches: pick a line and its path comes out on the right."}})
|
package/src/fetch.cljs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
;; fetch · what is at a URL (JSON, text or EDN), brought in as a task
|
|
2
|
+
;; Part of @kairotic/data. Written against the Kairotic API (api/…).
|
|
3
|
+
;; Its logic never touches the network: a URL asks for the task :get, the engine runs it (api/http-get)
|
|
4
|
+
;; and what it finds comes back to this box on inlet :task. By default the box keeps it as its data
|
|
5
|
+
;; ([:keep …]): one gesture of the history, the same for everyone in a room, and going back in the
|
|
6
|
+
;; history does not fetch it again. With `live` it is only held in this tab, for answers that change.
|
|
7
|
+
(let [formats #{:json :text :edn}
|
|
8
|
+
fmt-of (fn [args] (if (formats (:as args)) (:as args) :json))
|
|
9
|
+
;; what the box holds: kept in the work, or (live) only here
|
|
10
|
+
sane (fn [d] (if (and (map? d) (string? (:url d))) (select-keys d [:url :value :date]) {}))
|
|
11
|
+
;; kept in the work (its data) or, live or before it could be kept (inside a sub-patch, too long, played
|
|
12
|
+
;; by someone else in a jam), only here (its state)
|
|
13
|
+
held-of (fn [args data state] (if (and (not (:live args)) (contains? data :value)) data (:got state)))
|
|
14
|
+
kept? (fn [args data] (and (not (:live args)) (contains? data :value)))
|
|
15
|
+
summary (fn [v] (cond (map? v) (str "{} " (count v) (if (= 1 (count v)) " key" " keys"))
|
|
16
|
+
(sequential? v) (str "[] " (count v) (if (= 1 (count v)) " item" " items"))
|
|
17
|
+
(string? v) (str "\"\" " (count v) " characters")
|
|
18
|
+
(nil? v) "nil"
|
|
19
|
+
:else (pr-str v)))
|
|
20
|
+
url-in (fn [value]
|
|
21
|
+
(cond (api/url-of value) (api/url-of value)
|
|
22
|
+
(and (sequential? value) (#{:fetch :url "fetch" "url"} (first value))) (api/url-of (second value))
|
|
23
|
+
:else nil))
|
|
24
|
+
path-in (fn [value] (let [p (rest value)] (if (and (= 1 (count p)) (sequential? (first p))) (vec (first p)) (vec p))))]
|
|
25
|
+
{;; ── about ──
|
|
26
|
+
:class "fetch"
|
|
27
|
+
:license "MPL-2.0"
|
|
28
|
+
:doc "Brings in what is at a URL: JSON (read as EDN: keys that can be keywords become keywords), text or EDN. A URL (https://…, or github:owner/repo@ref/path through jsDelivr) asks for it; what arrives goes out on the left and is kept as the data of the box, in the history, so everyone in a room has the same and going back does not fetch again (a URL it already holds is sent again, not fetched: refresh fetches it again). With live it is only held in this tab, for answers that change. On the right: :loading, :ready or [:error \"…\"]."
|
|
29
|
+
:usage "fetch [json|text|edn] [live]"
|
|
30
|
+
:io [1 2]
|
|
31
|
+
:size [230 42]
|
|
32
|
+
|
|
33
|
+
;; ── arguments ──
|
|
34
|
+
:args [[:as nil] [:live false]]
|
|
35
|
+
|
|
36
|
+
;; ── data: what it keeps, in the history ──
|
|
37
|
+
:model {:empty {}
|
|
38
|
+
:step (fn [d [op x]] (case op :set (sane x) :clear {} d))
|
|
39
|
+
:load sane
|
|
40
|
+
:code (fn [id d] (when (contains? d :value) [(str "(keep! " id " " (pr-str d) ")")]))}
|
|
41
|
+
|
|
42
|
+
;; ── tasks: outside its pure logic ──
|
|
43
|
+
:tasks {:get (fn [{:keys [url as]} _]
|
|
44
|
+
(.then (api/http-get url {:as as})
|
|
45
|
+
(fn [v] {:body v :date (.toISOString (js/Date.))})))}
|
|
46
|
+
|
|
47
|
+
;; ── logic ──
|
|
48
|
+
:receive (fn [{:keys [inlet value state data args]}]
|
|
49
|
+
(let [st (or state {})
|
|
50
|
+
held (held-of args data st)
|
|
51
|
+
ask (fn [u] (let [req {:url u :as (fmt-of args) :n (inc (:n st 0))}]
|
|
52
|
+
{:state (assoc st :waiting req :n (:n req))
|
|
53
|
+
:show {:status :loading :url u}
|
|
54
|
+
:out [[1 :loading]]
|
|
55
|
+
:effects [[:get req]]}))]
|
|
56
|
+
(cond
|
|
57
|
+
;; what the task found
|
|
58
|
+
(= inlet :task)
|
|
59
|
+
(cond
|
|
60
|
+
(not= (:arg value) (:waiting st)) {} ; the answer to an older request: dropped
|
|
61
|
+
(:error value)
|
|
62
|
+
{:state (dissoc st :waiting)
|
|
63
|
+
:show {:status :error :url (:url (:arg value)) :error (:error value)}
|
|
64
|
+
:out [[1 [:error (:error value)]]]}
|
|
65
|
+
:else
|
|
66
|
+
(let [got {:url (:url (:arg value)) :value (:body (:value value)) :date (:date (:value value))}
|
|
67
|
+
same? (and (kept? args data) (= (:url data) (:url got)) (= (:value data) (:value got)))]
|
|
68
|
+
{:state (-> st (dissoc :waiting) (assoc :got got))
|
|
69
|
+
:show {:status :ready :url (:url got) :summary (summary (:value got))}
|
|
70
|
+
:out [[1 :ready] [0 (:value got)]]
|
|
71
|
+
:effects (if (or (:live args) same?) [] [[:keep got]])}))
|
|
72
|
+
|
|
73
|
+
;; a URL: fetched, unless it is the one it already keeps (sent again, then)
|
|
74
|
+
(url-in value)
|
|
75
|
+
(let [u (url-in value)]
|
|
76
|
+
(cond (= u (:url (:waiting st))) {}
|
|
77
|
+
(and (kept? args data) (= u (:url held))) {:out [[1 :ready] [0 (:value held)]]}
|
|
78
|
+
:else (ask u)))
|
|
79
|
+
(#{:refresh "refresh"} value)
|
|
80
|
+
(if-let [u (or (:url held) (:url (:waiting st)))] (ask u) {})
|
|
81
|
+
|
|
82
|
+
(#{:bang "bang"} value) (if held {:out [[1 :ready] [0 (:value held)]]} {})
|
|
83
|
+
(and (sequential? value) (#{:get "get"} (first value)))
|
|
84
|
+
(if held
|
|
85
|
+
(let [p (path-in value) x (get-in (:value held) p ::none)]
|
|
86
|
+
(if (= x ::none)
|
|
87
|
+
{:out [[1 [:error (str "nothing at " (pr-str p))]]]}
|
|
88
|
+
{:out [[0 x]]}))
|
|
89
|
+
{})
|
|
90
|
+
(#{:clear "clear"} value)
|
|
91
|
+
{:state {:n (:n st 0)} :show {:status :idle}
|
|
92
|
+
:effects (if (kept? args data) [[:keep nil]] [])}
|
|
93
|
+
:else {})))
|
|
94
|
+
|
|
95
|
+
:derive (fn [{:keys [shown data state args]}]
|
|
96
|
+
(let [held (held-of args data state)
|
|
97
|
+
status (or (:status shown) (if held :held :idle))
|
|
98
|
+
short (fn [s n] (if (> (count s) n) (str (subs s 0 (dec n)) "…") s))]
|
|
99
|
+
{:color (case status (:ready :held) :accent :loading :soft :error :kairos :grid)
|
|
100
|
+
:label (short (case status
|
|
101
|
+
:loading "loading…"
|
|
102
|
+
:error (str "✕ " (:error shown))
|
|
103
|
+
(:ready :held) (str (cond (kept? args data) "kept · " (:live args) "live · " :else "here · ")
|
|
104
|
+
(summary (:value held)))
|
|
105
|
+
(str "send a URL" (when (:live args) " · live")))
|
|
106
|
+
38)
|
|
107
|
+
:url (short (str (or (:url shown) (:url held) "")) 46)}))
|
|
108
|
+
|
|
109
|
+
;; ── view ──
|
|
110
|
+
:view [[:rect {:x 1 :y 1 :w 228 :h 40 :r 4 :stroke :grid :fill :bg}]
|
|
111
|
+
[:circle {:cx 12 :cy 14 :r 4 :fill :$color}]
|
|
112
|
+
[:text {:x 22 :y 17 :size 10 :fill :ink} :$label]
|
|
113
|
+
[:text {:x 8 :y 33 :size 8 :fill :soft} :$url]]
|
|
114
|
+
|
|
115
|
+
;; ── help ──
|
|
116
|
+
:help {:inlets ["a URL (msg https://… · a string · [:fetch url]); refresh; bang; [:get key …]; clear"]
|
|
117
|
+
:outlets ["what it found (or the part a [:get …] asks for)" ":loading, :ready or [:error \"…\"]"]
|
|
118
|
+
:messages [["a URL" "brings it in (if it already keeps that URL, sends it again)"]
|
|
119
|
+
["refresh" "fetches its URL again"]
|
|
120
|
+
["bang" "sends what it holds"]
|
|
121
|
+
["get items 0 name · [:get [:items 0 :name]]" "sends one part of it"]
|
|
122
|
+
["clear" "forgets it"]]
|
|
123
|
+
:try "Click the message with the URL: the answer comes out on the left, into an edn box. Go back in the history: it is still there, without fetching."}})
|