Browse Source

Add property which allows for giving a block a routable name

Also update router fn so that it works for all uses of routing to a
block
Gabriel Horner 3 years ago
parent
commit
b4f26a5c7b

+ 1 - 1
deps/graph-parser/src/logseq/graph_parser/property.cljs

@@ -47,7 +47,7 @@
   "Properties used by logseq that user can edit"
   []
   (into #{:title :icon :template :template-including-parent :public :filters :exclude-from-graph-view
-          :logseq.query/nlp-date
+          :logseq.query/nlp-date :logseq.block/route-name
           ;; org-mode only
           :macro :filetags}
         editable-linkable-built-in-properties))

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

@@ -2026,7 +2026,7 @@
 
 (def hidden-editable-block-properties
   "Properties that are hidden in a block (block property)"
-  #{:logseq.query/nlp-date})
+  #{:logseq.query/nlp-date :logseq.block/route-name})
 
 (assert (set/subset? hidden-editable-block-properties (gp-property/editable-built-in-properties))
         "Hidden editable page properties must be valid editable properties")

+ 26 - 8
src/main/frontend/components/page.cljs

@@ -43,6 +43,22 @@
   (let [route-match (first (:rum/args state))]
     (get-in route-match [:parameters :path :name])))
 
+;; Named block links only works on web (and publishing)
+(if util/web-platform?
+  (defn- get-block-uuid-by-block-route-name
+    "Return string block uuid for matching :name and :block params or nil if not found"
+    [state]
+    ;; Only query if block name is in the route
+    (when-let [route-name (get-in (first (:rum/args state))
+                                  [:parameters :path :block-route-name])]
+      (->> (model/get-block-by-page-name-and-route-name
+            (state/get-current-repo)
+            (get-page-name state)
+            route-name)
+           :block/uuid
+           str)))
+  (def get-block-uuid-by-block-route-name (constantly nil)))
+
 (defn- get-blocks
   [repo page-name block-id]
   (when page-name
@@ -357,6 +373,8 @@
   (rum/local nil   ::current-page)
   [state {:keys [repo page-name] :as option}]
   (when-let [path-page-name (or page-name
+                                (get-block-uuid-by-block-route-name state)
+                                ;; is page name or uuid
                                 (get-page-name state)
                                 (state/get-current-page))]
     (let [current-repo (state/sub :git/current-repo)
@@ -430,14 +448,14 @@
                [:div.mb-4
                 (component-block/breadcrumb config repo block-id {:level-limit 3})]))
 
-         ;; blocks
-         (let [page (if block?
-                      (db/entity repo [:block/uuid block-id])
-                      page)
-               _ (and block? page (reset! *current-block-page (:block/name (:block/page page))))
-               _ (when (and block? (not page))
-                   (route-handler/redirect-to-page! @*current-block-page))]
-           (page-blocks-cp repo page {:sidebar? sidebar? :whiteboard? whiteboard?}))]])
+           ;; blocks
+           (let [page (if block?
+                        (db/entity repo [:block/uuid block-id])
+                        page)
+                 _ (and block? page (reset! *current-block-page (:block/name (:block/page page))))
+                 _ (when (and block? (not page))
+                     (route-handler/redirect-to-page! @*current-block-page))]
+             (page-blocks-cp repo page {:sidebar? sidebar? :whiteboard? whiteboard?}))]])
 
        (when today?
          (today-queries repo today? sidebar?))

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

@@ -267,6 +267,21 @@
   [id]
   (db-utils/pull [:block/uuid (if (uuid? id) id (uuid id))]))
 
+(defn get-block-by-page-name-and-route-name
+  "Returns first block for given page name and route-name property"
+  [repo page-name route-name]
+  (->> (d/q '[:find (pull ?b [:block/uuid])
+              :in $ ?page-name ?route-name
+              :where
+              [?page :block/name ?page-name]
+              [?b :block/page ?page]
+              [?b :block/properties ?prop]
+              [(get ?prop :logseq.block/route-name) ?route-name]]
+            (conn/get-db repo)
+            page-name
+            route-name)
+       ffirst))
+
 (defn get-page-format
   [page-name]
   (or

+ 23 - 6
src/main/frontend/handler/route.cljs

@@ -3,6 +3,7 @@
             [frontend.config :as config]
             [frontend.date :as date]
             [frontend.db :as db]
+            [frontend.db.model :as model]
             [frontend.handler.recent :as recent-handler]
             [frontend.handler.search :as search-handler]
             [frontend.handler.ui :as ui-handler]
@@ -43,6 +44,23 @@
   []
   (redirect! {:to :whiteboards}))
 
+;; Named block links only works on web (and publishing)
+(if util/web-platform?
+  (defn- default-page-route [page-name-or-block-uuid]
+    ;; Only query if in a block context
+    (let [block (when (uuid? page-name-or-block-uuid)
+                  (model/get-block-by-uuid page-name-or-block-uuid))]
+      (if-let [route-name (get-in block [:block/properties :logseq.block/route-name])]
+        {:to :page-block
+         :path-params {:name (get-in block [:block/page :block/name])
+                       :block-route-name route-name}}
+        {:to :page
+         :path-params {:name (str page-name-or-block-uuid)}})))
+
+  (defn- default-page-route [page-name]
+    {:to :page
+     :path-params {:name (str page-name)}}))
+
 (defn redirect-to-page!
   "Must ensure `page-name` is dereferenced (not an alias), or it will create a wrong new page with that name (#3511)."
   ([page-name]
@@ -52,12 +70,11 @@
    (recent-handler/add-page-to-recent! (state/get-current-repo) page-name
                                        click-from-recent?)
    (let [m (cond->
-            {:to :page
-             :path-params {:name (str page-name)}}
-             anchor
-             (assoc :query-params {:anchor anchor})
-             push
-             (assoc :push push))]
+            (default-page-route page-name)
+            anchor
+            (assoc :query-params {:anchor anchor})
+            push
+            (assoc :push push))]
      (redirect! m))))
 
 (defn redirect-to-whiteboard!

+ 4 - 0
src/main/frontend/routes.cljs

@@ -51,6 +51,10 @@
     {:name :page
      :view page/page}]
 
+   ["/page/:name/block/:block-route-name"
+    {:name :page-block
+     :view page/page}]
+
    ["/all-pages"
     {:name :all-pages
      :view page/all-pages}]