File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ {:paths [" src" ]
2+ :deps {org.clojure/clojurescript {:mvn/version " 1.10.520" }
3+ reagent {:mvn/version " 0.8.1" }
4+ cljsjs/codemirror {:mvn/version " 5.44.0-1" }}}
Original file line number Diff line number Diff line change 1+ (ns re-codemirror.core
2+ (:require [reagent.core :as r]
3+ [cljsjs.codemirror]))
4+
5+ (defn opts
6+ " Grab the options componenets from the object."
7+ [this]
8+ (-> this
9+ r/argv
10+ rest
11+ first))
12+
13+ (defn conf
14+ " Grab the configuration componenets from the object."
15+ [this]
16+ (-> this
17+ r/argv
18+ rest
19+ second))
20+
21+ (defn value
22+ " Function used to take updates to CodeMirror and reflect them in
23+ a state manager."
24+ [this cm _]
25+ (let [{:keys [value]} (conf this)]
26+ (when-not (= value (.getValue cm))
27+ (.setValue cm value))))
28+
29+ (defn codemirror
30+ []
31+ (r/create-class
32+ {:reagent-render (fn [_ {:keys [name value]}]
33+ ; ; Render a textarea element and accept a name
34+ ; ; to use it as a form
35+ [:textarea
36+ {:name name
37+ :defaultValue value}])
38+ :component-did-mount (fn [this]
39+ (let [opts (opts this)
40+ conf (conf this)
41+ ; ; create a CodeMirror object from the textarea
42+ ; ; merge in default options with passed opts
43+ cm (-> this
44+ r/dom-node
45+ (js/CodeMirror.fromTextArea
46+ (clj->js (merge {:mode " javascript"
47+ :theme " default" }
48+ opts))))]
49+ ; ; add the CM object to the React component so it can be accessed
50+ (r/set-state this {:cm cm})
51+ ; ; attach any optional events to the CodeMirror target
52+ (doseq [[event-name event-fn]
53+ (:events conf)]
54+ (.on cm
55+ event-name
56+ (fn [& args]
57+ (event-fn this args))))))
58+ :component-did-update (fn [this old-argv]
59+ ; ; provide a way for CodeMirror to be updated by external state changes
60+ (let [cm (-> this
61+ r/state
62+ :cm )]
63+ (value this cm old-argv)))}))
You can’t perform that action at this time.
0 commit comments