Skip to content

Commit

Permalink
refactor: merge :block/ref-pages and :block/ref-blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tiensonqin committed Mar 21, 2021
1 parent 72f5a7c commit 89aff1a
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 99 deletions.
2 changes: 1 addition & 1 deletion src/main/frontend/components/page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
(db/transact! repo [{:block/name page-name
:block/original-name path-page-name}])
(db/entity repo [:block/name page-name])))
properties (:block/properties page)
{:keys [title] :as properties} (:block/properties page)
page-name (:block/name page)
page-original-name (:block/original-name page)
file (:block/file page)
Expand Down
35 changes: 1 addition & 34 deletions src/main/frontend/db/debug.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,4 @@
:git/cloned? (cloned? repo)
:git/status (get-key-value repo :git/status)
:git/error (get-key-value repo :git/error)})
repos)))

;; filtered blocks

(def page-and-aliases #{22})
(def excluded-pages #{59})
(def include-pages #{106})
(def page-linked-blocks
(->
(d/q
'[:find (pull ?b [:block/uuid
:block/title
{:block/children ...}])
:in $ ?pages
:where
[?b :block/refs ?ref-page]
[(contains? ?pages ?ref-page)]]
(get-conn)
page-and-aliases)
flatten))

(def page-linked-blocks-include-filter
(if (seq include-pages)
(filter (fn [{:block/keys [ref-pages]}]
(some include-pages (map :db/id ref-pages)))
page-linked-blocks)
page-linked-blocks))

(def page-linked-blocks-exclude-filter
(if (seq excluded-pages)
(remove (fn [{:block/keys [ref-pages]}]
(some excluded-pages (map :db/id ref-pages)))
page-linked-blocks-include-filter)
page-linked-blocks-include-filter)))
repos))))
4 changes: 2 additions & 2 deletions src/main/frontend/db/model.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -964,15 +964,15 @@
[(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
[?block :block/refs ?ref-page]
[(contains? ?pages ?ref-page)]]]]
(react/q repo [:page/refed-blocks page-id] {}
(react/q repo [:block/refed-blocks page-id] {}
'[:find (pull ?block [*])
:in $ % ?pages ?aliases
:where
(find-blocks ?block ?ref-page ?pages ?alias ?aliases)]
rules
pages
aliases))
(react/q repo [:page/refed-blocks page-id] {}
(react/q repo [:block/refed-blocks page-id] {}
'[:find (pull ?block [*])
:in $ ?pages
:where
Expand Down
10 changes: 5 additions & 5 deletions src/main/frontend/db/react.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,16 @@

(when current-page-id
[[:page/ref-pages current-page-id]
[:page/refed-blocks current-page-id]
[:block/refed-blocks current-page-id]
[:page/mentioned-pages current-page-id]])

;; refed-pages
(apply concat
(for [{:block/keys [ref-pages]} blocks]
(for [{:block/keys [refs]} blocks]
(map (fn [page]
(when-let [page (db-utils/entity [:block/name (:block/name page)])]
[:page/refed-blocks (:db/id page)]))
ref-pages)))
[:block/refed-blocks (:db/id page)]))
refs)))

;; refed-blocks
(apply concat
Expand All @@ -253,7 +253,7 @@
(distinct))
refed-pages (map
(fn [[k page-id]]
(if (= k :page/refed-blocks)
(if (= k :block/refed-blocks)
[:page/ref-pages page-id]))
handler-keys)
custom-queries (some->>
Expand Down
71 changes: 37 additions & 34 deletions src/main/frontend/format/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,23 @@
(assoc :repeated? true))))))]
(apply merge m)))

(defn- page-name->map
[original-page-name]
(when original-page-name
(let [page-name (string/lower-case original-page-name)
id-m (if-let [block (db/entity [:block/name page-name])]
{}
{:block/uuid (db/new-block-id)})
m (merge
{:block/name page-name
:block/original-name original-page-name}
id-m)]
(if-let [d (date/journal-title->int page-name)]
(merge m
{:block/journal? true
:block/journal-day d})
m))))

(defn with-page-refs
[{:keys [title body tags ref-pages] :as block}]
(let [ref-pages (->> (concat tags ref-pages)
Expand All @@ -241,8 +258,10 @@
(butlast (string/split p #"/"))))
ref-pages)
(remove string/blank?))
ref-pages (distinct (concat ref-pages children-pages))]
(assoc block :ref-pages ref-pages))))
ref-pages (->> (distinct (concat ref-pages children-pages))
(remove nil?))
ref-pages (map page-name->map ref-pages)]
(assoc block :refs ref-pages))))

(defn with-block-refs
[{:keys [title body] :as block}]
Expand All @@ -253,11 +272,13 @@
(swap! ref-blocks conj block))
form)
(concat title body))
(let [ref-blocks (remove string/blank? @ref-blocks)]
(assoc block :ref-blocks (map
(fn [id]
[:block/uuid (medley/uuid id)])
ref-blocks)))))
(let [ref-blocks (remove string/blank? @ref-blocks)
ref-blocks (map
(fn [id]
[:block/uuid (medley/uuid id)])
ref-blocks)
refs (distinct (concat (:refs block) ref-blocks))]
(assoc block :refs refs))))

