/src/app/comp/bookmark.cljs

https://github.com/Cirru/calcit-editor · ClojureScript · 65 lines · 57 code · 8 blank · 0 comment · 0 complexity · 06972f9042e26807902c08dfcdcffb79 MD5 · raw file

  1. (ns app.comp.bookmark
  2. (:require [clojure.string :as string]
  3. [hsl.core :refer [hsl]]
  4. [respo-ui.core :as ui]
  5. [respo.core :refer [defcomp <> span div a]]
  6. [respo.comp.space :refer [=<]]))
  7. (defn on-pick [bookmark idx]
  8. (fn [e d!]
  9. (let [event (:original-event e)
  10. shift? (.-shiftKey event)
  11. alt? (.-altKey event)
  12. meta? (.-metaKey event)]
  13. (cond
  14. meta? (d! :writer/collapse idx)
  15. alt? (d! :writer/remove-idx idx)
  16. :else (d! :writer/point-to idx)))))
  17. (def style-bookmark
  18. {:line-height "1.2em",
  19. :padding "4px 8px",
  20. :cursor :pointer,
  21. :position :relative,
  22. :white-space :nowrap})
  23. (def style-highlight {:color (hsl 0 0 100)})
  24. (def style-kind
  25. {:color (hsl 340 80 60),
  26. :font-family ui/font-normal,
  27. :font-size 12,
  28. :margin-right 4,
  29. :vertical-align :middle})
  30. (def style-main {:vertical-align :middle, :color (hsl 0 0 70), :font-family ui/font-normal})
  31. (def style-minor {:color (hsl 0 0 40), :font-size 12})
  32. (defcomp
  33. comp-bookmark
  34. (bookmark idx selected?)
  35. (div
  36. {:class-name "stack-bookmark",
  37. :draggable true,
  38. :on-click (on-pick bookmark idx),
  39. :on-dragstart (fn [e d!] (-> e :event .-dataTransfer (.setData "id" idx))),
  40. :on-drop (fn [e d!]
  41. (let [target-idx (js/parseInt (-> e :event .-dataTransfer (.getData "id")))]
  42. (when (not= target-idx idx) (d! :writer/move-order {:from target-idx, :to idx})))),
  43. :on-dragover (fn [e d!] (-> e :event .preventDefault))}
  44. (case (:kind bookmark)
  45. :def
  46. (div
  47. {:style (merge style-bookmark)}
  48. (div
  49. {}
  50. (span
  51. {:inner-text (:extra bookmark),
  52. :style (merge style-main (if selected? style-highlight))}))
  53. (div {:style ui/row-middle} (=< 4 nil) (<> (:ns bookmark) style-minor)))
  54. (div
  55. {:style (merge style-bookmark {:padding "8px"})}
  56. (<> span (str (:kind bookmark)) style-kind)
  57. (<> (:ns bookmark) (merge style-main (if selected? style-highlight)))))))