events.cljs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (ns microtables-frontend.events
  2. (:require
  3. [re-frame.core :as re-frame]
  4. [microtables-frontend.db :as db]
  5. [microtables-frontend.utils :as utils]))
  6. (re-frame/reg-event-db
  7. ::initialize-db
  8. (fn [_ _]
  9. (println "initializing db")
  10. (-> db/default-db
  11. (update-in [:table-data] #(utils/walk-modify-data % (fn [c r datum] (if (= (first (:value datum)) "=") (assoc datum :dirty true) datum))))
  12. (update-in [:table-data] utils/create-all-references)
  13. (update-in [:table-data] utils/create-all-back-references)
  14. (update-in [:table-data] utils/evaluate-all))))
  15. (re-frame/reg-event-db
  16. ::movement-enter-cell
  17. (fn [db [_ c r]]
  18. (println "::movement-enter-cell" c r)
  19. (assoc-in db [:position :cursor] {:col c :row r})))
  20. (re-frame/reg-event-db
  21. ::movement-leave-cell
  22. (fn [db [_ c r]]
  23. (println "::movement-leave-cell" c r)
  24. (-> db
  25. (assoc-in [:position :cursor] nil)
  26. (assoc-in [:position :selection] nil)
  27. (update-in [:table-data] #(utils/reset-references % c r))
  28. (update-in [:table-data] #(utils/evaluate-from-cell % c r)))))
  29. (re-frame/reg-event-db
  30. ::edit-cell-value
  31. (fn [db [_ c r value]]
  32. (println "::edit-cell-value" c r value)
  33. (update-in db [:table-data] #(utils/change-datum-value % c r value))))
  34. ; handle pressing enter (move to the next cell down)
  35. ; tab is taken care of natively, and is good enough
  36. (re-frame/reg-event-fx
  37. ::press-enter-in-cell
  38. (fn [{:keys [db]} [_ c r]]
  39. (let [max-row? (= (utils/highest-row (:table-data db)) r)
  40. max-col? (= (utils/highest-col (:table-data db)) c)
  41. new-col (if max-row?
  42. (if max-col?
  43. "A"
  44. (utils/next-letter c))
  45. c)
  46. new-row (if max-row?
  47. 1
  48. (inc r))]
  49. (println "::press-enter-in-cell" c r)
  50. {:focus-on-cell [new-col new-row]})))
  51. (re-frame/reg-fx
  52. :focus-on-cell
  53. (fn [[c r]]
  54. (println "fx for :press-enter" c r)
  55. (.focus (.getElementById js/document (str c r)))))