@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.
Files changed (96) hide show
  1. package/README.md +9 -2
  2. package/index.html +1101 -0
  3. package/kairotic/api.cljs +362 -0
  4. package/kairotic/assistant.cljs +505 -0
  5. package/kairotic/backends/gun-backend.js +371 -0
  6. package/kairotic/canvas.cljs +751 -0
  7. package/kairotic/code.cljs +108 -0
  8. package/kairotic/components.cljs +838 -0
  9. package/kairotic/core.cljs +96 -0
  10. package/kairotic/corefns.cljs +473 -0
  11. package/kairotic/docs.mjs +2 -0
  12. package/kairotic/engine.cljs +482 -0
  13. package/kairotic/examples.mjs +96 -0
  14. package/kairotic/faust.cljs +225 -0
  15. package/kairotic/help.cljs +1036 -0
  16. package/kairotic/hiccup.cljs +566 -0
  17. package/kairotic/input.cljs +271 -0
  18. package/kairotic/kfile.mjs +138 -0
  19. package/kairotic/library.cljs +214 -0
  20. package/kairotic/links.cljs +41 -0
  21. package/kairotic/mt.cljs +1604 -0
  22. package/kairotic/mtboxes.cljs +276 -0
  23. package/kairotic/packages/analysis.mjs +64 -0
  24. package/kairotic/packages/controls.mjs +28 -0
  25. package/kairotic/packages/data.mjs +22 -0
  26. package/kairotic/packages/faust-units/cmaes.mjs +82 -0
  27. package/kairotic/packages/faust-units/graph.mjs +212 -0
  28. package/kairotic/packages/faust-units/listen.mjs +125 -0
  29. package/kairotic/packages/faust-units/notation.mjs +139 -0
  30. package/kairotic/packages/faust-units/parse.mjs +439 -0
  31. package/kairotic/packages/faust-units/recipe.mjs +195 -0
  32. package/kairotic/packages/faust-units/render.mjs +69 -0
  33. package/kairotic/packages/faust-units/units.mjs +130 -0
  34. package/kairotic/packages/faust-units/view.mjs +353 -0
  35. package/kairotic/packages/faust-units.mjs +9 -0
  36. package/kairotic/packages/io.mjs +58 -0
  37. package/kairotic/packages/net.mjs +28 -0
  38. package/kairotic/packages/patterns.mjs +34 -0
  39. package/kairotic/packages/spatial.mjs +22 -0
  40. package/kairotic/packages/spectral/shift.mjs +169 -0
  41. package/kairotic/packages/spectral.mjs +16 -0
  42. package/kairotic/packages/synth.mjs +28 -0
  43. package/kairotic/packages/theory/counterpoint.mjs +798 -0
  44. package/kairotic/packages/theory/mirror.mjs +221 -0
  45. package/kairotic/packages/theory.mjs +46 -0
  46. package/kairotic/patterns.cljs +538 -0
  47. package/kairotic/presets.cljs +227 -0
  48. package/kairotic/quantize.cljs +782 -0
  49. package/kairotic/record.cljs +169 -0
  50. package/kairotic/release.cljs +90 -0
  51. package/kairotic/ring.cljs +129 -0
  52. package/kairotic/ringaudio.cljs +232 -0
  53. package/kairotic/ringcore.mjs +270 -0
  54. package/kairotic/score.cljs +176 -0
  55. package/kairotic/shapes.cljs +283 -0
  56. package/kairotic/signal.cljs +327 -0
  57. package/kairotic/sounds.cljs +565 -0
  58. package/kairotic/sounds.mjs +108 -0
  59. package/kairotic/span.cljs +129 -0
  60. package/kairotic/spell.cljs +281 -0
  61. package/kairotic/structure.cljs +145 -0
  62. package/kairotic/talk.cljs +115 -0
  63. package/kairotic/tempo.cljs +226 -0
  64. package/kairotic/treecanvas.cljs +299 -0
  65. package/kairotic/tutorials.mjs +1283 -0
  66. package/kairotic/vendor/codemirror.mjs +23 -0
  67. package/package.json +23 -4
  68. package/page/00-work.cljs +862 -0
  69. package/page/01-commands.cljs +1219 -0
  70. package/page/02-patch-view.cljs +1051 -0
  71. package/page/03-history-view.cljs +481 -0
  72. package/page/04-panels.cljs +448 -0
  73. package/page/05-gestures.cljs +1247 -0
  74. package/page/06-connection.cljs +219 -0
  75. package/page/07-claude.cljs +430 -0
  76. package/page/08-sessions.cljs +423 -0
  77. package/page/09-learn.cljs +602 -0
  78. package/page/10-embed.cljs +469 -0
  79. package/page/11-startup.cljs +61 -0
  80. package/page/12-feedback.cljs +172 -0
  81. package/page/13-perform.cljs +325 -0
  82. package/page/14-editor.cljs +656 -0
  83. package/page/15-presets.cljs +230 -0
  84. package/page/16-release.cljs +242 -0
  85. package/page/17-sounds.cljs +147 -0
  86. package/page/18-lines.cljs +594 -0
  87. package/page/19-files.cljs +247 -0
  88. package/page/19-html.cljs +152 -0
  89. package/page/20-talk.cljs +527 -0
  90. package/page/21-net.cljs +110 -0
  91. package/page/22-learn.cljs +303 -0
  92. package/page/23-ids.cljs +233 -0
  93. package/page/24-clipboard.cljs +136 -0
  94. package/page/25-tidy.cljs +74 -0
  95. package/page/26-probes.cljs +111 -0
  96. package/sources.mjs +2 -0
