subs.cljs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (ns microtables-frontend.subs
  2. (:require
  3. [re-frame.core :as re-frame]
  4. [microtables-frontend.utils :as utils]))
  5. (re-frame/reg-sub
  6. ::controls-state
  7. (fn [db]
  8. (println "reporting state of controls")
  9. (:controls db)))
  10. (defn highlight-cells
  11. "Modify all cells in data (set :view to :highlighted) that are within the given range."
  12. [data selection]
  13. (let [c1 (get-in selection [:start :col])
  14. r1 (get-in selection [:start :row])
  15. c2 (get-in selection [:end :col])
  16. r2 (get-in selection [:end :row])
  17. cells (utils/range->list c1 r1 c2 r2)]
  18. (loop [new-data data
  19. remaining cells]
  20. (if (empty? remaining)
  21. new-data
  22. (let [cur (first remaining)
  23. highlighted (assoc-in new-data [(:col cur) (:row cur) :view] :highlighted)]
  24. (recur highlighted (rest remaining)))))))
  25. ;TODO: insert other display mode data? ("value": formula (cursor), "display" (default): evaluated, "highlighted": in a selection (just a class?))
  26. (re-frame/reg-sub
  27. ::table-data
  28. (fn [db]
  29. (println "returning table data")
  30. (let [data (:table-data db)
  31. cursor (get-in db [:position :cursor])
  32. selection (get-in db [:position :selection])
  33. highlighted (if selection
  34. (highlight-cells data selection)
  35. data)]
  36. (if cursor
  37. (assoc-in highlighted [(:col cursor) (:row cursor) :view] :value)
  38. highlighted))))