Skip to content

Commit

Permalink
Update faust.cljs
Browse files Browse the repository at this point in the history
  • Loading branch information
bonuoq authored Feb 12, 2024
1 parent af6baec commit 49e606b
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions modules/faust.cljs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns pou.modules.faust
(:require [goog.dom :as gdom]
[klipse.common.registry :as klr]
[klipse.klipse-editors :as kleds]))
[klipse.common.registry :as klr]))

(def trusted-url js/goog.html.legacyconversions.trustedResourceUrlFromString)
(def js-safe-load js/goog.net.jsloader.safeLoad)
Expand All @@ -21,33 +20,36 @@
(. component appendChild (. js/document createComment code))
(. element replaceChildren component)))

(defn get-comp [id]
(some-> id gdom/getElement (.querySelector "faust-editor, faust-widget")))
(defn get-comp [id-or-element]
(when-let [elm (if (string? id-or-element) (gdom/getElement id-or-element) id-or-element)]
(. elm querySelector "faust-editor, faust-widget")))

(defn get-faust-code [id]
(some-> (get-comp id) .-shadowRoot (.querySelector ".cm-content") .-cmView .-view .-state .-doc .toString))
(defn get-faust-code [id-or-element]
(some-> (get-comp id-or-element) .-shadowRoot (.querySelector ".cm-content") .-cmView .-view .-state .-doc .toString))

(defn get-faust-node [id]
(. (get-comp id) -faustNode))
(defn get-faust-node [id-or-element]
(. (get-comp id-or-element) -faustNode))

(defn set-param [id param-path value]
(when-let [component (get-comp id)]
(defn set-param [id-or-element param-path value]
(when-let [component (get-comp id-or-element)]
(.. component -faustNode (setParamValue param-path value))
(.. component -faustUI (paramChangeByDSP param-path value))))

(defn action [id action]
(some-> (get-comp id) .-shadowRoot (.getElementById (clj->js action)) .click))
(defn action [id-or-element action]
(some-> (get-comp id-or-element) .-shadowRoot (.getElementById (clj->js action)) .click))

(defn on-action [id action handler]
(some-> (get-comp id) .-shadowRoot (.getElemenyById (cljs->js action))
(defn on-action [id-or-element action handler]
(some-> (get-comp id-or-element) .-shadowRoot (.getElemenyById (cljs->js action))
(.addEventListener "click" handler)))

(defn eval-faust [mode exp {:keys [container container-id] :as kwargs}]
(defn eval-faust [mode exp {:keys [container] :as kwargs}]
(if @loaded
(try
(place-in container :code (str exp) :mode mode)
(when (= mode :editor)
(js/console.log (some->> container-id last int (get @kleds/editors))))
(when (= mode :editor)
(when-let [elm (-> container .-parentElement (.querySelector ".CodeMirror"))]
(set! (.-hidden elm) true)
(on-action elm :run #(-> elm .-CodeMirror (.setValue (get-faust-code elm))))))
(catch :default e
(set! (. container -innerHTML) (str e))))
(js/setTimeout #(eval-faust exp kwargs) 500)))
Expand Down

0 comments on commit 49e606b

Please sign in to comment.