Skip to content

Commit

Permalink
fix: jump to specific block when one of its parents is collapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed May 13, 2021
1 parent ecfe40b commit 01a02b7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 31 deletions.
12 changes: 8 additions & 4 deletions src/main/frontend/components/search.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@

:block
(let [block-uuid (uuid (:block/uuid data))
page (:block/name (:block/page (db/entity [:block/uuid block-uuid])))]
(route/redirect! {:to :page
:path-params {:name page}
:query-params {:anchor (str "ls-block-" (:block/uuid data))}}))
collapsed? (db/parents-collapsed? (state/get-current-repo) block-uuid)]
(if collapsed?
(route/redirect! {:to :page
:path-params {:name (str block-uuid)}})
(let [page (:block/name (:block/page (db/entity [:block/uuid block-uuid])))]
(route/redirect! {:to :page
:path-params {:name page}
:query-params {:anchor (str "ls-block-" (:block/uuid data))}}))))
nil))
:on-shift-chosen (fn [{:keys [type data]}]
(case type
Expand Down
2 changes: 1 addition & 1 deletion src/main/frontend/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
filter-only-public-pages-and-blocks get-all-block-contents get-all-tagged-pages
get-all-templates get-block-and-children get-block-by-uuid get-block-children sort-by-left
get-block-children-ids get-block-file get-block-immediate-children get-block-page
get-block-page-end-pos get-block-parent get-block-parents get-block-referenced-blocks get-block-refs-count
get-block-page-end-pos get-block-parent get-block-parents parents-collapsed? get-block-referenced-blocks get-block-refs-count
get-blocks-by-priority get-blocks-contents get-custom-css
get-date-scheduled-or-deadlines get-db-type get-empty-pages get-file
get-file-blocks get-file-contents get-file-last-modified-at get-file-no-sub get-file-page get-file-page-id file-exists?
Expand Down
47 changes: 21 additions & 26 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,27 @@
(recur (:block/uuid parent) (conj parents parent) (inc d))
parents)))))

(comment
(defn get-immediate-children-v2
[repo block-id]
(d/pull (conn/get-conn repo)
'[:block/_parent]
[:block/uuid block-id])))

;; Use built-in recursive
(defn get-block-parents-v2
[repo block-id]
(d/pull (conn/get-conn repo)
'[:db/id :block/properties {:block/parent ...}]
[:block/uuid block-id]))

(defn parents-collapsed?
[repo block-id]
(when-let [block (:block/parent (get-block-parents-v2 repo block-id))]
(->> (tree-seq map? (fn [x] [(:block/parent x)]) block)
(map (comp :collapsed :block/properties))
(some true?))))

(defn get-block-page
[repo block-id]
(when-let [block (db-utils/entity repo [:block/uuid block-id])]
Expand Down Expand Up @@ -1262,29 +1283,3 @@
(conn/get-conn repo)
page-id)
ffirst))

(comment
(def page-names ["foo" "bar"])

(def page-ids (set (get-page-ids-by-names page-names)))

(d/q '[:find [(pull ?b [*]) ...]
:in $ % ?refs
:where
[?b :block/refs ?p]
;; Filter other blocks
[(contains? ?refs ?p)]
(or-join [?b ?refs]
(matches-all ?b :block/refs ?refs)
(and
(parent ?p ?b)
;; FIXME: not working
;; (matches-all (union ?p ?b) :block/refs ?refs)
[?p :block/refs ?p-ref]
[?b :block/refs ?b-ref]
[(not= ?p-ref ?b-ref)]
[(contains? ?refs ?p-ref)]
[(contains? ?refs ?b-ref)]))]
(conn/get-conn)
rules
page-ids))

0 comments on commit 01a02b7

Please sign in to comment.