Răsfoiți Sursa

fix: can't switch between modals by shortcuts

Tienson Qin 3 ani în urmă
părinte
comite
3a0fa79611

+ 0 - 13
src/main/frontend/components/command_palette.cljs

@@ -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))

+ 1 - 1
src/main/frontend/components/search.cljs

@@ -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)

+ 2 - 3
src/main/frontend/components/sidebar.cljs

@@ -366,7 +366,7 @@
                                      (route-handler/sidebar-journals!)
                                      (route-handler/sidebar-journals!)
                                      (route-handler/go-to-journals!)))
                                      (route-handler/go-to-journals!)))
                :icon             "calendar"})))
                :icon             "calendar"})))
-         
+
          (when enable-whiteboards?
          (when enable-whiteboards?
            (sidebar-item
            (sidebar-item
             {:class           "whiteboard"
             {:class           "whiteboard"
@@ -375,7 +375,7 @@
              :active          (and (not srs-open?) (#{:whiteboard :whiteboards} route-name))
              :active          (and (not srs-open?) (#{:whiteboard :whiteboards} route-name))
              :icon            "whiteboard"
              :icon            "whiteboard"
              :icon-extension? true}))
              :icon-extension? true}))
-         
+
          (when (state/enable-flashcards? (state/get-current-repo))
          (when (state/enable-flashcards? (state/get-current-repo))
            [:div.flashcards-nav
            [:div.flashcards-nav
             (flashcards srs-open?)])
             (flashcards srs-open?)])
@@ -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

+ 0 - 1
src/main/frontend/handler/command_palette.cljs

@@ -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))
 
 

+ 8 - 0
src/main/frontend/handler/events.cljs

@@ -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])]

+ 6 - 5
src/main/frontend/modules/shortcut/config.cljs

@@ -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
-                          :command-palette/toggle
+     (build-category-map [
                           :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

+ 18 - 11
src/main/frontend/state.cljs

@@ -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))
-     (close-sub-modal! true))
-   (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))
+   (let [opened? (modal-opened?)]
+     (when opened?
+       (close-modal!))
+     (when (seq (get-sub-modals))
+       (close-sub-modal! true))
+
+     (async/go
+       (when opened?
+         (<! (async/timeout 100)))
+       (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!
   []
   []