 (defonce app-state (atom
                     {:html-text ""
                      :hiccup-text ""}))
-
 (defonce hiccup-ace-editor (atom nil))
+(defonce html-ace-editor (atom nil))
 
 (declare beautify-hiccup)
 

  (display-name [this] "HiccupArea")
  (render [this] (html [:div
                        [:h2 "[:hiccup]"]
                        [:div#hiccup-ace-editor]]))
  (did-mount [this]
    (let [ace-editor (.edit js/ace "hiccup-ace-editor")]
      (.setOptions ace-editor (js-obj "minLines" 25 "maxLines" 25))
      (.setTheme ace-editor "ace/theme/github")
      (.setMode (.getSession ace-editor) "ace/mode/clojure")
      (.setReadOnly ace-editor true) ; Read-only for now
      (.. ace-editor
          getSession
          (on "change" #(put! (@state :action-chan) [:update-html (.getValue @hiccup-ace-editor)])))
      (set-ace-editor-value ace-editor (@app-state :hiccup-text))
      (reset! hiccup-ace-editor ace-editor)))
  (will-update [this _ {:keys [action-chan]}]
    (set-ace-editor-value @hiccup-ace-editor (@app-state :hiccup-text))))



(defn set-ace-editor-value [ace-editor value]
  (let [cursor (.getCursorPositionScreen ace-editor)]
    (.. ace-editor getSession (setValue (@app-state :hiccup-text) cursor))))

(defcomponentk html-input-view
  "HTML text input view"
  [state owner]
  (display-name [this] "HtmlArea")
  (render [this] (html [:div
                        [:h2 "<html>"]
                        [:div {:ref "html-ace-editor"}]]))
  (did-mount [this]
    (let [action-chan (@state :action-chan)
          ace-editor (.edit js/ace (om/get-node owner "html-ace-editor"))
          ace-session (.getSession ace-editor)]
      (.setOptions ace-editor (js-obj "minLines" 25 "maxLines" 25))
      (.setTheme ace-editor "ace/theme/github")
      (.setMode ace-session "ace/mode/html")
      (reset! html-ace-editor ace-editor)
      (.on ace-session "change"
           #(put! action-chan [:update-hiccup
                               (.getValue @html-ace-editor)])))))
      ;(set-ace-editor-value ace-editor (@app-state :html-text))
  ;(will-update [this _ {:keys [action-chan]}]
  ;  (set-ace-editor-value @html-ace-editor (@app-state :html-text))))

(defcomponentk hiccup-input-view
  "Hiccup text input view"
