Commit 6b993a56 authored by Bruno Burke's avatar Bruno Burke 🍔
Browse files

fix bug in requiring codemirror legacy modes

parent 35b581d2
Pipeline #116804 failed with stages
in 4 minutes and 4 seconds
......@@ -9,29 +9,7 @@
[reagent.core :as reagent]
[reagent.dom :as rdom]
[codemirror :refer [EditorView basicSetup]]
["@codemirror/language" :refer [StreamLanguage]]
["@codemirror/lang-javascript" :refer [javascript]]
["@codemirror/lang-cpp" :refer [cpp]]
["@codemirror/lang-markdown" :refer [markdown]]
["@codemirror/lang-python" :refer [python]]
["@codemirror/legacy-modes/mode/commonlisp" :refer [commonlisp]]
["@codemirror/legacy-modes/mode/r" :refer [r]]
["@codemirror/legacy-modes/mode/clike" :refer [clike]]
["@codemirror/legacy-modes/mode/clojure" :refer [clojure]]))
(defn get-codemirror-mode [language]
(let [language (or language "python")]
(case (name language)
"python2" (python)
"python3" (python)
"common-lisp" (.define StreamLanguage commonlisp)
"clojure" (.define StreamLanguage clojure)
"c" (.define StreamLanguage clike)
"r" (.define StreamLanguage r)
"rmarkdown" (markdown)
"markdown" (markdown)
(python))))
[leukipp.components.exercise-types.programming.views.common :refer [get-codemirror-mode]]))
(defn code-editor [{:keys [code language on-change cm-editor-id]}]
(let [cm-atom (atom nil)
......
(ns leukipp.components.exercise-types.programming.views.common
(:require
[leukipp.components.content-elements.code.show :as code-show]
[reagent.core :as reagent]
[reagent.dom :as rdom]))
[reagent.dom :as rdom]
[codemirror :refer [EditorView basicSetup]]
["@codemirror/language" :refer [StreamLanguage]]
["@codemirror/lang-javascript" :refer [javascript]]
["@codemirror/lang-cpp" :refer [cpp]]
["@codemirror/lang-markdown" :refer [markdown]]
["@codemirror/lang-python" :refer [python]]
["@codemirror/legacy-modes/mode/commonlisp" :refer [commonLisp]]
["@codemirror/legacy-modes/mode/r" :refer [r]]
["@codemirror/legacy-modes/mode/clike" :refer [c]]
["@codemirror/legacy-modes/mode/clojure" :refer [clojure]]))
(defn get-codemirror-mode [language]
(let [language (or language "python")]
(case (name language)
"python2" (python)
"python3" (python)
"common-lisp" (.define StreamLanguage commonLisp)
"clojure" (.define StreamLanguage clojure)
"c" (.define StreamLanguage c)
"r" (.define StreamLanguage r)
"rmarkdown" (markdown)
"markdown" (markdown)
(python))))
(defn code-editor [{:keys [code]}]
(let [cm-atom (atom nil)
......@@ -12,21 +35,42 @@
:component-did-mount
(fn [comp]
(let [dom-node (rdom/dom-node comp)
{:keys [language]} (reagent/props comp)
syntax-language (code-show/get-codemirror-mode language)
{:keys [language code]} (reagent/props comp)
syntax-language (get-codemirror-mode language)
code-editor (js/document.getElementById id)
cm-editor (.fromTextArea
js/CodeMirror
dom-node (clj->js {:lineNumbers true
:mode syntax-language}))]
(.on cm-editor "change" #(reset! code (.getValue cm-editor)))
(.setSize cm-editor nil 200)
(reset! cm-atom cm-editor)))
update-listener (fn [tr]
(let [new-text (-> tr
.-state
.-doc
.toString)]
(reset! code new-text)))
initial-code (or @code "")
cm-editor (new EditorView (clj->js {:extensions [basicSetup
(.of (-> EditorView
.-updateListener) update-listener)
(.of (-> EditorView
.-contentAttributes) (clj->js {:contenteditable (if (true? read-only)
false
true ;;; this will allow copy on readonly code-editors
)}))
syntax-language]
:parent code-editor}))]
(.dispatch cm-editor (clj->js {:changes {:from 0
:to (-> cm-editor
.-state
.-doc
.-length)
:insert initial-code}}))
#_(.setOption cm-editor "mode" syntax-language)
#_(.setValue cm-editor (or code ""))
;(.setSize cm-editor nil 200)
(reset! cm-atom cm-editor)
#_(reset! cm-atom cm-editor)))
:component-did-update
(fn [this]
(let [{:keys [language]} (reagent/props this)]
(when @cm-atom
(.setOption @cm-atom "mode" (code-show/get-codemirror-mode language)))))
#_(when @cm-atom
(.setOption @cm-atom "mode" (get-codemirror-mode language)))))
:reagent-render
(fn [{:keys [code]}]
[:textarea {:id id :defaultValue @code}])})))
[:div {:id id}])})))
......@@ -12,28 +12,7 @@
[bulma-ui.core :as ui]
[clj-helper.vector :as vhelper]
[codemirror :refer [EditorView basicSetup]]
["@codemirror/language" :refer [StreamLanguage]]
["@codemirror/lang-javascript" :refer [javascript]]
["@codemirror/lang-cpp" :refer [cpp]]
["@codemirror/lang-markdown" :refer [markdown]]
["@codemirror/lang-python" :refer [python]]
["@codemirror/legacy-modes/mode/commonlisp" :refer [commonlisp]]
["@codemirror/legacy-modes/mode/r" :refer [r]]
["@codemirror/legacy-modes/mode/clike" :refer [clike]]
["@codemirror/legacy-modes/mode/clojure" :refer [clojure]]))
(defn get-codemirror-mode [language]
(let [language (or language "python")]
(case (name language)
"python2" (python)
"python3" (python)
"common-lisp" (.define StreamLanguage commonlisp)
"clojure" (.define StreamLanguage clojure)
"c" (.define StreamLanguage clike)
"r" (.define StreamLanguage r)
"rmarkdown" (markdown)
"markdown" (markdown)
(python))))
[leukipp.components.exercise-types.programming.views.common :refer [get-codemirror-mode]]))
(defn is-correct [result]
......
(ns leukipp.components.devcards
(:require [devcards.core :as dc :include-macros true]
["highlight.js" :as hljs]
["marked" :as marked]))
["marked" :as marked]
[leukipp.components.core :as core]))
(js/goog.exportSymbol "DevcardsSyntaxHighlighter" hljs)
(js/goog.exportSymbol "DevcardsMarked" marked)
(defn ^:export init []
(core/init-exercise-types)
(dc/start-devcard-ui!))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment