@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.
- 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,172 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · the page, part 13 of 13: feedback
|
|
3
|
+
;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
|
|
4
|
+
|
|
5
|
+
(in-ns 'user)
|
|
6
|
+
|
|
7
|
+
;; ───────────────────────── feedback (0.1.0-alpha) ─────────────────────────
|
|
8
|
+
;; A bug, an idea or a question becomes a GitHub issue (or a Q&A discussion) written for you: the page
|
|
9
|
+
;; only opens GitHub with the text filled in; it is published there, under your own account, or not.
|
|
10
|
+
;; What is added is chosen: the patch on screen (as a link), recent errors, version and browser. The
|
|
11
|
+
;; draft stays in this browser until it is written.
|
|
12
|
+
|
|
13
|
+
(def fb-repo "https://github.com/bonuoq/kairotic")
|
|
14
|
+
(def fb-draft-key "kairotic.feedback-draft")
|
|
15
|
+
|
|
16
|
+
(js/window.addEventListener "error"
|
|
17
|
+
(fn [e] (swap! recent-errors (fn [es] (vec (take-last 10 (conj es (str "page: " (or (.-message e) e)))))))))
|
|
18
|
+
|
|
19
|
+
(defn fb-kind []
|
|
20
|
+
(keyword (.-value (.querySelector js/document "input[name=fb-kind]:checked"))))
|
|
21
|
+
|
|
22
|
+
(def fb-templates
|
|
23
|
+
{:bug "What I did:\n\n\nWhat I expected:\n\n\nWhat happened:\n"
|
|
24
|
+
:idea "What I would like to do:\n\n\nWhy (what it would let me make, play or teach):\n"
|
|
25
|
+
:question ""})
|
|
26
|
+
|
|
27
|
+
(defn fb-edition []
|
|
28
|
+
(cond (.-claude js/window) "claude.ai"
|
|
29
|
+
embed-mode (str "embed (" (name embed-mode) ")")
|
|
30
|
+
(pages?) "Kairotic Pages"
|
|
31
|
+
:else "local"))
|
|
32
|
+
|
|
33
|
+
(defn fb-body
|
|
34
|
+
"Promise of the text of the issue, with what was chosen."
|
|
35
|
+
[]
|
|
36
|
+
(let [text (str/trim (.-value ($ "fb-text")))
|
|
37
|
+
patch? (and (.-checked ($ "fb-patch")) (seq (:boxes (work-patch))))
|
|
38
|
+
errs (when (.-checked ($ "fb-errors")) (seq @recent-errors))
|
|
39
|
+
env? (.-checked ($ "fb-env"))]
|
|
40
|
+
(-> (if patch?
|
|
41
|
+
(.then ((.-kairoticPack js/window) (patch->code (work-patch))) (fn [z] (url-with [["code" z]] nil)))
|
|
42
|
+
(js/Promise.resolve nil))
|
|
43
|
+
(.then (fn [link]
|
|
44
|
+
(let [link (when (and link (< (count link) 5000)) link)]
|
|
45
|
+
(str text "\n"
|
|
46
|
+
(when link (str "\n**The patch:** [open it in Kairotic](" link ")\n"))
|
|
47
|
+
(when (and patch? (nil? link)) "\n_The patch is too large for a link: paste its code (Code tab) here._\n")
|
|
48
|
+
(when errs (str "\n<details><summary>Recent errors</summary>\n\n```\n" (str/join "\n" (map #(ai/cut % 300) errs)) "\n```\n</details>\n"))
|
|
49
|
+
(when env? (str "\n---\nKairotic " version " · " (fb-edition) " · " (.-userAgent js/navigator) "\n")))))))))
|
|
50
|
+
|
|
51
|
+
(defn fb-save-draft! []
|
|
52
|
+
(try (.setItem js/localStorage fb-draft-key
|
|
53
|
+
(js/JSON.stringify #js {:kind (name (fb-kind)) :title (.-value ($ "fb-title")) :text (.-value ($ "fb-text"))}))
|
|
54
|
+
(catch :default _ nil)))
|
|
55
|
+
|
|
56
|
+
(defn render-fb-preview! []
|
|
57
|
+
(set! (.-textContent ($ "fb-errn")) (str "(" (count @recent-errors) ")"))
|
|
58
|
+
(set! (.-placeholder ($ "fb-text"))
|
|
59
|
+
(case (fb-kind)
|
|
60
|
+
:bug "What you did, what you expected, what happened. The steps to make it happen again help most."
|
|
61
|
+
:idea "What you would like Kairotic to do, and what for."
|
|
62
|
+
"Your question."))
|
|
63
|
+
(set! (.-textContent ($ "fb-go")) (if (= :question (fb-kind)) "Ask it on GitHub…" "Write it on GitHub…"))
|
|
64
|
+
(.then (fb-body) (fn [b] (set! (.-textContent ($ "fb-preview")) b))))
|
|
65
|
+
|
|
66
|
+
(defn fb-url [title body]
|
|
67
|
+
(let [q (js/URLSearchParams.) kind (fb-kind)]
|
|
68
|
+
(.set q "title" (if (seq title) title (case kind :bug "Bug: " :idea "Idea: " "Question: ")))
|
|
69
|
+
(.set q "body" body)
|
|
70
|
+
(if (= kind :question)
|
|
71
|
+
(do (.set q "category" "q-a") (str fb-repo "/discussions/new?" (.toString q)))
|
|
72
|
+
(do (.set q "labels" (if (= kind :bug) "bug" "enhancement")) (str fb-repo "/issues/new?" (.toString q))))))
|
|
73
|
+
|
|
74
|
+
(.addEventListener ($ "fb-go") "click"
|
|
75
|
+
(fn [_]
|
|
76
|
+
(let [w (js/window.open "about:blank" "_blank") title (str/trim (.-value ($ "fb-title")))]
|
|
77
|
+
;; the window opens at the click (popup blockers allow it), then gets its address
|
|
78
|
+
(.then (fb-body)
|
|
79
|
+
(fn [b] (let [u (fb-url title b)] (if w (set! (.. w -location -href) u) (js/window.open u "_blank")))
|
|
80
|
+
;; written: the draft starts again
|
|
81
|
+
(set! (.-value ($ "fb-title")) "")
|
|
82
|
+
(set! (.-value ($ "fb-text")) (get fb-templates (fb-kind)))
|
|
83
|
+
(fb-save-draft!))))))
|
|
84
|
+
|
|
85
|
+
(.addEventListener ($ "fb-copy") "click"
|
|
86
|
+
(fn [_]
|
|
87
|
+
(-> (fb-body)
|
|
88
|
+
(.then (fn [b] (copy-text! (str "# " (.-value ($ "fb-title")) "\n\n" b))))
|
|
89
|
+
(.then (fn [ok] (set! (.-textContent ($ "fb-copy")) (if ok "Copied" "Not copied"))
|
|
90
|
+
(js/setTimeout (fn [] (set! (.-textContent ($ "fb-copy")) "Copy")) 1500))))))
|
|
91
|
+
|
|
92
|
+
(.addEventListener (.querySelector js/document ".fb-kind") "change"
|
|
93
|
+
(fn [_]
|
|
94
|
+
;; an untouched template follows the kind
|
|
95
|
+
(when (or (str/blank? (.-value ($ "fb-text"))) (some #(= (.-value ($ "fb-text")) %) (vals fb-templates)))
|
|
96
|
+
(set! (.-value ($ "fb-text")) (get fb-templates (fb-kind))))
|
|
97
|
+
(fb-save-draft!)
|
|
98
|
+
(render-fb-preview!)))
|
|
99
|
+
|
|
100
|
+
(doseq [id ["fb-title" "fb-text"]]
|
|
101
|
+
(.addEventListener ($ id) "input" (fn [_] (fb-save-draft!))))
|
|
102
|
+
(doseq [id ["fb-text" "fb-patch" "fb-errors" "fb-env"]]
|
|
103
|
+
(.addEventListener ($ id) "change" (fn [_] (render-fb-preview!))))
|
|
104
|
+
|
|
105
|
+
;; the open issues, read from GitHub's public API (where the page may reach it)
|
|
106
|
+
(defonce fb-issues (atom nil))
|
|
107
|
+
|
|
108
|
+
;; ───── an issue, to review and fix with Claude ─────
|
|
109
|
+
;; Each issue has a button that opens a new Claude chat (claude.ai/new) with a prompt about that issue
|
|
110
|
+
;; already written: you read it, change it if you like, and send it. Nothing is sent from here. The prompt
|
|
111
|
+
;; stays short, because it travels in the address.
|
|
112
|
+
|
|
113
|
+
(def claude-chat-url "https://claude.ai/new")
|
|
114
|
+
(def fb-repo-slug "bonuoq/kairotic")
|
|
115
|
+
|
|
116
|
+
(defn claude-prompt
|
|
117
|
+
"What Claude is asked about an issue: to read it in full on GitHub, review it and fix it in a pull request."
|
|
118
|
+
[{:keys [number title url body labels]}]
|
|
119
|
+
(str "Review issue #" number " of the GitHub repository " fb-repo-slug " (" fb-repo "): \"" title "\"" (when (seq labels) (str " (" (str/join ", " labels) ")")) ".\n"
|
|
120
|
+
url "\n\n"
|
|
121
|
+
(when (seq (str/trim (str body)))
|
|
122
|
+
(str "It says:\n" (ai/cut (str/trim (str body)) 900) "\n\n"))
|
|
123
|
+
"Read the whole issue and its comments on GitHub first. Then tell me what you understand and how you would "
|
|
124
|
+
"solve it; if it is clear, fix it on a new branch, following the conventions of the repository (CHANGELOG, "
|
|
125
|
+
"the guide in English and Spanish, tests in tests/), check that the tests pass, and open a pull request that "
|
|
126
|
+
"closes the issue."))
|
|
127
|
+
|
|
128
|
+
(defn claude-url [issue]
|
|
129
|
+
(let [q (js/URLSearchParams.)]
|
|
130
|
+
(.set q "q" (claude-prompt issue))
|
|
131
|
+
(str claude-chat-url "?" (.toString q))))
|
|
132
|
+
|
|
133
|
+
(defn render-fb-issues! []
|
|
134
|
+
(let [is @fb-issues]
|
|
135
|
+
(set! (.-innerHTML ($ "fb-list"))
|
|
136
|
+
(cond
|
|
137
|
+
(nil? is) "<li class=\"soft\">…</li>"
|
|
138
|
+
(= :error is) (str "<li class=\"soft\">They could not be read from here: <a href=\"" fb-repo "/issues\" target=\"_blank\" rel=\"noopener\">see them on GitHub</a>.</li>")
|
|
139
|
+
(empty? is) "<li class=\"soft\">None yet.</li>"
|
|
140
|
+
:else (str (apply str (for [i is]
|
|
141
|
+
(str "<li><a href=\"" (esc (:url i)) "\" target=\"_blank\" rel=\"noopener\">" (esc (:title i)) "</a>"
|
|
142
|
+
(when (seq (:labels i)) (str "<span class=\"lb\">" (esc (str/join " · " (:labels i))) "</span>"))
|
|
143
|
+
"<a class=\"fb-claude\" href=\"" (esc (claude-url i)) "\" target=\"_blank\" rel=\"noopener\""
|
|
144
|
+
" title=\"Open a Claude chat with a prompt to review and fix this issue (you send it)\">Claude ↗</a>"
|
|
145
|
+
"</li>")))
|
|
146
|
+
"<li class=\"soft\"><a href=\"" fb-repo "/issues\" target=\"_blank\" rel=\"noopener\">all of them…</a></li>")))))
|
|
147
|
+
|
|
148
|
+
(defn load-fb-issues! []
|
|
149
|
+
(when (or (nil? @fb-issues) (= :error @fb-issues))
|
|
150
|
+
(-> (js/fetch "https://api.github.com/repos/bonuoq/kairotic/issues?state=open&per_page=20")
|
|
151
|
+
(.then (fn [r] (if (.-ok r) (.json r) (throw (js/Error. (str (.-status r)))))))
|
|
152
|
+
(.then (fn [a]
|
|
153
|
+
(reset! fb-issues
|
|
154
|
+
(vec (for [i (array-seq a) :when (nil? (.-pull_request i))]
|
|
155
|
+
{:title (.-title i) :url (.-html_url i) :number (.-number i) :body (.-body i)
|
|
156
|
+
:labels (vec (for [l (array-seq (.-labels i))] (.-name l)))})))))
|
|
157
|
+
(.catch (fn [_] (reset! fb-issues :error)))
|
|
158
|
+
(.finally (fn [] (render-fb-issues!))))))
|
|
159
|
+
|
|
160
|
+
(.addEventListener ($ "tab-feedback") "click" (fn [_] (render-fb-preview!) (load-fb-issues!)))
|
|
161
|
+
|
|
162
|
+
;; the draft that was being written
|
|
163
|
+
(let [d (try (some-> (.getItem js/localStorage fb-draft-key) js/JSON.parse) (catch :default _ nil))]
|
|
164
|
+
(when-let [k (some-> d .-kind)]
|
|
165
|
+
(when-let [r (.querySelector js/document (str "input[name=fb-kind][value=" k "]"))] (set! (.-checked r) true)))
|
|
166
|
+
(set! (.-value ($ "fb-title")) (or (some-> d .-title) ""))
|
|
167
|
+
(set! (.-value ($ "fb-text")) (or (some-> d .-text) (get fb-templates (fb-kind)))))
|
|
168
|
+
|
|
169
|
+
(defn feedback
|
|
170
|
+
"Opens the Feedback tab: (feedback), or with a title (feedback \"the score does not redraw\")."
|
|
171
|
+
([] (.click ($ "tab-feedback")) :feedback)
|
|
172
|
+
([title] (set! (.-value ($ "fb-title")) (str title)) (fb-save-draft!) (feedback)))
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · the page, part 14 of 14: perform
|
|
3
|
+
;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
|
|
4
|
+
|
|
5
|
+
(in-ns 'user)
|
|
6
|
+
|
|
7
|
+
;; ───────────────────────── Compose and Perform (issue #5) ─────────────────────────
|
|
8
|
+
;; Compose is the usual mode: every change to the work is a point of its history.
|
|
9
|
+
;; Perform opens a time of its own to rehearse, try and improvise with the work: the changes make a take
|
|
10
|
+
;; that starts from a point of the history and stays out of it (the patch on screen and in the engine is
|
|
11
|
+
;; the take). ◀ / Ctrl+Z take back your last changes of the take. Going back to Compose keeps the take as
|
|
12
|
+
;; a new point (a branch from where it started, closed by a keyframe) or discards it and goes back to
|
|
13
|
+
;; where you were. In a shared room, those who perform from the same point share one take: a jam, where
|
|
14
|
+
;; only they receive each other's changes; whoever is not performing is invited to join it.
|
|
15
|
+
;; A snapshot, in either mode, writes a keyframe: a point of the history with the work as it is now.
|
|
16
|
+
;; The state of the take is `perf` (00-work.cljs); the take is not kept if the page is reloaded.
|
|
17
|
+
|
|
18
|
+
(defn layout-ev?
|
|
19
|
+
"A change of layout only: a box moved, placed in presentation or shown in another view (also inside a sub-patch)."
|
|
20
|
+
[p [op & args]]
|
|
21
|
+
(case op
|
|
22
|
+
(:move :present :view :size) true
|
|
23
|
+
:batch (let [[evs] args] (boolean (and (sequential? evs) (seq evs) (every? (fn [e] (and (vector? e) (layout-ev? p e))) evs))))
|
|
24
|
+
:in (let [[id inner] args b (get-in p [:boxes id])]
|
|
25
|
+
(boolean (and (embedded? b) (vector? inner) (layout-ev? (or (:data b) empty-patch) inner))))
|
|
26
|
+
false))
|
|
27
|
+
|
|
28
|
+
(defn take-summary
|
|
29
|
+
"How many changes a take has, and how many of them change the work (the rest only move boxes)."
|
|
30
|
+
[st]
|
|
31
|
+
(let [evs (map :ev (perf-events st))
|
|
32
|
+
lay (count (filter (fn [e] (layout-ev? (:base st) e)) evs))]
|
|
33
|
+
{:changes (count evs) :layout lay :work (- (count evs) lay)}))
|
|
34
|
+
|
|
35
|
+
(defn moment-name [pos] (if-let [p (norm pos)] (str "moment " (depth-of p)) "the start"))
|
|
36
|
+
|
|
37
|
+
;; ───── the jam: one document per performer in <work>/jams, with the changes of their take ─────
|
|
38
|
+
|
|
39
|
+
(defonce jam-unsub (atom nil))
|
|
40
|
+
(defonce jam-seen (atom #{})) ; jams already offered here (or your own)
|
|
41
|
+
|
|
42
|
+
;; the jams of the line on screen (the main line's are where they always were)
|
|
43
|
+
(defn jam-coll [] (when (and (= :room (:kind @work)) (persist?)) (str (line-base) "/jams")))
|
|
44
|
+
|
|
45
|
+
(defn new-jam-id []
|
|
46
|
+
(str "j" (.toString (js/Date.now) 36) (.toString (js/Math.floor (* (js/Math.random) 1679616)) 36)))
|
|
47
|
+
|
|
48
|
+
(defn jam-push!
|
|
49
|
+
"Writes your changes of the take where the others in the jam read them."
|
|
50
|
+
[]
|
|
51
|
+
(when-let [{:keys [jam jam-path at own]} @perf]
|
|
52
|
+
(when (and jam jam-path @dbref)
|
|
53
|
+
(let [body (clj->js {:jam jam :at at :session session-id :author (my-author) :t (js/Date.now)
|
|
54
|
+
:events (mapv (fn [e] (enc [(:t e) (:ev e)])) own)})]
|
|
55
|
+
(enqueue! jam-path (fn [] (.set (.doc @dbref jam-path) body)))))))
|
|
56
|
+
|
|
57
|
+
;; ───── messages played in the jam (issue #11) ─────
|
|
58
|
+
;; A knob turned, a number dragged, a msg clicked are messages, not changes of the take: they are written,
|
|
59
|
+
;; a few at a time, to a second document of each performer (<jam>-<session>-live, the last ones), and the
|
|
60
|
+
;; others play them in their engines as they arrive. What was there before you joined is not played.
|
|
61
|
+
|
|
62
|
+
(def live-keep 48)
|
|
63
|
+
(defonce live-buf (atom [])) ; your last messages: [{:t :f :i :v}]
|
|
64
|
+
(defonce live-seen (atom {})) ; session → the newest message of theirs already played here
|
|
65
|
+
(defonce live-timer (atom nil))
|
|
66
|
+
|
|
67
|
+
(defn live-path [] (when-let [p (:jam-path @perf)] (str p "-live")))
|
|
68
|
+
|
|
69
|
+
(defn live-flush! []
|
|
70
|
+
(reset! live-timer nil)
|
|
71
|
+
(when-let [path (live-path)]
|
|
72
|
+
(when @dbref
|
|
73
|
+
(let [body (clj->js {:jam (:jam @perf) :kind "live" :session session-id :t (js/Date.now)
|
|
74
|
+
:msgs (mapv (fn [{:keys [t f i v]}] (enc [t f i v])) @live-buf)})]
|
|
75
|
+
(enqueue! path (fn [] (.set (.doc @dbref path) body)))))))
|
|
76
|
+
|
|
77
|
+
(defn played!
|
|
78
|
+
"A message played here: in a jam, it goes to the others too (sent every 60 ms at most)."
|
|
79
|
+
[f inlet v]
|
|
80
|
+
(when (and (:jam @perf) (:jam-path @perf))
|
|
81
|
+
(swap! live-buf (fn [b] (vec (take-last live-keep (conj b {:t (js/Date.now) :f f :i inlet :v v})))))
|
|
82
|
+
(when-not @live-timer (reset! live-timer (js/setTimeout live-flush! 60)))))
|
|
83
|
+
|
|
84
|
+
(defn play-live!
|
|
85
|
+
"Plays here the new messages of another performer of the jam."
|
|
86
|
+
[s msgs]
|
|
87
|
+
(let [ms (sort-by first (filter (fn [[t f i]] (and (number? t) (keyword? f) (number? i))) msgs))
|
|
88
|
+
newest (reduce max 0 (map first ms))
|
|
89
|
+
seen (get @live-seen s)]
|
|
90
|
+
(when (seq ms)
|
|
91
|
+
(swap! live-seen assoc s (max newest (or seen 0)))
|
|
92
|
+
;; the first time: what was played before you came is not played again
|
|
93
|
+
(when seen
|
|
94
|
+
(let [p (work-patch)]
|
|
95
|
+
(doseq [[t f i v] ms :when (> t seen)]
|
|
96
|
+
(when (get-in (eng/flat p) [:boxes f])
|
|
97
|
+
;; played by someone else: what a task of theirs finds is theirs to keep (eng/*remote*)
|
|
98
|
+
(try (binding [eng/*remote* true] (eng/inject! engine-patch f i v))
|
|
99
|
+
(catch :default e (js/console.error "jam message" e))))))
|
|
100
|
+
(render-overlay!)))))
|
|
101
|
+
|
|
102
|
+
(defn on-jam
|
|
103
|
+
"The documents of the jams of this work: the changes of the others in yours join the take, and their
|
|
104
|
+
messages are played here."
|
|
105
|
+
[snap]
|
|
106
|
+
(when-let [{:keys [jam]} @perf]
|
|
107
|
+
(doseq [d (array-seq (.-docs snap)) :when (.-exists d)
|
|
108
|
+
:let [m (js->clj (.data d)) s (get m "session")]
|
|
109
|
+
:when (and (= jam (get m "jam")) (= "live" (get m "kind")) (string? s) (not= s session-id))]
|
|
110
|
+
(play-live! s (map dec* (get m "msgs"))))
|
|
111
|
+
(let [others (into {} (for [d (array-seq (.-docs snap)) :when (.-exists d)
|
|
112
|
+
:let [m (js->clj (.data d)) s (get m "session")]
|
|
113
|
+
:when (and (= jam (get m "jam")) (not= "live" (get m "kind")) (string? s) (not= s session-id))]
|
|
114
|
+
[s (vec (keep (fn [x]
|
|
115
|
+
(let [[t ev] (dec* x)]
|
|
116
|
+
(when (and (number? t) (vector? ev) (keyword? (first ev))) {:t t :s s :ev ev})))
|
|
117
|
+
(get m "events")))]))]
|
|
118
|
+
(when (not= others (:others @perf))
|
|
119
|
+
(swap! perf (fn [st] (when st (perf-recompute (assoc st :others others)))))))))
|
|
120
|
+
|
|
121
|
+
(defn jam-listen! []
|
|
122
|
+
(when-let [f @jam-unsub] (f))
|
|
123
|
+
(reset! jam-unsub (when-let [c (jam-coll)] (.onSnapshot (.collection @dbref c) on-jam db-failed!))))
|
|
124
|
+
|
|
125
|
+
(defn jam-at
|
|
126
|
+
"The jam someone is playing from this point of the history, if any."
|
|
127
|
+
[pos]
|
|
128
|
+
(some (fn [p] (when (and (:jam p) (not (:same p)) (same-line? p) (= (norm (:jam-at p)) pos)) (:jam p))) @peers))
|
|
129
|
+
|
|
130
|
+
(defn jam-mates
|
|
131
|
+
"Who else is in your jam now."
|
|
132
|
+
[]
|
|
133
|
+
(when-let [j (:jam @perf)] (filter (fn [p] (and (= j (:jam p)) (not (:same p)))) @peers)))
|
|
134
|
+
|
|
135
|
+
;; ───── commands ─────
|
|
136
|
+
|
|
137
|
+
(defn perform
|
|
138
|
+
"Perform: rehearse, try and improvise outside the history, from where you are. In a shared room, with
|
|
139
|
+
those performing from this same point (a jam). (compose) goes back, keeping the take or not."
|
|
140
|
+
([] (perform nil))
|
|
141
|
+
([jam-id]
|
|
142
|
+
(when @perf (fail "You are already in Perform: (compose) to go back."))
|
|
143
|
+
(when (#{:connecting :none} (:mode @hist)) (fail "Open a work before performing."))
|
|
144
|
+
(stop-follow!)
|
|
145
|
+
(let [pos (norm (:pos @world))
|
|
146
|
+
base (work-patch)
|
|
147
|
+
coll (jam-coll)
|
|
148
|
+
jam (when coll (or jam-id (jam-at pos) (new-jam-id)))]
|
|
149
|
+
(reset! perf {:from pos :origin pos :at pos :base base :patch base :own [] :others {} :redo [] :cut 0
|
|
150
|
+
:jam jam :jam-path (when jam (str coll "/" jam "-" session-id))
|
|
151
|
+
;; the lines it follows stay where they are now while performing (#117, decision 4)
|
|
152
|
+
:pins (followed-fronts)})
|
|
153
|
+
(when jam
|
|
154
|
+
(swap! jam-seen conj jam)
|
|
155
|
+
(jam-listen!)
|
|
156
|
+
(jam-push!))
|
|
157
|
+
(publish-presence!)
|
|
158
|
+
(cond-> {:perform-from (moment-name pos)}
|
|
159
|
+
jam (assoc :jam jam)))))
|
|
160
|
+
|
|
161
|
+
(defn jam
|
|
162
|
+
"Joins the jam of someone performing in this room: from the same point of the history, with them."
|
|
163
|
+
[id]
|
|
164
|
+
(let [p (first (filter (fn [p] (= (:jam p) id)) @peers))]
|
|
165
|
+
(cond
|
|
166
|
+
(nil? p) (fail "Nobody is performing in that jam now.")
|
|
167
|
+
(= id (:jam @perf)) (fail "You are already in that jam.")
|
|
168
|
+
@perf (fail "You are in Perform: (compose) first, then join the jam.")
|
|
169
|
+
(not (and (:jam-at p) (node (first (:jam-at p))))) (fail "That point of the history has not arrived here yet: try again in a moment."))
|
|
170
|
+
(stop-follow!)
|
|
171
|
+
(swap! world assoc :pos (norm (:jam-at p)) :auto false :prev-pos nil)
|
|
172
|
+
(perform id)))
|
|
173
|
+
|
|
174
|
+
(declare show-leave-dialog! hide-leave-dialog!)
|
|
175
|
+
|
|
176
|
+
(defn end-perform!
|
|
177
|
+
"Leaves Perform (and the jam): returns the take as it was."
|
|
178
|
+
[]
|
|
179
|
+
(let [st @perf]
|
|
180
|
+
(when-let [f @jam-unsub] (f) (reset! jam-unsub nil))
|
|
181
|
+
(when-let [path (:jam-path st)]
|
|
182
|
+
(when @dbref
|
|
183
|
+
(enqueue! path (fn [] (.delete (.doc @dbref path))))
|
|
184
|
+
(let [lp (str path "-live")] (enqueue! lp (fn [] (.delete (.doc @dbref lp)))))))
|
|
185
|
+
(when-let [t @live-timer] (js/clearTimeout t) (reset! live-timer nil))
|
|
186
|
+
(reset! live-buf [])
|
|
187
|
+
(reset! live-seen {})
|
|
188
|
+
(reset! perf nil)
|
|
189
|
+
(hide-leave-dialog!)
|
|
190
|
+
(publish-presence!)
|
|
191
|
+
st))
|
|
192
|
+
|
|
193
|
+
(defn write-take!
|
|
194
|
+
"Writes the changes of a take since its last snapshot as a new segment from where it starts, closed by
|
|
195
|
+
`mark`; returns the position after it."
|
|
196
|
+
[st mark]
|
|
197
|
+
(let [evs (vec (map :ev (perf-events st))) origin (:origin st)
|
|
198
|
+
id (new-node! (first origin) (if origin (second origin) 0) (conj evs mark) nil)]
|
|
199
|
+
[id (inc (count evs))]))
|
|
200
|
+
|
|
201
|
+
(defn compose
|
|
202
|
+
"Back to Compose. (compose) asks what to do with the take, if it has changes; (compose :keep) writes it
|
|
203
|
+
into the history as a new point, a branch from where it started; (compose :discard) drops it and goes
|
|
204
|
+
back to where you were before performing."
|
|
205
|
+
([] (when-not @perf (fail "You are already in Compose."))
|
|
206
|
+
(if (pos? (:changes (take-summary @perf)))
|
|
207
|
+
(do (show-leave-dialog!) :choose)
|
|
208
|
+
(compose :discard)))
|
|
209
|
+
([how]
|
|
210
|
+
(when-not @perf (fail "You are already in Compose."))
|
|
211
|
+
(case how
|
|
212
|
+
:keep (let [n (:changes (take-summary @perf))
|
|
213
|
+
st (end-perform!)]
|
|
214
|
+
(if (pos? n)
|
|
215
|
+
(let [tip (write-take! st [:mark {:kind :take}])]
|
|
216
|
+
(swap! world assoc :pos tip :auto true :prev-pos nil)
|
|
217
|
+
{:kept n :at (moment-name tip)})
|
|
218
|
+
(do (swap! world assoc :pos (:origin st) :prev-pos nil) {:kept 0})))
|
|
219
|
+
:discard (let [st (end-perform!) from (:from st)]
|
|
220
|
+
(swap! world assoc :pos from :auto (if from (leaf? from) true) :prev-pos nil)
|
|
221
|
+
{:back-to (moment-name from)})
|
|
222
|
+
(fail "Use (compose), (compose :keep) or (compose :discard)."))))
|
|
223
|
+
|
|
224
|
+
(defn snapshot
|
|
225
|
+
"A keyframe: a point of the history with the work as it is now, in Compose or in Perform (where the take
|
|
226
|
+
so far is written, and goes on from there). (snapshot \"intro\") names it."
|
|
227
|
+
([] (snapshot nil))
|
|
228
|
+
([label]
|
|
229
|
+
(let [fronts (followed-fronts)
|
|
230
|
+
;; where the lines it follows are, so this point plays them as they are now (#117, decision 5)
|
|
231
|
+
mark [:mark (cond-> {:kind :snapshot} (some? label) (assoc :label (str label)) (seq fronts) (assoc :fronts fronts))]]
|
|
232
|
+
(if-let [st @perf]
|
|
233
|
+
(let [evs (perf-events st)
|
|
234
|
+
tip (write-take! st mark)]
|
|
235
|
+
;; the take goes on from the snapshot: what it had is now in the history
|
|
236
|
+
(swap! perf (fn [s] (assoc s :origin tip :base (:patch s) :redo []
|
|
237
|
+
:cut (reduce max (or (:cut s) 0) (map :t evs)))))
|
|
238
|
+
(swap! world assoc :pos tip :auto false :prev-pos nil)
|
|
239
|
+
{:snapshot (moment-name tip) :changes (count evs)})
|
|
240
|
+
(do (dispatch! mark) (here))))))
|
|
241
|
+
|
|
242
|
+
;; ───── interface: the mode button, what the take has, leaving it, invitations ─────
|
|
243
|
+
|
|
244
|
+
(defn floating [id cls]
|
|
245
|
+
(or ($ id)
|
|
246
|
+
(let [el (js/document.createElement "div")]
|
|
247
|
+
(set! (.-id el) id)
|
|
248
|
+
(set! (.-className el) cls)
|
|
249
|
+
(set! (.-hidden el) true)
|
|
250
|
+
(.appendChild (.-body js/document) el)
|
|
251
|
+
el)))
|
|
252
|
+
|
|
253
|
+
(defn hide-leave-dialog! [] (when-let [el ($ "perform-pop")] (set! (.-hidden el) true)))
|
|
254
|
+
|
|
255
|
+
(defn show-leave-dialog! []
|
|
256
|
+
(let [st @perf {:keys [changes layout work]} (take-summary st) el (floating "perform-pop" "perform-pop")]
|
|
257
|
+
(set! (.-innerHTML el)
|
|
258
|
+
(str "<p><b>Back to Compose</b> · this take has " changes (if (= 1 changes) " change" " changes")
|
|
259
|
+
" (" work " to the work, " layout " of layout)"
|
|
260
|
+
(when (:jam st) " — the whole jam, everyone's changes") ".</p>"
|
|
261
|
+
"<div class=\"pp-actions\">"
|
|
262
|
+
"<button class=\"act primary\" data-perf=\":keep\" title=\"A new point of the history, as a branch from "
|
|
263
|
+
(esc (moment-name (:origin st))) "\">Keep it as a new point</button>"
|
|
264
|
+
"<button class=\"act\" data-perf=\":discard\">Discard: back to " (esc (moment-name (:from st))) "</button>"
|
|
265
|
+
"<button class=\"act\" data-perf=\"stay\">Keep performing</button>"
|
|
266
|
+
"</div>"))
|
|
267
|
+
(set! (.-hidden el) false)))
|
|
268
|
+
|
|
269
|
+
(defn render-perform! []
|
|
270
|
+
(let [st @perf on (boolean st) b ($ "perform-toggle") info ($ "perform-info")]
|
|
271
|
+
(.toggle (.-classList (.-body js/document)) "performing" on)
|
|
272
|
+
(when b
|
|
273
|
+
(.toggle (.-classList b) "on" on)
|
|
274
|
+
(set! (.-title b) (if on
|
|
275
|
+
"Back to Compose: keep the take as a new point of the history, or discard it"
|
|
276
|
+
"Perform: rehearse, try and improvise outside the history (in a shared room, a jam with whoever performs from the same point)")))
|
|
277
|
+
(when info
|
|
278
|
+
(set! (.-hidden info) (not on))
|
|
279
|
+
(when on
|
|
280
|
+
(let [{:keys [changes]} (take-summary st) n (count (jam-mates))]
|
|
281
|
+
(set! (.-textContent info)
|
|
282
|
+
(str "from " (moment-name (:origin st)) " · " changes (if (= 1 changes) " change" " changes")
|
|
283
|
+
(when (:jam st) (str " · jam" (if (pos? n) (str " with " n) ", alone for now"))))))))
|
|
284
|
+
(when-not on (hide-leave-dialog!))))
|
|
285
|
+
|
|
286
|
+
(defn render-jam-invite! []
|
|
287
|
+
(let [offer (when-not @perf
|
|
288
|
+
(first (filter (fn [p] (and (:jam p) (not (:same p)) (same-line? p) (not (contains? @jam-seen (:jam p))))) @peers)))
|
|
289
|
+
el (floating "jam-invite" "jam-invite")]
|
|
290
|
+
(if-not offer
|
|
291
|
+
(set! (.-hidden el) true)
|
|
292
|
+
(let [who (if (:me offer) "You, in another tab," (or (not-empty (get-in @profiles [(:by offer) :name])) "Someone"))
|
|
293
|
+
at (if (and (:jam-at offer) (node (first (:jam-at offer)))) (moment-name (:jam-at offer)) "a point of the history")]
|
|
294
|
+
(set! (.-innerHTML el)
|
|
295
|
+
(str "<p><b>" (esc who) "</b> is performing from " (esc at) ". Jam together from there?</p>"
|
|
296
|
+
"<div class=\"pp-actions\">"
|
|
297
|
+
"<button class=\"act primary\" data-jam=\"" (esc (:jam offer)) "\">Jam together</button>"
|
|
298
|
+
"<button class=\"act\" data-jam-no=\"" (esc (:jam offer)) "\">Not now</button>"
|
|
299
|
+
"</div>"))
|
|
300
|
+
(set! (.-hidden el) false)))))
|
|
301
|
+
|
|
302
|
+
(.addEventListener (floating "perform-pop" "perform-pop") "click"
|
|
303
|
+
(fn [e]
|
|
304
|
+
(when-let [a (.. e -target -dataset -perf)]
|
|
305
|
+
(if (= a "stay") (hide-leave-dialog!) (gesture! (str "(compose " a ")"))))))
|
|
306
|
+
|
|
307
|
+
(.addEventListener (floating "jam-invite" "jam-invite") "click"
|
|
308
|
+
(fn [e]
|
|
309
|
+
(let [ds (.. e -target -dataset)]
|
|
310
|
+
(cond
|
|
311
|
+
(.-jam ds) (do (swap! jam-seen conj (.-jam ds)) (gesture! (str "(jam " (pr-str (.-jam ds)) ")")))
|
|
312
|
+
(.-jamNo ds) (do (swap! jam-seen conj (.-jamNo ds)) (render-jam-invite!))))))
|
|
313
|
+
|
|
314
|
+
(when-let [b ($ "perform-toggle")]
|
|
315
|
+
(.addEventListener b "click" (fn [_] (gesture! (if @perf "(compose)" "(perform)")))))
|
|
316
|
+
|
|
317
|
+
(when-let [b ($ "snap-btn")]
|
|
318
|
+
(.addEventListener b "click" (fn [_] (gesture! "(snapshot)"))))
|
|
319
|
+
|
|
320
|
+
(add-watch perf ::render (fn [_ _ _ _] (render-all!) (render-perform!) (render-jam-invite!)))
|
|
321
|
+
(add-watch peers ::perform (fn [_ _ _ _] (render-perform!) (render-jam-invite!)))
|
|
322
|
+
;; another work opened: the take of the one before is left behind
|
|
323
|
+
(add-watch work ::perform (fn [_ _ o n] (when (and @perf (not= (select-keys o [:kind :id]) (select-keys n [:kind :id]))) (end-perform!))))
|
|
324
|
+
|
|
325
|
+
(render-perform!)
|