ui.cljs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. (ns frontend.handler.ui
  2. (:require [cljs-time.core :refer [plus days weeks]]
  3. [dommy.core :as dom]
  4. [frontend.util :as util]
  5. [frontend.db :as db]
  6. [frontend.db.model :as db-model]
  7. [frontend.config :as config]
  8. [frontend.state :as state]
  9. [frontend.storage :as storage]
  10. [frontend.fs :as fs]
  11. [frontend.loader :refer [load]]
  12. [goog.dom :as gdom]
  13. [goog.object :as gobj]
  14. [clojure.string :as string]
  15. [rum.core :as rum]
  16. [frontend.mobile.util :as mobile]
  17. [electron.ipc :as ipc]))
  18. (defn- get-css-var-value
  19. [var-name]
  20. (.getPropertyValue (js/getComputedStyle (.-documentElement js/document)) var-name))
  21. ;; sidebars
  22. (defn- get-right-sidebar-width
  23. []
  24. (or (.. (js/document.getElementById "right-sidebar") -style -width)
  25. (get-css-var-value "--right-sidebar-width")))
  26. (defn- persist-right-sidebar-width!
  27. []
  28. (storage/set "ls-right-sidebar-width" (get-right-sidebar-width)))
  29. (defn- restore-right-sidebar-width!
  30. []
  31. (when-let [width (storage/get "ls-right-sidebar-width")]
  32. (.setProperty (.-style (js/document.getElementById "right-sidebar")) "width" width)))
  33. (defn close-left-sidebar!
  34. []
  35. (when-let [elem (gdom/getElement "close-left-bar")]
  36. (persist-right-sidebar-width!)
  37. (.click elem)))
  38. (defn toggle-right-sidebar!
  39. []
  40. (if (:ui/sidebar-open? @state/state)
  41. (persist-right-sidebar-width!)
  42. (restore-right-sidebar-width!))
  43. (state/toggle-sidebar-open?!))
  44. (defn persist-right-sidebar-state!
  45. []
  46. (let [sidebar-open? (:ui/sidebar-open? @state/state)
  47. data (if sidebar-open? {:blocks (:sidebar/blocks @state/state)
  48. :collapsed (:ui/sidebar-collapsed-blocks @state/state)
  49. :open? true} {:open? false})]
  50. (storage/set "ls-right-sidebar-state" data)))
  51. (defn restore-right-sidebar-state!
  52. []
  53. (when-let [data' (storage/get "ls-right-sidebar-state")]
  54. (let [{:keys [open? collapsed blocks]} data']
  55. (when open?
  56. (state/set-state! :ui/sidebar-open? open?)
  57. (state/set-state! :sidebar/blocks blocks)
  58. (state/set-state! :ui/sidebar-collapsed-blocks collapsed)))))
  59. (defn toggle-contents!
  60. []
  61. (when-let [current-repo (state/get-current-repo)]
  62. (let [id "contents"]
  63. (if (state/sidebar-block-exists? id)
  64. (state/sidebar-remove-block! id)
  65. (state/sidebar-add-block! current-repo id :contents nil)))))
  66. (defn toggle-help!
  67. []
  68. (when-let [current-repo (state/get-current-repo)]
  69. (let [id "help"]
  70. (if (state/sidebar-block-exists? id)
  71. (state/sidebar-remove-block! id)
  72. (state/sidebar-add-block! current-repo id :help nil)))))
  73. (defn toggle-settings-modal!
  74. []
  75. (when-not (:srs/mode? @state/state)
  76. (state/toggle-settings!)))
  77. ;; FIXME: re-render all embedded blocks since they will not be re-rendered automatically
  78. (defn re-render-root!
  79. ([]
  80. (re-render-root! {}))
  81. ([{:keys [clear-all-query-state?]
  82. :or {clear-all-query-state? false}}]
  83. (when-let [component (state/get-root-component)]
  84. (if clear-all-query-state?
  85. (db/clear-query-state!)
  86. (db/clear-query-state-without-refs-and-embeds!))
  87. (rum/request-render component)
  88. (doseq [component (state/get-custom-query-components)]
  89. (rum/request-render component)))))
  90. (defn re-render-file!
  91. []
  92. (when-let [component (state/get-file-component)]
  93. (when (= :file (state/get-current-route))
  94. (rum/request-render component))))
  95. (defn highlight-element!
  96. [fragment]
  97. (let [id (and
  98. (> (count fragment) 36)
  99. (subs fragment (- (count fragment) 36)))]
  100. (if (and id (util/uuid-string? id))
  101. (let [elements (array-seq (js/document.getElementsByClassName id))]
  102. (when (first elements)
  103. (util/scroll-to-element (gobj/get (first elements) "id")))
  104. (state/exit-editing-and-set-selected-blocks! elements))
  105. (when-let [element (gdom/getElement fragment)]
  106. (util/scroll-to-element fragment)
  107. (dom/add-class! element "block-highlight")
  108. (js/setTimeout #(dom/remove-class! element "block-highlight")
  109. 4000)))))
  110. (defn add-style-if-exists!
  111. []
  112. (when-let [style (or
  113. (state/get-custom-css-link)
  114. (db-model/get-custom-css)
  115. ;; (state/get-custom-css-link)
  116. )]
  117. (util/add-style! style)))
  118. (def *js-execed (atom #{}))
  119. (defn exec-js-if-exists-&-allowed!
  120. [t]
  121. (when-not (mobile/is-native-platform?)
  122. (when-let [href (or
  123. (state/get-custom-js-link)
  124. (config/get-custom-js-path))]
  125. (let [k (str "ls-js-allowed-" href)
  126. execed #(swap! *js-execed conj href)
  127. execed? (contains? @*js-execed href)
  128. ask-allow #(let [r (js/confirm (t :plugin/custom-js-alert))]
  129. (if r
  130. (storage/set k (js/Date.now))
  131. (storage/set k false))
  132. r)
  133. allowed! (storage/get k)
  134. should-ask? (or (nil? allowed!)
  135. (> (- (js/Date.now) allowed!) 604800000))]
  136. (when (and (not execed?)
  137. (not= false allowed!))
  138. (if (string/starts-with? href "http")
  139. (when (or (not should-ask?)
  140. (ask-allow))
  141. (load href #(do (js/console.log "[custom js]" href) (execed))))
  142. (util/p-handle
  143. (fs/read-file (if (util/electron?) "" (config/get-repo-dir (state/get-current-repo))) href)
  144. #(when-let [scripts (and % (string/trim %))]
  145. (when-not (string/blank? scripts)
  146. (when (or (not should-ask?) (ask-allow))
  147. (try
  148. (js/eval scripts)
  149. (execed)
  150. (catch js/Error e
  151. (js/console.error "[custom js]" e)))))))))))))
  152. (defn toggle-wide-mode!
  153. []
  154. (let [wide? (state/get-wide-mode?)
  155. elements (array-seq (js/document.getElementsByClassName "cp__sidebar-main-content"))
  156. max-width (if wide? "var(--ls-main-content-max-width)" "var(--ls-main-content-max-width-wide)")]
  157. (when-let [element (first elements)]
  158. (dom/set-style! element :max-width max-width))
  159. (state/toggle-wide-mode!)))
  160. ;; auto-complete
  161. (defn auto-complete-prev
  162. [state e]
  163. (let [current-idx (get state :frontend.ui/current-idx)
  164. matched (first (:rum/args state))]
  165. (util/stop e)
  166. (cond
  167. (>= @current-idx 1)
  168. (swap! current-idx dec)
  169. (= @current-idx 0)
  170. (reset! current-idx (dec (count matched)))
  171. :else nil)
  172. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  173. (let [modal (gobj/get (gdom/getElement "ui__ac") "parentElement")
  174. height (or (gobj/get modal "offsetHeight") 300)
  175. scroll-top (- (gobj/get element "offsetTop") (/ height 2))]
  176. (set! (.-scrollTop modal) scroll-top)))))
  177. (defn auto-complete-next
  178. [state e]
  179. (let [current-idx (get state :frontend.ui/current-idx)
  180. matched (first (:rum/args state))]
  181. (util/stop e)
  182. (let [total (count matched)]
  183. (if (>= @current-idx (dec total))
  184. (reset! current-idx 0)
  185. (swap! current-idx inc)))
  186. (when-let [element (gdom/getElement (str "ac-" @current-idx))]
  187. (let [modal (gobj/get (gdom/getElement "ui__ac") "parentElement")
  188. height (or (gobj/get modal "offsetHeight") 300)
  189. scroll-top (- (gobj/get element "offsetTop") (/ height 2))]
  190. (set! (.-scrollTop modal) scroll-top)))))
  191. (defn auto-complete-complete
  192. [state e]
  193. (let [[matched {:keys [on-chosen on-enter]}] (:rum/args state)
  194. current-idx (get state :frontend.ui/current-idx)]
  195. (util/stop e)
  196. (if (and (seq matched)
  197. (> (count matched)
  198. @current-idx))
  199. (on-chosen (nth matched @current-idx) false)
  200. (and on-enter (on-enter state)))))
  201. (defn auto-complete-shift-complete
  202. [state e]
  203. (let [[matched {:keys [on-chosen on-shift-chosen on-enter]}] (:rum/args state)
  204. current-idx (get state :frontend.ui/current-idx)]
  205. (util/stop e)
  206. (if (and (seq matched)
  207. (> (count matched)
  208. @current-idx))
  209. ((or on-shift-chosen on-chosen) (nth matched @current-idx) false)
  210. (and on-enter (on-enter state)))))
  211. ;; date-picker
  212. ;; TODO: find a better way
  213. (def *internal-model (rum/cursor state/state :date-picker/date))
  214. (defn- non-edit-input?
  215. []
  216. (when-let [elem js/document.activeElement]
  217. (and (util/input? elem)
  218. (when-let [id (gobj/get elem "id")]
  219. (not (string/starts-with? id "edit-block-"))))))
  220. (defn- input-or-select?
  221. []
  222. (when-let [elem js/document.activeElement]
  223. (or (non-edit-input?)
  224. (util/select? elem))))
  225. (defn- inc-date [date n] (plus date (days n)))
  226. (defn- inc-week [date n] (plus date (weeks n)))
  227. (defn shortcut-complete
  228. [state e]
  229. (let [{:keys [on-change deadline-or-schedule?]} (last (:rum/args state))]
  230. (when (and on-change
  231. (not (input-or-select?)))
  232. (when-not deadline-or-schedule?
  233. (on-change e @*internal-model)))))
  234. (defn shortcut-prev-day
  235. [_state e]
  236. (when-not (input-or-select?)
  237. (util/stop e)
  238. (swap! *internal-model inc-date -1)))
  239. (defn shortcut-next-day
  240. [_state e]
  241. (when-not (input-or-select?)
  242. (util/stop e)
  243. (swap! *internal-model inc-date 1)))
  244. (defn shortcut-prev-week
  245. [_state e]
  246. (when-not (input-or-select?)
  247. (util/stop e)
  248. (swap! *internal-model inc-week -1)))
  249. (defn shortcut-next-week
  250. [_state e]
  251. (when-not (input-or-select?)
  252. (util/stop e)
  253. (swap! *internal-model inc-week 1)))
  254. (defn toggle-cards!
  255. []
  256. (if (:modal/show? @state/state)
  257. (state/close-modal!)
  258. (state/pub-event! [:modal/show-cards])))
  259. (defn open-new-window!
  260. []
  261. (ipc/ipc "openNewWindow"))