Jelajahi Sumber

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

Tienson Qin 4 tahun lalu
induk
melakukan
89aff1abc0

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

@@ -273,7 +273,7 @@
                                    (db/transact! repo [{:block/name page-name
                                    (db/transact! repo [{:block/name page-name
                                                         :block/original-name path-page-name}])
                                                         :block/original-name path-page-name}])
                                    (db/entity repo [:block/name 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-name (:block/name page)
               page-original-name (:block/original-name page)
               page-original-name (:block/original-name page)
               file (:block/file page)
               file (:block/file page)

+ 1 - 34
src/main/frontend/db/debug.cljs

@@ -18,37 +18,4 @@
                :git/cloned? (cloned? repo)
                :git/cloned? (cloned? repo)
                :git/status (get-key-value repo :git/status)
                :git/status (get-key-value repo :git/status)
                :git/error (get-key-value repo :git/error)})
                :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))))

+ 2 - 2
src/main/frontend/db/model.cljs

@@ -964,7 +964,7 @@
                                           [(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
                                           [(find-blocks ?block ?ref-page ?pages ?alias ?aliases)
                                            [?block :block/refs ?ref-page]
                                            [?block :block/refs ?ref-page]
                                            [(contains? ?pages ?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 [*])
                                        '[:find (pull ?block [*])
                                          :in $ % ?pages ?aliases
                                          :in $ % ?pages ?aliases
                                          :where
                                          :where
@@ -972,7 +972,7 @@
                                        rules
                                        rules
                                        pages
                                        pages
                                        aliases))
                                        aliases))
