@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,448 @@
|
|
|
1
|
+
;; SPDX-License-Identifier: MPL-2.0
|
|
2
|
+
;; Kairotic · the page, part 5 of 12: panels
|
|
3
|
+
;; The page's ClojureScript, in the order it is evaluated (app/index.html lists the files).
|
|
4
|
+
|
|
5
|
+
(in-ns 'user)
|
|
6
|
+
|
|
7
|
+
;; ───────────────────────── component library ─────────────────────────
|
|
8
|
+
;; Whatever is imported or taken from the library is copied into a component box of the work.
|
|
9
|
+
|
|
10
|
+
(defonce shared-lib (atom {})) ; class → {:class :source :doc :version :author :t}
|
|
11
|
+
|
|
12
|
+
(defn doc-id [cls-name] (str/replace (str cls-name) #"[^A-Za-z0-9_\-]" "_"))
|
|
13
|
+
|
|
14
|
+
(defn component-slot
|
|
15
|
+
"Where a new component box goes. A definition is not drawn in the patch (it is listed in the Components
|
|
16
|
+
tab, issue #8), so its place on the paper means nothing: the corner."
|
|
17
|
+
[] [0 0])
|
|
18
|
+
|
|
19
|
+
(defn new-definition!
|
|
20
|
+
"Creates, as gestures written in code, a component box with this source."
|
|
21
|
+
[source]
|
|
22
|
+
(let [id (next-id) [x y] (component-slot)]
|
|
23
|
+
(gesture! (str "(box " id " \"component\" " x " " y ")"))
|
|
24
|
+
(gesture! (str "(code " id " " (pr-str source) ")"))
|
|
25
|
+
id))
|
|
26
|
+
|
|
27
|
+
(declare select-tab! open-code-editor-at! wrap-path render-comp!)
|
|
28
|
+
|
|
29
|
+
(defn new-component!
|
|
30
|
+
"A new component of the work, from a template (or the source given), opened in the code window, with
|
|
31
|
+
the Components tab in sight."
|
|
32
|
+
([] (new-component! lib/template))
|
|
33
|
+
([source]
|
|
34
|
+
(let [id (new-definition! source)]
|
|
35
|
+
(select-tab! "comp")
|
|
36
|
+
(js/setTimeout (fn [] (open-code-editor-at! (cmd-path) id)) 0)
|
|
37
|
+
id)))
|
|
38
|
+
|
|
39
|
+
(defn definitions-in
|
|
40
|
+
"The component boxes of patch p and of the patches embedded in it: [[path id box] …], path being the
|
|
41
|
+
way down from the patch the work starts at."
|
|
42
|
+
([p] (definitions-in p []))
|
|
43
|
+
([p path]
|
|
44
|
+
(let [bs (sort-by (fn [[k _]] (box-num k)) (:boxes p))]
|
|
45
|
+
(concat (for [[id b] bs :when (definition? b)] [path id b])
|
|
46
|
+
(mapcat (fn [[id b]] (when (embedded? b) (definitions-in (or (:data b) empty-patch) (conj path id))))
|
|
47
|
+
bs)))))
|
|
48
|
+
|
|
49
|
+
(defn definitions
|
|
50
|
+
"The components this work defines, as the Components tab lists them: [[path id class status] …]."
|
|
51
|
+
[]
|
|
52
|
+
(vec (for [[path id b] (definitions-in (root-patch))
|
|
53
|
+
:let [r (cbox/compiled "component" (str (get-in b [:data :source])))]]
|
|
54
|
+
[path id (get-in r [:info :class]) (:status r)])))
|
|
55
|
+
|
|
56
|
+
(defn import!
|
|
57
|
+
"Copies a published component into the work: \"npm:package@version/path.mjs\" (jsDelivr) or a file
|
|
58
|
+
of this page (\"kairotic/packages/controls.mjs\"). It stays as an editable component box."
|
|
59
|
+
[origin]
|
|
60
|
+
(-> (lib/import-module origin)
|
|
61
|
+
(.then (fn [m]
|
|
62
|
+
(if (:package m)
|
|
63
|
+
(let [id (new-definition! (lib/package-source m origin))]
|
|
64
|
+
(repl-print! "out" (esc (str "; imported " (:package m) " " (:version m) " ("
|
|
65
|
+
(str/join ", " (map :class (:components m))) ") into " id))))
|
|
66
|
+
(let [id (new-definition! (str (lib/header m origin) (:source m)))]
|
|
67
|
+
(repl-print! "out" (esc (str "; imported «" (:class m) "» into " id)))))))
|
|
68
|
+
(.catch (fn [e] (repl-print! "err" (esc (str "Could not import " origin ": " (or (.-message e) e)))))))
|
|
69
|
+
:importing)
|
|
70
|
+
|
|
71
|
+
;; ───── official packages: brought into the work when one of their classes is written ─────
|
|
72
|
+
|
|
73
|
+
(defonce fetching (atom #{}))
|
|
74
|
+
|
|
75
|
+
(defn work-package?
|
|
76
|
+
"Whether the work (at its top level or the sub-patch on screen) already carries package `pkg`."
|
|
77
|
+
[pkg]
|
|
78
|
+
(let [needle (str ":package " (pr-str pkg))]
|
|
79
|
+
(some (fn [p] (some (fn [[_ b]] (and (= "component" (klass (:text b)))
|
|
80
|
+
(str/includes? (str (get-in b [:data :source])) needle)))
|
|
81
|
+
(:boxes p)))
|
|
82
|
+
[(current) (root-patch)])))
|
|
83
|
+
|
|
84
|
+
(defn add-package!
|
|
85
|
+
"Brings an official package into the work, as one component box pinned to its version:
|
|
86
|
+
(add-package! \"@kairotic/synth\"). It is looked for on npm and, if it is not there yet, in this page."
|
|
87
|
+
[pkg]
|
|
88
|
+
(when-not (get lib/packages pkg) (fail (str "Unknown package " pkg ": see (packages)")))
|
|
89
|
+
(when-not (or (@fetching pkg) (work-package? pkg))
|
|
90
|
+
(swap! fetching conj pkg)
|
|
91
|
+
(let [line (active-line)]
|
|
92
|
+
(-> (lib/import-package pkg)
|
|
93
|
+
(.then (fn [[m origin]]
|
|
94
|
+
;; into the line it was asked for: if another one is on screen by now, it comes when that
|
|
95
|
+
;; one is back (switch-line! brings what its boxes need)
|
|
96
|
+
(when-not (or (work-package? pkg) (not= line (active-line)))
|
|
97
|
+
(let [src (lib/package-source m origin)
|
|
98
|
+
_ (swap! cbox/trusted conj src) ; official: it runs without asking
|
|
99
|
+
id (new-definition! src)]
|
|
100
|
+
(repl-print! "out" (esc (str "; " pkg " " (:version m) " ("
|
|
101
|
+
(str/join ", " (map :class (:components m))) ") is now in the work: " id
|
|
102
|
+
(when-not (str/starts-with? origin "npm:") " · from this page"))))))))
|
|
103
|
+
(.catch (fn [e] (repl-print! "err" (esc (str "Could not bring " pkg ": " (or (.-message e) e))))))
|
|
104
|
+
(.finally (fn [] (swap! fetching disj pkg))))))
|
|
105
|
+
pkg)
|
|
106
|
+
|
|
107
|
+
(defn packages
|
|
108
|
+
"The official packages and their classes: [[package version [class …]] …]."
|
|
109
|
+
[]
|
|
110
|
+
(vec (for [[n p] (sort-by key lib/packages)] [n (:version p) (vec (sort (:classes p)))])))
|
|
111
|
+
|
|
112
|
+
(defn ensure-class!
|
|
113
|
+
"If box text t uses a class nobody defines yet but an official package does, brings it into the work."
|
|
114
|
+
[t]
|
|
115
|
+
(let [k (klass t)]
|
|
116
|
+
(when-let [[pkg _] (and (not (comp/lookup k)) (lib/package-of k))]
|
|
117
|
+
(add-package! pkg))))
|
|
118
|
+
|
|
119
|
+
(defn publish
|
|
120
|
+
"Saves the definition of a component box to the shared library of this space (a new version)."
|
|
121
|
+
[id]
|
|
122
|
+
(let [b (need-box id)]
|
|
123
|
+
(when-not (= "component" (klass (:text b))) (fail (str id " is not a component box")))
|
|
124
|
+
(let [source (str (get-in b [:data :source])) r (cbox/compiled "component" source)]
|
|
125
|
+
(when-not (= :ok (:status r)) (fail "The definition does not compile yet: fix it before publishing it"))
|
|
126
|
+
(when-not (and @dbref (:writable @hist)) (fail "No shared library available (you need to be signed in and able to write)"))
|
|
127
|
+
(let [cls-name (get-in r [:info :class]) prev-entry (get @shared-lib cls-name)
|
|
128
|
+
v (inc (or (:version prev-entry) 0))]
|
|
129
|
+
(-> (.set (.doc @dbref (str "library/" (doc-id cls-name)))
|
|
130
|
+
(clj->js {:class cls-name :source source :doc (str (:doc (:value r))) :version v
|
|
131
|
+
:author (my-author) :t (js/Date.now)}))
|
|
132
|
+
(.then (fn [_] (repl-print! "out" (esc (str "; «" cls-name "» version " v " published to the library")))))
|
|
133
|
+
(.catch db-failed!))
|
|
134
|
+
[cls-name v]))))
|
|
135
|
+
|
|
136
|
+
(defn library
|
|
137
|
+
"The components of the shared library: [[class version doc] …]."
|
|
138
|
+
[]
|
|
139
|
+
(vec (for [[c m] (sort-by key @shared-lib)] [c (:version m) (:doc m)])))
|
|
140
|
+
|
|
141
|
+
(defn use!
|
|
142
|
+
"Copies a component of the shared library into the work."
|
|
143
|
+
[cls-name]
|
|
144
|
+
(let [m (or (get @shared-lib (str cls-name)) (fail (str "The library has no «" cls-name "»: see (library)")))]
|
|
145
|
+
(new-definition! (str ";; " (:class m) " v" (:version m) " · from the library of this space\n" (:source m)))))
|
|
146
|
+
|
|
147
|
+
(defn listen-library! [db]
|
|
148
|
+
(.onSnapshot (.collection db "library")
|
|
149
|
+
(fn [snap]
|
|
150
|
+
(reset! shared-lib (into {} (for [d (array-seq (.-docs snap)) :when (.-exists d)
|
|
151
|
+
:let [m (js->clj (.data d) :keywordize-keys true)]
|
|
152
|
+
:when (and (string? (:class m)) (string? (:source m)))]
|
|
153
|
+
[(:class m) m]))))
|
|
154
|
+
(fn [_] nil)))
|
|
155
|
+
|
|
156
|
+
(defonce chosen-def (atom nil)) ; [path id]: the component chosen in the tab (for Claude's "change the selected component")
|
|
157
|
+
|
|
158
|
+
(defn def-row
|
|
159
|
+
"A row of the Components tab for the component box id (in the sub-patch at path): what it defines,
|
|
160
|
+
whether it compiles, and what can be done with it."
|
|
161
|
+
[path id b]
|
|
162
|
+
(let [r (cbox/compiled "component" (str (get-in b [:data :source])))
|
|
163
|
+
cls (get-in r [:info :class])
|
|
164
|
+
f (eng/flat-id (into (view-prefix) path) id)
|
|
165
|
+
used (when cls (get @lib/work-classes cls))
|
|
166
|
+
where (str (when (seq path) (str "in " (str/join " › " (map name path)) " · ")) "box " (name id))
|
|
167
|
+
attr (str " data-path=\"" (esc (pr-str path)) "\" data-id=\"" (esc (name id)) "\"")]
|
|
168
|
+
(str "<li class=\"def" (when (= :error (:status r)) " err") (when (= @chosen-def [path id]) " chosen") "\"" attr
|
|
169
|
+
" title=\"Click to choose it (Claude can then change it); Edit opens its code\">"
|
|
170
|
+
"<code>" (esc (or cls "—")) "</code>"
|
|
171
|
+
"<span title=\"" (esc (str where (when (:error r) (str " · " (:error r))))) "\">"
|
|
172
|
+
(esc (case (:status r)
|
|
173
|
+
:ok (str (or (get-in r [:info :summary]) "ready")
|
|
174
|
+
(when (and used (not= (first used) f)) (str " · not used: " (name (first used)) " defines it first"))
|
|
175
|
+
(when-let [d (:doc (second used))] (when (= (first used) f) (str " · " d))))
|
|
176
|
+
:compiling "compiling…"
|
|
177
|
+
:empty "empty: write its definition"
|
|
178
|
+
(str "✗ " (first (str/split-lines (str (:error r)))))))
|
|
179
|
+
" <small>" (esc where) "</small></span>"
|
|
180
|
+
"<button class=\"act\" data-def=\"edit\" title=\"Its code, in the code window\">Edit</button>"
|
|
181
|
+
(when (and (= :ok (:status r)) (empty? path) (not (:package (:value r))))
|
|
182
|
+
"<button class=\"act\" data-def=\"publish\" title=\"To the shared library of this space\">Publish</button>")
|
|
183
|
+
"<button class=\"act\" data-def=\"delete\" title=\"Remove it from the work (one event of the history)\" aria-label=\"Delete\">×</button>"
|
|
184
|
+
"</li>")))
|
|
185
|
+
|
|
186
|
+
(defonce comp-shown (atom nil)) ; what the tab last drew: it is drawn again only when that changes
|
|
187
|
+
|
|
188
|
+
(defn render-comp!
|
|
189
|
+
([] (render-comp! false))
|
|
190
|
+
([only-if-changed?]
|
|
191
|
+
(when-let [el ($ "comp-list")]
|
|
192
|
+
(let [defs (definitions-in (root-patch))
|
|
193
|
+
core-defs (filter (fn [[_ d]] (= :core (:origin d))) (sort-by key @comp/registry))
|
|
194
|
+
sig [@chosen-def (vec (for [[path id b] defs] [path id (cbox/compiled "component" (str (get-in b [:data :source])))]))
|
|
195
|
+
@lib/work-classes @shared-lib]
|
|
196
|
+
row (fn [label detail button]
|
|
197
|
+
(str "<li><code>" (esc label) "</code><span>" (esc detail) "</span>" button "</li>"))]
|
|
198
|
+
(when-not (and only-if-changed? (= sig @comp-shown))
|
|
199
|
+
(reset! comp-shown sig)
|
|
200
|
+
(set! (.-innerHTML el)
|
|
201
|
+
(str "<h4>In this work</h4>"
|
|
202
|
+
(if (seq defs)
|
|
203
|
+
(str "<ul>" (apply str (for [[path id b] defs] (def-row path id b))) "</ul>")
|
|
204
|
+
"<p class=\"empty\">None yet. The boxes that define components are kept here, not in the patch: they have no inlets nor outlets.</p>")
|
|
205
|
+
"<p class=\"comp-new\"><button class=\"act primary\" data-def=\"new\">New component</button> "
|
|
206
|
+
"<small>or import one, or ask Claude for it</small></p>"
|
|
207
|
+
"<h4>Shared library</h4>"
|
|
208
|
+
(if (seq @shared-lib)
|
|
209
|
+
(str "<ul>" (apply str (for [[c m] (sort-by key @shared-lib)]
|
|
210
|
+
(row c (str "v" (:version m) (when (seq (:doc m)) (str " · " (:doc m))))
|
|
211
|
+
(str "<button class=\"act\" data-use=\"" (esc c) "\">Use</button>"))))
|
|
212
|
+
"</ul>")
|
|
213
|
+
"<p class=\"empty\">Empty: publish a definition of the work here to use it in others.</p>")
|
|
214
|
+
"<h4>Import</h4>"
|
|
215
|
+
"<form id=\"comp-import\"><input id=\"comp-origin\" placeholder=\"npm:package@1.0.0/comp.mjs or kairotic/packages/controls.mjs\" value=\"kairotic/packages/controls.mjs\">"
|
|
216
|
+
"<button class=\"act\">Import</button></form>"
|
|
217
|
+
"<h4>Core</h4><p class=\"core-defs\">"
|
|
218
|
+
(str/join " " (for [[c d] core-defs] (str "<code class=\"help-link\" data-help=\"" (esc c) "\" title=\"" (esc (:doc d)) "\">" (esc c) "</code>")))
|
|
219
|
+
"</p>")))))))
|
|
220
|
+
|
|
221
|
+
(defn choose-definition!
|
|
222
|
+
"Chooses a component box of the work in the Components tab (nil: none)."
|
|
223
|
+
[path id]
|
|
224
|
+
(reset! chosen-def (when id [(vec path) id]))
|
|
225
|
+
(render-comp!))
|
|
226
|
+
|
|
227
|
+
(defn edit-definition!
|
|
228
|
+
"Opens the code of a component box of the work (in the sub-patch at path) in the code window."
|
|
229
|
+
[path id]
|
|
230
|
+
(choose-definition! path id)
|
|
231
|
+
;; from a sub-patch that does not hold it, the work is seen from its top first
|
|
232
|
+
(when-not (= (cmd-path) (vec (take (count (cmd-path)) path))) (gesture! "(top)"))
|
|
233
|
+
(open-code-editor-at! path id))
|
|
234
|
+
|
|
235
|
+
(add-watch shared-lib ::comp (fn [_ _ _ _] (render-comp!)))
|
|
236
|
+
|
|
237
|
+
(.addEventListener ($ "panel-comp") "click"
|
|
238
|
+
(fn [e]
|
|
239
|
+
(when-let [b (.closest (.-target e) "[data-help]")]
|
|
240
|
+
(show-help! (.getAttribute b "data-help")))
|
|
241
|
+
(when-let [b (.closest (.-target e) "[data-use]")]
|
|
242
|
+
(gesture! (str "(use! " (pr-str (.getAttribute b "data-use")) ")")))
|
|
243
|
+
(when-let [b (.closest (.-target e) "[data-publish]")]
|
|
244
|
+
(gesture! (str "(publish :" (.getAttribute b "data-publish") ")")))
|
|
245
|
+
(when-let [b (.closest (.-target e) "[data-def]")]
|
|
246
|
+
(let [li (.closest b "li[data-id]")
|
|
247
|
+
path (when li (clojure.edn/read-string (.getAttribute li "data-path")))
|
|
248
|
+
id (when li (keyword (.getAttribute li "data-id")))]
|
|
249
|
+
(case (.getAttribute b "data-def")
|
|
250
|
+
"new" (gesture! "(new-component!)")
|
|
251
|
+
"edit" (edit-definition! path id)
|
|
252
|
+
"publish" (gesture! (str "(publish " id ")"))
|
|
253
|
+
"delete" (do (when-not (= (cmd-path) (vec (take (count (cmd-path)) path))) (gesture! "(top)"))
|
|
254
|
+
(when (= @chosen-def [path id]) (reset! chosen-def nil))
|
|
255
|
+
(gesture! (wrap-path path (str "(delete " id ")"))))
|
|
256
|
+
nil)))
|
|
257
|
+
;; a click on the row itself chooses it
|
|
258
|
+
(when-let [li (and (not (.closest (.-target e) "button")) (.closest (.-target e) "li.def[data-id]"))]
|
|
259
|
+
(let [path (clojure.edn/read-string (.getAttribute li "data-path")) id (keyword (.getAttribute li "data-id"))]
|
|
260
|
+
(choose-definition! (when-not (= @chosen-def [path id]) path) (when-not (= @chosen-def [path id]) id))))))
|
|
261
|
+
|
|
262
|
+
(.addEventListener ($ "panel-comp") "submit"
|
|
263
|
+
(fn [e]
|
|
264
|
+
(.preventDefault e)
|
|
265
|
+
(let [v (str/trim (.-value ($ "comp-origin")))]
|
|
266
|
+
(when (seq v) (gesture! (str "(import! " (pr-str v) ")"))))))
|
|
267
|
+
|
|
268
|
+
;; ───────────────────────── code panel ─────────────────────────
|
|
269
|
+
|
|
270
|
+
(defn run-panel-code! []
|
|
271
|
+
(let [code (code-text)]
|
|
272
|
+
(try
|
|
273
|
+
(run-code! code)
|
|
274
|
+
(swap! ui assoc :dirty false)
|
|
275
|
+
(render-code (current))
|
|
276
|
+
(repl-print! "out" "; code run: the patch has been rebuilt (1 :load event)")
|
|
277
|
+
(catch :default e
|
|
278
|
+
(repl-print! "err" (esc (str "Error in the code: " (or (.-message e) (str e)))))))))
|
|
279
|
+
|
|
280
|
+
(.addEventListener ($ "code") "input" (fn [_] (when-not (:dirty @ui) (swap! ui assoc :dirty true) (render-code (current)))))
|
|
281
|
+
(.addEventListener ($ "code") "keydown"
|
|
282
|
+
(fn [e] (when (and (= (.-key e) "Enter") (or (.-ctrlKey e) (.-metaKey e)))
|
|
283
|
+
(.preventDefault e) (run-panel-code!))))
|
|
284
|
+
(.addEventListener ($ "code-run") "click" (fn [_] (run-panel-code!)))
|
|
285
|
+
(.addEventListener ($ "code-revert") "click" (fn [_] (swap! ui assoc :dirty false) (render-code (current))))
|
|
286
|
+
|
|
287
|
+
;; Calls / Data: the Code tab shows the work as calls (editable, run with Ctrl/⌘+Enter) or as data (the
|
|
288
|
+
;; state the history produces, read-only). Edits not run yet stay in the calls while the data is shown.
|
|
289
|
+
(defn show-code-view!
|
|
290
|
+
"Shows the work in the Code tab as :calls or :data."
|
|
291
|
+
[v]
|
|
292
|
+
(let [data (= v :data)
|
|
293
|
+
panel ($ "panel-code")
|
|
294
|
+
;; once the editor is loaded, the hosts it put in place of the text area and the <pre>
|
|
295
|
+
calls-el (or (.querySelector panel ".code-host") ($ "code"))
|
|
296
|
+
data-el (or (.querySelector panel ".data-host") ($ "edn"))]
|
|
297
|
+
(swap! ui assoc :code-view (if data :data :calls))
|
|
298
|
+
(set! (.-hidden calls-el) data)
|
|
299
|
+
(set! (.-hidden data-el) (not data))
|
|
300
|
+
(.setAttribute ($ "view-calls") "aria-pressed" (str (not data)))
|
|
301
|
+
(.setAttribute ($ "view-data") "aria-pressed" (str data))
|
|
302
|
+
(render-code (current))))
|
|
303
|
+
|
|
304
|
+
(.addEventListener ($ "view-calls") "click" (fn [_] (show-code-view! :calls)))
|
|
305
|
+
(.addEventListener ($ "view-data") "click" (fn [_] (show-code-view! :data)))
|
|
306
|
+
|
|
307
|
+
;; ───── the panel on the right (issue #40): hidden or shown, and as wide as you drag it ─────
|
|
308
|
+
;; {:open bool :w px-or-nil}: nil keeps the proportion of the page. It is a preference of this browser, kept in
|
|
309
|
+
;; localStorage, not part of the work. Opening a tab (Help with ? or F1, the Code tab…) shows it again.
|
|
310
|
+
|
|
311
|
+
(defonce side
|
|
312
|
+
(atom (merge {:open true :w nil}
|
|
313
|
+
(try (some-> (.getItem js/localStorage "kairotic.side") (clojure.edn/read-string)
|
|
314
|
+
(select-keys [:open :w]))
|
|
315
|
+
(catch :default _ nil)))))
|
|
316
|
+
|
|
317
|
+
(def side-min 260) ; the panel is never narrower than this
|
|
318
|
+
(def patch-min 320) ; nor the patch
|
|
319
|
+
|
|
320
|
+
(defn side-width-ok
|
|
321
|
+
"A width for the panel that leaves room for the patch, in a page area `total` wide."
|
|
322
|
+
[w total] (js/Math.round (max side-min (min w (- total patch-min 12)))))
|
|
323
|
+
|
|
324
|
+
(defn render-side! []
|
|
325
|
+
(let [{:keys [open w]} @side wk (.querySelector js/document ".work") t ($ "side-toggle")]
|
|
326
|
+
(when wk
|
|
327
|
+
(.toggle (.-classList wk) "side-off" (not open))
|
|
328
|
+
(.toggle (.-classList wk) "sized" (boolean (and open w)))
|
|
329
|
+
(if (and open w)
|
|
330
|
+
(.setProperty (.-style wk) "--side-w" (str (side-width-ok w (.-clientWidth wk)) "px"))
|
|
331
|
+
(.removeProperty (.-style wk) "--side-w")))
|
|
332
|
+
(when t
|
|
333
|
+
(set! (.-textContent t) (if open "›" "‹"))
|
|
334
|
+
(set! (.-title t) (if open "Hide the panel" "Show the panel (Code, Components, Help…)"))
|
|
335
|
+
(.setAttribute t "aria-expanded" (str (boolean open))))))
|
|
336
|
+
|
|
337
|
+
(defn save-side! []
|
|
338
|
+
(try (.setItem js/localStorage "kairotic.side" (pr-str @side)) (catch :default _ nil)))
|
|
339
|
+
|
|
340
|
+
(defn set-side! [m]
|
|
341
|
+
(when (not= (merge @side m) @side)
|
|
342
|
+
(swap! side merge m)
|
|
343
|
+
(save-side!)
|
|
344
|
+
(render-side!)
|
|
345
|
+
;; the patch can use the room it gets (or gives back)
|
|
346
|
+
(js/requestAnimationFrame (fn [_] (render-scene (current))))))
|
|
347
|
+
|
|
348
|
+
(defn side-panel
|
|
349
|
+
"The panel on the right: (side-panel false) hides it, (side-panel true) shows it, (side-panel 480) shows it
|
|
350
|
+
480 px wide, (side-panel :auto) back to its proportion of the page. Without arguments, how it is."
|
|
351
|
+
([] @side)
|
|
352
|
+
([x]
|
|
353
|
+
(cond (boolean? x) (set-side! {:open x})
|
|
354
|
+
(number? x) (set-side! {:open true :w x})
|
|
355
|
+
(= x :auto) (set-side! {:open true :w nil})
|
|
356
|
+
:else (fail "Use (side-panel true), (side-panel false), (side-panel 480) or (side-panel :auto)."))
|
|
357
|
+
@side))
|
|
358
|
+
|
|
359
|
+
(.addEventListener ($ "side-toggle") "click" (fn [e] (.stopPropagation e) (side-panel (not (:open @side)))))
|
|
360
|
+
(.addEventListener ($ "side-hide") "click" (fn [_] (side-panel false)))
|
|
361
|
+
|
|
362
|
+
;; dragging the splitter: the panel takes the width from there to the right edge of the work
|
|
363
|
+
(let [split ($ "side-split") drag (atom nil)]
|
|
364
|
+
(.addEventListener split "pointerdown"
|
|
365
|
+
(fn [e]
|
|
366
|
+
(when (and (:open @side) (not (.closest (.-target e) "button"))
|
|
367
|
+
(not (.-matches (.matchMedia js/window "(max-width: 860px)"))))
|
|
368
|
+
(.preventDefault e)
|
|
369
|
+
(.setPointerCapture split (.-pointerId e))
|
|
370
|
+
(.add (.-classList split) "dragging")
|
|
371
|
+
(reset! drag true))))
|
|
372
|
+
(.addEventListener split "pointermove"
|
|
373
|
+
(fn [e]
|
|
374
|
+
(when @drag
|
|
375
|
+
(let [wk (.querySelector js/document ".work") r (.getBoundingClientRect wk)]
|
|
376
|
+
(swap! side assoc :w (side-width-ok (- (.-right r) (.-clientX e) 6) (.-width r)))
|
|
377
|
+
(render-side!)))))
|
|
378
|
+
(doseq [ev ["pointerup" "pointercancel"]]
|
|
379
|
+
(.addEventListener split ev
|
|
380
|
+
(fn [_]
|
|
381
|
+
(when @drag
|
|
382
|
+
(reset! drag nil)
|
|
383
|
+
(.remove (.-classList split) "dragging")
|
|
384
|
+
(save-side!)
|
|
385
|
+
(render-scene (current))))))
|
|
386
|
+
(.addEventListener split "dblclick"
|
|
387
|
+
(fn [e] (when-not (.closest (.-target e) "button") (side-panel (not (:open @side)))))))
|
|
388
|
+
|
|
389
|
+
(.addEventListener js/window "resize" (fn [_] (render-side!)))
|
|
390
|
+
(render-side!)
|
|
391
|
+
|
|
392
|
+
(doseq [t (array-seq (.querySelectorAll js/document ".tab"))]
|
|
393
|
+
(.addEventListener t "click"
|
|
394
|
+
(fn [_]
|
|
395
|
+
(let [tab (.. t -dataset -tab)]
|
|
396
|
+
;; a tab asked for (a click, ? or F1, (docs …)) shows the panel if it was hidden
|
|
397
|
+
(when-not (:open @side) (set-side! {:open true}))
|
|
398
|
+
(doseq [x (array-seq (.querySelectorAll js/document ".tab"))]
|
|
399
|
+
(.setAttribute x "aria-selected" (str (= x t))))
|
|
400
|
+
(doseq [[k pid] [["code" "panel-code"] ["comp" "panel-comp"] ["claude" "panel-claude"] ["help" "panel-help"] ["feedback" "panel-feedback"] ["talk" "panel-talk"]]]
|
|
401
|
+
(set! (.-hidden ($ pid)) (not= k tab)))
|
|
402
|
+
;; opened by itself, the Help tab shows its index (or the page it was on)
|
|
403
|
+
(when (= tab "help") (refresh-help!))))))
|
|
404
|
+
|
|
405
|
+
;; ───────────────────────── gestures in the history ─────────────────────────
|
|
406
|
+
|
|
407
|
+
(.addEventListener ($ "nav-back") "click" (fn [_] (gesture! "(rewind)")))
|
|
408
|
+
(.addEventListener ($ "nav-fwd") "click" (fn [_] (gesture! "(forward)")))
|
|
409
|
+
(.addEventListener ($ "nav-tip") "click" (fn [_] (gesture! "(tip)")))
|
|
410
|
+
(.addEventListener ($ "start-example") "click" (fn [_] (gesture! "(load-example)")))
|
|
411
|
+
|
|
412
|
+
(declare toggle-pick!)
|
|
413
|
+
|
|
414
|
+
(.addEventListener ($ "tree") "click"
|
|
415
|
+
(fn [e]
|
|
416
|
+
(when-let [el (.closest (.-target e) "[data-goto]")]
|
|
417
|
+
(let [[nid k] (str/split (.getAttribute el "data-goto") #"\|")]
|
|
418
|
+
(cond
|
|
419
|
+
;; ⇧+click: a point chosen to be forgotten (issue #45)
|
|
420
|
+
(and (.-shiftKey e) (not (:merge-pick @ui))) (toggle-pick! nid (js/parseInt k 10))
|
|
421
|
+
(:merge-pick @ui)
|
|
422
|
+
(do (swap! ui assoc :merge-pick false)
|
|
423
|
+
(.remove (.-classList ($ "hist-wrap")) "picking")
|
|
424
|
+
(gesture! (str "(merge-from " (pr-str nid) " " k ")")))
|
|
425
|
+
:else (gesture! (str "(goto " (pr-str nid) " " k ")")))))))
|
|
426
|
+
|
|
427
|
+
(.addEventListener ($ "merge-btn") "click"
|
|
428
|
+
(fn [_]
|
|
429
|
+
(let [on (not (:merge-pick @ui))]
|
|
430
|
+
(swap! ui assoc :merge-pick on)
|
|
431
|
+
(.toggle (.-classList ($ "hist-wrap")) "picking" on))))
|
|
432
|
+
|
|
433
|
+
(.addEventListener ($ "auto") "change"
|
|
434
|
+
(fn [e] (gesture! (str "(front " (.. e -target -checked) ")"))))
|
|
435
|
+
|
|
436
|
+
(.addEventListener ($ "simul") "change"
|
|
437
|
+
(fn [e]
|
|
438
|
+
(let [v (js/parseInt (.. e -target -value) 10)]
|
|
439
|
+
(if (js/isNaN v)
|
|
440
|
+
(render-controls!)
|
|
441
|
+
(gesture! (str "(simultaneity " v ")"))))))
|
|
442
|
+
|
|
443
|
+
(.addEventListener ($ "peers") "click"
|
|
444
|
+
(fn [e]
|
|
445
|
+
(when-let [el (.closest (.-target e) "[data-peer]")]
|
|
446
|
+
(let [pr (.getAttribute el "data-peer")]
|
|
447
|
+
(swap! ui assoc :follow (if (= pr (:follow @ui)) nil pr))
|
|
448
|
+
(sync-follow!)))))
|