ui.cljs 10 KB

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