Skip to content

Commit

Permalink
Merge pull request #1954 from sid597/forward-back-keybinding
Browse files Browse the repository at this point in the history
enhance: Keyboard shortcut for navigating forward and backward
  • Loading branch information
neotyk authored Jan 13, 2022
2 parents c672f2b + a719311 commit e58c8bb
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 42 deletions.
78 changes: 40 additions & 38 deletions src/cljs/athens/listeners.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,47 @@

(defn key-down!
[e]
(let [{:keys [key-code ctrl meta shift alt]} (util/destruct-key-down e)
editing-uid @(subscribe [:editing/uid])]
(let [{:keys [key-code
ctrl
meta
shift
alt]
:as destruct-keys} (util/destruct-key-down e)
editing-uid @(subscribe [:editing/uid])]
(cond
(util/shortcut-key? meta ctrl) (condp = key-code
KeyCodes.S (dispatch [:save])

KeyCodes.EQUALS (dispatch [:zoom/in])
KeyCodes.DASH (dispatch [:zoom/out])
KeyCodes.ZERO (dispatch [:zoom/reset])

KeyCodes.K (dispatch [:athena/toggle])

KeyCodes.G (dispatch [:devtool/toggle])

KeyCodes.Z (let [editing-uid @(subscribe [:editing/uid])
selected-items @(subscribe [::select-subs/items])]
;; editing/uid must be nil or selected-items must be non-empty
(when (or (nil? editing-uid)
(not-empty selected-items))
(if shift
(dispatch [:redo])
(dispatch [:undo]))))

KeyCodes.BACKSLASH (if shift
(dispatch [:right-sidebar/toggle])
(dispatch [:left-sidebar/toggle]))

KeyCodes.COMMA (router/navigate :settings)

KeyCodes.T (util/toggle-10x)
nil)
alt (condp = key-code
KeyCodes.LEFT (when (nil? editing-uid) (.back js/window.history))
KeyCodes.RIGHT (when (nil? editing-uid) (.forward js/window.history))
KeyCodes.D (router/nav-daily-notes)
KeyCodes.G (router/navigate :graph)
KeyCodes.A (router/navigate :pages)
KeyCodes.T (dispatch [:theme/toggle])
nil))))
(util/shortcut-key? meta ctrl) (condp = key-code
KeyCodes.S (dispatch [:save])
KeyCodes.EQUALS (dispatch [:zoom/in])
KeyCodes.DASH (dispatch [:zoom/out])
KeyCodes.ZERO (dispatch [:zoom/reset])
KeyCodes.K (dispatch [:athena/toggle])
KeyCodes.G (dispatch [:devtool/toggle])
KeyCodes.Z (let [editing-uid @(subscribe [:editing/uid])
selected-items @(subscribe [::select-subs/items])]
;; editing/uid must be nil or selected-items must be non-empty
(when (or (nil? editing-uid)
(not-empty selected-items))
(if shift
(dispatch [:redo])
(dispatch [:undo]))))
KeyCodes.BACKSLASH (if shift
(dispatch [:right-sidebar/toggle])
(dispatch [:left-sidebar/toggle]))
KeyCodes.COMMA (router/navigate :settings)
KeyCodes.T (util/toggle-10x)
nil)
(util/navigate-key? destruct-keys) (condp = key-code
KeyCodes.LEFT (when (nil? editing-uid)
(.back js/window.history))
KeyCodes.RIGHT (when (nil? editing-uid)
(.forward js/window.history))
nil)
alt (condp = key-code
KeyCodes.D (router/nav-daily-notes)
KeyCodes.G (router/navigate :graph)
KeyCodes.A (router/navigate :pages)
KeyCodes.T (dispatch [:theme/toggle])
nil))))


;; -- Clipboard ----------------------------------------------------------
Expand Down
18 changes: 17 additions & 1 deletion src/cljs/athens/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
[goog.dom :refer [getElement setProperties]]
[posh.reagent :refer [#_pull]])
(:require-macros
[com.rpl.specter :refer [recursive-path]]))
[com.rpl.specter :refer [recursive-path]])
(:import
(goog.events
KeyCodes)))


;; Electron ipcMain Channels
Expand Down Expand Up @@ -259,6 +262,19 @@
(and (= os :linux) ctrl))))


(defn navigate-key?
"Used to navigate between current and last page
Use meta for mac, alt for others."
[{:keys [key-code
meta
alt]}]
(let [os (get-os)]
(and (#{KeyCodes.LEFT KeyCodes.RIGHT} key-code)
(or (and (= os :mac) meta)
(and (= os :windows) alt)
(and (= os :linux) alt)))))


;; re-frame-10x

(defn re-frame-10x-open?
Expand Down
21 changes: 18 additions & 3 deletions src/cljs/athens/views/blocks/textarea_keydown.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
[athens.events.selection :as select-events]
[athens.router :as router]
[athens.subs.selection :as select-subs]
[athens.util :refer [scroll-if-needed get-caret-position shortcut-key? escape-str]]
[athens.util :as util :refer [scroll-if-needed get-caret-position shortcut-key? escape-str]]
[athens.views.blocks.internal-representation :as internal-representation]
[clojure.string :refer [replace-first blank? includes? lower-case]]
[goog.dom :refer [getElement]]
Expand Down Expand Up @@ -353,7 +353,8 @@
shift
ctrl
target
selection]} (destruct-key-down e)
selection]
:as destruct-keys} (destruct-key-down e)
selection? (not (blank? selection))
start? (block-start? e)
end? (block-end? e)
Expand All @@ -371,9 +372,23 @@
down? (= key-code KeyCodes.DOWN)
left? (= key-code KeyCodes.LEFT)
right? (= key-code KeyCodes.RIGHT)
[char-offset _] (get-end-points target)]
[char-offset _] (get-end-points target)
os (util/get-os)]

(cond
;; These navigation options are derived from how chrome does keyboard shortcuts.
(util/navigate-key? destruct-keys) (cond
;; For mac only navigate when caret is at start or end of the block with
;; the corresponding arrow keys
(and (= os :mac)
(and start? left?)) (.back js/window.history)
(and (= os :mac)
(and end? right?)) (.forward js/window.history)

;; If the os is linux or windows then we navigate regardless of where the caret is
left? (.back js/window.history)
right? (.forward js/window.history))

;; Shift: select block if leaving block content boundaries (top or bottom rows). Otherwise select textarea text (default)
shift (cond
left? nil
Expand Down

0 comments on commit e58c8bb

Please sign in to comment.