Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
33 lines (29 sloc) 1.1 KB
(ns ws-ldn-2.components.editor
[reagent.core :as r]
[cljsjs.codemirror :as cm]
(defn cm-editor
"CodeMirror react component wrapper. Takes map of component props
and map of CM config opts. Props map MUST include an :on-change
handler and a :state value, which must be dereferencable (atom or
[props cm-opts]
(fn [this]
(let [editor (.fromTextArea js/CodeMirror (r/dom-node this) (clj->js cm-opts))]
(.on editor "change" #((:on-change props) (.getValue %)))
(r/set-state this {:editor editor})))
(fn [this]
(let [editor (:editor (r/state this))
val @(:state props)
update? (not= val (.getValue editor))]
(when update? (.setValue editor val))
(fn [_] [:textarea {:default-value (:default-value props)}])}))