@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,481 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · the page, part 4 of 12: history-view
3
+ ;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
4
+
5
+ (in-ns 'user)
6
+
7
+ ;; ───────────────────────── interface: history ─────────────────────────
8
+
9
+ (def palette ["#2f55d4" "#b4472e" "#2e7d4f" "#8a4fbf" "#b07a12" "#167c88" "#c0457e"])
10
+
11
+ (defn hash-color [s]
12
+ (nth palette (mod (reduce (fn [a c] (mod (+ (* a 31) (.charCodeAt c 0)) 1000003)) 7 (str s)) (count palette))))
13
+
14
+ (defn author-color [a] (or (not-empty (get-in @profiles [a :color])) (hash-color a)))
15
+ (defn session-color [s] (hash-color (str "session" s)))
16
+ (defn peer-color [p] (session-color (or (:session p) (:peer p))))
17
+ (defn person-name [a] (or (not-empty (get-in @profiles [a :name])) (if (= a (my-author)) "you" "someone")))
18
+
19
+ (defn valid-pos? [p] (and (vector? p) (= 2 (count p)) (string? (first p)) (number? (second p))))
20
+
21
+ (defn same-line?
22
+ "Is peer p on the line on screen? (A position is a point of one line: the others' are not drawn here.)"
23
+ [p] (= (or (:line p) main-line) (active-line)))
24
+
25
+ (defn lane-layout
26
+ "One lane per line: the first segment that continues at the tip of another shares its lane;
27
+ the other children (branches) open a new lane."
28
+ []
29
+ (let [ns (vals (:nodes @hist))
30
+ ids (set (map :id ns))
31
+ kids (group-by :parent ns)
32
+ roots (sort-by (fn [n] (or (:t n) 0)) (filter (fn [n] (or (nil? (:parent n)) (not (ids (:parent n))))) ns))
33
+ lane-of (atom {})
34
+ order (atom [])
35
+ counter (atom -1)
36
+ visit (fn visit [n lane seen]
37
+ (when-not (seen (:id n))
38
+ (swap! lane-of assoc (:id n) lane)
39
+ (swap! order conj n)
40
+ (let [cs (sort-by (fn [c] [(:at c) (or (:t c) 0)]) (get kids (:id n)))
41
+ cont (first (filter (fn [c] (= (:at c) (count (:events n)))) cs))
42
+ seen2 (conj seen (:id n))]
43
+ (when cont (visit cont lane seen2))
44
+ (doseq [c cs :when (not= (:id c) (:id cont))]
45
+ (visit c (swap! counter inc) seen2)))))]
46
+ (doseq [r roots] (visit r (swap! counter inc) #{}))
47
+ {:order @order :lane-of @lane-of :lanes (inc @counter)}))
48
+
49
+ (def hx 18)
50
+ (def hy 26)
51
+ (def hx0 30)
52
+ (def hy0 34)
53
+
54
+ (declare mark-picked!) ; 16-release.cljs: the points chosen to be forgotten (issue #45)
55
+
56
+ (defn render-tree! []
57
+ (let [{:keys [order lane-of lanes]} (lane-layout)
58
+ ls order
59
+ svg ($ "tree")
60
+ h @hist
61
+ show-empty (and (empty? ls) (:loaded h) (#{:shared :readonly :solo :local} (:mode h)))]
62
+ (set! (.-hidden ($ "hist-empty")) (not show-empty))
63
+ (set! (.. svg -style -display) (if (empty? ls) "none" "block"))
64
+ (when (seq ls)
65
+ (let [starts (into {} (for [n ls] [(:id n) (start-of (:id n))]))
66
+ X (fn [d] (+ hx0 (* d hx)))
67
+ Y (fn [i] (+ hy0 (* i hy)))
68
+ maxd (apply max 1 (for [n ls] (+ (starts (:id n)) (count (:events n)))))
69
+ w (+ (X maxd) 48)
70
+ ht (+ (Y (dec lanes)) 24)
71
+ ticks (for [d (range 0 (inc maxd) 8)]
72
+ (str "<line class=\"tick\" x1=\"" (X d) "\" x2=\"" (X d) "\" y1=\"20\" y2=\"" (- ht 6) "\"></line>"
73
+ "<text class=\"axis\" x=\"" (X d) "\" y=\"13\" text-anchor=\"middle\">" d "</text>"))
74
+ body (for [n ls]
75
+ (let [i (lane-of (:id n)) s (starts (:id n)) cnt (count (:events n)) y (Y i)
76
+ c (session-color (node-session n)) nid (esc (:id n))
77
+ who (person-name (:author n))
78
+ pl (when (:parent n) (lane-of (:parent n)))
79
+ [mn mk] (when (vector? (:merge n)) (:merge n))
80
+ ml (when mn (lane-of mn))]
81
+ (str (when (and pl (not= pl i))
82
+ (str "<path class=\"fork\" stroke=\"" c "\" d=\"M" (X s) " " (Y pl) " V" y "\"></path>"))
83
+ (when (and ml (starts mn))
84
+ (let [x1 (X (+ (starts mn) mk)) y1 (Y ml) x2 (X (+ s 1))]
85
+ (str "<path class=\"merge\" stroke=\"" c "\" d=\"M" x1 " " y1 " C" (+ x1 30) " " y1 " " (- x2 30) " " y " " x2 " " y "\"></path>")))
86
+ (when (pos? cnt)
87
+ (str "<line class=\"lane\" stroke=\"" c "\" stroke-opacity=\"0.5\" x1=\"" (X s) "\" x2=\"" (X (+ s cnt)) "\" y1=\"" y "\" y2=\"" y "\"></line>"))
88
+ (when-not pl
89
+ (str "<circle class=\"origin\" data-goto=\"" nid "|0\" cx=\"" (X s) "\" cy=\"" y "\" r=\"4.5\"><title>origin</title></circle>"))
90
+ (apply str
91
+ (map-indexed
92
+ (fn [k ev]
93
+ (let [mark? (= :merge-from (first ev))
94
+ ;; a snapshot, or a take kept from Perform: a keyframe of the work
95
+ key? (= :mark (first ev))
96
+ xk (X (+ s k 1))]
97
+ (cond
98
+ key?
99
+ (let [{:keys [label kind]} (when (map? (second ev)) (second ev))]
100
+ (str "<rect class=\"ev keyframe" (when (= kind :take) " take") "\" data-goto=\"" nid "|" (inc k) "\""
101
+ " x=\"" (- xk 5.5) "\" y=\"" (- y 5.5) "\" width=\"11\" height=\"11\" rx=\"2\" stroke=\"" c "\">"
102
+ "<title>" (esc (str who " · " (if (= kind :take) "take kept from Perform" "snapshot")
103
+ (when (seq (str label)) (str ": " label)))) "</title></rect>"))
104
+ :else
105
+ (str "<" (if mark? "rect" "circle") " class=\"ev" (when mark? " mergemark") "\" data-goto=\"" nid "|" (inc k) "\""
106
+ (if mark?
107
+ (str " x=\"" (- xk 5) "\" y=\"" (- y 5) "\" width=\"10\" height=\"10\" transform=\"rotate(45 " xk " " y ")\"")
108
+ (str " cx=\"" xk "\" cy=\"" y "\" r=\"4.5\""))
109
+ " fill=\"" c "\">"
110
+ "<title>" (esc (str who " · " (if mark? "merge" (let [t (pr-str ev)] (if (> (count t) 90) (str (subs t 0 90) "…") t))))) "</title></"
111
+ (if mark? "rect" "circle") ">"))))
112
+ (:events n))))))
113
+ mark (fn [pos r cls color]
114
+ (let [p (norm pos)]
115
+ (when (and p (lane-of (first p)))
116
+ (str "<circle class=\"" cls "\" cx=\"" (X (+ (starts (first p)) (second p))) "\" cy=\"" (Y (lane-of (first p)))
117
+ "\" r=\"" r "\"" (when color (str " stroke=\"" color "\"")) "></circle>"))))
118
+ others (filter (fn [p] (and (not (:same p)) (:pos p) (same-line? p))) @peers)
119
+ peer-marks (map-indexed (fn [k p] (mark (:pos p) (+ 11 (* 3 k)) "peer" (peer-color p))) others)]
120
+ (.setAttribute svg "width" w)
121
+ (.setAttribute svg "height" ht)
122
+ (set! (.-innerHTML svg)
123
+ (str (apply str ticks) (apply str body) (apply str peer-marks) (mark (:pos @world) 8 "here" nil)
124
+ ;; in Perform: the point the take starts from
125
+ (when-let [st @perf] (mark (:origin st) 13 "perform-at" nil))))
126
+ (mark-picked! svg)))))
127
+
128
+ (defn scroll-to-here! []
129
+ (let [p (norm (:pos @world)) wrap ($ "hist-wrap")]
130
+ (when p
131
+ (let [x (+ hx0 (* hx (depth-of p)))
132
+ l (.-scrollLeft wrap) vw (.-clientWidth wrap)]
133
+ (when (or (< x (+ l 30)) (> x (- (+ l vw) 30)))
134
+ (set! (.-scrollLeft wrap) (max 0 (- x (/ vw 2)))))))))
135
+
136
+ (defn render-peers! []
137
+ (let [others (remove (fn [p] (or (:same p) (= "agent" (:kind p)))) @peers)
138
+ f (:follow @ui)]
139
+ (set! (.-innerHTML ($ "peers"))
140
+ (str "<span class=\"chip me\"><span class=\"dot\" style=\"background:" (esc (session-color session-id)) "\"></span>you</span>"
141
+ (apply str
142
+ (for [p others]
143
+ (let [nm (if (:me p) "you, another tab" (or (not-empty (get-in @profiles [(:by p) :name])) "Someone"))
144
+ col (peer-color p)
145
+ on (= f (:peer p))
146
+ nm (if (same-line? p) nm (str nm " · on " (line-name (:line p))))]
147
+ (str "<button class=\"chip" (when on " following") "\" data-peer=\"" (esc (:peer p))
148
+ "\" title=\"" (if on "Stop following" "Follow their position in the history") "\">"
149
+ "<span class=\"dot\" style=\"background:" (esc col) "\"></span>" (esc nm) (when on " · following") "</button>"))))))))
150
+
151
+ (defn render-foot! []
152
+ (let [{:keys [mode nodes]} @hist
153
+ p (norm (:pos @world))
154
+ total (reduce + (map (comp count :events) (vals nodes)))
155
+ n (when p (node (first p)))
156
+ ahead (when n (- (count (:events n)) (second p)))
157
+ mine? (and n (= (node-session n) session-id))
158
+ who (when n (if mine? "by this session" (str "by " (person-name (:author n)) (when (= (:author n) (my-author)) ", another session"))))
159
+ kids (when (and n (zero? ahead)) (children-at (first p) (second p)))]
160
+ (set! (.-innerHTML ($ "hist-foot"))
161
+ (str "<span>" (if p (str "Moment " (depth-of p) " · segment " (esc who)) "No events yet") "</span>"
162
+ "<span>" (count nodes) (if (= 1 (count nodes)) " segment · " " segments · ") total " events</span>"
163
+ (cond
164
+ (:merge-pick @ui)
165
+ "<span class=\"ahead\">Pick in the tree the point of the branch you want to bring here · Esc to cancel</span>"
166
+ (and ahead (pos? ahead))
167
+ (str "<span class=\"ahead\">" ahead (if (= 1 ahead) " event" " events") " further along this branch: if you act here, you open a new branch</span>")
168
+ (seq kids)
169
+ "<span class=\"ahead\">This line continues further on: if you act here, you open a new branch</span>"
170
+ (:auto @world)
171
+ "<span>Following the front: you move forward with anyone's gestures</span>")
172
+ "<span style=\"margin-left:auto\">"
173
+ (case mode
174
+ :shared "Shared room: whoever enters it sees and extends the same tree."
175
+ :solo "Solo: only you see this work."
176
+ :readonly "You can walk through the history; your branches stay in this tab."
177
+ :local "Local mode: the history lives only in this tab."
178
+ :none "Open or create a work from the session manager."
179
+ "Connecting to the shared history…")
180
+ "</span>"))))
181
+
182
+ (defn render-status! []
183
+ (let [{:keys [mode]} @hist p (current) n (count (remove :same @peers))]
184
+ (set! (.-innerHTML ($ "status"))
185
+ (str (case mode
186
+ :shared "<b class=\"on\">● shared room</b>"
187
+ :solo "<b class=\"on\">● solo</b>"
188
+ :readonly "<b class=\"off\">● read only</b>"
189
+ :local "<b class=\"off\">● local</b>"
190
+ :none "<b>○ no work open</b>"
191
+ "<b>○ connecting…</b>")
192
+ " · " (count (:boxes p)) " boxes · " (count (:lines p)) " wires"
193
+ (when (pos? n) (str " · " (inc n) " in the room"))))))
194
+
195
+ (declare render-comp!)
196
+
197
+ ;; ───── data that flows by itself (Study 21) ─────
198
+ ;; A tree box sends its tree whenever it changes (an edit, the history moving, someone in the room), at
199
+ ;; most once per moment and only if it really changed; `tree manual` waits for a bang. A tree box that
200
+ ;; receives edits or trees changes itself through a gesture of the work; one that keeps being rewritten
201
+ ;; by messages (a loop without time) is stopped.
202
+
203
+ (defonce sent-trees (atom {})) ; engine id → the tree it last sent
204
+ (defonce sent-data (atom {})) ; engine id → [text data] a data box (a class with :sends) last sent
205
+ (defonce emit-timer (atom nil))
206
+ (defonce msg-edits (atom {})) ; box → instants it was changed by messages; :stopped #{boxes}
207
+ (defonce echoed (atom {})) ; engine id → the text a gesture here wrote after sending its value
208
+
209
+ (defn drain-prints!
210
+ "What boxes printed (and the loops the engine cut), written in the REPL when the engine is off too."
211
+ []
212
+ (doseq [[_ v label] (eng/take-prints!)]
213
+ (let [t (if (string? v) v (pr-str v))]
214
+ (if (and (nil? label) (re-find #"^(loop cut|too many messages)" t))
215
+ (repl-print! "err" (esc (str "; " t)))
216
+ (repl-print! "out" (esc (print-line v label)))))))
217
+
218
+ (declare sync-live-trees!)
219
+
220
+ (defonce sent-shape (atom nil)) ; the boxes and lines the trees were last sent through
221
+
222
+ (defn reaching
223
+ "The boxes from which one of `goal` can be reached along `lines` (the goal boxes included)."
224
+ [lines goal]
225
+ (loop [acc (set goal)]
226
+ (let [more (into acc (keep (fn [[a _ b _]] (when (acc b) a)) lines))]
227
+ (if (= (count more) (count acc)) acc (recur more)))))
228
+
229
+ (defn emit-trees!
230
+ "Every tree box sends its tree when it changed. When the patch changed (a new line, a box new or with a
231
+ new text), a tree goes again only toward what needs it: along its lines that lead to one of those
232
+ boxes (Study 23). A control whose value stays in its text (:persist, a knob) sends it when that text
233
+ changes: from another tab, a jam or a moment of the history (issue #11); not after a drag here,
234
+ which already sent it."
235
+ []
236
+ (reset! emit-timer nil)
237
+ (let [p (eng/flat (engine-patch))
238
+ shape [(:lines p) (into {} (map (fn [[id b]] [id (:text b)])) (:boxes p))]
239
+ [old-lines old-texts] @sent-shape
240
+ changed (when (not= shape @sent-shape)
241
+ (into (set (for [[f text] (second shape) :when (not= text (get old-texts f))] f))
242
+ (let [olds (set old-lines)] (for [l (:lines p) :when (not (olds l))] (nth l 2)))))
243
+ upstream (when (seq changed) (reaching (:lines p) changed))]
244
+ (when changed
245
+ ;; a box that is new, or whose text changed, starts empty: nothing of what an earlier box with
246
+ ;; that id showed or held
247
+ (doseq [[f text] (second shape) :when (not= text (get old-texts f))]
248
+ (eng/forget! f)
249
+ (swap! live-trees dissoc f)
250
+ (swap! score-cache dissoc f))
251
+ (reset! sent-shape shape)
252
+ (doseq [[f text] (second shape)
253
+ :when (and old-texts (contains? old-texts f) (not= text (get old-texts f))
254
+ (:persist (comp/lookup (klass text))))]
255
+ (if (= text (get @echoed f))
256
+ (swap! echoed dissoc f)
257
+ (try (eng/inject! engine-patch f 0 :bang)
258
+ (catch :default e (js/console.error e))))))
259
+ (doseq [[f b] (:boxes p)
260
+ :when (and (= "tree" (klass (:text b))) (not (some #{:manual} (eng/args-of (:text b)))))]
261
+ (let [t (mtb/tree-of (:data b))]
262
+ (when (some? t)
263
+ (try
264
+ (cond
265
+ ;; the tree changed: it goes everywhere
266
+ (or (not= t (get @sent-trees f)) (contains? changed f))
267
+ (do (swap! sent-trees assoc f t)
268
+ (eng/emit! engine-patch f 0 (mt/expand-source t f)))
269
+ ;; the patch after it changed: only along the lines that lead there
270
+ upstream
271
+ (let [targets (set (for [[a o bb ii] (:lines p) :when (and (= a f) (= o 0) (upstream bb))] [bb ii]))]
272
+ (when (seq targets)
273
+ (eng/emit-to! engine-patch f 0 (mt/expand-source t f) targets))))
274
+ (catch :default e (js/console.error e))))))
275
+ ;; the same for the data boxes of other classes: a class with :sends (fn [box] value) sends that
276
+ ;; value when the text or data of the box changed (a pbind sends its pattern), and again only toward
277
+ ;; what changed after it. A value that is not ready yet (a source still compiling) goes once it is.
278
+ (doseq [[f b] (:boxes p)
279
+ :let [d (comp/lookup (klass (:text b)))]
280
+ :when (fn? (:sends d))]
281
+ (let [k [(:text b) (:data b)]
282
+ v (try ((:sends d) {:id f :text (:text b) :args (comp/args-for d (:text b)) :data (:data b)})
283
+ (catch :default e (js/console.error e) nil))]
284
+ (when (some? v)
285
+ (try
286
+ (cond
287
+ (or (not= k (get @sent-data f)) (contains? changed f))
288
+ (do (swap! sent-data assoc f k)
289
+ (eng/emit! engine-patch f 0 v))
290
+ upstream
291
+ (let [targets (set (for [[a o bb ii] (:lines p) :when (and (= a f) (= o 0) (upstream bb))] [bb ii]))]
292
+ (when (seq targets)
293
+ (eng/emit-to! engine-patch f 0 v targets))))
294
+ (catch :default e (js/console.error e)))))))
295
+ (when (sync-live-trees!) (render-scene (current)))
296
+ (drain-prints!))
297
+
298
+ (defn schedule-emit! [] (when-not @emit-timer (reset! emit-timer (js/setTimeout emit-trees! 30))))
299
+
300
+ (defn refill-data! "Every tree box (and data box) sends its value again (after ■)." [] (reset! sent-trees {}) (reset! sent-data {}) (schedule-emit!))
301
+
302
+ (defn forget-box!
303
+ "A box that is created or deleted starts from nothing: what a box with its id showed and held is forgotten."
304
+ [id]
305
+ (let [f (fid id)]
306
+ (eng/forget! f)
307
+ (swap! live-trees dissoc f)
308
+ (swap! dismissed-trees dissoc f)
309
+ (swap! sent-trees dissoc f)
310
+ (swap! sent-data dissoc f)
311
+ (swap! score-cache dissoc id)))
312
+
313
+ (defn resume!
314
+ "Lets a tree box be changed by messages again, after a loop was stopped: (resume! :b2)."
315
+ [id] (swap! msg-edits (fn [m] (-> m (update :stopped disj id) (dissoc id)))) :ok)
316
+
317
+ (defn tree-effect!
318
+ "A tree box or a canvas asked (by a message) to change: done as a gesture, unless it keeps happening."
319
+ [f [k x] _]
320
+ (js/setTimeout
321
+ (fn []
322
+ (let [id f now (js/Date.now)
323
+ times (filterv (fn [t] (> t (- now 10000))) (get @msg-edits id))]
324
+ (cond
325
+ (not (get-in (current) [:boxes id]))
326
+ (repl-print! "err" (esc (str "; " (name f) " is inside a sub-patch: edits by message reach only the boxes of the patch on screen")))
327
+ (contains? (:stopped @msg-edits) id) nil
328
+ (>= (count times) 20)
329
+ (do (swap! msg-edits update :stopped (fnil conj #{}) id)
330
+ (repl-print! "err" (esc (str "; loop stopped: " id " was rewritten by messages 20 times in 10 seconds. Look for a line that brings its own tree back to it; (resume! " id ") lets messages change it again."))))
331
+ :else
332
+ (do (swap! msg-edits assoc id (conj times now))
333
+ (case k
334
+ :tree-edit (gesture! (str "(tree-edit! " id " " (str/join " " (map pr-str x)) ")"))
335
+ :tree-set (gesture! (str "(tree! " id " " (pr-str x) ")"))
336
+ :canvas-edit (gesture! (str "(canvas-edit! " id " " (pr-str x) ")"))
337
+ ;; what a box found (a task's outcome), kept as its data: only if it can be written back as
338
+ ;; it is (the history is code) and is not too long for the history and the room
339
+ :keep (let [code (str "(keep! " id " " (pr-str x) ")")]
340
+ (cond
341
+ (> (count code) keep-limit)
342
+ (repl-print! "err" (esc (str "; " id ": too long to keep in the work (" (count code) " characters, "
343
+ keep-limit " at most); it is still sent, and kept only in this tab")))
344
+ (not= x (try (clojure.edn/read-string (pr-str x)) (catch :default _ ::unreadable)))
345
+ (repl-print! "err" (esc (str "; " id ": what it found cannot be written as EDN, so it is not kept in the work")))
346
+ :else (gesture! code)))
347
+ nil)))))
348
+ 0))
349
+
350
+ (swap! eng/rt assoc :effect-hook tree-effect!)
351
+ (js/setInterval (fn [] (when-not (eng/running?) (drain-prints!))) 200)
352
+
353
+ (defn sync-live-trees!
354
+ "Takes the trees that canvases and freqts have received from the engine. True if any changed."
355
+ []
356
+ (let [changed (atom false)]
357
+ (doseq [[id b] (:boxes (current)) :when (#{"canvas" "freqt"} (klass (:text b)))]
358
+ (let [f (fid id) t (:tree (second (shown-flat f)))]
359
+ (when (and t (not= t (get @live-trees f)) (not= t (get @dismissed-trees f)))
360
+ (swap! dismissed-trees dissoc f)
361
+ (swap! live-trees assoc f t)
362
+ (reset! changed true))))
363
+ @changed))
364
+
365
+ (defn render-all! []
366
+ (sync-live-trees!)
367
+ (schedule-emit!)
368
+ (let [p (current)
369
+ ;; the classes of the line that plays too, while it plays from another tab (#117)
370
+ changed (lib/sync! (eng/flat (if @scratch (root-patch) (engine-patch))))]
371
+ (when changed (reset! eng/flat-cache [nil nil]))
372
+ ;; the Components tab lists the definitions of the work: drawn again when they or their status change
373
+ (render-comp! true)
374
+ (render-scene p)
375
+ (render-code p)
376
+ (render-tree!)
377
+ (render-peers!)
378
+ (render-foot!)
379
+ (render-status!)
380
+ (update-trust-bar!)))
381
+
382
+ ;; ───────────────────────── presence and following ─────────────────────────
383
+
384
+ (defonce named-room (atom nil))
385
+
386
+ (defn publish-presence! []
387
+ (when-let [room @named-room]
388
+ ;; also with no position (an empty line): the others still learn which line you are on
389
+ (let [p (:pos @world)]
390
+ ;; in a jam (Perform in a shared room): from which point, so others can be invited to it
391
+ (let [{:keys [jam origin]} @perf]
392
+ (.catch (.presence room (clj->js (cond-> {:pos p :session session-id :line (active-line)}
393
+ jam (assoc :jam jam :jamAt (or (:at @perf) origin)))))
394
+ (fn [_] nil))))))
395
+
396
+ (defn sync-follow! []
397
+ (when-let [f (:follow @ui)]
398
+ (if-let [p (first (filter (fn [p] (= (:peer p) f)) @peers))]
399
+ (let [pos (:pos p)]
400
+ ;; following someone onto another line: their tab first (#117)
401
+ (when-not (same-line? p) (switch-line! (:line p)))
402
+ (when (and (valid-pos? pos) (node (first pos)) (not= pos (:pos @world)))
403
+ (swap! world assoc :pos pos)))
404
+ (swap! ui assoc :follow nil))))
405
+
406
+ (defonce asked (atom #{}))
407
+
408
+ (defn want-profiles! []
409
+ (when-let [u @userref]
410
+ (let [ids (disj (set (concat (keep :author (vals (:nodes @hist))) (keep :by @peers))) "local" nil)]
411
+ (when (and (seq ids) (not= ids @asked))
412
+ (reset! asked ids)
413
+ (-> (.profiles u (clj->js (vec ids)))
414
+ (.then (fn [ps]
415
+ (reset! profiles
416
+ (into {} (for [k (array-seq (.keys js/Object ps))]
417
+ (let [p (aget ps k)] [k {:name (.-name p) :color (.-color p)}])))))))))))
418
+
419
+ (defn render-controls! []
420
+ (set! (.-checked ($ "auto")) (boolean (:auto @world)))
421
+ (let [inp ($ "simul")]
422
+ (when-not (= inp (.-activeElement js/document))
423
+ (set! (.-value inp) (str (:simul-ms @settings)))))
424
+ (let [b ($ "merge-btn")]
425
+ (set! (.-textContent b) (if (:merge-pick @ui) "Cancel merge" "Merge…"))
426
+ (.toggle (.-classList b) "primary" (boolean (:merge-pick @ui)))))
427
+
428
+ (add-watch hist ::render (fn [_ _ _ _] (reset! cache {}) (auto-advance!) (render-all!) (want-profiles!)))
429
+ (add-watch world ::render (fn [_ _ o n] (render-all!) (render-controls!) (scroll-to-here!) (when (not= (:pos o) (:pos n)) (publish-presence!))))
430
+ (add-watch settings ::render (fn [_ _ _ _] (render-controls!)))
431
+ (add-watch peers ::render (fn [_ _ _ _] (sync-follow!) (render-tree!) (render-peers!) (render-status!) (want-profiles!)))
432
+ (add-watch profiles ::render (fn [_ _ _ _] (render-tree!) (render-peers!) (render-foot!)))
433
+ (add-watch ui ::render
434
+ (fn [_ _ o n]
435
+ (when (not= (select-keys o [:sel :insp :drag :wire :hover-in :nsel :ldrag]) (select-keys n [:sel :insp :drag :wire :hover-in :nsel :ldrag]))
436
+ (render-scene (current)))
437
+ (when (not= (:follow o) (:follow n)) (render-peers!))
438
+ (when (not= (:merge-pick o) (:merge-pick n)) (render-controls!) (render-foot!))))
439
+
440
+ ;; ───────────────────────── REPL ─────────────────────────
441
+
442
+ (defn repl-eval! [code source]
443
+ ;; a gesture that writes a long source (a package copied into the work) is shown shortened
444
+ (repl-print! (str "in" (when (= source :patch) " from-patch"))
445
+ (str "<span class=\"p\">" (if (= source :patch) "⟵" "λ") "</span>"
446
+ (esc (if (and (= source :patch) (> (count code) 240)) (str (subs code 0 200) " … (" (count code) " characters)") code))))
447
+ (try
448
+ (let [v (eval-str code)]
449
+ (when (= source :repl) (repl-print! "out" (esc (pr-str v)))))
450
+ (catch :default e
451
+ (repl-print! "err" (esc (or (.-message e) (str e)))))))
452
+
453
+ (defn gesture! [code] (repl-eval! code :patch))
454
+
455
+ (defn repl-submit!
456
+ "Evaluates what is written in the REPL line, keeps it in its history and empties the line."
457
+ []
458
+ (let [code (str/trim (repl-text))]
459
+ (when (seq code)
460
+ (swap! ui (fn [u] (-> u (update :hist conj code) (assoc :hidx nil))))
461
+ (set-repl-text! "")
462
+ (repl-eval! code :repl))))
463
+
464
+ (defn repl-recall!
465
+ "↑ / ↓ in the REPL line: the earlier or later expressions written there."
466
+ [up?]
467
+ (let [{:keys [hist hidx]} @ui n (count hist)]
468
+ (when (pos? n)
469
+ (let [i (if up?
470
+ (if (nil? hidx) (dec n) (max 0 (dec hidx)))
471
+ (if (nil? hidx) nil (if (>= (inc hidx) n) nil (inc hidx))))]
472
+ (swap! ui assoc :hidx i)
473
+ (set-repl-text! (if (nil? i) "" (nth hist i)))
474
+ true))))
475
+
476
+ (.addEventListener ($ "repl-in") "keydown"
477
+ (fn [e]
478
+ (let [k (.-key e)]
479
+ (cond
480
+ (= k "Enter") (repl-submit!)
481
+ (or (= k "ArrowUp") (= k "ArrowDown")) (when (repl-recall! (= k "ArrowUp")) (.preventDefault e))))))