@kairotic/app 0.0.0-stage → 0.2.1

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,136 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · page · copy, cut, paste and duplicate boxes (issue #164)
3
+ ;; ⌘/Ctrl+C copies the selected boxes, ⌘/Ctrl+X cuts them, ⌘/Ctrl+V pastes them where the pointer is (keeping
4
+ ;; their layout), ⌘/Ctrl+D duplicates them a little below and to the right; (duplicate :b3 :b4) from code.
5
+ ;;
6
+ ;; What goes on the clipboard is the code of those boxes, as the Code tab writes it — (box …), (connect …), their
7
+ ;; data — so it can be read, pasted in a message, an issue or a document. Its last line holds the same boxes as
8
+ ;; data (;; kairotic-paste {…}): pasting reads that line as EDN, which evaluates nothing, so text copied from
9
+ ;; anywhere cannot run code here. What it holds goes through the same checks as anything that comes from a room.
10
+ ;;
11
+ ;; Pasted boxes get new ids, the next free numbers, and keep their lines among themselves; a line to a box that
12
+ ;; was not copied is kept when that box is in the patch where they are pasted (the same patch, usually). A paste
13
+ ;; is one event of the history.
14
+
15
+ (def clip-mark ";; kairotic-paste ")
16
+
17
+ (defn clip-of
18
+ "The boxes ids of patch p as a clipboard holds them: {:boxes {id box} :lines [lines that touch them]}."
19
+ [p ids]
20
+ (let [ids (set ids)]
21
+ {:boxes (into (sorted-map) (select-keys (:boxes p) ids))
22
+ :lines (vec (filter (fn [[a _ b _]] (or (ids a) (ids b))) (:lines p)))}))
23
+
24
+ (defn clip-text
25
+ "The text a copy of boxes ids of patch p puts on the clipboard: their code, then the same as data."
26
+ [p ids]
27
+ (let [c (clip-of p ids) n (count (:boxes c))]
28
+ (str ";; Kairotic · " n (if (= 1 n) " box" " boxes") " (" (str/join " " (keys (:boxes c)))
29
+ ") · paste on a patch with ⌘/Ctrl+V\n"
30
+ (patch->code c) "\n"
31
+ clip-mark (pr-str c))))
32
+
33
+ (defn read-clip
34
+ "The boxes and lines a clipboard text holds (made safe, as what comes from a room is), or nil if it holds none."
35
+ [text]
36
+ (when-let [line (some (fn [l] (when (str/starts-with? l clip-mark) l)) (str/split-lines (str text)))]
37
+ (let [x (try (clojure.edn/read-string (subs line (count clip-mark))) (catch :default _ nil))]
38
+ (when (and (map? x) (map? (:boxes x)))
39
+ (let [p (sane-patch {:boxes (:boxes x) :lines (if (sequential? (:lines x)) (vec (:lines x)) [])})]
40
+ (when (seq (:boxes p)) (select-keys p [:boxes :lines])))))))
41
+
42
+ (defn clip-corner
43
+ "The top left corner of the boxes of a clipboard."
44
+ [c]
45
+ [(reduce min (map :x (vals (:boxes c)))) (reduce min (map :y (vals (:boxes c))))])
46
+
47
+ (defn paste-patch!
48
+ "Adds the boxes and lines of clipboard c to the patch on screen, moved by dx dy, as one event of the history.
49
+ They get the next free ids; a line to a box that was not copied is kept when that box is here. Selects them
50
+ and returns their ids."
51
+ [c [dx dy]]
52
+ (let [p (current)
53
+ start (inc (reduce max 0 (map id-z (keys (:boxes p)))))
54
+ order (sort-by id-z (keys (:boxes c)))
55
+ m (zipmap order (map (fn [i] (keyword (str "b" (+ start i)))) (range)))
56
+ nb (into {} (for [[id b] (:boxes c)]
57
+ [(m id) (-> b (update :x (fn [x] (max 0 (js/Math.round (+ x dx)))))
58
+ (update :y (fn [y] (max 0 (js/Math.round (+ y dy)))))
59
+ ;; placed in the patch, not in the presentation
60
+ (dissoc :present))]))
61
+ end-of (fn [x] (or (m x) (when (contains? (:boxes p) x) x)))
62
+ nl (vec (distinct (for [[a o b i] (:lines c)
63
+ :let [a2 (end-of a) b2 (end-of b) l [a2 o b2 i]]
64
+ :when (and a2 b2 (not (some #{l} (:lines p))))]
65
+ l)))
66
+ q (-> p (update :boxes into nb) (update :lines into nl))
67
+ ids (vec (sort-by id-z (vals m)))]
68
+ (doseq [id ids] (forget-box! id))
69
+ (dispatch-all! (rel/patch-diff p q))
70
+ (doseq [[_ b] nb] (ensure-class! (:text b)))
71
+ (swap! ui assoc :sel (sel-of ids) :insp nil :nsel nil :tsel nil)
72
+ ids))
73
+
74
+ (defn duplicate
75
+ "Duplicates boxes a little below and to the right, with their lines among themselves (and their lines to the
76
+ others): (duplicate :b3 :b4). One event of the history; returns the new ids."
77
+ [& ids]
78
+ (when (empty? ids) (fail "Which boxes? (duplicate :b3 :b4)"))
79
+ (doseq [id ids] (need-box id))
80
+ (paste-patch! (clip-of (current) ids) [20 20]))
81
+
82
+ ;; ───── the keys: the clipboard's own events, and ⌘/Ctrl+D ─────
83
+
84
+ (defn typing-somewhere?
85
+ "Is something being written or selected on the page: a field, an editor, text selected, a field of an html box?"
86
+ []
87
+ (let [a (.-activeElement js/document)]
88
+ (or (#{"INPUT" "TEXTAREA" "SELECT"} (some-> a .-tagName))
89
+ (some-> a .-isContentEditable)
90
+ (some? (some-> a .-shadowRoot .-activeElement))
91
+ (not (str/blank? (str (.getSelection js/window)))))))
92
+
93
+ (defn clip-ready?
94
+ "Do the clipboard's keys act on boxes now?"
95
+ []
96
+ (and (not (typing-somewhere?)) (not (locked?)) (not embed-mode) (.-hidden ($ "manager"))))
97
+
98
+ (defn copied! [ids verb]
99
+ (repl-print! "out" (esc (str ";; " verb " " (count ids) (if (= 1 (count ids)) " box: " " boxes: ") (str/join " " ids)))))
100
+
101
+ (.addEventListener js/document "copy"
102
+ (fn [e]
103
+ (let [ids (vec (sort-by id-z (sel-ids)))]
104
+ (when (and (seq ids) (clip-ready?))
105
+ (.preventDefault e)
106
+ (.setData (.-clipboardData e) "text/plain" (clip-text (current) ids))
107
+ (copied! ids "copied")))))
108
+
109
+ (.addEventListener js/document "cut"
110
+ (fn [e]
111
+ (let [ids (vec (sort-by id-z (sel-ids)))]
112
+ (when (and (seq ids) (clip-ready?))
113
+ (.preventDefault e)
114
+ (.setData (.-clipboardData e) "text/plain" (clip-text (current) ids))
115
+ (swap! ui assoc :sel nil :insp nil)
116
+ (gesture! (str "(delete " (str/join " " ids) ")"))))))
117
+
118
+ (.addEventListener js/document "paste"
119
+ (fn [e]
120
+ (when (clip-ready?)
121
+ (when-let [c (read-clip (.getData (.-clipboardData e) "text/plain"))]
122
+ (.preventDefault e)
123
+ (let [[x0 y0] (clip-corner c)
124
+ ;; where the pointer is over the patch, or a little below and to the right of where they were
125
+ [tx ty] (or @last-patch-pt [(+ x0 20) (+ y0 20)])
126
+ ids (paste-patch! c [(- tx x0) (- ty y0)])]
127
+ (copied! ids "pasted"))))))
128
+
129
+ (.addEventListener js/document "keydown"
130
+ (fn [e]
131
+ (when (and (or (.-ctrlKey e) (.-metaKey e)) (not (.-shiftKey e)) (not (.-altKey e))
132
+ (= "d" (str/lower-case (.-key e))) (clip-ready?))
133
+ (let [ids (vec (sort-by id-z (sel-ids)))]
134
+ (when (seq ids)
135
+ (.preventDefault e)
136
+ (gesture! (str "(duplicate " (str/join " " ids) ")")))))))
@@ -0,0 +1,74 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · page · boxes that cover each other (issue #166, 2)
3
+ ;; Where a box is drawn is written in the work, but how big it is is only known once it is drawn: a faust~ grows
4
+ ;; with the sliders its code compiles to, a score with the notation Verovio draws. So boxes placed by hand (in a
5
+ ;; help patch, say) can end up under one another.
6
+ ;; (overlaps) the pairs of boxes on screen that cover each other, as they are drawn now
7
+ ;; (tidy) moves boxes down until none covers another, as one event; they keep their order and columns
8
+ ;; (layout-settled?) every box has its size: code boxes compiled, scores drawn (or failed)
9
+ ;; The help patches are checked in CI (tests/helplayout.mjs): none of their steps has boxes over one another.
10
+
11
+ (defn box-rects
12
+ "The boxes on screen as {id [x y w h]}, at the size they are drawn."
13
+ []
14
+ (into {} (for [[id b] (:boxes (scene-of (current)))
15
+ :let [[x y] (box-pos id b) [w h] (box-dims id b)]]
16
+ [id [x y w h]])))
17
+
18
+ (def overlap-margin
19
+ "How much two boxes may share before they cover each other (a line, a port, a pixel of rounding)."
20
+ 3)
21
+
22
+ (defn covers?
23
+ "Do the rectangles [x y w h] a and b share more than a sliver?"
24
+ [[ax ay aw ah] [bx by bw bh]]
25
+ (and (> (- (min (+ ax aw) (+ bx bw)) (max ax bx)) overlap-margin)
26
+ (> (- (min (+ ay ah) (+ by bh)) (max ay by)) overlap-margin)))
27
+
28
+ (defn overlaps
29
+ "The pairs of boxes on screen that cover each other: [[:b2 :b5] …], each pair in the order of its ids."
30
+ ([] (overlaps (box-rects)))
31
+ ([rects]
32
+ (let [ids (vec (sort-by id-z (keys rects)))]
33
+ (vec (for [i (range (count ids)) j (range (inc i) (count ids))
34
+ :let [a (nth ids i) b (nth ids j)]
35
+ :when (covers? (get rects a) (get rects b))]
36
+ [a b])))))
37
+
38
+ (defn layout-settled?
39
+ "Has every box on screen its size: its code compiled (or failed), its score drawn (or failed)?"
40
+ []
41
+ (let [p (scene-of (current))]
42
+ (every? (fn [[id b]]
43
+ (case (display-of b)
44
+ :code (let [d (box-def b) src (str (get-in b [:data :source]))]
45
+ (or (str/blank? src) (not= :compiling (:status (cbox/compiled (:language d) src)))))
46
+ :score (or (= :error @vrv-status) (some? (get @score-cache id)))
47
+ true))
48
+ (:boxes p))))
49
+
50
+ (defn tidy-moves
51
+ "Where boxes go so that none covers another: from the top, each box that covers one placed before it goes
52
+ down below it (and below anything it then meets), in its own column. {id [x y]} of the boxes that move."
53
+ [rects]
54
+ (let [gap 14
55
+ order (sort-by (fn [id] (let [[x y] (get rects id)] [y x (id-z id)])) (keys rects))]
56
+ (loop [[id & more] order placed {} moves {}]
57
+ (if (nil? id)
58
+ moves
59
+ (let [[x y w h] (get rects id)
60
+ y2 (loop [y y n 0]
61
+ (let [hit (filter (fn [[_ r]] (covers? [x y w h] r)) placed)]
62
+ (if (and (seq hit) (< n 200))
63
+ (recur (+ gap (reduce max (map (fn [[_ [_ ry _ rh]]] (+ ry rh)) hit))) (inc n))
64
+ y)))]
65
+ (recur more (assoc placed id [x y2 w h]) (if (not= y2 y) (assoc moves id [x (js/Math.round y2)]) moves)))))))
66
+
67
+ (defn tidy
68
+ "Moves boxes down until none covers another, as they are drawn now (a faust~ with its sliders, a score with its
69
+ notation): (tidy). One event of the history; returns the boxes moved."
70
+ []
71
+ (when @pres-mode (fail "In presentation the boxes are where the performer sees them: (tidy) works on the patch"))
72
+ (let [moves (tidy-moves (box-rects))]
73
+ (dispatch-all! (for [[id [x y]] (sort-by key moves)] [:move id x y]))
74
+ (vec (sort-by id-z (keys moves)))))
@@ -0,0 +1,111 @@
1
+ ;; SPDX-License-Identifier: MPL-2.0
2
+ ;; Kairotic · page · probes on wires (discussion #183)
3
+ ;; To see what travels along a wire, without wiring a print in: ⌥/Alt+click a wire puts a probe on it (again,
4
+ ;; takes it away), or (probe :b1 0 :b2 0). A probed wire is drawn dashed, and a label at its middle shows the last
5
+ ;; value that went along it, flashing when one passes, with how many a second when they come often (12/s).
6
+ ;; Probes are this tab's, as the selection is: they are not part of the work, its history or its code, and two
7
+ ;; people in a room watch their own. What is watched is the outlet a wire leaves from (every wire from it carries
8
+ ;; the same), so the engine keeps only what leaves the outlets watched (kairotic.engine/probe!).
9
+ ;; A wire out of a sub-patch box shows nothing yet (its messages leave from a box inside it); signal wires carry
10
+ ;; no messages.
11
+
12
+ (defonce wire-probes (atom {})) ; [view-path [a o b i]] → [flat-id o], the wires probed here
13
+
14
+ (defn probe-key [[a o _ _]] [(fid a) o])
15
+
16
+ (defn probed? "Is this wire of the patch on screen probed?" [l]
17
+ (contains? @wire-probes [(engine-path) l]))
18
+
19
+ (defn sync-probes!
20
+ "The engine watches the outlets of the probed wires (and forgets what it saw of the others)."
21
+ []
22
+ (let [ks (set (vals @wire-probes))]
23
+ (reset! eng/probes ks)
24
+ (swap! eng/probe-seen select-keys ks)))
25
+
26
+ (defn probe
27
+ "Puts a probe on a wire, or takes it away: (probe :b1 0 :b2 0). The wire shows the last value that went along it,
28
+ and how many a second. Only for this tab: nothing goes into the work. Returns :on or :off."
29
+ [a o b i]
30
+ (let [l [a o b i]]
31
+ (when-not (some #{l} (:lines (current))) (fail (str "There is no line " (pr-str l) " in the patch on screen")))
32
+ (let [k [(engine-path) l]
33
+ on (not (contains? @wire-probes k))]
34
+ (swap! wire-probes (fn [m] (if on (assoc m k (probe-key l)) (dissoc m k))))
35
+ (sync-probes!)
36
+ (render-scene (current))
37
+ (if on :on :off))))
38
+
39
+ (defn unprobe-all!
40
+ "Takes away every probe."
41
+ []
42
+ (reset! wire-probes {})
43
+ (sync-probes!)
44
+ (render-scene (current))
45
+ :ok)
46
+
47
+ (defn probe-summary
48
+ "A value as a probe shows it: short."
49
+ [v]
50
+ (let [cut (fn [s n] (if (> (count s) n) (str (subs s 0 (dec n)) "…") s))]
51
+ (cond
52
+ (nil? v) "nil"
53
+ (number? v) (cv/fmt v)
54
+ (keyword? v) (str v)
55
+ (string? v) (pr-str (cut v 22))
56
+ (mt/node? v) (str "tree · " (count (try (mt/pitches v) (catch :default _ []))) " notes")
57
+ :else (cut (pr-str v) 30))))
58
+
59
+ (defn probe-rate
60
+ "How many messages a second went out of a probed outlet, over the last second."
61
+ [seen]
62
+ (let [now (.now js/performance)]
63
+ (count (filter (fn [t] (< (- now t) 1000)) (:times seen)))))
64
+
65
+ (defn probes-svg
66
+ "The labels of the probes of the patch on screen p, at the middle of their wires."
67
+ [p]
68
+ (let [path (engine-path) lines (set (:lines p))
69
+ mine (filter (fn [[[vp l] _]] (= vp path)) @wire-probes)]
70
+ ;; the wires that are gone (deleted, or back in the history) lose their probe
71
+ (let [gone (keep (fn [[[_ l :as k] _]] (when-not (contains? lines l) k)) mine)]
72
+ (when (seq gone) (js/setTimeout (fn [] (swap! wire-probes (fn [m] (apply dissoc m gone))) (sync-probes!)) 0)))
73
+ (apply str
74
+ (for [[[_ [a o b i :as l]] k] mine :when (contains? lines l)
75
+ :when (and (get-in p [:boxes a]) (get-in p [:boxes b]))]
76
+ (let [[x1 y1] (out-xy p a o) [x2 y2] (in-xy p b i)
77
+ mx (/ (+ x1 x2) 2) my (/ (+ y1 y2) 2)
78
+ seen (get @eng/probe-seen k)
79
+ rate (when seen (probe-rate seen))
80
+ label (cond (nil? seen) "—"
81
+ (and rate (> rate 1)) (str (probe-summary (:v seen)) " · " rate "/s")
82
+ :else (probe-summary (:v seen)))
83
+ hot (and seen (< (- (.now js/performance) (last (:times seen))) 180))
84
+ w (+ 12 (* 6.4 (count label)))]
85
+ (str "<g class=\"probe" (when hot " hot") "\" transform=\"translate(" (js/Math.round mx) "," (js/Math.round my) ")\">"
86
+ "<rect x=\"" (js/Math.round (- (/ w 2))) "\" y=\"-9\" width=\"" (js/Math.round w) "\" height=\"18\" rx=\"4\"></rect>"
87
+ "<text x=\"0\" y=\"4\" text-anchor=\"middle\">" (esc label) "</text></g>"))))))
88
+
89
+ ;; a value seen by a probe is drawn on the next frame (also with the engine stopped)
90
+ (defonce probe-frame (atom false))
91
+ (defonce probe-watch
92
+ (add-watch eng/probe-seen ::draw
93
+ (fn [_ _ _ _]
94
+ (when-not @probe-frame
95
+ (reset! probe-frame true)
96
+ (js/requestAnimationFrame (fn [_] (reset! probe-frame false) (render-overlay!)))))))
97
+
98
+ ;; ⌥/Alt+click on a wire: a probe on it, or away
99
+ (.addEventListener svg "pointerdown"
100
+ (fn [e]
101
+ (when (and (.-altKey e) (not (locked?)) (not (:annot @ui)))
102
+ (when-let [el (closest e ".wire-hit")]
103
+ (let [idx (js/parseInt (.. el -dataset -line) 10)
104
+ l (nth (:lines (scene-of (current))) idx nil)]
105
+ (when l
106
+ (.preventDefault e)
107
+ (.stopImmediatePropagation e)
108
+ (gesture! (str "(probe " (str/join " " l) ")")))))))
109
+ true)
110
+
111
+ (reset! probe-hooks {:probed? probed? :labels probes-svg})