(defn update-src-pos-meta!
[{:keys [body] :as block}]
Expand Down Expand Up @@ -432,22 +453,7 @@
blocks)]
(with-path-refs blocks))))

(defn- page-name->map
[original-page-name]
(when original-page-name
(let [page-name (string/lower-case original-page-name)
id-m (if-let [block (db/entity [:block/name page-name])]
{}
{:block/uuid (db/new-block-id)})
m (merge
{:block/name page-name
:block/original-name original-page-name}
id-m)]
(if-let [d (date/journal-title->int page-name)]
(merge m
{:block/journal? true
:block/journal-day d})
m))))


(defn parse-block
([block format]
Expand All @@ -465,12 +471,9 @@
(map :db/id))
blocks (doall
(map-indexed
(fn [idx {:block/keys [ref-pages ref-blocks meta] :as block}]
(let [path-ref-pages (->> ref-pages
(remove string/blank?)
(map string/lower-case)
(map (fn [p] [:block/name p]))
(concat parent-ref-pages))
(fn [idx {:block/keys [refs meta] :as block}]
(let [ref-pages (filter :block/name refs)
path-ref-pages (concat ref-pages parent-ref-pages)
block (merge
block
{:block/meta meta
Expand All @@ -486,14 +489,14 @@
;; Preserve the original block id
(when (zero? idx)
{:block/uuid uuid})
(when (seq ref-pages)
(when (seq refs)
{:block/refs
(mapv
(fn [page]
(let [page (page-name->map page)]
(swap! ref-pages-atom conj page)
page))
ref-pages)}))]
(when (:block/name page)
(swap! ref-pages-atom conj page))
page)
refs)}))]
(-> block
(assoc-in [:block/meta :start-pos] (+ (:start-pos meta) start-pos))
(assoc-in [:block/meta :end-pos] (+ (:end-pos meta) start-pos)))))
Expand Down
20 changes: 2 additions & 18 deletions src/main/frontend/handler/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,8 @@
:end-pos new-end-pos})
(block/parse-block block format))
block-retracted-attrs (when-not pre-block?
;; TODO: should we retract the whole block instead?
(when-let [id (:db/id block)]
[[:db/retract id :block/properties]
[:db/retract id :block/priority]
[:db/retract id :block/deadline]
[:db/retract id :block/deadline-ast]
[:db/retract id :block/scheduled]
[:db/retract id :block/scheduled-ast]
[:db/retract id :block/marker]
[:db/retract id :block/repeated?]]))
[[:db/retractEntity id]]))
[after-blocks block-children-content new-end-pos] (rebuild-after-blocks-indent-outdent repo file block (:end-pos (:block/meta block)) end-pos indent-left?)
retract-refs (compute-retract-refs (:db/id e) (first blocks) ref-pages ref-blocks)
page-id (:db/id page)
Expand Down Expand Up @@ -681,16 +673,8 @@
after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
files [[file-path new-content]]
block-retracted-attrs (when-not pre-block?
;; TODO: should we retract the whole block instead?
(when-let [id (:db/id block)]
[[:db/retract id :block/properties]
[:db/retract id :block/priority]
[:db/retract id :block/deadline]
[:db/retract id :block/deadline-ast]
[:db/retract id :block/scheduled]
[:db/retract id :block/scheduled-ast]
[:db/retract id :block/marker]
[:db/retract id :block/repeated?]]))
[[:db/retractEntity id]]))
transact-fn (fn []
(repo-handler/transact-react-and-alter-file!
repo
Expand Down
4 changes: 2 additions & 2 deletions src/main/frontend/handler/export.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@
[page-or-block]
(db/get-page-blocks
repo page-or-block {:use-cache? false
:pull-keys '[:block/ref-pages :block/ref-blocks]}))
(filterv #(or (:block/ref-blocks %) (:block/ref-pages %)))
:pull-keys '[:block/refs]}))
(filterv :block/refs)
(mapv (fn [b] [(:block/ref-blocks b), (:block/ref-pages b)]))
(apply mapv vector)
(mapv #(vec (distinct (flatten (remove nil? %))))))
Expand Down
3 changes: 1 addition & 2 deletions src/main/frontend/handler/extract.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@
:block/file [:file/path file]
:block/format format
:block/page [:block/name (string/lower-case page)]
:block/refs (mapv
(fn [page]
:block/refs (mapv (fn [page]
(block/page-name->map page))
block-ref-pages)
:block/path-refs block-path-ref-pages))))
Expand Down
1 change: 0 additions & 1 deletion src/main/frontend/handler/repo.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,6 @@
(state/set-state! :editor/last-edit-block {:block edit-block
:idx idx
:container (gobj/get container "id")})))

;; try catch so that if db transaction failed, it'll not write to the files
(try
(do
Expand Down

0 comments on commit 89aff1a

Please sign in to comment.