refactor(shortcuts): use mousetrap to manage all the shortcuts

This commit is contained in:
Tienson Qin
2021-02-15 21:42:08 +08:00
parent 7c586ec366
commit e7bd754e92
7 changed files with 52 additions and 123 deletions

View File

@@ -718,6 +718,7 @@
(when config/mobile? (mobile-bar state id))
(ui/ls-textarea
{:id id
:class "mousetrap"
:cacheMeasurements true
:default-value (or content "")
:minRows (if (state/enable-grammarly?) 2 1)

View File

@@ -111,10 +111,7 @@
(when intro? (onboarding/intro))]))
(rum/defc journals <
{:did-mount (fn [state]
(editor-handler/open-last-block! true)
state)}
(rum/defc journals
[latest-journals]
[:div#journals
(ui/infinite-list

View File

@@ -224,15 +224,10 @@
[:a {:href (rfe/href :page {:name name})}
original-name]])] false)]])))
(defonce last-route (atom :home))
;; A page is just a logical block
(rum/defcs page < rum/reactive
{:did-mount (fn [state]
(ui-handler/scroll-and-highlight! state)
;; only when route changed
(when (not= @last-route (state/get-current-route))
(editor-handler/open-last-block! false))
(reset! last-route (state/get-current-route))
state)
:did-update (fn [state]
(ui-handler/scroll-and-highlight! state)

View File

@@ -290,7 +290,6 @@
{:did-mount (fn [state]
(keyboards/bind-shortcuts!)
state)}
(mixins/keyboards-mixin keyboards/keyboards)
[state route-match main-content]
(let [{:keys [open? close-fn open-fn]} state
close-fn (fn []

View File

@@ -110,22 +110,23 @@
(defn toggle-between-page-and-file!
[state e]
(let [current-route (state/get-current-route)]
(case current-route
:home
(redirect-to-file! (date/today))
(when-not (state/editing?)
(let [current-route (state/get-current-route)]
(case current-route
:home
(redirect-to-file! (date/today))
:all-journals
(redirect-to-file! (date/today))
:all-journals
(redirect-to-file! (date/today))
:page
(when-let [page-name (get-in (state/get-route-match) [:path-params :name])]
(redirect-to-file! page-name))
:page
(when-let [page-name (get-in (state/get-route-match) [:path-params :name])]
(redirect-to-file! page-name))
:file
(when-let [path (get-in (state/get-route-match) [:path-params :path])]
(when-let [page (db/get-file-page path)]
(redirect! {:to :page
:path-params {:name page}})))
:file
(when-let [path (get-in (state/get-route-match) [:path-params :path])]
(when-let [page (db/get-file-page path)]
(redirect! {:to :page
:path-params {:name page}})))
nil)))
nil))))

View File

@@ -12,73 +12,9 @@
["mousetrap" :as mousetrap]
[goog.object :as gobj]))
;; KeyCodes.QUESTION_MARK
;; Credits to roamresearch
;; Triggers
;; Slash Autocomplete /
;; Block Insert Autocomplete <
;; Page reference Autocomplete [[]]
;; Block Reference Autocomplete (())
;; Key Commands (working with lists)
;; Indent Block Tab
;; Unindent Block Shift-Tab
;; Create New Block Enter
;; New Line in Block Shift-Enter
;; Undo Ctrl-z
;; Redo Ctrl-y
;; Zoom In Alt-Right
;; Zoom out Alt-left
;; Follow link under cursor Ctrl-o
;; Open link in Sidebar Ctrl-shift-o
;; Select Block Above Shift-Up
;; Select Block Below Shift-Down
;; Select All Blocks Ctrl-Shift-a
;; General
;; Full Text Search
;; Open Link in Sidebar
;; Context Menu
;; Jump to Journals
;; Formatting
;; Bold Ctrl-b
;; Italics Ctrl-i
;; Html Link Ctrl-k
;; Highlight Ctrl-h
;; Markdown
;; Block
(def keyboards
(->>
{"tab" (editor-handler/on-tab :right)
"shift+tab" (editor-handler/on-tab :left)
"ctrl+z" history-handler/undo!
"ctrl+y" history-handler/redo!
"ctrl+u" route-handler/go-to-search!
"alt+j" route-handler/go-to-journals!
(or (state/get-shortcut :editor/zoom-in)
(if util/mac? "alt+." "alt+right")) editor-handler/zoom-in!
(or (state/get-shortcut :editor/zoom-out)
(if util/mac? "alt+," "alt+left")) editor-handler/zoom-out!
"ctrl+enter" editor-handler/cycle-todo!
"ctrl+down" editor-handler/expand!
"ctrl+up" editor-handler/collapse!
"ctrl+o" editor-handler/follow-link-under-cursor!
"ctrl+shift+o" editor-handler/open-link-in-sidebar!
"ctrl+b" editor-handler/bold-format!
"ctrl+i" editor-handler/italics-format!
"ctrl+k" editor-handler/html-link-format!
"ctrl+h" editor-handler/highlight-format!
"ctrl+shift+a" editor-handler/select-all-blocks!
"alt+shift+up" (fn [state e] (editor-handler/move-up-down e true))
"alt+shift+down" (fn [state e] (editor-handler/move-up-down e false))}
(medley/map-keys util/->system-modifier)))
(defn chord-aux
(defn prevent-default-behavior
[f]
(fn [state e]
(f state e)
@@ -86,20 +22,47 @@
;; and stop event from bubbling
false))
;; TODO: make the shortcuts configurable
(defonce chords
{;; Toggle
{
"t d" state/toggle-document-mode!
"t t" state/toggle-theme!
"t r" ui-handler/toggle-right-sidebar!
"t e" state/toggle-new-block-shortcut!
"s" (chord-aux route-handler/toggle-between-page-and-file!)
"mod+s" (chord-aux editor-handler/save!)
"mod+c mod+s" (chord-aux search-handler/rebuild-indices!)
"mod+c mod+b" (chord-aux config-handler/toggle-ui-show-brackets!)})
"s" route-handler/toggle-between-page-and-file!
"tab" (editor-handler/on-tab :right)
"shift+tab" (editor-handler/on-tab :left)
"mod+z" [history-handler/undo! true]
"mod+y" [history-handler/redo! true]
"mod+u" [route-handler/go-to-search! true]
"alt+j" [route-handler/go-to-journals! true]
(or (state/get-shortcut :editor/zoom-in)
(if util/mac? "alt+." "alt+right")) [editor-handler/zoom-in! true]
(or (state/get-shortcut :editor/zoom-out)
(if util/mac? "alt+," "alt+left")) [editor-handler/zoom-out! true]
"mod+enter" [editor-handler/cycle-todo! true]
"mod+down" [editor-handler/expand! true]
"mod+up" [editor-handler/collapse! true]
"mod+shift+o" [editor-handler/open-link-in-sidebar! true]
"mod+b" [editor-handler/bold-format! true]
"mod+i" [editor-handler/italics-format! true]
"mod+k" [editor-handler/html-link-format! true]
"mod+h" [editor-handler/highlight-format! true]
"mod+shift+a" [editor-handler/select-all-blocks! true]
"alt+shift+up" [(fn [state e] (editor-handler/move-up-down e true)) true]
"alt+shift+down" [(fn [state e] (editor-handler/move-up-down e false)) true]
"mod+s" [editor-handler/save! true]
"mod+c mod+s" [search-handler/rebuild-indices! true]
"mod+c mod+b" [config-handler/toggle-ui-show-brackets! true]
"ctrl+o" [editor-handler/follow-link-under-cursor! true]})
(defonce bind! (gobj/get mousetrap "bind"))
(defn bind-shortcuts!
[]
(doseq [[k f] chords]
(bind! k f)))
(let [[f prevent?] (if (coll? f)
f
[f false])
f (if prevent? (prevent-default-behavior f) f)]
(bind! k f))))

View File

@@ -187,33 +187,6 @@
(f))
(dissoc state k)))})))
(defn keyboards-mixin
([m] (keyboards-mixin m (fn [_] true) js/window))
([m enable-f] (keyboards-mixin m enable-f js/window))
([m enable-f target]
(when (seq m)
(let [target-fn (if (fn? target) target (fn [_] target))]
{:did-mount
(fn [state]
(if (enable-f state)
(let [keyboards (doall
(map
(fn [[key f]]
[key
(keyboard/install-shortcut! key
(fn [e] (f state e))
false
(target-fn state))])
m))]
(assoc state ::keyboards-listener keyboards))
state))
:will-unmount
(fn [state]
(when (enable-f state)
(doseq [[_k f] (get state ::keyboards-listener)]
(f)))
state)}))))
;; also, from https://github.com/tonsky/rum/blob/75174b9ea0cf4b7a761d9293929bd40c95d35f74/doc/useful-mixins.md