fix: can't switch between modals by shortcuts

pull/7820/head
Tienson Qin 2022-12-21 06:05:18 +08:00
parent b6abd05a97
commit 3a0fa79611
7 changed files with 35 additions and 34 deletions

View File

@ -36,9 +36,6 @@
(rum/defcs command-palette < (rum/defcs command-palette <
(shortcut/disable-all-shortcuts) (shortcut/disable-all-shortcuts)
(rum/local "" ::input) (rum/local "" ::input)
{:will-unmount (fn [state]
(state/set-state! :ui/command-palette-open? false)
state)}
[state {:keys [commands limit] [state {:keys [commands limit]
:or {limit 100}}] :or {limit 100}}]
(let [input (::input state)] (let [input (::input state)]
@ -59,13 +56,3 @@
{:item-render render-command {:item-render render-command
:class "cp__palette-results" :class "cp__palette-results"
:on-chosen (fn [cmd] (cp/invoke-command cmd))})]])) :on-chosen (fn [cmd] (cp/invoke-command cmd))})]]))
(rum/defc command-palette-modal < rum/reactive
[]
(let [open? (state/sub :ui/command-palette-open?)]
(when open?
(state/set-modal!
#(command-palette {:commands (cp/get-commands)})
{:fullscreen? false
:close-btn? false}))
nil))

View File

