@kairotic/app 0.0.0-stage → 0.2.2
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 +9 -2
- package/index.html +1101 -0
- package/kairotic/api.cljs +362 -0
- package/kairotic/assistant.cljs +505 -0
- package/kairotic/backends/gun-backend.js +371 -0
- package/kairotic/canvas.cljs +751 -0
- package/kairotic/code.cljs +108 -0
- package/kairotic/components.cljs +838 -0
- package/kairotic/core.cljs +96 -0
- package/kairotic/corefns.cljs +473 -0
- package/kairotic/docs.mjs +2 -0
- package/kairotic/engine.cljs +482 -0
- package/kairotic/examples.mjs +96 -0
- package/kairotic/faust.cljs +225 -0
- package/kairotic/help.cljs +1036 -0
- package/kairotic/hiccup.cljs +566 -0
- package/kairotic/input.cljs +271 -0
- package/kairotic/kfile.mjs +138 -0
- package/kairotic/library.cljs +214 -0
- package/kairotic/links.cljs +41 -0
- package/kairotic/mt.cljs +1604 -0
- package/kairotic/mtboxes.cljs +276 -0
- package/kairotic/packages/analysis.mjs +64 -0
- package/kairotic/packages/controls.mjs +28 -0
- package/kairotic/packages/data.mjs +22 -0
- package/kairotic/packages/faust-units/cmaes.mjs +82 -0
- package/kairotic/packages/faust-units/graph.mjs +212 -0
- package/kairotic/packages/faust-units/listen.mjs +125 -0
- package/kairotic/packages/faust-units/notation.mjs +139 -0
- package/kairotic/packages/faust-units/parse.mjs +439 -0
- package/kairotic/packages/faust-units/recipe.mjs +195 -0
- package/kairotic/packages/faust-units/render.mjs +69 -0
- package/kairotic/packages/faust-units/units.mjs +130 -0
- package/kairotic/packages/faust-units/view.mjs +353 -0
- package/kairotic/packages/faust-units.mjs +9 -0
- package/kairotic/packages/io.mjs +58 -0
- package/kairotic/packages/net.mjs +28 -0
- package/kairotic/packages/patterns.mjs +34 -0
- package/kairotic/packages/spatial.mjs +22 -0
- package/kairotic/packages/spectral/shift.mjs +169 -0
- package/kairotic/packages/spectral.mjs +16 -0
- package/kairotic/packages/synth.mjs +28 -0
- package/kairotic/packages/theory/counterpoint.mjs +798 -0
- package/kairotic/packages/theory/mirror.mjs +221 -0
- package/kairotic/packages/theory.mjs +46 -0
- package/kairotic/patterns.cljs +538 -0
- package/kairotic/presets.cljs +227 -0
- package/kairotic/quantize.cljs +782 -0
- package/kairotic/record.cljs +169 -0
- package/kairotic/release.cljs +90 -0
- package/kairotic/ring.cljs +129 -0
- package/kairotic/ringaudio.cljs +232 -0
- package/kairotic/ringcore.mjs +270 -0
- package/kairotic/score.cljs +176 -0
- package/kairotic/shapes.cljs +283 -0
- package/kairotic/signal.cljs +327 -0
- package/kairotic/sounds.cljs +565 -0
- package/kairotic/sounds.mjs +108 -0
- package/kairotic/span.cljs +129 -0
- package/kairotic/spell.cljs +281 -0
- package/kairotic/structure.cljs +145 -0
- package/kairotic/talk.cljs +115 -0
- package/kairotic/tempo.cljs +226 -0
- package/kairotic/treecanvas.cljs +299 -0
- package/kairotic/tutorials.mjs +1283 -0
- package/kairotic/vendor/codemirror.mjs +23 -0
- package/package.json +23 -4
- package/page/00-work.cljs +862 -0
- package/page/01-commands.cljs +1219 -0
- package/page/02-patch-view.cljs +1051 -0
- package/page/03-history-view.cljs +481 -0
- package/page/04-panels.cljs +448 -0
- package/page/05-gestures.cljs +1247 -0
- package/page/06-connection.cljs +219 -0
- package/page/07-claude.cljs +430 -0
- package/page/08-sessions.cljs +423 -0
- package/page/09-learn.cljs +602 -0
- package/page/10-embed.cljs +469 -0
- package/page/11-startup.cljs +61 -0
- package/page/12-feedback.cljs +172 -0
- package/page/13-perform.cljs +325 -0
- package/page/14-editor.cljs +656 -0
- package/page/15-presets.cljs +230 -0
- package/page/16-release.cljs +242 -0
- package/page/17-sounds.cljs +147 -0
- package/page/18-lines.cljs +594 -0
- package/page/19-files.cljs +247 -0
- package/page/19-html.cljs +152 -0
- package/page/20-talk.cljs +527 -0
- package/page/21-net.cljs +110 -0
- package/page/22-learn.cljs +303 -0
- package/page/23-ids.cljs +233 -0
- package/page/24-clipboard.cljs +136 -0
- package/page/25-tidy.cljs +74 -0
- package/page/26-probes.cljs +111 -0
- package/sources.mjs +2 -0
|
@@ -0,0 +1,566 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · hiccup: a box whose view is a small HTML document, written as data (issue #146)
|
|
3
|
+
;; Sliders, fields, menus, buttons and text, laid out like a web page, for interfaces that would be slow
|
|
4
|
+
;; to draw with shapes (kairotic.shapes). The view is hiccup in EDN, never HTML text: before it is kept,
|
|
5
|
+
;; and again before it is drawn, it goes through an allowlist of tags, attributes and style properties,
|
|
6
|
+
;; and it is built with the DOM (createElement, textContent), never with innerHTML. It carries no code,
|
|
7
|
+
;; so it is safe in a shared room without (trust-scripts true), like a tree box.
|
|
8
|
+
;;
|
|
9
|
+
;; [:div.row a tag, with classes after dots
|
|
10
|
+
;; [:label "freq " [:input {:type "range" :name "freq" :min 100 :max 1000 :value 440 :out 0}]]
|
|
11
|
+
;; [:span :$freq " Hz"] a hole: the value of freq, kept up to date
|
|
12
|
+
;; [:button {:out 1 :send :bang} "go"]] a click sends :bang out of outlet 1
|
|
13
|
+
;;
|
|
14
|
+
;; :out o what the element gives goes out of outlet o (a slider or a number field a number, a checkbox
|
|
15
|
+
;; 1 or 0, a menu or a radio its value, a text field its text, a button its :send or a bang)
|
|
16
|
+
;; :name n the element shows the value called n, and what it gives becomes that value: a hole :$n
|
|
17
|
+
;; anywhere in the view shows it too. A map {:freq 220} on the inlet sets values, [:freq 220] one,
|
|
18
|
+
;; as a number sets a knob: shown (a slider within its range) and sent on by the elements named so
|
|
19
|
+
;; that have :out; a bang sends again what they hold.
|
|
20
|
+
;; Let go (a slider) or chosen (a menu, a text entered), the value is kept in the box's data,
|
|
21
|
+
;; (values! :bN {:freq 440}), one event of the history, as a knob keeps its value in its text.
|
|
22
|
+
;; :style a map of the allowed properties: {:gap 8 :color :accent} (numbers are pixels, a palette
|
|
23
|
+
;; colour :ink :soft :accent :kairos :bg :grid follows the light or dark theme)
|
|
24
|
+
;;
|
|
25
|
+
;; HTML can be pasted too (in the code window or with (code :bN "<div>…</div>")): it is read with the
|
|
26
|
+
;; browser's DOMParser, turned into hiccup (data-out and data-send become :out and :send, "$freq" in a
|
|
27
|
+
;; text becomes a hole) and goes through the same allowlist. Only the hiccup is kept.
|
|
28
|
+
|
|
29
|
+
(ns kairotic.hiccup
|
|
30
|
+
(:require [clojure.string :as str]
|
|
31
|
+
[clojure.edn :as edn]
|
|
32
|
+
[kairotic.components :as comp]))
|
|
33
|
+
|
|
34
|
+
;; ───────── the allowlist ─────────
|
|
35
|
+
|
|
36
|
+
(def tags #{:div :span :p :label :input :select :option :button :textarea :output
|
|
37
|
+
:h1 :h2 :h3 :h4 :ul :ol :li :br :hr :b :i :em :strong :small :code :pre :sub :sup
|
|
38
|
+
:table :thead :tbody :tr :td :th :fieldset :legend :meter :progress :details :summary})
|
|
39
|
+
|
|
40
|
+
(def void-tags #{:input :br :hr})
|
|
41
|
+
|
|
42
|
+
;; every allowed element: :class :title :name :out :send :style :hidden; and these, by tag
|
|
43
|
+
(def common-attrs #{:class :title :name :out :send :style :hidden})
|
|
44
|
+
(def tag-attrs
|
|
45
|
+
{:input #{:type :min :max :step :value :placeholder :checked :disabled :size :maxlength}
|
|
46
|
+
:select #{:value :disabled}
|
|
47
|
+
:option #{:value :selected :disabled}
|
|
48
|
+
:button #{:disabled}
|
|
49
|
+
:textarea #{:placeholder :rows :cols :value :disabled :maxlength}
|
|
50
|
+
:meter #{:min :max :value :low :high :optimum}
|
|
51
|
+
:progress #{:max :value}
|
|
52
|
+
:td #{:colspan :rowspan}
|
|
53
|
+
:th #{:colspan :rowspan}
|
|
54
|
+
:ol #{:start}
|
|
55
|
+
:details #{:open}})
|
|
56
|
+
|
|
57
|
+
(def flag-attrs #{:checked :disabled :selected :hidden :open})
|
|
58
|
+
(def input-types #{"range" "number" "text" "checkbox" "radio" "color"})
|
|
59
|
+
|
|
60
|
+
;; style properties: no position, no background images, nothing that loads anything or leaves the box
|
|
61
|
+
(def style-props
|
|
62
|
+
#{:color :background-color :border :border-color :border-width :border-style :border-radius
|
|
63
|
+
:font-size :font-weight :font-style :font-family :text-align :text-decoration :line-height :letter-spacing
|
|
64
|
+
:white-space :vertical-align
|
|
65
|
+
:padding :padding-top :padding-right :padding-bottom :padding-left
|
|
66
|
+
:margin :margin-top :margin-right :margin-bottom :margin-left
|
|
67
|
+
:width :height :min-width :max-width :min-height :max-height
|
|
68
|
+
:display :flex :flex-direction :flex-wrap :flex-grow :flex-shrink :flex-basis
|
|
69
|
+
:align-items :align-self :justify-content :gap :row-gap :column-gap
|
|
70
|
+
:grid-template-columns :grid-template-rows :grid-column :grid-row
|
|
71
|
+
:opacity :overflow :accent-color})
|
|
72
|
+
(def unitless #{:opacity :flex :flex-grow :flex-shrink :font-weight :line-height})
|
|
73
|
+
(def palette {:ink "var(--ink)" :soft "var(--ink-soft)" :accent "var(--accent)" :kairos "var(--kairos)"
|
|
74
|
+
:bg "var(--box)" :grid "var(--grid)"})
|
|
75
|
+
|
|
76
|
+
(def max-nodes 400)
|
|
77
|
+
(def max-depth 12)
|
|
78
|
+
(def max-text 2000)
|
|
79
|
+
(def max-attr 200)
|
|
80
|
+
(def max-outlets 16)
|
|
81
|
+
|
|
82
|
+
;; ───────── reading it safely ─────────
|
|
83
|
+
|
|
84
|
+
(defn hole? [x] (and (keyword? x) (str/starts-with? (name x) "$") (> (count (name x)) 1)))
|
|
85
|
+
(defn hole-key [x] (keyword (subs (name x) 1)))
|
|
86
|
+
|
|
87
|
+
(defn value-name
|
|
88
|
+
"A name for a value (:freq, \"freq\"), as a keyword, or nil if it is not one."
|
|
89
|
+
[x]
|
|
90
|
+
(let [s (cond (keyword? x) (name x) (string? x) x :else nil)]
|
|
91
|
+
(when (and s (re-matches #"[A-Za-z][A-Za-z0-9_-]{0,40}" s)) (keyword s))))
|
|
92
|
+
|
|
93
|
+
(defn- css-value
|
|
94
|
+
"A style value written safely, or nil: palette colours, numbers, and short plain words."
|
|
95
|
+
[k v]
|
|
96
|
+
(cond
|
|
97
|
+
(contains? palette v) (palette v)
|
|
98
|
+
(and (number? v) (js/isFinite v)) (if (unitless k) (str v) (str v "px"))
|
|
99
|
+
(string? v) (let [s (str/trim v)]
|
|
100
|
+
(when (and (<= (count s) 80)
|
|
101
|
+
(re-matches #"[-#%.,()a-zA-Z0-9 ]*" s)
|
|
102
|
+
(not (re-find #"(?i)url|expression|image|attr|src|import" s)))
|
|
103
|
+
s))
|
|
104
|
+
(keyword? v) (css-value k (name v))
|
|
105
|
+
:else nil))
|
|
106
|
+
|
|
107
|
+
(defn- clean-style [m]
|
|
108
|
+
(when (map? m)
|
|
109
|
+
(not-empty (into (sorted-map) (for [[k v] m :let [k (keyword k)] :when (style-props k)
|
|
110
|
+
:let [s (css-value k v)] :when (some? s)]
|
|
111
|
+
[k (if (or (contains? palette v) (number? v)) v s)])))))
|
|
112
|
+
|
|
113
|
+
(defn- clean-classes [x]
|
|
114
|
+
(let [words (cond (string? x) (str/split (str/trim x) #"\s+")
|
|
115
|
+
(keyword? x) [(name x)]
|
|
116
|
+
(sequential? x) (map (fn [w] (if (keyword? w) (name w) (str w))) x)
|
|
117
|
+
:else [])]
|
|
118
|
+
(not-empty (str/join " " (filter (fn [w] (re-matches #"[A-Za-z][A-Za-z0-9_-]{0,30}" w)) words)))))
|
|
119
|
+
|
|
120
|
+
(defn- scalar? [v] (or (string? v) (number? v) (boolean? v) (keyword? v)))
|
|
121
|
+
|
|
122
|
+
(defn- clean-send [v]
|
|
123
|
+
(cond (and (string? v) (<= (count v) max-attr)) v
|
|
124
|
+
(and (number? v) (js/isFinite v)) v
|
|
125
|
+
(or (keyword? v) (boolean? v)) v
|
|
126
|
+
(and (vector? v) (<= (count v) 16) (every? (fn [x] (and (scalar? x) (not (and (string? x) (> (count x) max-attr))))) v)) v
|
|
127
|
+
:else nil))
|
|
128
|
+
|
|
129
|
+
(defn- clean-attr [tag k v]
|
|
130
|
+
(case k
|
|
131
|
+
:class (clean-classes v)
|
|
132
|
+
:style (clean-style v)
|
|
133
|
+
:name (value-name v)
|
|
134
|
+
:out (when (and (integer? v) (<= 0 v) (< v max-outlets)) v)
|
|
135
|
+
:send (clean-send v)
|
|
136
|
+
:type (when (contains? input-types (str (if (keyword? v) (name v) v))) (str (if (keyword? v) (name v) v)))
|
|
137
|
+
(cond
|
|
138
|
+
(flag-attrs k) (when (or (true? v) (hole? v) (= v "") (= v (name k))) (if (hole? v) v true))
|
|
139
|
+
(hole? v) v
|
|
140
|
+
(and (number? v) (js/isFinite v)) v
|
|
141
|
+
(and (string? v) (<= (count v) max-attr)) v
|
|
142
|
+
(keyword? v) (name v)
|
|
143
|
+
:else nil)))
|
|
144
|
+
|
|
145
|
+
(defn- clean-attrs [tag m classes]
|
|
146
|
+
(let [allowed (into common-attrs (get tag-attrs tag))
|
|
147
|
+
m (if (map? m) m {})
|
|
148
|
+
m (if (seq classes) (update m :class (fn [c] (str/join " " (remove str/blank? (concat classes [(clean-classes c)]))))) m)]
|
|
149
|
+
(into (sorted-map)
|
|
150
|
+
(for [[k v] m :let [k (keyword k)
|
|
151
|
+
;; pasted HTML spells them data-out and data-send
|
|
152
|
+
k (get {:data-out :out :data-send :send :data-name :name} k k)]
|
|
153
|
+
:when (allowed k)
|
|
154
|
+
:let [c (clean-attr tag k v)] :when (some? c)]
|
|
155
|
+
[k c]))))
|
|
156
|
+
|
|
157
|
+
(defn- split-tag
|
|
158
|
+
"A tag keyword with classes after dots (:div.row.big) → [:div [\"row\" \"big\"]]."
|
|
159
|
+
[kw]
|
|
160
|
+
(let [[t & cs] (str/split (name kw) #"\.")]
|
|
161
|
+
[(keyword (str/lower-case t)) (vec (remove str/blank? cs))]))
|
|
162
|
+
|
|
163
|
+
(defn sanitize
|
|
164
|
+
"Hiccup made safe: what is not allowed is dropped (a tag with everything inside it, an attribute, a
|
|
165
|
+
style property). Returns {:view hiccup-or-nil :dropped [\"…\" …]}."
|
|
166
|
+
[x]
|
|
167
|
+
(let [budget (atom max-nodes) dropped (atom [])
|
|
168
|
+
drop! (fn [what] (when (< (count @dropped) 12) (swap! dropped conj what)) nil)
|
|
169
|
+
node (fn node [x depth]
|
|
170
|
+
(cond
|
|
171
|
+
(<= @budget 0) (drop! "more than 400 elements")
|
|
172
|
+
(> depth max-depth) (drop! "more than 12 levels deep")
|
|
173
|
+
(string? x) (subs x 0 (min (count x) max-text))
|
|
174
|
+
(and (number? x) (js/isFinite x)) (str x)
|
|
175
|
+
(hole? x) x
|
|
176
|
+
(and (vector? x) (keyword? (first x)))
|
|
177
|
+
(let [[tag classes] (split-tag (first x))
|
|
178
|
+
[attrs kids] (if (map? (second x)) [(second x) (drop 2 x)] [{} (rest x)])]
|
|
179
|
+
(if-not (tags tag)
|
|
180
|
+
(drop! (str "<" (name tag) ">"))
|
|
181
|
+
(do (swap! budget dec)
|
|
182
|
+
(doseq [k (keys attrs) :let [k (keyword k) k (get {:data-out :out :data-send :send :data-name :name} k k)]
|
|
183
|
+
:when (not (contains? (into common-attrs (get tag-attrs tag)) k))]
|
|
184
|
+
(drop! (str (name tag) " " (name k))))
|
|
185
|
+
(let [a (clean-attrs tag attrs classes)
|
|
186
|
+
cs (if (void-tags tag) [] (vec (keep (fn [c] (node c (inc depth))) (mapcat (fn [c] (if (and (sequential? c) (not (and (vector? c) (keyword? (first c))))) c [c])) kids))))]
|
|
187
|
+
(into (if (seq a) [tag a] [tag]) cs)))))
|
|
188
|
+
(nil? x) nil
|
|
189
|
+
:else (drop! (str "a " (cond (map? x) "map" (sequential? x) "list" :else (str (type x))) " outside an element"))))]
|
|
190
|
+
{:view (let [v (node x 0)] (when (vector? v) v)) :dropped @dropped}))
|
|
191
|
+
|
|
192
|
+
(defn clean
|
|
193
|
+
"The safe form of a view, or nil."
|
|
194
|
+
[x]
|
|
195
|
+
(:view (sanitize x)))
|
|
196
|
+
|
|
197
|
+
;; ───────── HTML → hiccup (in the browser) ─────────
|
|
198
|
+
|
|
199
|
+
(defn- text-parts [s]
|
|
200
|
+
(vec (remove nil? (let [ps (str/split s #"\$[A-Za-z][A-Za-z0-9_-]*" -1)
|
|
201
|
+
hs (mapv (fn [m] (keyword m)) (re-seq #"\$[A-Za-z][A-Za-z0-9_-]*" s))]
|
|
202
|
+
(concat (mapcat (fn [i p] (cond-> (if (= p "") [] [p]) (< i (count hs)) (conj (nth hs i))))
|
|
203
|
+
(range) ps))))))
|
|
204
|
+
|
|
205
|
+
(defn- read-scalar
|
|
206
|
+
"An attribute of pasted HTML: a number, a :keyword, a $hole, or the text."
|
|
207
|
+
[s]
|
|
208
|
+
(cond (re-matches #"-?\d+(\.\d+)?" s) (js/parseFloat s)
|
|
209
|
+
(re-matches #":[A-Za-z][A-Za-z0-9_-]*" s) (keyword (subs s 1))
|
|
210
|
+
(re-matches #"\$[A-Za-z][A-Za-z0-9_-]*" s) (keyword s)
|
|
211
|
+
:else s))
|
|
212
|
+
|
|
213
|
+
(defn- parse-style [s]
|
|
214
|
+
(into {} (for [decl (str/split (str s) #";")
|
|
215
|
+
:let [[k v] (map str/trim (str/split decl #":" 2))]
|
|
216
|
+
:when (and (seq k) (seq v))]
|
|
217
|
+
[(keyword (str/lower-case k)) (if (re-matches #"-?\d+(\.\d+)?px" v) (js/parseFloat v) v)])))
|
|
218
|
+
|
|
219
|
+
(defn- dom->hiccup [n]
|
|
220
|
+
(case (.-nodeType n)
|
|
221
|
+
3 (let [t (.-textContent n)] (when-not (str/blank? t) (text-parts (str/replace t #"\s+" " "))))
|
|
222
|
+
1 (let [tag (keyword (str/lower-case (.-tagName n)))
|
|
223
|
+
attrs (into {} (for [a (array-seq (.-attributes n))
|
|
224
|
+
:let [k (keyword (str/lower-case (.-name a))) v (.-value a)]]
|
|
225
|
+
[k (case k
|
|
226
|
+
:style (parse-style v)
|
|
227
|
+
(:checked :disabled :selected :hidden :open) true
|
|
228
|
+
(:class :title :placeholder :type) v
|
|
229
|
+
(read-scalar v))]))
|
|
230
|
+
kids (vec (mapcat (fn [c] (let [h (dom->hiccup c)]
|
|
231
|
+
(cond (nil? h) [] (and (vector? h) (keyword? (first h))) [h] :else h)))
|
|
232
|
+
(array-seq (.-childNodes n))))]
|
|
233
|
+
(into (if (seq attrs) [tag attrs] [tag]) kids))
|
|
234
|
+
nil))
|
|
235
|
+
|
|
236
|
+
(defn html->hiccup
|
|
237
|
+
"HTML text → hiccup (not yet made safe: `sanitize` does that). Several elements are wrapped in a div."
|
|
238
|
+
[text]
|
|
239
|
+
(let [doc (.parseFromString (js/DOMParser.) (str text) "text/html")
|
|
240
|
+
kids (vec (mapcat (fn [c] (let [h (dom->hiccup c)]
|
|
241
|
+
(cond (nil? h) [] (and (vector? h) (keyword? (first h))) [h] :else h)))
|
|
242
|
+
(array-seq (.. doc -body -childNodes))))]
|
|
243
|
+
(if (and (= 1 (count kids)) (vector? (first kids))) (first kids) (into [:div] kids))))
|
|
244
|
+
|
|
245
|
+
(defn read-view
|
|
246
|
+
"The text of a view (EDN hiccup, or HTML when it starts with <) → safe hiccup. Throws with a reason."
|
|
247
|
+
[text]
|
|
248
|
+
(let [s (str/trim (str text))
|
|
249
|
+
raw (cond (str/blank? s) (throw (js/Error. "the view is empty: [:div …]"))
|
|
250
|
+
(str/starts-with? s "<") (if (some? (.-DOMParser js/globalThis))
|
|
251
|
+
(html->hiccup s)
|
|
252
|
+
(throw (js/Error. "HTML can only be read in a browser")))
|
|
253
|
+
:else (try (edn/read-string s)
|
|
254
|
+
(catch :default e (throw (js/Error. (str "That is not EDN: " (.-message e)))))))
|
|
255
|
+
{:keys [view]} (when (and (vector? raw) (keyword? (first raw))) (sanitize raw))]
|
|
256
|
+
(if view view (throw (js/Error. "a view is an element of hiccup: [:div …] (or HTML)")))))
|
|
257
|
+
|
|
258
|
+
;; ───────── writing it ─────────
|
|
259
|
+
|
|
260
|
+
(defn- element? [x] (and (vector? x) (keyword? (first x))))
|
|
261
|
+
|
|
262
|
+
(defn pp
|
|
263
|
+
"The view as text, one element per line where it holds other elements."
|
|
264
|
+
([h] (pp h 0))
|
|
265
|
+
([h indent]
|
|
266
|
+
(let [[tag & more] h
|
|
267
|
+
[attrs kids] (if (map? (first more)) [(first more) (rest more)] [nil more])
|
|
268
|
+
head (str "[" (pr-str tag) (when (seq attrs) (str " " (pr-str attrs))))]
|
|
269
|
+
(if (some element? kids)
|
|
270
|
+
(let [pad (apply str (repeat (inc indent) " "))]
|
|
271
|
+
(str head (apply str (map (fn [k] (str "\n" pad (if (element? k) (pp k (inc indent)) (pr-str k)))) kids)) "]"))
|
|
272
|
+
(str head (apply str (map (fn [k] (str " " (pr-str k))) kids)) "]")))))
|
|
273
|
+
|
|
274
|
+
(defn outlets
|
|
275
|
+
"How many outlets a view asks for: one more than its highest :out (at least one)."
|
|
276
|
+
[h]
|
|
277
|
+
(let [os (keep (fn [x] (when (map? x) (:out x))) (tree-seq sequential? seq h))]
|
|
278
|
+
(if (seq os) (inc (apply max os)) 1)))
|
|
279
|
+
|
|
280
|
+
;; ───────── the box ─────────
|
|
281
|
+
|
|
282
|
+
(def default-view
|
|
283
|
+
[:div {:class "col"}
|
|
284
|
+
[:label "freq " [:input {:type "range" :name "freq" :min 100 :max 1000 :step 1 :value 440 :out 0}]]
|
|
285
|
+
[:div {:class "row"} [:output :$freq " Hz"] [:button {:out 1 :send :bang} "go"]]])
|
|
286
|
+
|
|
287
|
+
(defn view-of
|
|
288
|
+
"The (safe) view a box shows: what it keeps, made safe again (it may come from a room or a link)."
|
|
289
|
+
[data]
|
|
290
|
+
(or (clean (:view data)) default-view))
|
|
291
|
+
|
|
292
|
+
;; The values its named elements hold are kept in its data, as a knob keeps its value in its text: when a
|
|
293
|
+
;; slider is let go, a menu chosen, a text entered, (values! :bN {:freq 440}) writes them as one event of the
|
|
294
|
+
;; history, so they travel with the work and going back in the history brings back the ones it had.
|
|
295
|
+
|
|
296
|
+
(def max-values 64)
|
|
297
|
+
|
|
298
|
+
(defn- value-ok? [v]
|
|
299
|
+
(or (nil? v) (boolean? v) (keyword? v)
|
|
300
|
+
(and (number? v) (js/isFinite v))
|
|
301
|
+
(and (string? v) (<= (count v) max-text))))
|
|
302
|
+
|
|
303
|
+
(defn clean-values
|
|
304
|
+
"The values of a map that can be kept: named by a word, scalars (a text up to 2000 characters), 64 at most."
|
|
305
|
+
[m]
|
|
306
|
+
(if (map? m)
|
|
307
|
+
(into (sorted-map) (take max-values (for [[k v] m :let [k (value-name k)] :when (and k (value-ok? v))] [k v])))
|
|
308
|
+
(sorted-map)))
|
|
309
|
+
|
|
310
|
+
(defn- with-values [d vs] (if (seq vs) (assoc d :values vs) (dissoc d :values)))
|
|
311
|
+
|
|
312
|
+
(def model
|
|
313
|
+
{:empty {:view default-view}
|
|
314
|
+
:step (fn step [d [op x]]
|
|
315
|
+
(case op
|
|
316
|
+
:set (if-let [v (clean x)] (assoc d :view v) d)
|
|
317
|
+
;; the values its elements hold, merged into the ones it keeps (nil forgets one)
|
|
318
|
+
:values (let [vs (clean-values x)
|
|
319
|
+
kept (reduce (fn [acc [k v]] (if (nil? v) (dissoc acc k) (assoc acc k v)))
|
|
320
|
+
(clean-values (:values d)) vs)]
|
|
321
|
+
(with-values d kept))
|
|
322
|
+
;; what :diff returns arrives as one [:batch [ev …]]
|
|
323
|
+
:batch (if (sequential? x) (reduce step d (filter vector? x)) d)
|
|
324
|
+
d))
|
|
325
|
+
:load (fn [x] (with-values {:view (or (and (map? x) (clean (:view x))) default-view)}
|
|
326
|
+
(when (map? x) (into (sorted-map) (remove (fn [[_ v]] (nil? v)) (clean-values (:values x)))))))
|
|
327
|
+
:code (fn [id d] (cond-> [(str "(code " id " " (pr-str (pp (view-of d))) ")")]
|
|
328
|
+
(seq (:values d)) (conj (str "(values! " id " " (pr-str (clean-values (:values d))) ")"))))
|
|
329
|
+
:source (fn [d] (pp (view-of d)))
|
|
330
|
+
:read (fn [_ text] {:view (read-view text)})
|
|
331
|
+
:diff (fn [d d2] (if (= (view-of d) (:view d2)) [] [[:set (:view d2)]]))})
|
|
332
|
+
|
|
333
|
+
(defn current-values
|
|
334
|
+
"The values a box shows: what its elements did since its data last changed, or else what its data keeps
|
|
335
|
+
(the page forgets what they did when the data changes: going back in the history, someone else's change)."
|
|
336
|
+
[data state]
|
|
337
|
+
(let [kept (clean-values (:values data))]
|
|
338
|
+
(if (map? (:values state)) (merge kept (:values state)) kept)))
|
|
339
|
+
|
|
340
|
+
(defn defaults-of
|
|
341
|
+
"The value each named element of a view starts with (its own :value)."
|
|
342
|
+
[h]
|
|
343
|
+
(reduce (fn [acc x]
|
|
344
|
+
(let [a (when (element? x) (second x))]
|
|
345
|
+
(if (and (map? a) (:name a) (some? (:value a)) (not (hole? (:value a))) (not (contains? acc (:name a))))
|
|
346
|
+
(assoc acc (:name a) (:value a))
|
|
347
|
+
acc)))
|
|
348
|
+
{} (tree-seq sequential? seq h)))
|
|
349
|
+
|
|
350
|
+
(defn- set-values [vals m]
|
|
351
|
+
(reduce (fn [acc [k v]] (if-let [k (value-name k)] (if (value-ok? v) (assoc acc k v) acc) acc)) vals m))
|
|
352
|
+
|
|
353
|
+
(defn ui-message?
|
|
354
|
+
"What an element of the view sends its own box: [:ui outlet value name] (outlet or name may be nil)."
|
|
355
|
+
[v]
|
|
356
|
+
(and (vector? v) (= 4 (count v)) (= :ui (first v))))
|
|
357
|
+
|
|
358
|
+
(defn named-outlets
|
|
359
|
+
"The named elements of a view that send out of an outlet: name → [{:out o :min lo :max hi} …] (a slider's or a
|
|
360
|
+
number field's own range, to keep a value within it)."
|
|
361
|
+
[h]
|
|
362
|
+
(reduce (fn [acc x]
|
|
363
|
+
(if (and (element? x) (map? (second x)) (:name (second x)) (some? (:out (second x))))
|
|
364
|
+
(let [a (second x) num? (and (= :input (first x)) (#{"range" "number"} (:type a)))]
|
|
365
|
+
(update acc (:name a) (fnil conj [])
|
|
366
|
+
{:out (:out a) :min (when (and num? (number? (:min a))) (:min a))
|
|
367
|
+
:max (when (and num? (number? (:max a))) (:max a))}))
|
|
368
|
+
acc))
|
|
369
|
+
{} (tree-seq sequential? seq h)))
|
|
370
|
+
|
|
371
|
+
(defn- within [v {:keys [min max]}]
|
|
372
|
+
(if (number? v) (cond-> v (number? min) (js/Math.max min) (number? max) (js/Math.min max)) v))
|
|
373
|
+
|
|
374
|
+
(defn- sends-of
|
|
375
|
+
"What values that arrive on the inlet send on, as a knob does: each one out of the outlets of the elements
|
|
376
|
+
named so (within their range), outlets in order."
|
|
377
|
+
[view vals ks]
|
|
378
|
+
(let [no (named-outlets view)]
|
|
379
|
+
(vec (sort-by first (for [k ks :let [v (get vals k)] :when (some? v) t (get no k)] [(:out t) (within v t)])))))
|
|
380
|
+
|
|
381
|
+
(defn receive [{:keys [value state data]}]
|
|
382
|
+
(let [vals (current-values data state)
|
|
383
|
+
view (view-of data)
|
|
384
|
+
st (fn [vs] (assoc (or state {}) :values vs))
|
|
385
|
+
;; values from the inlet: set (a slider within its range), shown, and sent on (a knob does the same)
|
|
386
|
+
arrive (fn [m]
|
|
387
|
+
(let [no (named-outlets view)
|
|
388
|
+
m (into {} (for [[k v] (set-values {} m)] [k (if-let [t (first (get no k))] (within v t) v)]))
|
|
389
|
+
vals (merge vals m)]
|
|
390
|
+
{:state (st vals) :show vals :out (sends-of view vals (keys m))}))]
|
|
391
|
+
(cond
|
|
392
|
+
(ui-message? value)
|
|
393
|
+
(let [[_ o v nm] value
|
|
394
|
+
vals (if-let [k (value-name nm)] (if (value-ok? v) (assoc vals k v) vals) vals)]
|
|
395
|
+
{:state (st vals) :show vals
|
|
396
|
+
:out (if (and (integer? o) (<= 0 o) (< o max-outlets)) [[o v]] [])})
|
|
397
|
+
(map? value) (arrive value)
|
|
398
|
+
(and (vector? value) (= 2 (count value)) (value-name (first value))) (arrive [value])
|
|
399
|
+
;; a bang sends again what every named element holds
|
|
400
|
+
(= value :bang) {:show vals :out (sends-of view (merge (defaults-of view) vals) (keys (named-outlets view)))}
|
|
401
|
+
;; anything else (a number, a word, a list: the futuribles of a box) is the value :in, shown by :$in
|
|
402
|
+
(some? value) (let [vals (assoc vals :in value)] {:state (st vals) :show vals})
|
|
403
|
+
:else {:show vals})))
|
|
404
|
+
|
|
405
|
+
(comp/define! "html"
|
|
406
|
+
{:doc "A small interface written as hiccup (HTML as EDN data): sliders, fields, menus, buttons and text, laid out like a page. An element with :out sends what it gives out of that outlet; one with :name keeps it as a value, shown by every hole :$name in the view, and when it is let go (a slider) or chosen (a menu, a text entered) the box keeps it in the work, as a knob keeps its value: (values! :bN {:freq 440}). A list of maps in a hole is shown as a table (a row each, a column for each key: the futuribles of comes or cancrizans), and anything else on the inlet that is not a map, a value or a bang (a number, a list) is the value :in, shown by :$in. A map on the inlet ({:freq 220}) sets values, as a number sets a knob: the elements named so show it (a slider within its range) and those with :out send it on; a bang sends again what they hold. What is not allowed (scripts, links, images, positions, unknown tags or attributes) is dropped, so it is safe in a shared room. Its view opens as text from { } (HTML can be pasted there too)."
|
|
407
|
+
:usage "html"
|
|
408
|
+
:license "MPL-2.0"
|
|
409
|
+
:display :html
|
|
410
|
+
:model model
|
|
411
|
+
:dynamic-io true
|
|
412
|
+
:io (fn [box] [1 (outlets (view-of (:data box)))])
|
|
413
|
+
:receive receive
|
|
414
|
+
:help {:inlets ["a map of values {:freq 220}, or one [:freq 220]: the elements named so and the holes :$freq show them, and the named elements with :out send them on (a slider within its range); a bang sends again what they hold"]
|
|
415
|
+
:outlets ["what each element with :out 0 gives" "… :out 1, and so on: as many outlets as the view asks for"]
|
|
416
|
+
:messages [["{:freq 220}" "sets values: the elements named so show them and send them on"] ["[:freq 220]" "sets one value"] ["bang" "sends again what every named element with :out holds"] ["[{:name \"D4\" :futures 12} …]" "anything else is the value :in (:$in); a list of maps is shown as a table"]]
|
|
417
|
+
:example "(box :b1 \"html\" 40 30)\n(box :b2 \"print\" 40 230)\n(box :b3 \"print\" 200 230)\n(connect :b1 0 :b2 0)\n(connect :b1 1 :b3 0)"
|
|
418
|
+
:try "Move the slider: its value goes out of the left outlet and shows beside it; let it go and the box keeps it, as a knob does (back in the history, it moves back). { } in the header opens the view as text: change it and apply."
|
|
419
|
+
:see ["number" "knob" "msg"]}})
|
|
420
|
+
|
|
421
|
+
;; ───────── drawing it (in the browser) ─────────
|
|
422
|
+
;; build returns {:node el :live [[el kind key default] …] :binds [{:out :send :name :type} …] :defaults {name value}}:
|
|
423
|
+
;; the holes and the named elements to update when the values change, what each element with :out or :name
|
|
424
|
+
;; sends, and the value each named element starts with (its own :value).
|
|
425
|
+
|
|
426
|
+
(def base-css
|
|
427
|
+
(str ":host{display:block;width:100%;height:100%;overflow:auto;box-sizing:border-box;padding:6px 8px;"
|
|
428
|
+
"font:12px/1.4 var(--f-ui, sans-serif);color:var(--ink);user-select:text;-webkit-user-select:text;touch-action:manipulation;}"
|
|
429
|
+
"*{box-sizing:border-box;}"
|
|
430
|
+
".row{display:flex;flex-direction:row;flex-wrap:wrap;align-items:center;gap:6px;}"
|
|
431
|
+
".col{display:flex;flex-direction:column;gap:6px;}"
|
|
432
|
+
".grid{display:grid;grid-template-columns:auto 1fr;gap:4px 8px;align-items:center;}"
|
|
433
|
+
".soft{color:var(--ink-soft);}.accent{color:var(--accent);}.kairos{color:var(--kairos);}"
|
|
434
|
+
"h1,h2,h3,h4{font-family:var(--f-head, sans-serif);margin:0 0 4px;font-weight:600;}"
|
|
435
|
+
"h1{font-size:18px;}h2{font-size:15px;}h3{font-size:13px;}h4{font-size:12px;}"
|
|
436
|
+
"p{margin:0 0 4px;}ul,ol{margin:0;padding-left:18px;}code,pre{font-family:var(--f-code, monospace);font-size:11px;}"
|
|
437
|
+
"input,select,textarea,button{font:inherit;color:var(--ink);accent-color:var(--accent);}"
|
|
438
|
+
"input[type=text],input[type=number],select,textarea{background:var(--box);border:1px solid var(--rule);border-radius:3px;padding:2px 4px;}"
|
|
439
|
+
"input[type=range]{vertical-align:middle;}"
|
|
440
|
+
"button{background:color-mix(in srgb, var(--accent) 12%, var(--box));border:1px solid var(--rule);border-radius:3px;padding:2px 10px;cursor:pointer;}"
|
|
441
|
+
"button:active{background:var(--accent-soft);}"
|
|
442
|
+
"table{border-collapse:collapse;}td,th{padding:2px 6px;border-bottom:1px solid var(--grid);text-align:left;}"
|
|
443
|
+
"output{font-family:var(--f-code, monospace);}"))
|
|
444
|
+
|
|
445
|
+
(defn shown-str [v]
|
|
446
|
+
(cond (nil? v) "" (string? v) v (keyword? v) (name v)
|
|
447
|
+
(number? v) (str (if (integer? v) v (/ (js/Math.round (* v 1000)) 1000))) :else (pr-str v)))
|
|
448
|
+
|
|
449
|
+
(defn- set-prop!
|
|
450
|
+
"An attribute (or the property that shows it) on el."
|
|
451
|
+
[el tag k v]
|
|
452
|
+
(cond
|
|
453
|
+
(flag-attrs k) (let [on (boolean (if (number? v) (pos? v) v))]
|
|
454
|
+
(case k
|
|
455
|
+
:checked (set! (.-checked el) on)
|
|
456
|
+
:selected (set! (.-selected el) on)
|
|
457
|
+
(if on (.setAttribute el (name k) "") (.removeAttribute el (name k)))))
|
|
458
|
+
(and (= k :value) (#{:input :textarea :select :meter :progress} tag)) (set! (.-value el) (shown-str v))
|
|
459
|
+
:else (.setAttribute el (name k) (shown-str v))))
|
|
460
|
+
|
|
461
|
+
(defn build
|
|
462
|
+
"The DOM of a safe view, with the values in `vals` (a map keyword → value)."
|
|
463
|
+
[h vals]
|
|
464
|
+
(let [live (atom []) binds (atom []) defaults (atom {})
|
|
465
|
+
mk (fn mk [x]
|
|
466
|
+
(cond
|
|
467
|
+
(string? x) (js/document.createTextNode x)
|
|
468
|
+
(hole? x) (let [t (js/document.createTextNode (shown-str (get vals (hole-key x))))]
|
|
469
|
+
(swap! live conj [t :text (hole-key x) nil]) t)
|
|
470
|
+
(element? x)
|
|
471
|
+
(let [[tag & more] x
|
|
472
|
+
[attrs kids] (if (map? (first more)) [(first more) (rest more)] [{} more])
|
|
473
|
+
el (js/document.createElement (name tag))
|
|
474
|
+
nm (:name attrs)]
|
|
475
|
+
;; its type first and its value last (a slider's value is kept within its min and max)
|
|
476
|
+
(doseq [[k v] (sort-by (fn [[k]] (case k :type 0 :value 2 1)) attrs)
|
|
477
|
+
:when (not (#{:out :send :name :style} k))]
|
|
478
|
+
(if (hole? v)
|
|
479
|
+
(do (set-prop! el tag k (get vals (hole-key v))) (swap! live conj [el [:attr tag k] (hole-key v) nil]))
|
|
480
|
+
(set-prop! el tag k v)))
|
|
481
|
+
(when-let [st (:style attrs)]
|
|
482
|
+
(doseq [[k v] st :let [s (css-value k v)] :when s] (.setProperty (.-style el) (name k) s)))
|
|
483
|
+
(when nm (.setAttribute el "name" (name nm)))
|
|
484
|
+
(when (or (some? (:out attrs)) nm)
|
|
485
|
+
(set! (.. el -dataset -k) (str (count @binds)))
|
|
486
|
+
(swap! binds conj {:out (:out attrs) :send (:send attrs) :name nm :tag tag :type (:type attrs)}))
|
|
487
|
+
;; a named element shows the value of its name (its own :value until it has one)
|
|
488
|
+
(when (and nm (#{:input :select :textarea :meter :progress :output} tag))
|
|
489
|
+
(when (and (some? (:value attrs)) (not (hole? (:value attrs))) (not (contains? @defaults nm)))
|
|
490
|
+
(swap! defaults assoc nm (:value attrs)))
|
|
491
|
+
(swap! live conj [el [:control tag (:type attrs)] nm (:value attrs)]))
|
|
492
|
+
(doseq [k kids :let [c (mk k)] :when c] (.appendChild el c))
|
|
493
|
+
;; a menu chooses its value once its options are in it
|
|
494
|
+
(when (and (= tag :select) (some? (:value attrs)) (not (hole? (:value attrs))))
|
|
495
|
+
(set! (.-value el) (shown-str (:value attrs))))
|
|
496
|
+
el)
|
|
497
|
+
:else nil))
|
|
498
|
+
node (mk h)]
|
|
499
|
+
{:node node :live @live :binds @binds :defaults @defaults}))
|
|
500
|
+
|
|
501
|
+
(defn- number-or [s]
|
|
502
|
+
(let [n (js/parseFloat s)] (if (and (js/isFinite n) (re-matches #"\s*-?\d*\.?\d+(e-?\d+)?\s*" (str s))) n s)))
|
|
503
|
+
|
|
504
|
+
(defn rows?
|
|
505
|
+
"Is v a list of maps (shown as a table: a row each, a column for each key)?"
|
|
506
|
+
[v]
|
|
507
|
+
(and (sequential? v) (seq v) (every? map? v)))
|
|
508
|
+
|
|
509
|
+
(defn- table-of
|
|
510
|
+
"A table for a list of maps: the keys of the first in order, then any others, as the head."
|
|
511
|
+
[v]
|
|
512
|
+
(let [ks (distinct (mapcat keys v))
|
|
513
|
+
tbl (js/document.createElement "table")
|
|
514
|
+
tr (fn [cells tag] (let [r (js/document.createElement "tr")]
|
|
515
|
+
(doseq [c cells] (let [e (js/document.createElement tag)] (set! (.-textContent e) c) (.appendChild r e)))
|
|
516
|
+
r))]
|
|
517
|
+
(.appendChild tbl (tr (map name ks) "th"))
|
|
518
|
+
(doseq [m v] (.appendChild tbl (tr (map #(shown-str (get m %)) ks) "td")))
|
|
519
|
+
tbl))
|
|
520
|
+
|
|
521
|
+
(defn- show-hole!
|
|
522
|
+
"A hole (a text node) showing v: as text, or a list of maps as a table beside it."
|
|
523
|
+
[el v]
|
|
524
|
+
(let [old (aget el "kTable")]
|
|
525
|
+
(if (rows? v)
|
|
526
|
+
(let [t (table-of v)]
|
|
527
|
+
(when (not= "" (.-textContent el)) (set! (.-textContent el) ""))
|
|
528
|
+
(if (and old (.-parentNode old)) (.replaceWith old t) (when-let [p (.-parentNode el)] (.insertBefore p t (.-nextSibling el))))
|
|
529
|
+
(aset el "kTable" t))
|
|
530
|
+
(let [s (if (and (sequential? v) (empty? v)) "" (shown-str v))]
|
|
531
|
+
(when old (.remove old) (aset el "kTable" nil))
|
|
532
|
+
(when (not= s (.-textContent el)) (set! (.-textContent el) s))))))
|
|
533
|
+
|
|
534
|
+
(defn refresh!
|
|
535
|
+
"Shows the values `vals` in a built view; `skip` is an element being used (its value is not touched)."
|
|
536
|
+
[{:keys [live]} vals skip]
|
|
537
|
+
(doseq [[el kind k dflt] live :let [has? (contains? vals k) v (get vals k)]]
|
|
538
|
+
(cond
|
|
539
|
+
(= kind :text) (show-hole! el v)
|
|
540
|
+
(= (first kind) :attr) (let [[_ tag a] kind] (set-prop! el tag a v))
|
|
541
|
+
(= (first kind) :control)
|
|
542
|
+
(when (and has? (not (identical? el skip)))
|
|
543
|
+
(let [[_ tag typ] kind]
|
|
544
|
+
(cond
|
|
545
|
+
(= typ "checkbox") (set! (.-checked el) (boolean (if (number? v) (pos? v) v)))
|
|
546
|
+
(= typ "radio") (set! (.-checked el) (= (shown-str v) (.-value el)))
|
|
547
|
+
(= tag :output) (set! (.-textContent el) (shown-str v))
|
|
548
|
+
:else (let [s (shown-str v)] (when (not= s (.-value el)) (set! (.-value el) s)))))))))
|
|
549
|
+
|
|
550
|
+
(defn element-value
|
|
551
|
+
"What an element with :out or :name gives when it is used: [value commit?] for this event, or nil."
|
|
552
|
+
[el bind ev-type]
|
|
553
|
+
(let [{:keys [tag type send]} bind]
|
|
554
|
+
(case tag
|
|
555
|
+
:button (when (= ev-type "click") [(if (some? send) send :bang) true])
|
|
556
|
+
:input (case type
|
|
557
|
+
"checkbox" (when (= ev-type "change") [(if (some? send) (if (.-checked el) send 0) (if (.-checked el) 1 0)) true])
|
|
558
|
+
"radio" (when (and (= ev-type "change") (.-checked el)) [(number-or (.-value el)) true])
|
|
559
|
+
("range" "number") (let [n (js/parseFloat (.-value el))]
|
|
560
|
+
(when (js/isFinite n) (case ev-type "input" [n false] "change" [n true] nil)))
|
|
561
|
+
"color" (case ev-type "input" [(.-value el) false] "change" [(.-value el) true] nil)
|
|
562
|
+
;; text: on Enter or when it is left
|
|
563
|
+
(when (= ev-type "change") [(.-value el) true]))
|
|
564
|
+
:select (when (= ev-type "change") [(number-or (.-value el)) true])
|
|
565
|
+
:textarea (when (= ev-type "change") [(.-value el) true])
|
|
566
|
+
nil)))
|