@@ -0,0 +1,594 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page: lines (discussion #117, phase 1)
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+ ;;
5
+ ;; A work is a project of lines: independent histories, each a whole tree of its own (branches, merges,
6
+ ;; snapshots), shown as tabs above the patch. The main line is the work as it always was; the others are
7
+ ;; studies, sketches, parts. Their segments are kept with the work's history, each one marked with its line
8
+ ;; (00-work.cljs), and their names in <work>/lines, one document per line. The tab on screen is the line
9
+ ;; everything else works on: the patch, the history view, Perform and its jams, Share/Export, Claude.
10
+ ;; The engine plays the line where ▶ was pressed, whichever tab is on screen (decision 6, 00-work.cljs).
11
+ ;; One line is used inside another as `patch @name` (phase 2, below).
12
+
13
+ (in-ns 'user)
14
+
15
+ ;; ───────────────────────── names ─────────────────────────
16
+
17
+ (defn line-entry [lid] (get-in @line-state [:index lid]))
18
+
19
+ (defn line-name
20
+ "The name of line lid as the tabs show it."
21
+ [lid]
22
+ (or (not-empty (:name (line-entry lid))) (if (= lid main-line) "main" "a line")))
23
+
24
+ (defn known-lines
25
+ "Every line of the work: the main one, those with a name, and those whose segments arrived before their name."
26
+ []
27
+ (-> (set (keys (:index @line-state))) (into (keys (:other @hist))) (conj main-line (active-line))))
28
+
29
+ (defn line-order [lid] [(if (= lid main-line) 0 1) (or (:created (line-entry lid)) 9e15) (line-name lid)])
30
+
31
+ (defn tab-lines
32
+ "The lines shown as tabs, in order: the main one first, then by creation (the one on screen always)."
33
+ []
34
+ (sort-by line-order (remove (fn [l] (and (:archived (line-entry l)) (not= l (active-line)))) (known-lines))))
35
+
36
+ (defn archived-lines [] (sort-by line-order (filter (fn [l] (and (:archived (line-entry l)) (not= l (active-line)))) (known-lines))))
37
+
38
+ (defn line-by-name
39
+ "The id of the line called nm (or with that id), archived or not."
40
+ [nm]
41
+ (let [nm (str/trim (str nm)) ls (known-lines)]
42
+ (or (first (filter (fn [l] (= nm (line-name l))) (sort-by line-order ls)))
43
+ (when (contains? ls nm) nm))))
44
+
45
+ (defn ref-slug
46
+ "How a box writes a line's name after @: its spaces as - (patch @bass-line uses the line \"bass line\")."
47
+ [nm] (str/replace (str/trim (str nm)) #"\s+" "-"))
48
+
49
+ (defn line-by-ref
50
+ "The id of the line a `patch @r` box uses: by its name (spaces as -), or by a name it had before."
51
+ [r]
52
+ (let [ls (sort-by line-order (known-lines))]
53
+ (or (first (filter (fn [l] (= r (ref-slug (line-name l)))) ls))
54
+ (first (filter (fn [l] (some (fn [a] (= r (ref-slug a))) (:aliases (line-entry l)))) ls))
55
+ (when (contains? (set ls) r) r))))
56
+
57
+ ;; ───────────────────────── storage: <work>/lines/<id> ─────────────────────────
58
+
59
+ (defn lines-path [] (when-let [b (work-base)] (str b "/lines")))
60
+
61
+ (defn on-lines
62
+ "The names of the lines, as they arrive (they are only ever added to or changed: a line is archived, not deleted)."
63
+ [snap]
64
+ (let [incoming (into {} (for [d (array-seq (.-docs snap)) :when (.-exists d)
65
+ :let [m (js->clj (.data d))]]
66
+ [(.-id d) {:name (let [n (get m "name")] (when (string? n) n))
67
+ :created (let [t (get m "created")] (if (number? t) t 0))
68
+ :archived (true? (get m "archived"))
69
+ :aliases (let [a (get m "aliases")] (if (sequential? a) (vec (filter string? a)) []))}]))]
70
+ (swap! line-state update :index merge incoming)))
71
+
72
+ (defn save-line!
73
+ "Writes what changed of line lid's entry (its name, archived) where the others read it."
74
+ [lid changes]
75
+ (swap! line-state update-in [:index lid] merge changes)
76
+ (when (persist?)
77
+ (let [path (str (lines-path) "/" lid)
78
+ body (clj->js (merge (select-keys (line-entry lid) [:name :created :archived :aliases]) {:author (my-author)} changes))]
79
+ (enqueue! path (fn [] (.set (.doc @dbref path) body))))))
80
+
81
+ (defn new-line-id []
82
+ (if (persist?)
83
+ (.-id (.doc (.collection @dbref (lines-path))))
84
+ (str "l" (.toString (js/Math.floor (* (js/Math.random) 2176782336)) 36) (.toString (js/Date.now) 36))))
85
+
86
+ ;; ───────────────────────── a line used as a sub-patch: patch @name (phase 2) ─────────────────────────
87
+ ;; A `patch @name` box reads the line called name: at its newest point (it follows the line, so it changes
88
+ ;; as the line advances, like an abstraction of Max or Pd), or at the point it is pinned to ([:pin id pos]).
89
+ ;; While performing, the lines followed stay where they were when Perform started (decision 4); away from
90
+ ;; the newest point of the line on screen, they read where they were at the last snapshot before it, if it
91
+ ;; kept them (decision 5). Inside a line used so, adc~ and dac~ are quiet: they play when the line plays
92
+ ;; alone, and sound goes in and out through inlet~ / outlet~ (decision 7).
93
+
94
+ (defn line-nodes [lid] (if (= lid (active-line)) (:nodes @hist) (get-in @hist [:other lid] {})))
95
+
96
+ (defn nodes-front [nodes]
97
+ (when (seq nodes) (let [n (apply max-key (fn [n] (or (:t n) 0)) (vals nodes))] [(:id n) (count (:events n))])))
98
+
99
+ (defn nodes-depth
100
+ "How many events from the root of a line to pos (its moment)."
101
+ [nodes [nid idx]]
102
+ (loop [nid nid acc idx guard 0]
103
+ (let [n (get nodes nid)]
104
+ (if (and n (:parent n) (get nodes (:parent n)) (< guard 512))
105
+ (recur (:parent n) (+ acc (:at n)) (inc guard))
106
+ acc))))
107
+
108
+ (defonce ref-cache (atom {})) ; [line pos] → [its nodes, the patch]
109
+
110
+ (defn line-patch-at [lid pos]
111
+ (let [nodes (line-nodes lid) [k p] (get @ref-cache [lid pos])]
112
+ (if (identical? k nodes)
113
+ p
114
+ (let [p (if pos (nodes-patch nodes pos) empty-patch)]
115
+ (swap! ref-cache (fn [c] (assoc (if (> (count c) 200) {} c) [lid pos] [nodes p])))
116
+ p))))
117
+
118
+ (defonce fronts-cache (atom [nil nil nil]))
119
+
120
+ (defn snapshot-fronts
121
+ "Where the lines followed were at the last snapshot before the point on screen, when it is not the newest
122
+ point of its line (decision 5): {line pos}, or nil."
123
+ []
124
+ (let [here (norm (:pos @world)) nodes (:nodes @hist) [k1 k2 v] @fronts-cache]
125
+ (if (and (identical? k1 nodes) (= k2 here))
126
+ v
127
+ (let [v (when (and here (not= here (newest-tip)))
128
+ (some (fn [e] (when (and (vector? e) (= :mark (first e))) (:fronts (second e))))
129
+ (reverse (mapcat (fn [g] (seg-events g 0)) (segments here)))))]
130
+ (reset! fronts-cache [nodes here v])
131
+ v))))
132
+
133
+ (defn ref-pos
134
+ "The point of its line that box b reads."
135
+ [b lid]
136
+ (or (:pin b)
137
+ (get-in @perf [:pins lid])
138
+ (get (snapshot-fronts) lid)
139
+ (nodes-front (line-nodes lid))))
140
+
141
+ (defn resolve-ref
142
+ "{:line :pos :pinned :patch} for a `patch @name` box, or {:error}."
143
+ [b]
144
+ (let [r (eng/line-ref b) lid (when r (line-by-ref r))]
145
+ (cond (nil? r) {:error "not a patch @name box"}
146
+ (nil? lid) {:error (str "There is no line called " r " in this work.")}
147
+ :else (let [pos (ref-pos b lid)]
148
+ {:line lid :pos pos :pinned (some? (:pin b)) :patch (if pos (line-patch-at lid pos) empty-patch)}))))
149
+
150
+ (defn refs-in
151
+ "The `patch @name` boxes of a patch, through its embedded patches: [[path id box] …]."
152
+ ([p] (refs-in p []))
153
+ ([p path]
154
+ (mapcat (fn [[id b]]
155
+ (cond (line-ref? b) [[path id b]]
156
+ (embedded? b) (refs-in (or (:data b) empty-patch) (conj path id))
157
+ :else []))
158
+ (:boxes p))))
159
+
160
+ (defn lines-reached
161
+ "Every line reached from patch p through references, at the point each one is read: {line pos}."
162
+ [p]
163
+ (loop [todo [p] seen {} guard 0]
164
+ (if (or (empty? todo) (> guard 64))
165
+ seen
166
+ (let [found (for [[_ _ b] (refs-in (peek todo)) :let [r (resolve-ref b)] :when (:line r)] r)
167
+ new (remove (fn [r] (contains? seen (:line r))) found)]
168
+ (recur (into (pop todo) (map :patch new))
169
+ (into seen (map (fn [r] [(:line r) (:pos r)]) found)) (inc guard))))))
170
+
171
+ (defn check-ref-text!
172
+ "Fails when box text t is a `patch @name` that cannot be: no such line, or one that would contain itself."
173
+ [t]
174
+ (when-let [r (eng/line-ref {:text t})]
175
+ (let [lid (line-by-ref r) here (active-line)]
176
+ (cond (nil? lid) (fail (str "There is no line called " r " in this work (spaces in a name are written -)."))
177
+ (= lid here) (fail "A line cannot contain itself.")
178
+ (contains? (lines-reached (line-patch-at lid (nodes-front (line-nodes lid)))) here)
179
+ (fail (str (line-name lid) " already uses " (line-name here) ": that would be a cycle."))
180
+ :else nil))))
181
+
182
+ (defn followed-fronts
183
+ "The lines the work on screen follows (not pinned), where each one is now: {line pos}."
184
+ []
185
+ (into {} (for [[lid pos] (lines-reached (work-patch))
186
+ :when (not-any? (fn [[_ _ b]] (and (:pin b) (= lid (line-by-ref (eng/line-ref b))))) (refs-in (work-patch)))]
187
+ [lid pos])))
188
+
189
+ (defn quiet
190
+ "A line as a sub-patch plays: without its adc~ and dac~ (decision 7)."
191
+ [p]
192
+ (let [io (set (keep (fn [[id b]] (when (#{"adc~" "dac~"} (klass (:text b))) id)) (:boxes p)))]
193
+ (if (empty? io)
194
+ p
195
+ (-> p (update :boxes (fn [bx] (into (sorted-map) (remove (fn [[id _]] (io id)) bx))))
196
+ (update :lines (fn [ls] (vec (remove (fn [[a _ b _]] (or (io a) (io b))) ls))))))))
197
+
198
+ (reset! eng/line-resolver (fn [b] (let [r (resolve-ref b)] (when-let [p (:patch r)] (quiet p)))))
199
+
200
+ (defn ref-badge
201
+ "What a `patch @name` box shows beside its name: ↻ following its line, ⌖ and the moment it is pinned to,
202
+ ⚠ when something is wrong."
203
+ [b]
204
+ (let [r (resolve-ref b)]
205
+ (cond (:error r) "⚠"
206
+ (or (ref-warning b)) "⚠"
207
+ (:pinned r) (str "⌖" (nodes-depth (line-nodes (:line r)) (:pos r)))
208
+ :else "↻")))
209
+
210
+ (defn ref-warning
211
+ "Why a `patch @name` box may not do what it seems, or nil."
212
+ [b]
213
+ (let [r (resolve-ref b) cls (frequencies (map (comp klass :text) (vals (:boxes (:patch r)))))]
214
+ (cond (:error r) (:error r)
215
+ (and (cls "dac~") (not (cls "outlet~")))
216
+ (str (line-name (:line r)) " has dac~ but no outlet~: nothing comes out here (its dac~ plays only when the line plays alone)")
217
+ (and (cls "adc~") (not (cls "inlet~")))
218
+ (str (line-name (:line r)) " has adc~ but no inlet~: no sound goes in (its adc~ listens only when the line plays alone)")
219
+ :else nil)))
220
+
221
+ (defn need-ref [id]
222
+ (let [b (or (get-in (current) [:boxes id]) (fail (str "There is no box " id)))]
223
+ (when-not (line-ref? b) (fail (str id " is not a patch @name box")))
224
+ b))
225
+
226
+ (defn pin-ref
227
+ "(pin-ref :b3): the line box :b3 uses stays at the point it reads now. (pin-ref :b3 [node n]) at that one."
228
+ ([id] (pin-ref id nil))
229
+ ([id pos]
230
+ (let [b (need-ref id)
231
+ pos (or pos (:pos (resolve-ref b)) (fail (str (eng/line-ref b) " has no history yet: nothing to pin")))]
232
+ (dispatch! [:pin id (vec pos)])
233
+ pos)))
234
+
235
+ (defn follow-ref
236
+ "(follow-ref :b3): the box follows its line again (its newest point)."
237
+ [id]
238
+ (need-ref id)
239
+ (dispatch! [:pin id nil])
240
+ :follows)
241
+
242
+ (declare need-switch)
243
+
244
+ (defn open-ref
245
+ "(open-ref :b3): the line box :b3 uses, in its tab (at the point it is pinned to, if it is)."
246
+ [id]
247
+ (let [b (need-ref id) r (resolve-ref b)]
248
+ (when (:error r) (fail (:error r)))
249
+ (need-switch (:line r))
250
+ (swap! ui assoc :follow nil)
251
+ (when (:pinned r)
252
+ (swap! world assoc :pos (:pos r) :auto false :prev-pos nil))
253
+ (line-name (:line r))))
254
+
255
+ ;; ───── a line's tab while the line that plays uses it (decision 6, what you play there) ─────
256
+ ;; When the line that plays uses the line on screen (following it, and the tab is at its newest point), the
257
+ ;; tab shows that line where it plays: its first instance. What you play there goes to every instance.
258
+
259
+ (defn instances-in
260
+ "Where line lid plays inside patch p, through embedded patches and the lines p uses: [path …]."
261
+ [lid p path depth]
262
+ (if (> depth 12)
263
+ []
264
+ (vec (mapcat (fn [[id b]]
265
+ (cond (line-ref? b)
266
+ (let [r (resolve-ref b)]
267
+ (cond (:error r) []
268
+ (= lid (:line r)) (if (:pinned r) [] [(conj path id)])
269
+ :else (instances-in lid (:patch r) (conj path id) (inc depth))))
270
+ (embedded? b) (instances-in lid (or (:data b) empty-patch) (conj path id) (inc depth))
271
+ :else []))
272
+ (:boxes p)))))
273
+
274
+ (defonce instances-cache (atom [nil nil nil nil]))
275
+
276
+ (defn view-instances
277
+ "Where the line on screen plays inside the line that plays: [path …] (empty when it does not, or when its
278
+ tab is not at its newest point, or in Perform)."
279
+ []
280
+ (if-not (and (playing-elsewhere?) (not @perf) (= (norm (:pos @world)) (newest-tip)))
281
+ []
282
+ (let [off (off-screen-patch (playing-line)) nodes (:nodes @hist) a (active-line)
283
+ [k1 k2 k3 v] @instances-cache]
284
+ (if (and (identical? k1 off) (identical? k2 nodes) (= k3 a))
285
+ v
286
+ (let [v (instances-in a off [] 0)] (reset! instances-cache [off nodes a v]) v)))))
287
+
288
+ ;; ───── one line, when the work leaves: links and releases (decision 8) ─────
289
+ ;; A link or a release carries one patch, not the lines of a work: every `patch @name` in it becomes an
290
+ ;; embedded patch holding its line at the point it reads (its pin, the fronts given — those a snapshot kept —
291
+ ;; or its newest point), quiet as it played there (without its adc~ and dac~), and so on inside it.
292
+
293
+ (defn inline-lines
294
+ "Patch p with every `patch @name` (through embedded patches, and inside the lines it uses) made an embedded
295
+ patch holding its line: at its pin, at (fronts line) when given, or as it reads now."
296
+ ([p] (inline-lines p nil 0))
297
+ ([p fronts] (inline-lines p fronts 0))
298
+ ([p fronts depth]
299
+ (if (> depth 16)
300
+ p
301
+ (update p :boxes
302
+ (fn [bx]
303
+ (into (empty bx)
304
+ (for [[id b] bx]
305
+ [id (cond
306
+ (line-ref? b)
307
+ (let [lid (line-by-ref (eng/line-ref b))
308
+ pos (when lid (or (:pin b) (get fronts lid) (ref-pos b lid)))
309
+ inner (if pos (line-patch-at lid pos) empty-patch)]
310
+ (-> b (dissoc :pin)
311
+ (assoc :text (str "patch " (eng/line-ref b))
312
+ :data (quiet (inline-lines inner fronts (inc depth))))))
313
+ (embedded? b) (update b :data (fn [d] (when d (inline-lines d fronts (inc depth)))))
314
+ :else b)])))))))
315
+
316
+ ;; ───── from a box to a line and back (decision 2) ─────
317
+
318
+ (declare check-line-name)
319
+
320
+ (defn inline-ref
321
+ "(inline-ref :b3): the line box :b3 uses becomes a copy inside the box (an embedded patch), as it plays now:
322
+ detached from the line, which stays as it is."
323
+ [id]
324
+ (let [b (need-ref id) r (resolve-ref b)]
325
+ (when (:error r) (fail (:error r)))
326
+ (dispatch! [:batch [[:text id (str "patch " (eng/line-ref b))] [:pin id nil]
327
+ [:data id (quiet (inline-lines (:patch r)))]]])
328
+ id))
329
+
330
+ (defn events-inside
331
+ "The events that reached the inside of box id (in the patch at path) along ev: [:in :b2 [:in :b5 e]] for
332
+ path [:b2] and id :b5 gives e, through batches."
333
+ [path id ev]
334
+ (letfn [(walk [path ev]
335
+ (cond (not (vector? ev)) []
336
+ (= :batch (first ev)) (mapcat (fn [e] (walk path e)) (second ev))
337
+ (seq path) (if (and (= :in (first ev)) (= (first path) (second ev))) (walk (rest path) (nth ev 2 nil)) [])
338
+ :else [ev]))]
339
+ (walk path ev)))
340
+
341
+ (defn box-history
342
+ "The history box id had inside it (in the patch at path of the line on screen), from the root to here: the
343
+ events it received, from the last time it was made (or loaded)."
344
+ [path id]
345
+ (let [evs (mapcat (fn [g] (seg-events g 0)) (segments (norm (:pos @world))))]
346
+ (reduce (fn [acc ev]
347
+ (reduce (fn [acc e]
348
+ (let [[op a x] e]
349
+ (case op
350
+ (:box :delete) (if (= a id) [] acc)
351
+ :data (if (= a id) (if (map? x) [[:load x]] []) acc)
352
+ :in (if (= a id) (conj acc x) acc)
353
+ :load (if-let [d (get-in a [:boxes id :data])] [[:load d]] [])
354
+ :clear []
355
+ acc)))
356
+ acc (events-inside path id ev)))
357
+ [] evs)))
358
+
359
+ (defn write-line-node!
360
+ "A first segment for line lid (not on screen): events evs, saved as any other."
361
+ [lid evs]
362
+ (let [id (new-id)
363
+ n {:id id :parent nil :at 0 :author (my-author) :session session-id :t (js/Date.now) :merge nil
364
+ :events (vec evs) :line lid}]
365
+ (swap! hist assoc-in [:other lid id] n)
366
+ (when (and (persist?) (not (str/starts-with? id "local-")))
367
+ (let [path (str (hist-path) "/" id) body (node->js n)]
368
+ (enqueue! path (fn [] (.set (.doc @dbref path) body)))))
369
+ id))
370
+
371
+ (defn extract-line
372
+ "(extract-line :b3 \"filters\"): the embedded patch :b3 becomes a new line, with the history it had inside the
373
+ box, and the box uses it (patch @filters, following it)."
374
+ [id nm]
375
+ (when (or @perf @lesson @scratch) (fail "Not now: lines change outside Perform, a lesson or the scratch patch."))
376
+ (let [b (or (get-in (current) [:boxes id]) (fail (str "There is no box " id)))]
377
+ (when-not (embedded? b) (fail (str id " is not an embedded patch")))
378
+ (let [nm (check-line-name nm nil)
379
+ evs (box-history (cmd-path) id)
380
+ evs (if (and (empty? evs) (seq (:boxes (:data b)))) [[:load (:data b)]] evs)
381
+ lid (new-line-id)]
382
+ (save-line! lid {:name nm :created (js/Date.now) :archived false})
383
+ (when (seq evs) (write-line-node! lid evs))
384
+ (dispatch! [:batch [[:text id (str "patch @" (ref-slug nm))] [:data id nil]]])
385
+ nm)))
386
+
387
+ ;; a line that changes changes what its users play: the engine's flat patch is made again
388
+ (add-watch hist ::refs (fn [_ _ o n] (when-not (and (identical? (:nodes o) (:nodes n)) (identical? (:other o) (:other n)))
389
+ (reset! eng/flat-cache [nil nil]))))
390
+
391
+ ;; ───────────────────────── the line on screen ─────────────────────────
392
+
393
+ (defn switch-line!
394
+ "Puts line lid on screen: its segments become the history, at the point its tab was left (or its newest).
395
+ The engine goes on playing the line where ▶ was pressed (decision 6): the boxes of the one on screen are
396
+ known to it under the prefix of their line, so nothing of what they show or hold meets the other's. With
397
+ the engine stopped, what the boxes showed is forgotten, as when another work opens. False when it cannot
398
+ be now (in Perform, a lesson or the scratch patch)."
399
+ [lid]
400
+ (cond
401
+ (= lid (active-line)) true
402
+ (or @perf @lesson @scratch) false
403
+ :else
404
+ (let [from (active-line) on? (eng/running?)]
405
+ (swap! line-state (fn [st] (-> st (assoc-in [:pos from] (:pos @world)) (assoc :active lid))))
406
+ (reset! world {:pos nil :auto true :prev-pos nil :jumped-at 0})
407
+ (swap! hist (fn [h] (-> h
408
+ (assoc-in [:other from] (:nodes h))
409
+ (assoc :nodes (get-in h [:other lid] {}))
410
+ (update :other dissoc lid))))
411
+ (reset! cache {})
412
+ (when-not on? (reset-patch-state!))
413
+ (reset! view-path [])
414
+ (swap! ui assoc :sel nil :drag nil :wire nil :merge-pick false)
415
+ (let [saved (get-in @line-state [:pos lid])]
416
+ (swap! world assoc :pos (if (and saved (node (first saved))) saved (newest-tip))))
417
+ (publish-presence!)
418
+ (transport-info!)
419
+ (render-all!)
420
+ (render-lines!)
421
+ ;; the official classes its boxes use and no definition of its own brings (one asked for while
422
+ ;; another line was on screen waits for this one)
423
+ (ensure-classes!)
424
+ true)))
425
+
426
+ ;; ───────────────────────── commands (what the tabs write in the REPL) ─────────────────────────
427
+
428
+ (defn check-line-name [nm except]
429
+ (let [nm (str/trim (str nm))]
430
+ (cond (str/blank? nm) (fail "A line needs a name.")
431
+ (> (count nm) 40) (fail "A line's name: 40 characters at most.")
432
+ (re-find #"[\n\r\"]" nm) (fail "A line's name: one line, without quotes.")
433
+ (some (fn [l] (and (not= l except) (= nm (line-name l)))) (known-lines))
434
+ (fail (str "There is already a line called " (pr-str nm) "."))
435
+ :else nm)))
436
+
437
+ (defn need-line [nm]
438
+ (or (line-by-name nm) (fail (str "There is no line called " (pr-str (str nm)) ". (work-lines) lists them."))))
439
+
440
+ (defn need-switch [lid]
441
+ (when-not (switch-line! lid)
442
+ (fail "Not now: lines change outside Perform, a lesson or the scratch patch.")))
443
+
444
+ (defn go-line
445
+ "(go-line \"sketch\"): puts the line called so on screen (its tab). (go-line) says which one is."
446
+ ([] (line-name (active-line)))
447
+ ([nm]
448
+ (let [lid (need-line nm)]
449
+ (need-switch lid)
450
+ (swap! ui assoc :follow nil)
451
+ (line-name lid))))
452
+
453
+ (defn new-line
454
+ "(new-line \"sketch\"): a new line in this work, empty, with a history of its own; it goes on screen."
455
+ [nm]
456
+ (when-not (:kind @work) (fail "Open or create a work first."))
457
+ (when (or @perf @lesson @scratch) (fail "Not now: lines change outside Perform, a lesson or the scratch patch."))
458
+ (let [nm (check-line-name nm nil) lid (new-line-id)]
459
+ (save-line! lid {:name nm :created (js/Date.now) :archived false})
460
+ (need-switch lid)
461
+ (swap! ui assoc :follow nil)
462
+ nm))
463
+
464
+ (defn rename-line
465
+ "(rename-line \"intro\"): another name for the line on screen. (rename-line \"old\" \"new\") for any line."
466
+ ([nm] (rename-line (line-name (active-line)) nm))
467
+ ([old nm]
468
+ (let [lid (need-line old) nm (check-line-name nm lid) was (line-name lid)]
469
+ ;; the old name stays as an alias: boxes of other lines (and older points) that say patch @old still find it
470
+ (save-line! lid {:name nm :created (or (:created (line-entry lid)) (if (= lid main-line) 0 (js/Date.now)))
471
+ :aliases (vec (distinct (remove #{nm} (conj (vec (:aliases (line-entry lid))) was))))})
472
+ ;; the boxes of the line on screen that use it say the new name
473
+ (when-not (or @perf @scratch)
474
+ (let [evs (for [[path id b] (refs-in (work-patch)) :when (= lid (line-by-ref (eng/line-ref b)))]
475
+ (wrap-in path [:text id (str/join " " (into ["patch" (str "@" (ref-slug nm))] (drop 2 (str/split (str/trim (:text b)) #"\s+"))))]))]
476
+ (when (seq evs) (dispatch-root! [:batch (vec evs)]))))
477
+ (render-lines!)
478
+ nm)))
479
+
480
+ (defn archive-line
481
+ "(archive-line \"sketch\"): hides a line's tab (it is kept, with its history: (restore-line \"sketch\")).
482
+ The main line cannot be archived."
483
+ ([] (archive-line (line-name (active-line))))
484
+ ([nm]
485
+ (let [lid (need-line nm)]
486
+ (when (= lid main-line) (fail "The main line cannot be archived."))
487
+ (when (= lid (active-line)) (need-switch main-line))
488
+ (save-line! lid {:archived true})
489
+ (render-lines!)
490
+ (line-name lid))))
491
+
492
+ (defn restore-line
493
+ "(restore-line \"sketch\"): its tab again, on screen."
494
+ [nm]
495
+ (let [lid (need-line nm)]
496
+ (save-line! lid {:archived false})
497
+ (need-switch lid)
498
+ (line-name lid)))
499
+
500
+ (defn work-lines
501
+ "The lines of this work: {:on \"main\" :lines [\"main\" \"sketch\" …] :archived […]}."
502
+ []
503
+ {:on (line-name (active-line)) :lines (mapv line-name (tab-lines)) :archived (mapv line-name (archived-lines))})
504
+
505
+ ;; ───────────────────────── the tabs ─────────────────────────
506
+
507
+ (defonce line-edit (atom nil)) ; nil, :new, or the id of the line being renamed
508
+
509
+ (defn render-lines! []
510
+ (let [el ($ "line-tabs")
511
+ show? (and (:kind @work) (not @lesson) (not embed-mode))]
512
+ (set! (.-hidden el) (not show?))
513
+ (when show?
514
+ (let [ed @line-edit
515
+ others (remove :same @peers)
516
+ on-line (fn [l] (count (filter (fn [p] (= (or (:line p) main-line) l)) others)))
517
+ busy? (boolean (or @perf @scratch))]
518
+ (set! (.-innerHTML el)
519
+ (str (apply str
520
+ (for [l (tab-lines) :let [sel? (= l (active-line)) n (on-line l)]]
521
+ (if (= ed l)
522
+ (str "<input id=\"line-name-in\" data-line=\"" (esc l) "\" value=\"" (esc (line-name l)) "\" aria-label=\"Name of the line\" spellcheck=\"false\" autocomplete=\"off\">")
523
+ (str "<button class=\"ltab\" role=\"tab\" data-line=\"" (esc l) "\" aria-selected=\"" sel? "\""
524
+ (when (and busy? (not sel?)) " disabled")
525
+ " title=\"" (if sel? "The line on screen · double-click to rename it" (str "Put the line " (esc (line-name l)) " on screen")) "\">"
526
+ (when (= l main-line) "<span class=\"star\" title=\"the main line\">★</span>")
527
+ (when (= l (playing-line)) "<span class=\"plays\" title=\"The engine plays this line (■ stops it, ▶ on another tab plays that one)\">▶</span>")
528
+ (esc (line-name l))
529
+ (when (pos? n) (str "<span class=\"who\" title=\"" n (if (= 1 n) " person" " people") " on this line\">●" (when (> n 1) n) "</span>"))
530
+ (when (and sel? (not= l main-line) (not busy?)) "<span class=\"x\" data-line-archive=\"1\" title=\"Archive this line (it is kept, with its history)\">×</span>")
531
+ "</button>"))))
532
+ (if (= ed :new)
533
+ "<input id=\"line-name-in\" placeholder=\"name of the new line\" aria-label=\"Name of the new line\" spellcheck=\"false\" autocomplete=\"off\">"
534
+ (str "<button class=\"ladd\" id=\"line-add\" title=\"A new line: an empty history of its own, in this work\" aria-label=\"New line\""
535
+ (when busy? " disabled") ">+</button>"))
536
+ (let [arch (archived-lines)]
537
+ (when (seq arch)
538
+ (str "<select id=\"line-restore\" aria-label=\"Archived lines\"><option value=\"\">archived (" (count arch) ")</option>"
539
+ (apply str (for [l arch] (str "<option value=\"" (esc l) "\">restore " (esc (line-name l)) "</option>")))
540
+ "</select>")))))
541
+ (when ed (when-let [i ($ "line-name-in")] (.focus i) (.select i)))))))
542
+
543
+ (defn line-gesture! [code] (swap! line-edit (constantly nil)) (gesture! code))
544
+
545
+ (let [el ($ "line-tabs")]
546
+ (.addEventListener el "click"
547
+ (fn [e]
548
+ (let [t (.-target e)]
549
+ (cond
550
+ (.closest t "[data-line-archive]")
551
+ (line-gesture! "(archive-line)")
552
+ (.closest t "#line-add")
553
+ (do (reset! line-edit :new) (render-lines!))
554
+ :else
555
+ (when-let [b (.closest t "button.ltab")]
556
+ (let [lid (.getAttribute b "data-line")]
557
+ (when (not= lid (active-line)) (line-gesture! (str "(go-line " (pr-str (line-name lid)) ")")))))))))
558
+ (.addEventListener el "dblclick"
559
+ (fn [e]
560
+ (when-let [b (.closest (.-target e) "button.ltab")]
561
+ (let [lid (.getAttribute b "data-line")]
562
+ (when (= lid (active-line)) (reset! line-edit lid) (render-lines!))))))
563
+ (.addEventListener el "change"
564
+ (fn [e]
565
+ (when (= "line-restore" (.. e -target -id))
566
+ (let [lid (.. e -target -value)]
567
+ (when (seq lid) (line-gesture! (str "(restore-line " (pr-str (line-name lid)) ")")))))))
568
+ (.addEventListener el "keydown"
569
+ (fn [e]
570
+ (when (= "line-name-in" (.. e -target -id))
571
+ (let [k (.-key e) v (str/trim (.. e -target -value)) ed @line-edit]
572
+ (cond
573
+ (= k "Escape") (do (reset! line-edit nil) (render-lines!))
574
+ (= k "Enter") (do (.preventDefault e)
575
+ (cond (str/blank? v) (do (reset! line-edit nil) (render-lines!))
576
+ (= ed :new) (line-gesture! (str "(new-line " (pr-str v) ")"))
577
+ (= v (line-name ed)) (do (reset! line-edit nil) (render-lines!))
578
+ :else (line-gesture! (str "(rename-line " (pr-str (line-name ed)) " " (pr-str v) ")"))))
579
+ :else nil)))))
580
+ (.addEventListener el "focusout"
581
+ (fn [e]
582
+ (when (and (= "line-name-in" (.. e -target -id)) @line-edit)
583
+ (js/setTimeout (fn [] (when (and @line-edit (not= ($ "line-name-in") (.-activeElement js/document)))
584
+ (reset! line-edit nil) (render-lines!)))
585
+ 150)))))
586
+
587
+ (add-watch eng/rt ::lines (fn [_ _ o n] (when (not= (:on o) (:on n)) (render-lines!))))
588
+ (add-watch line-state ::render (fn [_ _ o n] (when (not= o n) (render-lines!))))
589
+ (add-watch work ::lines (fn [_ _ o n] (when (not= (select-keys o [:kind :id]) (select-keys n [:kind :id])) (render-lines!))))
590
+ (add-watch peers ::lines (fn [_ _ _ _] (render-lines!)))
591
+ (add-watch perf ::lines (fn [_ _ o n] (when (not= (some? o) (some? n)) (render-lines!))))
592
+ (add-watch lesson ::lines (fn [_ _ _ _] (render-lines!)))
593
+ (add-watch hist ::lines (fn [_ _ o n] (when (not= (set (keys (:other o))) (set (keys (:other n)))) (render-lines!))))
594
+ (render-lines!)