-                            (react/q repo [:page/refed-blocks page-id] {}
+                            (react/q repo [:block/refed-blocks page-id] {}
                                      '[:find (pull ?block [*])
                                      '[:find (pull ?block [*])
                                        :in $ ?pages
                                        :in $ ?pages
                                        :where
                                        :where

+ 5 - 5
src/main/frontend/db/react.cljs

@@ -233,16 +233,16 @@
 
 
                              (when current-page-id
                              (when current-page-id
                                [[:page/ref-pages 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]])
                                 [:page/mentioned-pages current-page-id]])
 
 
                              ;; refed-pages
                              ;; refed-pages
                              (apply concat
                              (apply concat
-                                    (for [{:block/keys [ref-pages]} blocks]
+                                    (for [{:block/keys [refs]} blocks]
                                       (map (fn [page]
                                       (map (fn [page]
                                              (when-let [page (db-utils/entity [:block/name (:block/name 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
                              ;; refed-blocks
                              (apply concat
                              (apply concat
@@ -253,7 +253,7 @@
                             (distinct))
                             (distinct))
               refed-pages (map
               refed-pages (map
                            (fn [[k page-id]]
                            (fn [[k page-id]]
-                             (if (= k :page/refed-blocks)
+                             (if (= k :block/refed-blocks)
                                [:page/ref-pages page-id]))
                                [:page/ref-pages page-id]))
                            handler-keys)
                            handler-keys)
               custom-queries (some->>
               custom-queries (some->>

+ 37 - 34
src/main/frontend/format/block.cljs

@@ -219,6 +219,23 @@
                               (assoc :repeated? true))))))]
                               (assoc :repeated? true))))))]
     (apply merge m)))
     (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
 (defn with-page-refs
   [{:keys [title body tags ref-pages] :as block}]
   [{:keys [title body tags ref-pages] :as block}]
   (let [ref-pages (->> (concat tags ref-pages)
   (let [ref-pages (->> (concat tags ref-pages)
@@ -241,8 +258,10 @@
                                           (butlast (string/split p #"/"))))
                                           (butlast (string/split p #"/"))))
                                       ref-pages)
                                       ref-pages)
                               (remove string/blank?))
                               (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
 (defn with-block-refs
   [{:keys [title body] :as block}]
   [{:keys [title body] :as block}]
@@ -253,11 +272,13 @@
          (swap! ref-blocks conj block))
          (swap! ref-blocks conj block))
        form)
        form)
      (concat title body))
      (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!
 (defn update-src-pos-meta!
   [{:keys [body] :as block}]
   [{:keys [body] :as block}]
@@ -432,22 +453,7 @@
                    blocks)]
                    blocks)]
       (with-path-refs 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
 (defn parse-block
   ([block format]
   ([block format]
@@ -465,12 +471,9 @@
                                  (map :db/id))
                                  (map :db/id))
            blocks (doall
            blocks (doall
                    (map-indexed
                    (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 (merge
                                    block
                                    block
                                    {:block/meta meta
                                    {:block/meta meta
@@ -486,14 +489,14 @@
                                    ;; Preserve the original block id
                                    ;; Preserve the original block id
                                    (when (zero? idx)
                                    (when (zero? idx)
                                      {:block/uuid uuid})
                                      {:block/uuid uuid})
-                                   (when (seq ref-pages)
+                                   (when (seq refs)
                                      {:block/refs
                                      {:block/refs
                                       (mapv
                                       (mapv
                                        (fn [page]
                                        (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
                         (-> block
                             (assoc-in [:block/meta :start-pos] (+ (:start-pos meta) start-pos))
                             (assoc-in [:block/meta :start-pos] (+ (:start-pos meta) start-pos))
                             (assoc-in [:block/meta :end-pos] (+ (:end-pos meta) start-pos)))))
                             (assoc-in [:block/meta :end-pos] (+ (:end-pos meta) start-pos)))))

+ 2 - 18
src/main/frontend/handler/editor.cljs

@@ -490,16 +490,8 @@
                                                       :end-pos new-end-pos})
                                                       :end-pos new-end-pos})
                                                    (block/parse-block block format))
                                                    (block/parse-block block format))
         block-retracted-attrs (when-not pre-block?
         block-retracted-attrs (when-not pre-block?
-                                ;; TODO: should we retract the whole block instead?
                                 (when-let [id (:db/id block)]
                                 (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?)
         [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)
         retract-refs (compute-retract-refs (:db/id e) (first blocks) ref-pages ref-blocks)
         page-id (:db/id page)
         page-id (:db/id page)
@@ -681,16 +673,8 @@
         after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
         after-blocks (rebuild-after-blocks repo file (:end-pos meta) end-pos)
         files [[file-path new-content]]
         files [[file-path new-content]]
         block-retracted-attrs (when-not pre-block?
         block-retracted-attrs (when-not pre-block?
-                                ;; TODO: should we retract the whole block instead?
                                 (when-let [id (:db/id block)]
                                 (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 []
         transact-fn (fn []
                       (repo-handler/transact-react-and-alter-file!
                       (repo-handler/transact-react-and-alter-file!
                        repo
                        repo

+ 2 - 2
src/main/frontend/handler/export.cljs

@@ -131,8 +131,8 @@
                [page-or-block]
                [page-or-block]
                (db/get-page-blocks
                (db/get-page-blocks
                 repo page-or-block {:use-cache? false
                 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)]))
              (mapv (fn [b] [(:block/ref-blocks b), (:block/ref-pages b)]))
              (apply mapv vector)
              (apply mapv vector)
              (mapv #(vec (distinct (flatten (remove nil? %))))))
              (mapv #(vec (distinct (flatten (remove nil? %))))))

+ 1 - 2
src/main/frontend/handler/extract.cljs

@@ -46,8 +46,7 @@
                                            :block/file [:file/path file]
                                            :block/file [:file/path file]
                                            :block/format format
                                            :block/format format
                                            :block/page [:block/name (string/lower-case page)]
                                            :block/page [:block/name (string/lower-case page)]
-                                           :block/refs (mapv
-                                                             (fn [page]
+                                           :block/refs (mapv (fn [page]
                                                                (block/page-name->map page))
                                                                (block/page-name->map page))
                                                              block-ref-pages)
                                                              block-ref-pages)
                                            :block/path-refs block-path-ref-pages))))
                                            :block/path-refs block-path-ref-pages))))

+ 0 - 1
src/main/frontend/handler/repo.cljs

@@ -371,7 +371,6 @@
          (state/set-state! :editor/last-edit-block {:block edit-block
          (state/set-state! :editor/last-edit-block {:block edit-block
                                                     :idx idx
                                                     :idx idx
                                                     :container (gobj/get container "id")})))
                                                     :container (gobj/get container "id")})))
-
      ;; try catch so that if db transaction failed, it'll not write to the files
      ;; try catch so that if db transaction failed, it'll not write to the files
      (try
      (try
        (do
        (do