code.cljs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. (ns frontend.extensions.code
  2. (:require [clojure.string :as string]
  3. ["codemirror" :as cm]
  4. ["codemirror/addon/edit/closebrackets"]
  5. ["codemirror/addon/edit/matchbrackets"]
  6. ["codemirror/addon/selection/active-line"]
  7. ["codemirror/mode/apl/apl"]
  8. ["codemirror/mode/asciiarmor/asciiarmor"]
  9. ["codemirror/mode/asn.1/asn.1"]
  10. ["codemirror/mode/asterisk/asterisk"]
  11. ["codemirror/mode/brainfuck/brainfuck"]
  12. ["codemirror/mode/clike/clike"]
  13. ["codemirror/mode/clojure/clojure"]
  14. ["codemirror/mode/cmake/cmake"]
  15. ["codemirror/mode/cobol/cobol"]
  16. ["codemirror/mode/coffeescript/coffeescript"]
  17. ["codemirror/mode/commonlisp/commonlisp"]
  18. ["codemirror/mode/crystal/crystal"]
  19. ["codemirror/mode/css/css"]
  20. ["codemirror/mode/cypher/cypher"]
  21. ["codemirror/mode/d/d"]
  22. ["codemirror/mode/dart/dart"]
  23. ["codemirror/mode/diff/diff"]
  24. ["codemirror/mode/django/django"]
  25. ["codemirror/mode/dockerfile/dockerfile"]
  26. ["codemirror/mode/dtd/dtd"]
  27. ["codemirror/mode/dylan/dylan"]
  28. ["codemirror/mode/ebnf/ebnf"]
  29. ["codemirror/mode/ecl/ecl"]
  30. ["codemirror/mode/eiffel/eiffel"]
  31. ["codemirror/mode/elm/elm"]
  32. ["codemirror/mode/erlang/erlang"]
  33. ["codemirror/mode/factor/factor"]
  34. ["codemirror/mode/fcl/fcl"]
  35. ["codemirror/mode/forth/forth"]
  36. ["codemirror/mode/fortran/fortran"]
  37. ["codemirror/mode/gas/gas"]
  38. ["codemirror/mode/gfm/gfm"]
  39. ["codemirror/mode/gherkin/gherkin"]
  40. ["codemirror/mode/go/go"]
  41. ["codemirror/mode/groovy/groovy"]
  42. ["codemirror/mode/haml/haml"]
  43. ["codemirror/mode/handlebars/handlebars"]
  44. ["codemirror/mode/haskell-literate/haskell-literate"]
  45. ["codemirror/mode/haskell/haskell"]
  46. ["codemirror/mode/haxe/haxe"]
  47. ["codemirror/mode/htmlembedded/htmlembedded"]
  48. ["codemirror/mode/htmlmixed/htmlmixed"]
  49. ["codemirror/mode/http/http"]
  50. ["codemirror/mode/idl/idl"]
  51. ["codemirror/mode/javascript/javascript"]
  52. ["codemirror/mode/jinja2/jinja2"]
  53. ["codemirror/mode/jsx/jsx"]
  54. ["codemirror/mode/julia/julia"]
  55. ["codemirror/mode/livescript/livescript"]
  56. ["codemirror/mode/lua/lua"]
  57. ["codemirror/mode/markdown/markdown"]
  58. ["codemirror/mode/mathematica/mathematica"]
  59. ["codemirror/mode/mbox/mbox"]
  60. ["codemirror/mode/meta"]
  61. ["codemirror/mode/mirc/mirc"]
  62. ["codemirror/mode/mllike/mllike"]
  63. ["codemirror/mode/modelica/modelica"]
  64. ["codemirror/mode/mscgen/mscgen"]
  65. ["codemirror/mode/mumps/mumps"]
  66. ["codemirror/mode/nginx/nginx"]
  67. ["codemirror/mode/nsis/nsis"]
  68. ["codemirror/mode/ntriples/ntriples"]
  69. ["codemirror/mode/octave/octave"]
  70. ["codemirror/mode/oz/oz"]
  71. ["codemirror/mode/pascal/pascal"]
  72. ["codemirror/mode/pegjs/pegjs"]
  73. ["codemirror/mode/perl/perl"]
  74. ["codemirror/mode/php/php"]
  75. ["codemirror/mode/pig/pig"]
  76. ["codemirror/mode/powershell/powershell"]
  77. ["codemirror/mode/properties/properties"]
  78. ["codemirror/mode/protobuf/protobuf"]
  79. ["codemirror/mode/pug/pug"]
  80. ["codemirror/mode/puppet/puppet"]
  81. ["codemirror/mode/python/python"]
  82. ["codemirror/mode/q/q"]
  83. ["codemirror/mode/r/r"]
  84. ["codemirror/mode/rpm/rpm"]
  85. ["codemirror/mode/rst/rst"]
  86. ["codemirror/mode/ruby/ruby"]
  87. ["codemirror/mode/rust/rust"]
  88. ["codemirror/mode/sas/sas"]
  89. ["codemirror/mode/sass/sass"]
  90. ["codemirror/mode/scheme/scheme"]
  91. ["codemirror/mode/shell/shell"]
  92. ["codemirror/mode/sieve/sieve"]
  93. ["codemirror/mode/slim/slim"]
  94. ["codemirror/mode/smalltalk/smalltalk"]
  95. ["codemirror/mode/smarty/smarty"]
  96. ["codemirror/mode/solr/solr"]
  97. ["codemirror/mode/soy/soy"]
  98. ["codemirror/mode/sparql/sparql"]
  99. ["codemirror/mode/spreadsheet/spreadsheet"]
  100. ["codemirror/mode/sql/sql"]
  101. ["codemirror/mode/stex/stex"]
  102. ["codemirror/mode/stylus/stylus"]
  103. ["codemirror/mode/swift/swift"]
  104. ["codemirror/mode/tcl/tcl"]
  105. ["codemirror/mode/textile/textile"]
  106. ["codemirror/mode/tiddlywiki/tiddlywiki"]
  107. ["codemirror/mode/tiki/tiki"]
  108. ["codemirror/mode/toml/toml"]
  109. ["codemirror/mode/tornado/tornado"]
  110. ["codemirror/mode/troff/troff"]
  111. ["codemirror/mode/ttcn-cfg/ttcn-cfg"]
  112. ["codemirror/mode/ttcn/ttcn"]
  113. ["codemirror/mode/turtle/turtle"]
  114. ["codemirror/mode/twig/twig"]
  115. ["codemirror/mode/vb/vb"]
  116. ["codemirror/mode/vbscript/vbscript"]
  117. ["codemirror/mode/velocity/velocity"]
  118. ["codemirror/mode/verilog/verilog"]
  119. ["codemirror/mode/vhdl/vhdl"]
  120. ["codemirror/mode/vue/vue"]
  121. ["codemirror/mode/wast/wast"]
  122. ["codemirror/mode/webidl/webidl"]
  123. ["codemirror/mode/xml/xml"]
  124. ["codemirror/mode/xquery/xquery"]
  125. ["codemirror/mode/yacas/yacas"]
  126. ["codemirror/mode/yaml-frontmatter/yaml-frontmatter"]
  127. ["codemirror/mode/yaml/yaml"]
  128. ["codemirror/mode/z80/z80"]
  129. [dommy.core :as dom]
  130. [frontend.commands :as commands]
  131. [frontend.db :as db]
  132. [frontend.extensions.calc :as calc]
  133. [frontend.handler.editor :as editor-handler]
  134. [frontend.handler.file :as file-handler]
  135. [frontend.state :as state]
  136. [frontend.utf8 :as utf8]
  137. [frontend.util :as util]
  138. [goog.dom :as gdom]
  139. [goog.object :as gobj]
  140. [rum.core :as rum]))
  141. ;; codemirror
  142. (def from-textarea (gobj/get cm "fromTextArea"))
  143. (def textarea-ref-name "textarea")
  144. (def codemirror-ref-name "codemirror-instance")
  145. (defn- extra-codemirror-options []
  146. (get (state/get-config)
  147. :editor/extra-codemirror-options {}))
  148. (defn- save-file-or-block!
  149. [editor textarea config state]
  150. (.save editor)
  151. (let [value (gobj/get textarea "value")
  152. default-value (gobj/get textarea "defaultValue")]
  153. (when (not= value default-value)
  154. (cond
  155. (:block/uuid config)
  156. (let [block (db/pull [:block/uuid (:block/uuid config)])
  157. content (:block/content block)
  158. {:keys [start_pos end_pos]} (:pos_meta (last (:rum/args state)))
  159. offset (if (:block/pre-block? block) 0 2)
  160. raw-content (utf8/encode content) ;; NOTE: :pos_meta is based on byte position
  161. prefix (utf8/decode (.slice raw-content 0 (- start_pos offset)))
  162. surfix (utf8/decode (.slice raw-content (- end_pos offset)))
  163. new-content (if (string/blank? value)
  164. (str prefix surfix)
  165. (str prefix value "\n" surfix))]
  166. (editor-handler/save-block-if-changed! block new-content))
  167. (:file-path config)
  168. (let [path (:file-path config)
  169. content (db/get-file path)
  170. [_ id _ _ _] (:rum/args state)
  171. value (some-> (gdom/getElement id)
  172. (gobj/get "value"))]
  173. (when (and
  174. (not (string/blank? value))
  175. (not= (string/trim value) (string/trim content)))
  176. (file-handler/alter-file (state/get-current-repo)
  177. path
  178. (str (string/trim value) "\n")
  179. {:re-render-root? true})))
  180. :else
  181. nil))))
  182. ;; Avoid reentrancy
  183. (def *code-saving (atom false))
  184. (defn save-file-or-block-when-blur-or-esc!
  185. [editor textarea config state]
  186. (when-not @*code-saving
  187. (reset! *code-saving true)
  188. (save-file-or-block! editor textarea config state)
  189. (reset! *code-saving false)))
  190. (defn- text->cm-mode
  191. ([text]
  192. (text->cm-mode text :name))
  193. ([text by]
  194. (when text
  195. (let [mode (string/lower-case text)
  196. find-fn-name (case by
  197. :name "findModeByName"
  198. :ext "findModeByExtension"
  199. :file-name "findModeByFileName"
  200. "findModeByName")
  201. find-fn (gobj/get cm find-fn-name)
  202. cm-mode (find-fn mode)]
  203. (if cm-mode
  204. (.-mime cm-mode)
  205. mode)))))
  206. (defn render!
  207. [state]
  208. (let [[config id attr _code theme] (:rum/args state)
  209. default-open? (and (:editor/code-mode? @state/state)
  210. (= (:block/uuid (state/get-edit-block))
  211. (get-in config [:block :block/uuid])))
  212. _ (state/set-state! :editor/code-mode? false)
  213. original-mode (get attr :data-lang)
  214. mode (if (:file? config)
  215. (text->cm-mode original-mode :ext) ;; ref: src/main/frontend/components/file.cljs
  216. (text->cm-mode original-mode :name))
  217. lisp-like? (contains? #{"scheme" "lisp" "clojure" "edn"} mode)
  218. textarea (gdom/getElement id)
  219. default-cm-options {:theme (str "solarized " theme)
  220. :autoCloseBrackets true
  221. :lineNumbers true
  222. :matchBrackets lisp-like?
  223. :styleActiveLine true}
  224. cm-options (merge default-cm-options
  225. (extra-codemirror-options)
  226. {:mode mode
  227. :extraKeys #js {"Esc" (fn [cm]
  228. (save-file-or-block-when-blur-or-esc! cm textarea config state)
  229. (when-let [block-id (:block/uuid config)]
  230. (let [block (db/pull [:block/uuid block-id])]
  231. (editor-handler/edit-block! block :max block-id))))}})
  232. editor (when textarea
  233. (from-textarea textarea (clj->js cm-options)))]
  234. (when editor
  235. (let [textarea-ref (rum/ref-node state textarea-ref-name)
  236. element (.getWrapperElement editor)]
  237. (gobj/set textarea-ref codemirror-ref-name editor)
  238. (when (= mode "calc")
  239. (.on editor "change" (fn [_cm _e]
  240. (let [new-code (.getValue editor)]
  241. (reset! (:calc-atom state) (calc/eval-lines new-code))))))
  242. (.on editor "blur" (fn [_cm e]
  243. (when e (util/stop e))
  244. (state/set-block-component-editing-mode! false)
  245. (save-file-or-block-when-blur-or-esc! editor textarea config state)))
  246. (.addEventListener element "mousedown"
  247. (fn [e]
  248. (state/clear-selection!)
  249. (when-let [block (and (:block/uuid config) (into {} (db/get-block-by-uuid (:block/uuid config))))]
  250. (state/set-editing! id (.getValue editor) block nil false))
  251. (util/stop e)
  252. (state/set-block-component-editing-mode! true)))
  253. (.save editor)
  254. (.refresh editor)
  255. (when default-open?
  256. (.focus editor))))
  257. editor))
  258. (defn- load-and-render!
  259. [state]
  260. (let [editor-atom (:editor-atom state)
  261. editor (render! state)]
  262. (reset! editor-atom editor)))
  263. (rum/defcs editor < rum/reactive
  264. {:init (fn [state]
  265. (let [[_ _ _ code _] (:rum/args state)]
  266. (assoc state :editor-atom (atom nil) :calc-atom (atom (calc/eval-lines code)))))
  267. :did-mount (fn [state]
  268. (load-and-render! state)
  269. state)
  270. :will-update (fn [state]
  271. (when-let [editor @(:editor-atom state)]
  272. (.toTextArea ^js editor)
  273. (let [[_ _ _ code _] (:rum/args state)]
  274. (when-let [textarea (rum/ref-node state textarea-ref-name)]
  275. (gobj/set textarea "defaultValue" code)
  276. (gobj/set textarea "value" code))))
  277. state)
  278. :did-update (fn [state]
  279. (load-and-render! state)
  280. state)}
  281. [state _config id attr code _theme _options]
  282. [:div.extensions__code
  283. (when-let [mode (:data-lang attr)]
  284. (when-not (= mode "calc")
  285. [:div.extensions__code-lang
  286. (string/lower-case mode)]))
  287. [:div.flex.flex-1.flex-row.w-full.mt-6
  288. [:textarea (merge {:id id
  289. ;; Expose the textarea associated with the CodeMirror instance via
  290. ;; ref so that we can autofocus into the CodeMirror instance later.
  291. :ref textarea-ref-name
  292. :default-value code} attr)]
  293. (when (= (:data-lang attr) "calc")
  294. (calc/results (:calc-atom state)))]])
  295. ;; Focus into the CodeMirror editor rather than the normal "raw" editor
  296. (defmethod commands/handle-step :codemirror/focus [[_]]
  297. ;; This requestAnimationFrame is necessary because, for some reason, when you
  298. ;; type /calculate and then click the "Calculate" command in the dropdown
  299. ;; *with your mouse* (but not when you do so via your keyboard with the
  300. ;; arrow + enter keys!), React doesn't re-render before the :codemirror/focus
  301. ;; command kicks off. As a result, you get an error saying that the node
  302. ;; you're trying to focus doesn't yet exist. Adding the requestAnimationFrame
  303. ;; ensures that the React component re-renders before the :codemirror/focus
  304. ;; command is run. It's not elegant... open to suggestions for how to fix it!
  305. (js/window.requestAnimationFrame
  306. (fn []
  307. (let [block (state/get-edit-block)
  308. block-uuid (:block/uuid block)
  309. block-node (util/get-first-block-by-id block-uuid)]
  310. (editor-handler/select-block! (:block/uuid block))
  311. (let [textarea-ref (.querySelector block-node "textarea")]
  312. (.focus (gobj/get textarea-ref codemirror-ref-name)))
  313. (util/select-unhighlight! (dom/by-class "selected"))
  314. (state/clear-selection!)))))