@ -411,7 +411,7 @@
:theme "monospace"} :theme "monospace"}
[:a.flex.fade-link.items-center [:a.flex.fade-link.items-center
{:style {:margin-left 12} {:style {:margin-left 12}
:on-click #(state/toggle! :ui/command-palette-open?)} :on-click #(state/pub-event! :modal/command-palette)}
(ui/icon "command" {:style {:font-size 20}})])])]] (ui/icon "command" {:style {:font-size 20}})])])]]
(let [recent-search (mapv (fn [q] {:type :search :data q}) (db/get-key-value :recent/search)) (let [recent-search (mapv (fn [q] {:type :search :data q}) (db/get-key-value :recent/search))
pages (->> (db/get-key-value :recent/pages) pages (->> (db/get-key-value :recent/pages)

View File

@ -804,7 +804,6 @@
(ui/notification) (ui/notification)
(ui/modal) (ui/modal)
(ui/sub-modal) (ui/sub-modal)
(command-palette/command-palette-modal)
(select/select-modal) (select/select-modal)
(custom-context-menu) (custom-context-menu)
(plugins/custom-js-installer {:t t (plugins/custom-js-installer {:t t

View File

@ -52,7 +52,6 @@
(defn invoke-command [{:keys [action] :as cmd}] (defn invoke-command [{:keys [action] :as cmd}]
(add-history cmd) (add-history cmd)
(state/set-state! :ui/command-palette-open? false)
(state/close-modal!) (state/close-modal!)
(action)) (action))

View File

@ -15,6 +15,7 @@
[frontend.components.plugins :as plugin] [frontend.components.plugins :as plugin]
[frontend.components.search :as component-search] [frontend.components.search :as component-search]
[frontend.components.shell :as shell] [frontend.components.shell :as shell]
[frontend.components.command-palette :as command-palette]
[frontend.config :as config] [frontend.config :as config]
[frontend.context.i18n :refer [t]] [frontend.context.i18n :refer [t]]
[frontend.db :as db] [frontend.db :as db]
@ -42,6 +43,7 @@
[frontend.handler.user :as user-handler] [frontend.handler.user :as user-handler]
[frontend.handler.shell :as shell-handler] [frontend.handler.shell :as shell-handler]
[frontend.handler.web.nfs :as nfs-handler] [frontend.handler.web.nfs :as nfs-handler]
[frontend.handler.command-palette :as cp]
[frontend.mobile.core :as mobile] [frontend.mobile.core :as mobile]
[frontend.mobile.util :as mobile-util] [frontend.mobile.util :as mobile-util]
[frontend.mobile.graph-picker :as graph-picker] [frontend.mobile.graph-picker :as graph-picker]
@ -708,6 +710,12 @@
opts)) opts))
{:center? true :close-btn? false :close-backdrop? false})) {:center? true :close-btn? false :close-backdrop? false}))
(defmethod handle :modal/command-palette [_]
(state/set-modal!
#(command-palette/command-palette {:commands (cp/get-commands)})
{:fullscreen? false
:close-btn? false}))
(defmethod handle :journal/insert-template [[_ page-name]] (defmethod handle :journal/insert-template [[_ page-name]]
(let [page-name (util/page-name-sanity-lc page-name)] (let [page-name (util/page-name-sanity-lc page-name)]
(when-let [page (db/pull [:block/name page-name])] (when-let [page (db/pull [:block/name page-name])]

View File

@ -257,7 +257,7 @@
:go/search {:binding "mod+k" :go/search {:binding "mod+k"
:fn #(do :fn #(do
(editor-handler/escape-editing) (editor-handler/escape-editing false)
(route-handler/go-to-search! :global))} (route-handler/go-to-search! :global))}
:go/electron-find-in-page {:binding "mod+f" :go/electron-find-in-page {:binding "mod+f"
@ -301,7 +301,7 @@
:command-palette/toggle {:binding "mod+shift+p" :command-palette/toggle {:binding "mod+shift+p"
:fn #(do :fn #(do
(editor-handler/escape-editing) (editor-handler/escape-editing)
(state/toggle! :ui/command-palette-open?))} (state/pub-event! [:modal/command-palette]))}
:graph/export-as-html {:fn #(export-handler/export-repo-as-html! :graph/export-as-html {:fn #(export-handler/export-repo-as-html!
(state/get-current-repo)) (state/get-current-repo))
@ -499,8 +499,7 @@
:shortcut.handler/editor-global :shortcut.handler/editor-global
(-> (->
(build-category-map [:command/run (build-category-map [
:command-palette/toggle
:graph/export-as-html :graph/export-as-html
:graph/open :graph/open
:graph/remove :graph/remove
@ -548,7 +547,9 @@
:go/forward :go/forward
:search/re-index :search/re-index
:sidebar/open-today-page :sidebar/open-today-page
:sidebar/clear]) :sidebar/clear
:command/run
:command-palette/toggle])
(with-meta {:before m/prevent-default-behavior})) (with-meta {:before m/prevent-default-behavior}))
:shortcut.handler/misc :shortcut.handler/misc

View File

@ -94,7 +94,6 @@
:ui/file-component nil :ui/file-component nil
:ui/custom-query-components {} :ui/custom-query-components {}
:ui/show-recent? false :ui/show-recent? false
:ui/command-palette-open? false
:ui/developer-mode? (or (= (storage/get "developer-mode") "true") :ui/developer-mode? (or (= (storage/get "developer-mode") "true")
false) false)
;; remember scroll positions of visited paths ;; remember scroll positions of visited paths
@ -1338,16 +1337,24 @@ Similar to re-frame subscriptions"
{:fullscreen? false {:fullscreen? false
:close-btn? true})) :close-btn? true}))
([modal-panel-content {:keys [id label fullscreen? close-btn? close-backdrop? center?]}] ([modal-panel-content {:keys [id label fullscreen? close-btn? close-backdrop? center?]}]
(when (seq (get-sub-modals)) (let [opened? (modal-opened?)]
(close-sub-modal! true)) (when opened?
(swap! state assoc (close-modal!))
:modal/id id (when (seq (get-sub-modals))
:modal/label (or label (if center? "ls-modal-align-center" "")) (close-sub-modal! true))
:modal/show? (boolean modal-panel-content)
:modal/panel-content modal-panel-content (async/go
:modal/fullscreen? fullscreen? (when opened?
:modal/close-btn? close-btn? (<! (async/timeout 100)))
:modal/close-backdrop? (if (boolean? close-backdrop?) close-backdrop? true)) nil)) (swap! state assoc
:modal/id id
:modal/label (or label (if center? "ls-modal-align-center" ""))
:modal/show? (boolean modal-panel-content)
:modal/panel-content modal-panel-content
:modal/fullscreen? fullscreen?
:modal/close-btn? close-btn?
:modal/close-backdrop? (if (boolean? close-backdrop?) close-backdrop? true))))
nil))
(defn close-modal! (defn close-modal!
[] []