Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. (ns ui.editor
  2. (:require [reagent.core :as r]
  3. [re-frame.core :as rf]
  4. ))
  5.  
  6. (def Doc (.-Doc js/CodeMirror))
  7.  
  8. (defn create-editor
  9. ([config]
  10. (js/CodeMirror (.-body js/document) (clj->js config)))
  11. ([dom config]
  12. (js/CodeMirror dom (clj->js config))))
  13.  
  14. (defn fromTextArea
  15. ([textarea]
  16. (.fromTextArea js/CodeMirror textarea))
  17. ([textarea config]
  18. (.fromTextArea js/CodeMirror textarea (clj->js config))))
  19.  
  20.  
  21. ; Functions on top of editor instance
  22.  
  23. ; Content manipulation methods
  24. (defn get-value
  25. ([editor] (.getValue editor))
  26. ([editor sep] (.getValue editor sep)))
  27.  
  28. (defn set-value
  29. [editor value] (.setValue editor value))
  30.  
  31.  
  32. (defn get-range
  33. ([editor from to]
  34. (.getRange editor (clj->js from) (clj->js to)))
  35. ([editor from to sep]
  36. (.getRange editor (clj->js from) (clj->js to) sep)))
  37.  
  38.  
  39. (defn replace-range
  40. ([editor string from]
  41. (.replaceRange editor string (clj->js from)))
  42. ([editor string from to]
  43. (.replaceRange editor string (clj->js from) (clj->js to))))
  44.  
  45. (defn get-line
  46. [editor n]
  47. (.getLine editor n))
  48.  
  49. (defn set-line
  50. [editor n text]
  51. (.setLine editor n text))
  52.  
  53. (defn remove-line
  54. [editor n]
  55. (.removeLine editor n))
  56.  
  57. (defn line-count
  58. [editor]
  59. (.lineCount editor))
  60.  
  61. (defn first-line
  62. [editor]
  63. (.firstLine editor))
  64.  
  65. (defn last-line
  66. [editor]
  67. (.lastLine editor))
  68.  
  69. (defn get-line-handle
  70. [editor n]
  71. (.getLineHandle editor n))
  72.  
  73. (defn get-line-number
  74. [editor handle]
  75. (.getLineNumber editor handle))
  76.  
  77. (defn each-line
  78. ([editor function]
  79. (.eachLine editor function))
  80. ([editor start end function]
  81. (.eachLine editor start end function)))
  82.  
  83. (defn mark-clean
  84. [editor]
  85. (.markClean editor))
  86.  
  87. (defn change-generation
  88. [editor]
  89. (.changeGeneration editor))
  90.  
  91. (defn is-clean
  92. [editor]
  93. (.isClean editor))
  94.  
  95.  
  96. ; Cursor and selection methods
  97. (defn get-selection
  98. [editor]
  99. (.getSelection editor))
  100.  
  101. ; -
  102. (defn replace-selection
  103. [editor replacement]
  104. (.replaceSelection editor replacement))
  105.  
  106. ; - make it return a cljs obj
  107. (defn get-cursor
  108. [editor]
  109. (.getCursor editor))
  110.  
  111. (defn something-selected
  112. [editor]
  113. (.somethingSelected editor))
  114.  
  115. (defn set-cursor
  116. [editor pos]
  117. (.setCursor editor (clj->js pos)))
  118.  
  119. ; -
  120. (defn set-selection
  121. [editor anchor]
  122. (.setSelection editor (clj->js anchor)))
  123.  
  124. ; -
  125. (defn extend-selection
  126. [editor from]
  127. (.extendSelection editor (clj->js from)))
  128.  
  129. (defn set-extending
  130. [editor value]
  131. (.setExtending editor value))
  132.  
  133. (defn has-focus
  134. [editor]
  135. (.hasFocus editor))
  136.  
  137. (defn find-pos-h
  138. [])
  139.  
  140. (defn find-pos-v
  141. [])
  142.  
  143.  
  144. ; Configuration methods
  145.  
  146. (defn set-option
  147. [editor option value]
  148. (.setOption editor option value))
  149.  
  150. (defn get-option
  151. [editor option]
  152. (.getOption editor option))
  153.  
  154. (defn add-key-map
  155. [])
  156.  
  157. (defn remove-key-map
  158. [])
  159.  
  160. (defn add-overlay
  161. [])
  162.  
  163. (defn remove-overlay
  164. [])
  165.  
  166. (defn on
  167. [])
  168.  
  169. (defn off
  170. [])
  171.  
  172.  
  173.  
  174. ; Document management methods
  175.  
  176. (defn get-doc
  177. [editor]
  178. (.getDoc editor))
  179.  
  180. (defn get-editor
  181. [doc]
  182. (.getEditor doc))
  183.  
  184. (defn swap-doc
  185. [editor doc]
  186. (.swapDoc editor doc))
  187.  
  188. (defn copy
  189. [doc]
  190. (.copy doc))
  191.  
  192. (defn linked-doc
  193. [])
  194.  
  195. (defn unlink-doc
  196. [])
  197.  
  198. (defn iter-linked-docs
  199. [])
  200.  
  201.  
  202. ; History related methods
  203. (defn undo
  204. [editor]
  205. (.undo editor))
  206.  
  207. (defn redo
  208. [editor]
  209. (.redo editor))
  210.  
  211. (defn history-size
  212. [editor]
  213. (.historySize editor))
  214.  
  215. (defn clear-history
  216. [editor]
  217. (.clearHistory editor))
  218.  
  219. (defn get-history
  220. [editor]
  221. (.getHistory editor))
  222.  
  223. (defn set-history
  224. [])
  225.  
  226. ; Text marking methods
  227.  
  228. (defn mark-text [editor from to options]
  229. (.markText editor
  230. (clj->js from)
  231. (clj->js to)
  232. (clj->js options)))
  233.  
  234. (defn set-bookmard [])
  235.  
  236. (defn find-marks-at [])
  237.  
  238. (defn get-all-marks [])
  239.  
  240. (def cm-modes
  241. {:sql "text/x-sql"
  242. :json "application/json" })
  243.  
  244. (def default-cm-options
  245. {:lineNumbers true
  246. :height "auto"
  247. :matchBrackets true
  248. :viewportMargin js/Infinity})
  249.  
  250. (defn codemirror [opts]
  251. #_(println :recreate)
  252. (let [value (rf/subscribe [:re-form/data (into (:base-path opts) (:path opts))])
  253. cm (atom nil)
  254. path (into (:base-path opts) (:path opts))
  255. cm-opts (merge default-cm-options (:code-mirror opts))]
  256. (r/create-class
  257. {:reagent-render
  258. (fn [opts]
  259. @value
  260. [:textarea])
  261.  
  262. :component-did-mount
  263. (fn [this]
  264. #_(println :component-did-mount)
  265. (let [*cm (fromTextArea (r/dom-node this) cm-opts)]
  266. (reset! cm *cm)
  267. (.setValue *cm (.toString (or @value "")))
  268. (.on *cm "change"
  269. (fn [& _] (rf/dispatch [:re-form/change path (.getValue *cm)])))))
  270.  
  271. :component-did-update
  272. (fn [this [_ old-props]]
  273. (let [*cm @cm v @value]
  274. #_(println :component-did-update v (.getValue *cm))
  275. (when (not= (.getValue *cm) v)
  276. (.setValue *cm (.toString v)))))
  277.  
  278. :component-will-receive-props
  279. (fn [this [_ props]]
  280. #_(println :component-will-receive-props props "IMPLEMENT"))})))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement