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