refactor: merge :block/ref-pages and :block/ref-blocks

pull/1656/head
Tienson Qin 2021-03-21 15:36:14 +08:00
parent 72f5a7c07b
commit 89aff1abc0
9 changed files with 51 additions and 99 deletions

View File

@ -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)

View File

@ -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))))

View File

@ -964,7 +964,7 @@
[(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
@ -972,7 +972,7 @@
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

View File

@ -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
@ -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->>

View File

@ -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)
@ -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}]
@ -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}]
@ -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]
@ -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
@ -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)))))

View File

@ -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)
@ -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

View File

@ -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? %))))))

View File

@ -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))))

View File

@ -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