-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
code_editor.cljs
104 lines (86 loc) · 2.93 KB
/
code_editor.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(ns shadow.cljs.ui.components.code-editor
{:shadow.css/include ["shadow/cljs/ui/components/code-editor.css"]}
(:require
["codemirror" :as cm]
["codemirror/mode/clojure/clojure"]
["parinfer-codemirror" :as par-cm]
[clojure.string :as str]
[shadow.arborist.protocols :as ap]
[shadow.arborist.common :as common]
[shadow.arborist.dom-scheduler :as ds]
[shadow.grove :as sg]
[shadow.grove.components :as comp]
[shadow.grove.protocols :as gp]))
(deftype EditorRoot
[env
marker
^:mutable opts
^:mutable editor
^:mutable editor-el]
ap/IManaged
(supports? [this next]
(ap/identical-creator? opts next))
(dom-sync! [this next-opts]
(let [{:keys [value cm-opts]} next-opts]
(when (and editor (seq value))
(.setValue editor value))
(reduce-kv
(fn [_ key val]
(.setOption editor (name key) val))
nil
cm-opts)
(set! opts next-opts)
))
(dom-insert [this parent anchor]
(.insertBefore parent marker anchor))
(dom-first [this]
(or editor-el marker))
;; codemirror doesn't render correctly if added to an element
;; that isn't actually in the dcoument, so we delay construction until actually entered
;; codemirror also does a bunch of force layouts/render when mounting
;; which kill performance quite badly
(dom-entered! [this]
(ds/write!
(let [{:keys [value cm-opts clojure]}
opts
;; FIXME: this config stuff needs to be cleaned up, this is horrible
cm-opts
(js/Object.assign
#js {:lineNumbers true
:theme "github"}
(when cm-opts (clj->js cm-opts))
(when-not (false? clojure)
#js {:mode "clojure"
:matchBrackets true})
(when (seq value)
#js {:value value}))
ed
(cm.
(fn [el]
(set! editor-el el)
(.insertBefore (.-parentElement marker) el marker))
cm-opts)
submit-fn
(fn [e]
(let [val (str/trim (.getValue ed))]
(when (seq val)
(let [comp (comp/get-component env)]
(gp/handle-event! comp (assoc (:submit-event opts) :code val) e env)
(.setValue ed "")))))]
(set! editor ed)
(when (:submit-event opts)
(.setOption ed "extraKeys"
#js {"Ctrl-Enter" submit-fn
"Shift-Enter" submit-fn}))
(when-not (false? clojure)
(par-cm/init ed)))))
(destroy! [this dom-remove?]
(when dom-remove?
(when editor-el
;; FIXME: can't find a dispose method on codemirror?
(.remove editor-el))
(.remove marker))))
(defn make-editor [opts env]
(EditorRoot. env (common/dom-marker env) opts nil nil))
(defn codemirror [opts]
(with-meta opts {`ap/as-managed make-editor}))