Browse Source

enhance: property editing support shortcut

Tienson Qin 1 year ago
parent
commit
6ced3bff8f

+ 6 - 0
deps/db/src/logseq/db.cljs

@@ -551,3 +551,9 @@
         (d/pull-many db
                      '[:db/id :block/name :block/original-name]
                      ids)))))
+
+(defn get-all-properties
+  [db]
+  (->> (d/datoms db :avet :block/type "property")
+       (map (fn [d]
+              (d/entity db (:e d))))))

+ 12 - 4
src/main/frontend/components/container.cljs

@@ -34,6 +34,7 @@
             [frontend.handler.user :as user-handler]
             [frontend.handler.whiteboard :as whiteboard-handler]
             [frontend.handler.recent :as recent-handler]
+            [frontend.handler.db-based.property :as db-property-handler]
             [frontend.mixins :as mixins]
             [frontend.mobile.action-bar :as action-bar]
             [frontend.mobile.footer :as footer]
@@ -875,7 +876,7 @@
   []
   nil)
 
-(rum/defcs ^:large-vars/cleanup-todo sidebar <
+(rum/defcs ^:large-vars/cleanup-todo root-container <
   (mixins/modal :modal/show?)
   rum/reactive
   (mixins/event-mixin
@@ -883,7 +884,8 @@
      (mixins/listen state js/window "pointerdown" hide-context-menu-and-clear-selection)
      (mixins/listen state js/window "keydown"
                     (fn [e]
-                      (when (= 27 (.-keyCode e))
+                      (cond
+                        (= 27 (.-keyCode e))
                         (if (and (state/modal-opened?)
                                  (not
                                   (and
@@ -891,7 +893,13 @@
                                    util/node-test?
                                    (state/editing?))))
                           (state/close-modal!)
-                          (hide-context-menu-and-clear-selection e)))))))
+                          (hide-context-menu-and-clear-selection e))
+
+                        (and (seq (state/get-selection-block-ids))
+                             (not (or (.-ctrlKey e) (.-metaKey e) (.-altKey e))))
+                        (let [shift? (.-shiftKey e)
+                              shortcut (if shift? (str "shift+" (.-key e)) (.-key e))]
+                          (db-property-handler/set-property-by-shortcut! shortcut)))))))
   [state route-match main-content]
   (let [{:keys [open-fn]} state
         current-repo (state/sub :git/current-repo)
@@ -958,7 +966,7 @@
                        (ui/focus-element (ui/main-node))))}
        (t :accessibility/skip-to-main-content)]
       [:div.#app-container (cond-> {} selection-mode?
-                             (assoc :class "blocks-selection-mode"))
+                                   (assoc :class "blocks-selection-mode"))
        [:div#left-container
         {:class (if (state/sub :ui/sidebar-open?) "overflow-hidden" "w-full")}
         (header/header {:open-fn        open-fn

+ 7 - 0
src/main/frontend/db/model.cljs

@@ -768,6 +768,13 @@ independent of format as format specific heading characters are stripped"
                    {:id (str uuid)
                     :nonce (:nonce shape)}))))))
 
+(defn get-all-properties
+  [repo]
+  (when-let [db (conn/get-db repo)]
+    (->> (d/datoms db :avet :block/type "property")
+         (map (fn [d]
+                (d/entity db (:e d)))))))
+
 (defn get-all-classes
   [repo]
   (d/q

+ 9 - 0
src/main/frontend/handler/db_based/property.cljs

@@ -97,3 +97,12 @@
   (ui-outliner-tx/transact!
    {:outliner-op :add-existing-values-to-closed-values}
     (outliner-op/add-existing-values-to-closed-values! property-id values)))
+
+(defn set-property-by-shortcut!
+  [shortcut]
+  ;; TODO: support setting multiple blocks property
+  (let [properties (ldb/get-all-properties (db/get-db))
+        ;; TODO: what if multiple properties have the same shortcut?
+        property (some (fn [p] (when (= shortcut (get-in p [:block/schema :shortcut])) p)) properties)]
+    (when property
+      (state/pub-event! [:editor/new-property {:property-key (:block/original-name property)}]))))

+ 1 - 1
src/main/frontend/page.cljs

@@ -104,7 +104,7 @@
           [:<>
            (if (= :draw route-name)
              (view route-match)
-             (container/sidebar
+             (container/root-container
                route-match
                (view route-match)))
            (when config/lsp-enabled?