@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,247 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · the page: a work as a file (discussion #149)
|
|
3
|
+
;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
|
|
4
|
+
;;
|
|
5
|
+
;; A .kairotic is a zip (kairotic/kfile.mjs) holding a whole work, in EDN:
|
|
6
|
+
;; manifest.edn {:format 1 :kairotic "0.2.0-alpha" :made <ms> :kind :history | :release :anonymous bool}
|
|
7
|
+
;; work.edn {:name … :simultaneity-ms … :origin …}
|
|
8
|
+
;; lines.edn {"main" {:name "main"} "<id>" {:name "sketch" :created … :archived false :aliases […]}}
|
|
9
|
+
;; history.edn every segment of every line, as the history keeps them
|
|
10
|
+
;; people.edn {author {:name :color}} (not when anonymous)
|
|
11
|
+
;; sounds/<hash>.wav the sounds recorded, 32-bit float (samples from URLs stay references)
|
|
12
|
+
;; Opening one makes a new work (solo, or local without a database); the file and any other work are not
|
|
13
|
+
;; touched. Exported as a release, it holds one line with a compact history (as a link or a release has).
|
|
14
|
+
|
|
15
|
+
(in-ns 'user)
|
|
16
|
+
|
|
17
|
+
(def kfile-format 1)
|
|
18
|
+
|
|
19
|
+
(defn kfile-lib "Promise of kairotic/kfile.mjs." [] (.kairoticImport js/window "kairotic/kfile.mjs"))
|
|
20
|
+
|
|
21
|
+
;; ───────────────────────── what a work is, as data ─────────────────────────
|
|
22
|
+
|
|
23
|
+
(defn all-segments
|
|
24
|
+
"Every segment of the work: those of the line on screen and of every other line."
|
|
25
|
+
[] (vec (concat (vals (:nodes @hist)) (mapcat vals (vals (:other @hist))))))
|
|
26
|
+
|
|
27
|
+
(defn recorded-sounds
|
|
28
|
+
"The keys of the sounds recorded that the events use (those from a URL stay references)."
|
|
29
|
+
[segs]
|
|
30
|
+
(set (for [x (tree-seq coll? seq (mapv :events segs))
|
|
31
|
+
:when (and (map? x) (string? (:audio x)) (not (snd/url? (:url x))))]
|
|
32
|
+
(:audio x))))
|
|
33
|
+
|
|
34
|
+
(defn anonymize
|
|
35
|
+
"The segments with every author and session made a plain name of its own (p1, p2… s1, s2…)."
|
|
36
|
+
[segs]
|
|
37
|
+
(let [ids (fn [k pre] (into {} (map-indexed (fn [i a] [a (str pre (inc i))]) (distinct (keep k segs)))))
|
|
38
|
+
as (ids :author "p") ss (ids :session "s")]
|
|
39
|
+
(mapv (fn [n] (cond-> (assoc n :author (get as (:author n) "p0"))
|
|
40
|
+
(:session n) (assoc :session (get ss (:session n)))))
|
|
41
|
+
segs)))
|
|
42
|
+
|
|
43
|
+
(defn work-data
|
|
44
|
+
"The work as the file keeps it: {:manifest :work :lines :history :people}."
|
|
45
|
+
[{:keys [release anonymous]}]
|
|
46
|
+
(let [now (js/Date.now)
|
|
47
|
+
segs (if release
|
|
48
|
+
[{:id "release" :parent nil :at 0 :author (my-author) :session session-id :t now :merge nil
|
|
49
|
+
:events (release-events)}]
|
|
50
|
+
(all-segments))
|
|
51
|
+
segs (mapv (fn [n] (select-keys n [:id :parent :at :author :session :t :merge :events :line])) segs)
|
|
52
|
+
segs (if anonymous (anonymize segs) segs)
|
|
53
|
+
idx (:index @line-state)]
|
|
54
|
+
{:manifest {:format kfile-format :kairotic version :made now :kind (if release :release :history) :anonymous (boolean anonymous)}
|
|
55
|
+
:work (cond-> {:name (or (:name @work) "Untitled") :simultaneity-ms (:simul-ms @settings)}
|
|
56
|
+
(and (not anonymous) (:kind @work)) (assoc :origin (str (name (:kind @work)) ":" (:id @work))))
|
|
57
|
+
:lines (if release
|
|
58
|
+
{main-line {:name (line-name (active-line))}}
|
|
59
|
+
(into {main-line (select-keys (get idx main-line {:name "main"}) [:name :created :archived :aliases])}
|
|
60
|
+
(for [l (known-lines) :when (not= l main-line)]
|
|
61
|
+
[l (select-keys (merge {:name (line-name l)} (get idx l)) [:name :created :archived :aliases])])))
|
|
62
|
+
:history segs
|
|
63
|
+
:people (when-not anonymous (into {} (for [a (distinct (map :author segs)) :let [p (get @profiles a)] :when p] [a p])))}))
|
|
64
|
+
|
|
65
|
+
(defn export-bytes
|
|
66
|
+
"Promise of the bytes of a .kairotic of the work: {:release true} for a release, {:anonymous true} without
|
|
67
|
+
who made it."
|
|
68
|
+
[opts]
|
|
69
|
+
(let [d (work-data opts)
|
|
70
|
+
hashes (recorded-sounds (:history d))]
|
|
71
|
+
(-> (js/Promise.all (clj->js (for [h hashes] (.then (snd/sound h) (fn [s] [h s])))))
|
|
72
|
+
(.then (fn [found]
|
|
73
|
+
(let [found (array-seq found)]
|
|
74
|
+
(.then (kfile-lib)
|
|
75
|
+
(fn [K]
|
|
76
|
+
(let [have (filter (fn [[_ s]] (some? s)) found)
|
|
77
|
+
missing (vec (for [[h s] found :when (nil? s)] h))
|
|
78
|
+
edn (fn [nm x] #js {:name nm :data (pr-str x)})]
|
|
79
|
+
(.zip K (clj->js
|
|
80
|
+
(concat
|
|
81
|
+
[(edn "manifest.edn" (cond-> (:manifest d) (seq missing) (assoc :missing-sounds missing)))
|
|
82
|
+
(edn "work.edn" (:work d))
|
|
83
|
+
(edn "lines.edn" (:lines d))
|
|
84
|
+
(edn "history.edn" (:history d))]
|
|
85
|
+
(when (seq (:people d)) [(edn "people.edn" (:people d))])
|
|
86
|
+
(for [[h s] have]
|
|
87
|
+
#js {:name (str "sounds/" h ".wav") :deflate false
|
|
88
|
+
:data (.wav K (:sr s) (:chans s))})))))))))))))
|
|
89
|
+
|
|
90
|
+
(defn file-name [nm ext]
|
|
91
|
+
(str (or (not-empty (-> (str nm) str/lower-case (str/replace #"[^a-z0-9\u00e0-\u024f]+" "-") (str/replace #"^-|-$" ""))) "work") ext))
|
|
92
|
+
|
|
93
|
+
(defn download-bytes! [bytes nm]
|
|
94
|
+
(let [a (js/document.createElement "a")
|
|
95
|
+
url (js/URL.createObjectURL (js/Blob. #js [bytes] #js {:type "application/zip"}))]
|
|
96
|
+
(set! (.-href a) url)
|
|
97
|
+
(set! (.-download a) nm)
|
|
98
|
+
(.appendChild js/document.body a)
|
|
99
|
+
(.click a)
|
|
100
|
+
(.remove a)
|
|
101
|
+
(js/setTimeout (fn [] (js/URL.revokeObjectURL url)) 4000)))
|
|
102
|
+
|
|
103
|
+
(defn export-work
|
|
104
|
+
"(export-work): downloads the work as a file (.kairotic), with its whole history, every line and the sounds
|
|
105
|
+
recorded. (export-work :release) a release of it (one line, one point per keyframe);
|
|
106
|
+
(export-work {:anonymous true}) without who made it (also {:release true :anonymous true})."
|
|
107
|
+
([] (export-work {}))
|
|
108
|
+
([opts]
|
|
109
|
+
(when-not (:kind @work) (fail "Open a work first."))
|
|
110
|
+
(when @perf (fail "You are in Perform: (compose) first."))
|
|
111
|
+
(let [opts (cond (map? opts) opts (= :release opts) {:release true} (= :anonymous opts) {:anonymous true} :else {})
|
|
112
|
+
nm (file-name (str (or (:name @work) "work") (when (:release opts) " release")) ".kairotic")]
|
|
113
|
+
(-> (export-bytes opts)
|
|
114
|
+
(.then (fn [b]
|
|
115
|
+
(download-bytes! b nm)
|
|
116
|
+
(repl-print! "out" (esc (str "; " nm " · " (js/Math.round (/ (.-length b) 1024)) " KB")))))
|
|
117
|
+
(.catch (fn [e] (repl-print! "err" (esc (str "Could not export the work: " (or (.-message e) e)))))))
|
|
118
|
+
nm)))
|
|
119
|
+
|
|
120
|
+
;; ───────────────────────── opening one ─────────────────────────
|
|
121
|
+
|
|
122
|
+
(defn sane-segment [n]
|
|
123
|
+
(when (and (map? n) (string? (:id n)) (seq (:id n)))
|
|
124
|
+
{:id (:id n)
|
|
125
|
+
:parent (when (string? (:parent n)) (:parent n))
|
|
126
|
+
:at (if (number? (:at n)) (:at n) 0)
|
|
127
|
+
:author (str (or (:author n) "unknown"))
|
|
128
|
+
:session (when (string? (:session n)) (:session n))
|
|
129
|
+
:t (if (number? (:t n)) (:t n) 0)
|
|
130
|
+
:merge (when (valid-pos? (:merge n)) (:merge n))
|
|
131
|
+
:line (when (and (string? (:line n)) (seq (:line n)) (not= main-line (:line n))) (:line n))
|
|
132
|
+
:events (if (sequential? (:events n)) (vec (filter (fn [e] (and (vector? e) (keyword? (first e)))) (:events n))) [])}))
|
|
133
|
+
|
|
134
|
+
(defn read-work-bytes
|
|
135
|
+
"Promise of what a .kairotic holds: {:manifest :work :lines :history :people :sounds {hash {:sr :chans}}}."
|
|
136
|
+
[bytes]
|
|
137
|
+
(.then (kfile-lib)
|
|
138
|
+
(fn [K]
|
|
139
|
+
(.then (.unzip K bytes)
|
|
140
|
+
(fn [files]
|
|
141
|
+
(let [by (into {} (for [f (array-seq files)] [(.-name f) (.-data f)]))
|
|
142
|
+
edn (fn [nm] (when-let [b (get by nm)]
|
|
143
|
+
(try (clojure.edn/read-string (.text K b))
|
|
144
|
+
(catch :default e (throw (js/Error. (str nm ": " (.-message e))))))))
|
|
145
|
+
manifest (edn "manifest.edn")]
|
|
146
|
+
(when-not (map? manifest) (throw (js/Error. "This is not a Kairotic work (it has no manifest.edn).")))
|
|
147
|
+
(when (> (or (:format manifest) 0) kfile-format)
|
|
148
|
+
(throw (js/Error. (str "This work was saved by a newer Kairotic (format " (:format manifest) "): update this page."))))
|
|
149
|
+
{:manifest manifest
|
|
150
|
+
:work (or (edn "work.edn") {})
|
|
151
|
+
:lines (let [l (edn "lines.edn")] (if (map? l) l {}))
|
|
152
|
+
:history (vec (keep sane-segment (edn "history.edn")))
|
|
153
|
+
:people (or (edn "people.edn") {})
|
|
154
|
+
:sounds (into {} (for [[nm b] by :let [[_ h] (re-matches #"sounds/([0-9a-z]+)\.wav" nm)] :when h]
|
|
155
|
+
(let [s (.unwav K b)] [h {:sr (.-sr s) :chans (.-chans s)}])))}))))))
|
|
156
|
+
|
|
157
|
+
(defn sane-lines [ls]
|
|
158
|
+
(into {} (for [[k v] ls :when (and (string? k) (map? v))]
|
|
159
|
+
[k (cond-> {:name (str (or (:name v) (if (= k main-line) "main" "a line")))
|
|
160
|
+
:created (if (number? (:created v)) (:created v) 0)
|
|
161
|
+
:archived (true? (:archived v))}
|
|
162
|
+
(sequential? (:aliases v)) (assoc :aliases (vec (filter string? (:aliases v)))))])))
|
|
163
|
+
|
|
164
|
+
(defn fresh-ids
|
|
165
|
+
"The segments with the ids made in a local work (local-…, never saved, so never extended there either) made
|
|
166
|
+
ids of their own, and every reference to them (a parent, a merge, a [:merge-from …] event) with them."
|
|
167
|
+
[segs]
|
|
168
|
+
(let [m (into {} (for [n segs :when (str/starts-with? (:id n) "local-")]
|
|
169
|
+
[(:id n) (str "f" (.toString (js/Math.floor (* (js/Math.random) 2176782336)) 36) (.toString (js/Date.now) 36)
|
|
170
|
+
(.toString (js/Math.floor (* (js/Math.random) 1296)) 36))]))
|
|
171
|
+
f (fn [x] (get m x x))]
|
|
172
|
+
(if (empty? m)
|
|
173
|
+
segs
|
|
174
|
+
(mapv (fn [n] (cond-> (-> n (update :id f) (update :parent (fn [p] (when p (f p)))))
|
|
175
|
+
(:merge n) (update :merge (fn [[a i]] [(f a) i]))
|
|
176
|
+
true (update :events (fn [evs] (mapv (fn [e] (if (= :merge-from (first e)) (assoc e 1 (f (second e))) e)) evs)))))
|
|
177
|
+
segs))))
|
|
178
|
+
|
|
179
|
+
(defn open-work-data!
|
|
180
|
+
"Makes a new work of what a file holds: solo when there is a database to write it to, else local.
|
|
181
|
+
Promise of its name."
|
|
182
|
+
[{:keys [work lines people sounds] :as d} fallback-name]
|
|
183
|
+
(let [history (fresh-ids (:history d))
|
|
184
|
+
nm (str (or (not-empty (str (:name work))) fallback-name "Opened work"))
|
|
185
|
+
simul (let [v (:simultaneity-ms work)] (if (and (number? v) (<= 0 v 10000)) v 1500))
|
|
186
|
+
lines (sane-lines lines)
|
|
187
|
+
main (into {} (for [n history :when (nil? (:line n))] [(:id n) n]))
|
|
188
|
+
other (reduce (fn [m n] (assoc-in m [(:line n) (:id n)] n)) {} (filter :line history))]
|
|
189
|
+
(-> (js/Promise.all (clj->js (for [[h s] sounds] (snd/store! h (:sr s) (:chans s)))))
|
|
190
|
+
(.then
|
|
191
|
+
(fn [_]
|
|
192
|
+
(when (seq people) (swap! profiles merge people))
|
|
193
|
+
(if (and @dbref (uid) (:writable @hist))
|
|
194
|
+
(let [db @dbref
|
|
195
|
+
ref (.doc (.collection db (coll-for :solo)))
|
|
196
|
+
id (.-id ref)
|
|
197
|
+
base (str (coll-for :solo) "/" id)]
|
|
198
|
+
(busy! "Opening the file…")
|
|
199
|
+
(-> (.set ref (clj->js {:name nm :created (js/Date.now) :author (uid) :simultaneity_ms simul :origin "file"}))
|
|
200
|
+
(.then (fn [_] (reduce (fn [p n] (.then p (fn [_] (.set (.doc db (str base "/history/" (:id n))) (node->js n)))))
|
|
201
|
+
(js/Promise.resolve nil) history)))
|
|
202
|
+
(.then (fn [_] (reduce (fn [p [l e]] (.then p (fn [_] (.set (.doc db (str base "/lines/" l)) (clj->js e)))))
|
|
203
|
+
(js/Promise.resolve nil) lines)))
|
|
204
|
+
(.then (fn [_] (busy! nil) (open-work! :solo id nm) nm))))
|
|
205
|
+
(do (open-work! :local "file" nm)
|
|
206
|
+
(reset! cache {})
|
|
207
|
+
(swap! hist assoc :nodes main :other other :loaded true)
|
|
208
|
+
(swap! settings assoc :simul-ms simul)
|
|
209
|
+
(swap! line-state assoc :index lines)
|
|
210
|
+
(swap! world assoc :pos (newest-tip) :auto true :prev-pos nil)
|
|
211
|
+
(render-all!)
|
|
212
|
+
(render-lines!)
|
|
213
|
+
nm)))))))
|
|
214
|
+
|
|
215
|
+
(defn open-work-bytes!
|
|
216
|
+
"Promise: the work a .kairotic holds (its bytes), opened as a new work."
|
|
217
|
+
([bytes] (open-work-bytes! bytes nil))
|
|
218
|
+
([bytes fallback]
|
|
219
|
+
(-> (read-work-bytes bytes)
|
|
220
|
+
(.then (fn [d] (open-work-data! d fallback)))
|
|
221
|
+
(.then (fn [nm] (repl-print! "out" (esc (str "; opened from a file: «" nm "»"))) nm)))))
|
|
222
|
+
|
|
223
|
+
(defn open-work-file!
|
|
224
|
+
"Asks for a .kairotic file and opens the work it holds, as a new one."
|
|
225
|
+
[]
|
|
226
|
+
(let [in ($ "kfile-in")]
|
|
227
|
+
(set! (.-value in) "")
|
|
228
|
+
(.click in)
|
|
229
|
+
:choosing))
|
|
230
|
+
|
|
231
|
+
(.addEventListener ($ "kfile-in") "change"
|
|
232
|
+
(fn [e]
|
|
233
|
+
(when-let [f (aget (.. e -target -files) 0)]
|
|
234
|
+
(-> (.arrayBuffer f)
|
|
235
|
+
(.then (fn [ab] (open-work-bytes! (js/Uint8Array. ab) (str/replace (.-name f) #"\.kairotic$" ""))))
|
|
236
|
+
(.catch (fn [err]
|
|
237
|
+
(busy! nil)
|
|
238
|
+
(repl-print! "err" (esc (str "Could not open " (.-name f) ": " (or (.-message err) err))))))))))
|
|
239
|
+
|
|
240
|
+
;; ───────────────────────── the Share/Export panel: first, the work as a file ─────────────────────────
|
|
241
|
+
|
|
242
|
+
(.addEventListener ($ "ep-file") "click"
|
|
243
|
+
(fn [e]
|
|
244
|
+
(when-let [b (.closest (.-target e) "[data-kfile]")]
|
|
245
|
+
(let [anon (.-checked ($ "ep-anon"))
|
|
246
|
+
release (= "release" (.getAttribute b "data-kfile"))]
|
|
247
|
+
(gesture! (str "(export-work " (pr-str (cond-> {} release (assoc :release true) anon (assoc :anonymous true))) ")"))))))
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · page · html boxes (issue #146)
|
|
3
|
+
;; Loaded right after 05-gestures (app/index.html), not last: the startup (11) already draws the patch,
|
|
4
|
+
;; and the scene calls these.
|
|
5
|
+
;; An html box draws a small HTML document (kairotic.hiccup) inside the patch: a foreignObject in the
|
|
6
|
+
;; box, holding an element of its own with a shadow root, so its styles stay inside and the page's stay
|
|
7
|
+
;; out. The scene is written again with innerHTML on every change, so each html box keeps its element
|
|
8
|
+
;; here (by engine id) and it is put back into its slot after the scene is written, like the notation of
|
|
9
|
+
;; a score: what is typed or dragged in it is not lost. It is only built again when its view changes.
|
|
10
|
+
;;
|
|
11
|
+
;; Using an element sends the box [:ui outlet value name], a message: while a slider moves only here (and
|
|
12
|
+
;; to a jam), as a knob does while it is dragged. When it is let go, a menu is chosen or a text entered,
|
|
13
|
+
;; the box keeps the value of the named element, as a knob keeps its value in its text: (values! :bN
|
|
14
|
+
;; {name value}), one event of the history (this tab already sent the value: it is not sent again). An
|
|
15
|
+
;; element without a name (a button) is a gesture written in the REPL, (send! :bN [:ui …]).
|
|
16
|
+
|
|
17
|
+
(defonce html-nodes (atom {})) ; engine id → {:id :view :host :root :built}
|
|
18
|
+
(defonce html-focus (atom nil))
|
|
19
|
+
(defonce html-pressed (atom nil)) ; the element of an html box under a pressed pointer (a slider being dragged) ; {:f :i :start :end}: the element that had the focus before the scene was written
|
|
20
|
+
|
|
21
|
+
(defn html-slot-svg
|
|
22
|
+
"The slot of an html box, under its header: its element is put in it after the scene is written."
|
|
23
|
+
[id b w h]
|
|
24
|
+
(str "<foreignObject class=\"html-slot\" data-box=\"" (esc (name id)) "\" x=\"2\" y=\"" hdr
|
|
25
|
+
"\" width=\"" (max 0 (- w 4)) "\" height=\"" (max 0 (- h hdr 3)) "\"></foreignObject>"))
|
|
26
|
+
|
|
27
|
+
(defn html-values
|
|
28
|
+
"The values an html box shows: the ones its elements start with, under what it keeps or did since."
|
|
29
|
+
[f e]
|
|
30
|
+
(let [b (get-in (current) [:boxes (:id e)])]
|
|
31
|
+
(merge (get-in e [:built :defaults]) (hic/current-values (:data b) (get-in @eng/rt [:states f])))))
|
|
32
|
+
|
|
33
|
+
(defn html-in-use
|
|
34
|
+
"The element of an html box whose value is left alone: one being dragged, or a field being typed in."
|
|
35
|
+
[e]
|
|
36
|
+
(let [a (.-activeElement (:root e))]
|
|
37
|
+
(or (when (and @html-pressed (.contains (:root e) @html-pressed)) @html-pressed)
|
|
38
|
+
(when (and a (or (= "TEXTAREA" (.-tagName a))
|
|
39
|
+
(and (= "INPUT" (.-tagName a)) (#{"text" "number"} (.-type a)))))
|
|
40
|
+
a))))
|
|
41
|
+
|
|
42
|
+
(defn html-sync-kept!
|
|
43
|
+
"When the values an html box keeps change (back in the history, kept just now, someone else in a room),
|
|
44
|
+
what its elements did since is forgotten: it shows what it keeps."
|
|
45
|
+
[f e]
|
|
46
|
+
(let [kept (hic/clean-values (get-in (current) [:boxes (:id e) :data :values]))]
|
|
47
|
+
(when (not= kept (:kept e))
|
|
48
|
+
(swap! html-nodes assoc-in [f :kept] kept)
|
|
49
|
+
(when (contains? e :kept)
|
|
50
|
+
(swap! eng/rt (fn [rt] (if (get-in rt [:states f]) (update-in rt [:states f] dissoc :values :from) rt)))))))
|
|
51
|
+
|
|
52
|
+
(defn html-update!
|
|
53
|
+
"Shows the current values in the html box with engine id f (not in the element being used)."
|
|
54
|
+
[f]
|
|
55
|
+
(when-let [e (get @html-nodes f)]
|
|
56
|
+
(html-sync-kept! f e)
|
|
57
|
+
(try (hic/refresh! (:built e) (html-values f e) (html-in-use e))
|
|
58
|
+
(catch :default ex (js/console.error "html box" ex)))))
|
|
59
|
+
|
|
60
|
+
(defn refresh-html-boxes! [] (doseq [f (keys @html-nodes)] (html-update! f)))
|
|
61
|
+
|
|
62
|
+
(defn html-event!
|
|
63
|
+
"An element of an html box was used: what it gives goes to the box."
|
|
64
|
+
[f ev]
|
|
65
|
+
(when-let [e (get @html-nodes f)]
|
|
66
|
+
(let [el (some-> (.-target ev) (.closest "[data-k]"))
|
|
67
|
+
k (some-> el .-dataset .-k js/parseInt)
|
|
68
|
+
bind (when (and k (js/isFinite k)) (get-in e [:built :binds k]))]
|
|
69
|
+
(when bind
|
|
70
|
+
(when-let [[v commit?] (hic/element-value el bind (.-type ev))]
|
|
71
|
+
(let [msg [:ui (:out bind) v (:name bind)]]
|
|
72
|
+
(cond
|
|
73
|
+
(not commit?) (send-flat-quiet! f msg)
|
|
74
|
+
;; a named element: sent here, and kept in the box as one event (a knob let go)
|
|
75
|
+
(:name bind) (do (send-flat-quiet! f msg)
|
|
76
|
+
(gesture! (str "(values! " (:id e) " " (pr-str {(:name bind) v}) ")")))
|
|
77
|
+
:else (gesture! (str "(send! " (:id e) " " (pr-str msg) ")")))))))))
|
|
78
|
+
|
|
79
|
+
(defn html-make!
|
|
80
|
+
"Builds the element of an html box (engine id f, id in the view) for its view."
|
|
81
|
+
[f id view]
|
|
82
|
+
(let [host (js/document.createElement "div")
|
|
83
|
+
root (.attachShadow host #js {:mode "open"})
|
|
84
|
+
style (js/document.createElement "style")
|
|
85
|
+
built (hic/build view {})]
|
|
86
|
+
(set! (.-className host) "html-host")
|
|
87
|
+
(set! (.-textContent style) hic/base-css)
|
|
88
|
+
(.appendChild root style)
|
|
89
|
+
(when (:node built) (.appendChild root (:node built)))
|
|
90
|
+
;; what is pressed or typed in it is its own: the patch does not move the box, select it or play
|
|
91
|
+
(doseq [t ["pointerdown" "mousedown" "touchstart" "dblclick" "contextmenu" "keydown" "keyup"]]
|
|
92
|
+
(.addEventListener host t (fn [ev] (.stopPropagation ev))))
|
|
93
|
+
(.addEventListener root "pointerdown" (fn [ev] (reset! html-pressed (.-target ev))))
|
|
94
|
+
(doseq [t ["input" "change" "click"]]
|
|
95
|
+
(.addEventListener root t (fn [ev] (html-event! f ev))))
|
|
96
|
+
(let [e {:id id :view view :host host :root root :built built}]
|
|
97
|
+
(swap! html-nodes assoc f e)
|
|
98
|
+
e)))
|
|
99
|
+
|
|
100
|
+
(defn html-save-focus!
|
|
101
|
+
"Before the scene is written: which element of an html box has the focus (and where its text cursor is)."
|
|
102
|
+
[]
|
|
103
|
+
(reset! html-focus
|
|
104
|
+
(some (fn [[f e]]
|
|
105
|
+
(when-let [a (.-activeElement (:root e))]
|
|
106
|
+
(let [all (vec (array-seq (.querySelectorAll (:root e) "*")))
|
|
107
|
+
i (first (keep-indexed (fn [i x] (when (identical? x a) i)) all))]
|
|
108
|
+
(when i {:f f :i i
|
|
109
|
+
:start (try (.-selectionStart a) (catch :default _ nil))
|
|
110
|
+
:end (try (.-selectionEnd a) (catch :default _ nil))}))))
|
|
111
|
+
@html-nodes)))
|
|
112
|
+
|
|
113
|
+
(defn mount-html-slots!
|
|
114
|
+
"After the scene is written: each html box gets its element back (built again only if its view changed)."
|
|
115
|
+
[]
|
|
116
|
+
(let [seen (atom #{})]
|
|
117
|
+
(doseq [slot (array-seq (.querySelectorAll ($ "scene") ".html-slot"))]
|
|
118
|
+
(let [id (keyword (.. slot -dataset -box)) b (get-in (current) [:boxes id])]
|
|
119
|
+
;; the data may have changed (going back in the history): its values are shown again below
|
|
120
|
+
(when b
|
|
121
|
+
(let [f (fid id) view (hic/view-of (:data b)) old (get @html-nodes f)
|
|
122
|
+
e (if (and old (= view (:view old)))
|
|
123
|
+
(do (swap! html-nodes assoc-in [f :id] id) old)
|
|
124
|
+
(html-make! f id view))]
|
|
125
|
+
(swap! seen conj f)
|
|
126
|
+
(.appendChild slot (:host e))))))
|
|
127
|
+
;; the element that had the focus gets it back
|
|
128
|
+
(when-let [{:keys [f i start end]} @html-focus]
|
|
129
|
+
(when-let [e (get @html-nodes f)]
|
|
130
|
+
(when-let [el (nth (vec (array-seq (.querySelectorAll (:root e) "*"))) i nil)]
|
|
131
|
+
(try (.focus el #js {:preventScroll true})
|
|
132
|
+
(when (and (number? start) (number? end)) (.setSelectionRange el start end))
|
|
133
|
+
(catch :default _ nil)))))
|
|
134
|
+
(reset! html-focus nil)
|
|
135
|
+
;; the elements of boxes no longer drawn are forgotten (they are built again if they come back)
|
|
136
|
+
(swap! html-nodes select-keys @seen)))
|
|
137
|
+
|
|
138
|
+
;; A message can reach an html box while nothing draws the overlay (the engine stopped, a msg clicked
|
|
139
|
+
;; elsewhere): when the state the engine keeps for one of them changes, they are refreshed, once a frame.
|
|
140
|
+
(defonce html-frame (atom false))
|
|
141
|
+
(defonce html-watch
|
|
142
|
+
(add-watch eng/rt ::html
|
|
143
|
+
(fn [_ _ old new]
|
|
144
|
+
(when (and (not @html-frame) (seq @html-nodes)
|
|
145
|
+
(some (fn [f] (not (identical? (get-in old [:states f]) (get-in new [:states f])))) (keys @html-nodes)))
|
|
146
|
+
(reset! html-frame true)
|
|
147
|
+
(js/requestAnimationFrame (fn [_] (reset! html-frame false) (refresh-html-boxes!)))))))
|
|
148
|
+
|
|
149
|
+
;; a pointer let go anywhere: no element of an html box is being dragged any more
|
|
150
|
+
(defonce html-release
|
|
151
|
+
(doseq [t ["pointerup" "pointercancel"]]
|
|
152
|
+
(.addEventListener js/window t (fn [_] (reset! html-pressed nil)) true)))
|