Skip to content

Commit

Permalink
Add property which allows for giving a block a routable name
Browse files Browse the repository at this point in the history
Also update router fn so that it works for all uses of routing to a
block
  • Loading branch information
logseq-cldwalker committed Dec 15, 2022
1 parent ab7d10a commit b4f26a5
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
2 changes: 1 addition & 1 deletion deps/graph-parser/src/logseq/graph_parser/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/components/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 26 additions & 8 deletions src/main/frontend/components/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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?))
Expand Down
15 changes: 15 additions & 0 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 23 additions & 6 deletions src/main/frontend/handler/route.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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!
Expand Down
4 changes: 4 additions & 0 deletions src/main/frontend/routes.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down

0 comments on commit b4f26a5

Please sign in to comment.