config.cljs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. (ns frontend.modules.shortcut.config
  2. (:require [frontend.components.commit :as commit]
  3. [frontend.extensions.srs.handler :as srs]
  4. [frontend.extensions.pdf.utils :as pdf-utils]
  5. [frontend.handler.config :as config-handler]
  6. [frontend.handler.editor :as editor-handler]
  7. [frontend.handler.paste :as paste-handler]
  8. [frontend.handler.history :as history]
  9. [frontend.handler.page :as page-handler]
  10. [frontend.handler.route :as route-handler]
  11. [frontend.handler.journal :as journal-handler]
  12. [frontend.handler.search :as search-handler]
  13. [frontend.handler.ui :as ui-handler]
  14. [frontend.handler.plugin :as plugin-handler]
  15. [frontend.handler.export :as export-handler]
  16. [frontend.handler.whiteboard :as whiteboard-handler]
  17. [frontend.handler.plugin-config :as plugin-config-handler]
  18. [frontend.modules.editor.undo-redo :as undo-redo]
  19. [frontend.modules.shortcut.dicts :as dicts]
  20. [frontend.modules.shortcut.before :as m]
  21. [frontend.state :as state]
  22. [frontend.util :refer [mac?] :as util]
  23. [frontend.commands :as commands]
  24. [frontend.config :as config]
  25. [electron.ipc :as ipc]
  26. [promesa.core :as p]
  27. [clojure.data :as data]
  28. [medley.core :as medley]))
  29. ;; TODO: Namespace all-default-keyboard-shortcuts keys with `:command` e.g.
  30. ;; `:command.date-picker/complete`. They are namespaced in translation but
  31. ;; almost everywhere else they are not which could cause needless conflicts
  32. ;; with other config keys
  33. ;; To add a new entry to this map, first add it here and then a description for
  34. ;; it in frontend.modules.shortcut.dicts/all-default-keyboard-shortcuts.
  35. ;; A shortcut is a map with the following keys:
  36. ;; * :binding - A string representing a keybinding. Avoid using single letter
  37. ;; shortcuts to allow chords that start with those characters
  38. ;; * :fn - Fn or a qualified keyword that represents a fn
  39. ;; * :inactive - Optional boolean to disable a shortcut for certain conditions
  40. ;; e.g. a given platform or feature condition
  41. (def ^:large-vars/data-var all-default-keyboard-shortcuts
  42. ;; BUG: Actually, "enter" is registered by mixin behind a "when inputing" guard
  43. ;; So this setting item does not cover all cases.
  44. ;; See-also: frontend.components.datetime/time-repeater
  45. {:date-picker/complete {:binding "enter"
  46. :fn ui-handler/shortcut-complete}
  47. :date-picker/prev-day {:binding "left"
  48. :fn ui-handler/shortcut-prev-day}
  49. :date-picker/next-day {:binding "right"
  50. :fn ui-handler/shortcut-next-day}
  51. :date-picker/prev-week {:binding ["up" "ctrl+p"]
  52. :fn ui-handler/shortcut-prev-week}
  53. :date-picker/next-week {:binding ["down" "ctrl+n"]
  54. :fn ui-handler/shortcut-next-week}
  55. :pdf/previous-page {:binding "alt+p"
  56. :fn pdf-utils/prev-page}
  57. :pdf/next-page {:binding "alt+n"
  58. :fn pdf-utils/next-page}
  59. :pdf/close {:binding "alt+x"
  60. :fn #(state/set-state! :pdf/current nil)}
  61. :pdf/find {:binding "alt+f"
  62. :fn pdf-utils/open-finder}
  63. :whiteboard/select {:binding ["1" "s"]
  64. :fn #(.selectTool ^js (state/active-tldraw-app) "select")}
  65. :whiteboard/pan {:binding ["2" "p"]
  66. :fn #(.selectTool ^js (state/active-tldraw-app) "move")}
  67. :whiteboard/portal {:binding "3"
  68. :fn #(.selectTool ^js (state/active-tldraw-app) "logseq-portal")}
  69. :whiteboard/pencil {:binding ["4" "d"]
  70. :fn #(.selectTool ^js (state/active-tldraw-app) "pencil")}
  71. :whiteboard/highlighter {:binding ["5" "h"]
  72. :fn #(.selectTool ^js (state/active-tldraw-app) "highlighter")}
  73. :whiteboard/eraser {:binding ["6" "e"]
  74. :fn #(.selectTool ^js (state/active-tldraw-app) "erase")}
  75. :whiteboard/connector {:binding ["7" "c"]
  76. :fn #(.selectTool ^js (state/active-tldraw-app) "line")}
  77. :whiteboard/text {:binding ["8" "t"]
  78. :fn #(.selectTool ^js (state/active-tldraw-app) "text")}
  79. :whiteboard/rectangle {:binding ["9" "r"]
  80. :fn #(.selectTool ^js (state/active-tldraw-app) "box")}
  81. :whiteboard/ellipse {:binding "o"
  82. :fn #(.selectTool ^js (state/active-tldraw-app) "ellipse")}
  83. :whiteboard/reset-zoom {:binding "shift+0"
  84. :fn #(.resetZoom (.-api ^js (state/active-tldraw-app)))}
  85. :whiteboard/zoom-to-fit {:binding "shift+1"
  86. :fn #(.zoomToFit (.-api ^js (state/active-tldraw-app)))}
  87. :whiteboard/zoom-to-selection {:binding "shift+2"
  88. :fn #(.zoomToSelection (.-api ^js (state/active-tldraw-app)))}
  89. :whiteboard/zoom-out {:binding "shift+dash"
  90. :fn #(.zoomOut (.-api ^js (state/active-tldraw-app)) false)}
  91. :whiteboard/zoom-in {:binding "shift+="
  92. :fn #(.zoomIn (.-api ^js (state/active-tldraw-app)) false)}
  93. :whiteboard/send-backward {:binding "["
  94. :fn #(.sendBackward ^js (state/active-tldraw-app))}
  95. :whiteboard/send-to-back {:binding "shift+["
  96. :fn #(.sendToBack ^js (state/active-tldraw-app))}
  97. :whiteboard/bring-forward {:binding "]"
  98. :fn #(.bringForward ^js (state/active-tldraw-app))}
  99. :whiteboard/bring-to-front {:binding "shift+]"
  100. :fn #(.bringToFront ^js (state/active-tldraw-app))}
  101. :whiteboard/lock {:binding "mod+l"
  102. :fn #(.setLocked ^js (state/active-tldraw-app) true)}
  103. :whiteboard/unlock {:binding "mod+shift+l"
  104. :fn #(.setLocked ^js (state/active-tldraw-app) false)}
  105. :whiteboard/group {:binding "mod+g"
  106. :fn #(.doGroup (.-api ^js (state/active-tldraw-app)))}
  107. :whiteboard/ungroup {:binding "mod+shift+g"
  108. :fn #(.unGroup (.-api ^js (state/active-tldraw-app)))}
  109. :whiteboard/toggle-grid {:binding "shift+g"
  110. :fn #(.toggleGrid (.-api ^js (state/active-tldraw-app)))}
  111. :auto-complete/complete {:binding "enter"
  112. :fn ui-handler/auto-complete-complete}
  113. :auto-complete/prev {:binding ["up" "ctrl+p"]
  114. :fn ui-handler/auto-complete-prev}
  115. :auto-complete/next {:binding ["down" "ctrl+n"]
  116. :fn ui-handler/auto-complete-next}
  117. :auto-complete/shift-complete {:binding "shift+enter"
  118. :fn ui-handler/auto-complete-shift-complete}
  119. :auto-complete/open-link {:binding "mod+o"
  120. :fn ui-handler/auto-complete-open-link}
  121. :cards/toggle-answers {:binding "s"
  122. :fn srs/toggle-answers}
  123. :cards/next-card {:binding "n"
  124. :fn srs/next-card}
  125. :cards/forgotten {:binding "f"
  126. :fn srs/forgotten}
  127. :cards/remembered {:binding "r"
  128. :fn srs/remembered}
  129. :cards/recall {:binding "t"
  130. :fn srs/recall}
  131. :editor/escape-editing {:binding false
  132. :fn (fn [_ _]
  133. (editor-handler/escape-editing))}
  134. :editor/backspace {:binding "backspace"
  135. :fn editor-handler/editor-backspace}
  136. :editor/delete {:binding "delete"
  137. :fn editor-handler/editor-delete}
  138. :editor/new-block {:binding "enter"
  139. :fn editor-handler/keydown-new-block-handler}
  140. :editor/new-line {:binding "shift+enter"
  141. :fn editor-handler/keydown-new-line-handler}
  142. :editor/new-whiteboard {:binding "n w"
  143. :fn #(whiteboard-handler/create-new-whiteboard-and-redirect!)}
  144. :editor/follow-link {:binding "mod+o"
  145. :fn editor-handler/follow-link-under-cursor!}
  146. :editor/open-link-in-sidebar {:binding "mod+shift+o"
  147. :fn editor-handler/open-link-in-sidebar!}
  148. :editor/bold {:binding "mod+b"
  149. :fn editor-handler/bold-format!}
  150. :editor/italics {:binding "mod+i"
  151. :fn editor-handler/italics-format!}
  152. :editor/highlight {:binding "mod+shift+h"
  153. :fn editor-handler/highlight-format!}
  154. :editor/strike-through {:binding "mod+shift+s"
  155. :fn editor-handler/strike-through-format!}
  156. :editor/clear-block {:binding (if mac? "ctrl+l" "alt+l")
  157. :fn editor-handler/clear-block-content!}
  158. :editor/kill-line-before {:binding (if mac? "ctrl+u" "alt+u")
  159. :fn editor-handler/kill-line-before!}
  160. :editor/kill-line-after {:binding (if mac? false "alt+k")
  161. :fn editor-handler/kill-line-after!}
  162. :editor/beginning-of-block {:binding (if mac? false "alt+a")
  163. :fn editor-handler/beginning-of-block}
  164. :editor/end-of-block {:binding (if mac? false "alt+e")
  165. :fn editor-handler/end-of-block}
  166. :editor/forward-word {:binding (if mac? "ctrl+shift+f" "alt+f")
  167. :fn editor-handler/cursor-forward-word}
  168. :editor/backward-word {:binding (if mac? "ctrl+shift+b" "alt+b")
  169. :fn editor-handler/cursor-backward-word}
  170. :editor/forward-kill-word {:binding (if mac? "ctrl+w" "alt+d")
  171. :fn editor-handler/forward-kill-word}
  172. :editor/backward-kill-word {:binding (if mac? false "alt+w")
  173. :fn editor-handler/backward-kill-word}
  174. :editor/replace-block-reference-at-point {:binding "mod+shift+r"
  175. :fn editor-handler/replace-block-reference-with-content-at-point}
  176. :editor/copy-embed {:binding "mod+e"
  177. :fn editor-handler/copy-current-block-embed}
  178. :editor/paste-text-in-one-block-at-point {:binding "mod+shift+v"
  179. :fn paste-handler/editor-on-paste-raw!}
  180. :editor/insert-youtube-timestamp {:binding "mod+shift+y"
  181. :fn commands/insert-youtube-timestamp}
  182. :editor/cycle-todo {:binding "mod+enter"
  183. :fn editor-handler/cycle-todo!}
  184. :editor/up {:binding ["up" "ctrl+p"]
  185. :fn (editor-handler/shortcut-up-down :up)}
  186. :editor/down {:binding ["down" "ctrl+n"]
  187. :fn (editor-handler/shortcut-up-down :down)}
  188. :editor/left {:binding "left"
  189. :fn (editor-handler/shortcut-left-right :left)}
  190. :editor/right {:binding "right"
  191. :fn (editor-handler/shortcut-left-right :right)}
  192. :editor/move-block-up {:binding (if mac? "mod+shift+up" "alt+shift+up")
  193. :fn (editor-handler/move-up-down true)}
  194. :editor/move-block-down {:binding (if mac? "mod+shift+down" "alt+shift+down")
  195. :fn (editor-handler/move-up-down false)}
  196. ;; FIXME: add open edit in non-selection mode
  197. :editor/open-edit {:binding "enter"
  198. :fn (partial editor-handler/open-selected-block! :right)}
  199. :editor/select-block-up {:binding "alt+up"
  200. :fn (editor-handler/on-select-block :up)}
  201. :editor/select-block-down {:binding "alt+down"
  202. :fn (editor-handler/on-select-block :down)}
  203. :editor/select-up {:binding "shift+up"
  204. :fn (editor-handler/shortcut-select-up-down :up)}
  205. :editor/select-down {:binding "shift+down"
  206. :fn (editor-handler/shortcut-select-up-down :down)}
  207. :editor/delete-selection {:binding ["backspace" "delete"]
  208. :fn editor-handler/delete-selection}
  209. :editor/expand-block-children {:binding "mod+down"
  210. :fn editor-handler/expand!}
  211. :editor/collapse-block-children {:binding "mod+up"
  212. :fn editor-handler/collapse!}
  213. :editor/indent {:binding "tab"
  214. :fn (editor-handler/keydown-tab-handler :right)}
  215. :editor/outdent {:binding "shift+tab"
  216. :fn (editor-handler/keydown-tab-handler :left)}
  217. :editor/copy {:binding "mod+c"
  218. :fn editor-handler/shortcut-copy}
  219. :editor/copy-text {:binding "mod+shift+c"
  220. :fn editor-handler/shortcut-copy-text}
  221. :editor/cut {:binding "mod+x"
  222. :fn editor-handler/shortcut-cut}
  223. :editor/undo {:binding "mod+z"
  224. :fn history/undo!}
  225. :editor/redo {:binding ["mod+shift+z" "mod+y"]
  226. :fn history/redo!}
  227. :editor/insert-link {:binding "mod+l"
  228. :fn #(editor-handler/html-link-format!)}
  229. :editor/select-all-blocks {:binding "mod+shift+a"
  230. :fn editor-handler/select-all-blocks!}
  231. :editor/select-parent {:binding "mod+a"
  232. :fn editor-handler/select-parent}
  233. :editor/zoom-in {:binding (if mac? "mod+." "alt+right")
  234. :fn editor-handler/zoom-in!}
  235. :editor/zoom-out {:binding (if mac? "mod+," "alt+left")
  236. :fn editor-handler/zoom-out!}
  237. :editor/toggle-undo-redo-mode {:fn undo-redo/toggle-undo-redo-mode!}
  238. :ui/toggle-brackets {:binding "mod+c mod+b"
  239. :fn config-handler/toggle-ui-show-brackets!}
  240. :go/search-in-page {:binding "mod+shift+k"
  241. :fn #(do
  242. (editor-handler/escape-editing)
  243. (route-handler/go-to-search! :page))}
  244. :go/search {:binding "mod+k"
  245. :fn #(do
  246. (editor-handler/escape-editing false)
  247. (route-handler/go-to-search! :global))}
  248. :go/electron-find-in-page {:binding "mod+f"
  249. :inactive (not (util/electron?))
  250. :fn #(search-handler/open-find-in-page!)}
  251. :go/electron-jump-to-the-next {:binding ["enter" "mod+g"]
  252. :inactive (not (util/electron?))
  253. :fn #(search-handler/loop-find-in-page! false)}
  254. :go/electron-jump-to-the-previous {:binding ["shift+enter" "mod+shift+g"]
  255. :inactive (not (util/electron?))
  256. :fn #(search-handler/loop-find-in-page! true)}
  257. :go/journals {:binding "g j"
  258. :fn route-handler/go-to-journals!}
  259. :go/backward {:binding "mod+open-square-bracket"
  260. :fn (fn [_] (js/window.history.back))}
  261. :go/forward {:binding "mod+close-square-bracket"
  262. :fn (fn [_] (js/window.history.forward))}
  263. :search/re-index {:binding "mod+c mod+s"
  264. :fn (fn [_] (search-handler/rebuild-indices! true))}
  265. :sidebar/open-today-page {:binding (if mac? "mod+shift+j" "alt+shift+j")
  266. :fn page-handler/open-today-in-sidebar}
  267. :sidebar/close-top {:binding "c t"
  268. :fn #(state/sidebar-remove-block! 0)}
  269. :sidebar/clear {:binding "mod+c mod+c"
  270. :fn #(do
  271. (state/clear-sidebar-blocks!)
  272. (state/hide-right-sidebar!))}
  273. :misc/copy {:binding "mod+c"
  274. :fn (fn [] (js/document.execCommand "copy"))}
  275. :command-palette/toggle {:binding "mod+shift+p"
  276. :fn #(do
  277. (editor-handler/escape-editing)
  278. (state/pub-event! [:modal/command-palette]))}
  279. :graph/export-as-html {:fn #(export-handler/download-repo-as-html!
  280. (state/get-current-repo))
  281. :binding false}
  282. :graph/open {:fn #(do
  283. (editor-handler/escape-editing)
  284. (state/set-state! :ui/open-select :graph-open))
  285. :binding "alt+shift+g"}
  286. :graph/remove {:fn #(do
  287. (editor-handler/escape-editing)
  288. (state/set-state! :ui/open-select :graph-remove))
  289. :binding false}
  290. :graph/add {:fn (fn [] (route-handler/redirect! {:to :repo-add}))
  291. :binding false}
  292. :graph/save {:fn #(state/pub-event! [:graph/save])
  293. :binding false}
  294. :graph/re-index {:fn (fn []
  295. (p/let [multiple-windows? (ipc/ipc "graphHasMultipleWindows" (state/get-current-repo))]
  296. (state/pub-event! [:graph/ask-for-re-index (atom multiple-windows?) nil])))
  297. :binding false}
  298. :command/run {:binding "mod+shift+1"
  299. :inactive (not (util/electron?))
  300. :fn #(do
  301. (editor-handler/escape-editing)
  302. (state/pub-event! [:command/run]))}
  303. :go/home {:binding "g h"
  304. :fn #(route-handler/redirect-to-home!)}
  305. :go/all-pages {:binding "g a"
  306. :fn route-handler/redirect-to-all-pages!}
  307. :go/graph-view {:binding "g g"
  308. :fn route-handler/redirect-to-graph-view!}
  309. :go/all-graphs {:binding "g shift+g"
  310. :fn route-handler/redirect-to-all-graphs}
  311. :go/whiteboards {:binding "g w"
  312. :fn route-handler/redirect-to-whiteboard-dashboard!}
  313. :go/keyboard-shortcuts {:binding "g s"
  314. :fn #(route-handler/redirect! {:to :shortcut-setting})}
  315. :go/tomorrow {:binding "g t"
  316. :fn journal-handler/go-to-tomorrow!}
  317. :go/next-journal {:binding "g n"
  318. :fn journal-handler/go-to-next-journal!}
  319. :go/prev-journal {:binding "g p"
  320. :fn journal-handler/go-to-prev-journal!}
  321. :go/flashcards {:binding "g f"
  322. :fn (fn []
  323. (if (state/modal-opened?)
  324. (state/close-modal!)
  325. (state/pub-event! [:modal/show-cards])))}
  326. :ui/toggle-document-mode {:binding "t d"
  327. :fn state/toggle-document-mode!}
  328. :ui/toggle-settings {:binding (if mac? "t s" ["t s" "mod+,"])
  329. :fn ui-handler/toggle-settings-modal!}
  330. :ui/toggle-right-sidebar {:binding "t r"
  331. :fn ui-handler/toggle-right-sidebar!}
  332. :ui/toggle-left-sidebar {:binding "t l"
  333. :fn state/toggle-left-sidebar!}
  334. :ui/toggle-help {:binding "shift+/"
  335. :fn ui-handler/toggle-help!}
  336. :ui/toggle-theme {:binding "t t"
  337. :fn state/toggle-theme!}
  338. :ui/toggle-contents {:binding "alt+shift+c"
  339. :fn ui-handler/toggle-contents!}
  340. :command/toggle-favorite {:binding "mod+shift+f"
  341. :fn page-handler/toggle-favorite!}
  342. :editor/open-file-in-default-app {:binding "mod+d mod+a"
  343. :inactive (not (util/electron?))
  344. :fn page-handler/open-file-in-default-app}
  345. :editor/open-file-in-directory {:binding "mod+d mod+i"
  346. :inactive (not (util/electron?))
  347. :fn page-handler/open-file-in-directory}
  348. :editor/copy-current-file {:binding false
  349. :inactive (not (util/electron?))
  350. :fn page-handler/copy-current-file}
  351. :editor/copy-page-url {:binding false
  352. :inactive (not (util/electron?))
  353. :fn #(page-handler/copy-page-url)}
  354. :ui/toggle-wide-mode {:binding "t w"
  355. :fn ui-handler/toggle-wide-mode!}
  356. :ui/select-theme-color {:binding "t i"
  357. :fn plugin-handler/show-themes-modal!}
  358. :ui/goto-plugins {:binding "t p"
  359. :inactive (not config/lsp-enabled?)
  360. :fn plugin-handler/goto-plugins-dashboard!}
  361. :ui/install-plugins-from-file {:binding false
  362. :inactive (not (config/plugin-config-enabled?))
  363. :fn plugin-config-handler/open-replace-plugins-modal}
  364. :ui/clear-all-notifications {:binding false
  365. :fn :frontend.handler.notification/clear-all!}
  366. :editor/toggle-open-blocks {:binding "t o"
  367. :fn editor-handler/toggle-open!}
  368. :ui/toggle-cards {:binding "t c"
  369. :fn ui-handler/toggle-cards!}
  370. :git/commit {:binding "mod+g c"
  371. :inactive (not (util/electron?))
  372. :fn commit/show-commit-modal!}
  373. :dev/show-block-data {:binding false
  374. :inactive (not (state/developer-mode?))
  375. :fn :frontend.handler.common.developer/show-block-data}
  376. :dev/show-block-ast {:binding false
  377. :inactive (not (state/developer-mode?))
  378. :fn :frontend.handler.common.developer/show-block-ast}
  379. :dev/show-page-data {:binding false
  380. :inactive (not (state/developer-mode?))
  381. :fn :frontend.handler.common.developer/show-page-data}
  382. :dev/show-page-ast {:binding false
  383. :inactive (not (state/developer-mode?))
  384. :fn :frontend.handler.common.developer/show-page-ast}})
  385. (let [keyboard-shortcuts
  386. {::keyboard-shortcuts (set (keys all-default-keyboard-shortcuts))
  387. ::dicts/keyboard-shortcuts (set (keys dicts/all-default-keyboard-shortcuts))}]
  388. (assert (= (::keyboard-shortcuts keyboard-shortcuts) (::dicts/keyboard-shortcuts keyboard-shortcuts))
  389. (str "Keys for keyboard shortcuts must be the same "
  390. (data/diff (::keyboard-shortcuts keyboard-shortcuts) (::dicts/keyboard-shortcuts keyboard-shortcuts)))))
  391. (defn- resolve-fn
  392. "Converts a keyword fn to the actual fn. The fn to be resolved needs to be
  393. marked as ^:export for advanced mode"
  394. [keyword-fn]
  395. (fn []
  396. (if-let [resolved-fn (some-> (find-ns-obj (namespace keyword-fn))
  397. (aget (munge (name keyword-fn))))]
  398. (resolved-fn)
  399. (throw (ex-info (str "Unable to resolve " keyword-fn " to a fn") {})))))
  400. (defn build-category-map [ks]
  401. (->> (select-keys all-default-keyboard-shortcuts ks)
  402. (remove (comp :inactive val))
  403. ;; Convert keyword fns to real fns
  404. (map (fn [[k v]]
  405. [k (if (keyword? (:fn v))
  406. (assoc v :fn (resolve-fn (:fn v)))
  407. v)]))
  408. (into {})))
  409. ;; This is the only var that should be publicly expose :fn functionality
  410. (defonce ^:large-vars/data-var config
  411. (atom
  412. {:shortcut.handler/date-picker
  413. (build-category-map [:date-picker/complete
  414. :date-picker/prev-day
  415. :date-picker/next-day
  416. :date-picker/prev-week
  417. :date-picker/next-week])
  418. :shortcut.handler/pdf
  419. (-> (build-category-map [:pdf/previous-page
  420. :pdf/next-page
  421. :pdf/close
  422. :pdf/find])
  423. (with-meta {:before m/enable-when-not-editing-mode!}))
  424. :shortcut.handler/whiteboard
  425. (-> (build-category-map [:whiteboard/select
  426. :whiteboard/pan
  427. :whiteboard/portal
  428. :whiteboard/pencil
  429. :whiteboard/highlighter
  430. :whiteboard/eraser
  431. :whiteboard/connector
  432. :whiteboard/text
  433. :whiteboard/rectangle
  434. :whiteboard/ellipse
  435. :whiteboard/reset-zoom
  436. :whiteboard/zoom-to-fit
  437. :whiteboard/zoom-to-selection
  438. :whiteboard/zoom-out
  439. :whiteboard/zoom-in
  440. :whiteboard/send-backward
  441. :whiteboard/send-to-back
  442. :whiteboard/bring-forward
  443. :whiteboard/bring-to-front
  444. :whiteboard/lock
  445. :whiteboard/unlock
  446. :whiteboard/group
  447. :whiteboard/ungroup
  448. :whiteboard/toggle-grid])
  449. (with-meta {:before m/enable-when-not-editing-mode!}))
  450. :shortcut.handler/auto-complete
  451. (build-category-map [:auto-complete/complete
  452. :auto-complete/prev
  453. :auto-complete/next
  454. :auto-complete/shift-complete
  455. :auto-complete/open-link])
  456. :shortcut.handler/cards
  457. (-> (build-category-map [:cards/toggle-answers
  458. :cards/next-card
  459. :cards/forgotten
  460. :cards/remembered
  461. :cards/recall])
  462. (with-meta {:before m/enable-when-not-editing-mode!}))
  463. :shortcut.handler/block-editing-only
  464. (->
  465. (build-category-map [:editor/escape-editing
  466. :editor/backspace
  467. :editor/delete
  468. :editor/new-block
  469. :editor/new-line
  470. :editor/follow-link
  471. :editor/open-link-in-sidebar
  472. :editor/bold
  473. :editor/italics
  474. :editor/highlight
  475. :editor/strike-through
  476. :editor/clear-block
  477. :editor/kill-line-before
  478. :editor/kill-line-after
  479. :editor/beginning-of-block
  480. :editor/end-of-block
  481. :editor/forward-word
  482. :editor/backward-word
  483. :editor/forward-kill-word
  484. :editor/backward-kill-word
  485. :editor/replace-block-reference-at-point
  486. :editor/copy-embed
  487. :editor/paste-text-in-one-block-at-point
  488. :editor/insert-youtube-timestamp])
  489. (with-meta {:before m/enable-when-editing-mode!}))
  490. :shortcut.handler/editor-global
  491. (->
  492. (build-category-map [:graph/export-as-html
  493. :graph/open
  494. :graph/remove
  495. :graph/add
  496. :graph/save
  497. :graph/re-index
  498. :editor/cycle-todo
  499. :editor/up
  500. :editor/down
  501. :editor/left
  502. :editor/right
  503. :editor/select-up
  504. :editor/select-down
  505. :editor/move-block-up
  506. :editor/move-block-down
  507. :editor/open-edit
  508. :editor/select-block-up
  509. :editor/select-block-down
  510. :editor/select-parent
  511. :editor/delete-selection
  512. :editor/expand-block-children
  513. :editor/collapse-block-children
  514. :editor/indent
  515. :editor/outdent
  516. :editor/copy
  517. :editor/copy-text
  518. :editor/cut
  519. :command/toggle-favorite])
  520. (with-meta {:before m/enable-when-not-component-editing!}))
  521. :shortcut.handler/global-prevent-default
  522. (->
  523. (build-category-map [:editor/insert-link
  524. :editor/select-all-blocks
  525. :editor/zoom-in
  526. :editor/zoom-out
  527. :editor/toggle-undo-redo-mode
  528. :editor/undo
  529. :editor/redo
  530. :ui/toggle-brackets
  531. :go/search-in-page
  532. :go/search
  533. :go/electron-find-in-page
  534. :go/electron-jump-to-the-next
  535. :go/electron-jump-to-the-previous
  536. :go/backward
  537. :go/forward
  538. :search/re-index
  539. :sidebar/open-today-page
  540. :sidebar/clear
  541. :command/run
  542. :command-palette/toggle])
  543. (with-meta {:before m/prevent-default-behavior}))
  544. :shortcut.handler/misc
  545. ;; always overrides the copy due to "mod+c mod+s"
  546. {:misc/copy (:misc/copy all-default-keyboard-shortcuts)}
  547. :shortcut.handler/global-non-editing-only
  548. (->
  549. (build-category-map [:go/home
  550. :go/journals
  551. :go/all-pages
  552. :go/flashcards
  553. :go/graph-view
  554. :go/all-graphs
  555. :go/whiteboards
  556. :go/keyboard-shortcuts
  557. :go/tomorrow
  558. :go/next-journal
  559. :go/prev-journal
  560. :ui/toggle-document-mode
  561. :ui/toggle-settings
  562. :ui/toggle-right-sidebar
  563. :ui/toggle-left-sidebar
  564. :ui/toggle-help
  565. :ui/toggle-theme
  566. :ui/toggle-contents
  567. :editor/open-file-in-default-app
  568. :editor/open-file-in-directory
  569. :editor/copy-current-file
  570. :editor/copy-page-url
  571. :editor/new-whiteboard
  572. :ui/toggle-wide-mode
  573. :ui/select-theme-color
  574. :ui/goto-plugins
  575. :ui/install-plugins-from-file
  576. :editor/toggle-open-blocks
  577. :ui/toggle-cards
  578. :ui/clear-all-notifications
  579. :git/commit
  580. :sidebar/close-top
  581. :dev/show-block-data
  582. :dev/show-block-ast
  583. :dev/show-page-data
  584. :dev/show-page-ast])
  585. (with-meta {:before m/enable-when-not-editing-mode!}))}))
  586. ;; To add a new entry to this map, first add it here and then
  587. ;; a description for it in frontend.modules.shortcut.dicts/category
  588. (def ^:large-vars/data-var category*
  589. "Full list of categories for docs purpose"
  590. {:shortcut.category/basics
  591. [:editor/new-block
  592. :editor/new-line
  593. :editor/indent
  594. :editor/outdent
  595. :editor/select-all-blocks
  596. :editor/select-parent
  597. :go/search
  598. :go/search-in-page
  599. :go/electron-find-in-page
  600. :go/electron-jump-to-the-next
  601. :go/electron-jump-to-the-previous
  602. :editor/undo
  603. :editor/redo
  604. :editor/copy
  605. :editor/copy-text
  606. :editor/cut]
  607. :shortcut.category/formatting
  608. [:editor/bold
  609. :editor/insert-link
  610. :editor/italics
  611. :editor/strike-through
  612. :editor/highlight]
  613. :shortcut.category/navigating
  614. [:editor/up
  615. :editor/down
  616. :editor/left
  617. :editor/right
  618. :editor/zoom-in
  619. :editor/zoom-out
  620. :editor/collapse-block-children
  621. :editor/expand-block-children
  622. :editor/toggle-open-blocks
  623. :go/backward
  624. :go/forward
  625. :go/home
  626. :go/journals
  627. :go/all-pages
  628. :go/graph-view
  629. :go/all-graphs
  630. :go/whiteboards
  631. :go/flashcards
  632. :go/tomorrow
  633. :go/next-journal
  634. :go/prev-journal
  635. :go/keyboard-shortcuts]
  636. :shortcut.category/block-editing
  637. [:editor/backspace
  638. :editor/delete
  639. :editor/indent
  640. :editor/outdent
  641. :editor/new-block
  642. :editor/new-line
  643. :editor/zoom-in
  644. :editor/zoom-out
  645. :editor/cycle-todo
  646. :editor/follow-link
  647. :editor/open-link-in-sidebar
  648. :editor/move-block-up
  649. :editor/move-block-down
  650. :editor/escape-editing]
  651. :shortcut.category/block-command-editing
  652. [:editor/backspace
  653. :editor/clear-block
  654. :editor/kill-line-before
  655. :editor/kill-line-after
  656. :editor/beginning-of-block
  657. :editor/end-of-block
  658. :editor/forward-word
  659. :editor/backward-word
  660. :editor/forward-kill-word
  661. :editor/backward-kill-word
  662. :editor/replace-block-reference-at-point
  663. :editor/copy-embed
  664. :editor/paste-text-in-one-block-at-point
  665. :editor/select-up
  666. :editor/select-down]
  667. :shortcut.category/block-selection
  668. [:editor/open-edit
  669. :editor/select-all-blocks
  670. :editor/select-parent
  671. :editor/select-block-up
  672. :editor/select-block-down
  673. :editor/delete-selection]
  674. :shortcut.category/toggle
  675. [:ui/toggle-help
  676. :editor/toggle-open-blocks
  677. :editor/toggle-undo-redo-mode
  678. :ui/toggle-wide-mode
  679. :ui/toggle-cards
  680. :ui/toggle-document-mode
  681. :ui/toggle-brackets
  682. :ui/toggle-theme
  683. :ui/toggle-left-sidebar
  684. :ui/toggle-right-sidebar
  685. :ui/toggle-settings
  686. :ui/toggle-contents]
  687. :shortcut.category/whiteboard
  688. [:editor/new-whiteboard
  689. :whiteboard/select
  690. :whiteboard/pan
  691. :whiteboard/portal
  692. :whiteboard/pencil
  693. :whiteboard/highlighter
  694. :whiteboard/eraser
  695. :whiteboard/connector
  696. :whiteboard/text
  697. :whiteboard/rectangle
  698. :whiteboard/ellipse
  699. :whiteboard/reset-zoom
  700. :whiteboard/zoom-to-fit
  701. :whiteboard/zoom-to-selection
  702. :whiteboard/zoom-out
  703. :whiteboard/zoom-in
  704. :whiteboard/send-backward
  705. :whiteboard/send-to-back
  706. :whiteboard/bring-forward
  707. :whiteboard/bring-to-front
  708. :whiteboard/lock
  709. :whiteboard/unlock
  710. :whiteboard/group
  711. :whiteboard/ungroup
  712. :whiteboard/toggle-grid]
  713. :shortcut.category/others
  714. [:pdf/previous-page
  715. :pdf/next-page
  716. :pdf/close
  717. :pdf/find
  718. :command/toggle-favorite
  719. :command/run
  720. :command-palette/toggle
  721. :graph/export-as-html
  722. :graph/open
  723. :graph/remove
  724. :graph/add
  725. :graph/save
  726. :graph/re-index
  727. :sidebar/close-top
  728. :sidebar/clear
  729. :sidebar/open-today-page
  730. :search/re-index
  731. :editor/insert-youtube-timestamp
  732. :editor/open-file-in-default-app
  733. :editor/open-file-in-directory
  734. :editor/copy-page-url
  735. :auto-complete/prev
  736. :auto-complete/next
  737. :auto-complete/complete
  738. :auto-complete/shift-complete
  739. :auto-complete/open-link
  740. :date-picker/prev-day
  741. :date-picker/next-day
  742. :date-picker/prev-week
  743. :date-picker/next-week
  744. :date-picker/complete
  745. :git/commit
  746. :dev/show-block-data
  747. :dev/show-block-ast
  748. :dev/show-page-data
  749. :dev/show-page-ast
  750. :ui/clear-all-notifications]})
  751. (let [category-maps {::category (set (keys category*))
  752. ::dicts/category (set (keys dicts/category))}]
  753. (assert (= (::category category-maps) (::dicts/category category-maps))
  754. (str "Keys for category maps must be the same "
  755. (data/diff (::category category-maps) (::dicts/category category-maps)))))
  756. (def category
  757. "Active list of categories for docs purpose"
  758. (update-vals
  759. category*
  760. (fn [v]
  761. (vec (remove #(:inactive (get all-default-keyboard-shortcuts %)) v)))))
  762. (defn add-shortcut!
  763. [handler-id id shortcut-map]
  764. (swap! config assoc-in [handler-id id] shortcut-map))
  765. (defn remove-shortcut!
  766. [handler-id id]
  767. (swap! config medley/dissoc-in [handler-id id]))