Browse Source

Merge pull request #209 from logseq/refactor/children

Refactor :block/children
Tienson Qin 5 years ago
parent
commit
ba110322fb

+ 52 - 31
src/main/frontend/db.cljs

@@ -1173,8 +1173,7 @@
                [?e2 :block/children ?e1]]
               [(parent ?e2 ?e1)
                [?t :block/children ?e1]
-               [?t :block/uuid ?tid]
-               (parent ?e2 ?tid)]])
+               (parent ?e2 ?t)]])
            (seq-flatten)))))
 
 ;; FIXME: :block/children should contain all
@@ -1183,8 +1182,7 @@
   (when-let [conn (get-conn repo)]
     (let [ids (:block/children (entity repo [:block/uuid block-uuid]))]
       (when (seq ids)
-        (pull-many repo '[*]
-                   (map (fn [id] [:block/uuid id]) ids))))))
+        (pull-many repo '[*] ids)))))
 
 (defn get-block-children
   [repo block-uuid]
@@ -2252,32 +2250,21 @@
      [?h :block/collapsed? true]]
    (get-conn)))
 
-;; recursive query might be slow, need benchmarks
-;; Could replace this with a recursive call or , see below
-(defn get-block-parents-rec
-  [repo block-id depth]
-  (when-let [conn (get-conn repo)]
-    (let [ids (->> (d/q
-                    '[:find ?e2
-                      :in $ ?e1 %
-                      :where (parent ?e2 ?e1)]
-                    conn
-                    block-id
-                     ;; recursive rules
-                    '[[(parent ?e2 ?e1)
-                       [?e2 :block/children ?e1]]
-                      [(parent ?e2 ?e1)
-                       [?t :block/children ?e1]
-                       [?t :block/uuid ?tid]
-                       (parent ?e2 ?tid)]])
-                   (seq-flatten))]
-      (when (seq ids)
-        (pull-many repo '[:block/uuid :block/title] ids)))))
-
 (defn get-block-parent
   [repo block-id]
   (when-let [conn (get-conn repo)]
-    (d/entity conn [:block/children block-id])))
+    (when-let [id (:db/id (d/entity conn [:block/uuid block-id]))]
+      (d/entity conn [:block/children id]))))
+
+;; Using reverse lookup, a bit slow compared to get-block-parents
+#_(defn get-block-parents-rec
+  [repo block-id depth]
+  (when-let [conn (get-conn repo)]
+    (let [id (:db/id (entity repo [:block/uuid block-id]))]
+      (d/pull conn
+              '[:db/id :block/uuid :block/title :block/content
+                {:block/_children ...}]
+              id))))
 
 ;; non recursive query
 (defn get-block-parents
@@ -2421,6 +2408,7 @@
            last-level 10000]
       (if (seq blocks)
         (let [[{:block/keys [uuid level] :as block} & others] blocks
+              db-id (:db/id block)
               [tx children] (cond
                               (< level last-level)        ; parent
                               (let [cur-children (get children last-level)
@@ -2435,15 +2423,15 @@
                                          tx)
                                     children (-> children
                                                  (dissoc last-level)
-                                                 (update level conj uuid))]
+                                                 (update level conj db-id))]
                                 [tx children])
 
                               (> level last-level)        ; child of sibling
-                              (let [children (update children level conj uuid)]
+                              (let [children (update children level conj db-id)]
                                 [tx children])
 
                               :else                       ; sibling
-                              (let [children (update children last-level conj uuid)]
+                              (let [children (update children last-level conj db-id)]
                                 [tx children]))]
           (recur others tx children level))
         ;; TODO: add top-level children to the "Page" block (we might remove the Page from db schema)
@@ -2514,4 +2502,37 @@
                :git/cloned? (cloned? repo)
                :git/status (get-key-value repo :git/status)
                :git/error (get-key-value repo :git/error)})
-            repos))))
+            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/ref-pages ?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)))

+ 8 - 3
src/main/frontend/format/block.cljs

@@ -221,7 +221,10 @@
 (defn block-keywordize
   [block]
   (medley/map-keys
-   (fn [k] (keyword "block" k))
+   (fn [k]
+     (if (namespace k)
+       k
+       (keyword "block" k)))
    block))
 
 (defn safe-blocks
@@ -269,21 +272,23 @@
                                  (when (util/uuid-string? custom-id)
                                    (uuid custom-id))))
                              (d/squuid))
+                      temp-id (d/tempid :db.part/user)
                       block (second block)
                       level (:level block)
                       [children current-block-children]
                       (cond
                         (>= level last-level)
-                        [(conj children [id level])
+                        [(conj children [temp-id level])
                          #{}]
 
                         (< level last-level)
                         (let [current-block-children (set (->> (filter #(< level (second %)) children)
                                                                (map first)))
                               others (vec (remove #(< level (second %)) children))]
-                          [(conj others [id level])
+                          [(conj others [temp-id level])
                            current-block-children]))
                       block (-> (assoc block
+                                       :db/id temp-id
                                        :uuid id
                                        :body (vec (reverse block-body))
                                        :properties (:properties properties)

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

@@ -26,6 +26,7 @@
   ([repo]
    (when (and
           repo
+          (db/cloned? repo)
           (gobj/get js/window "workerThread")
           (gobj/get js/window.workerThread "getChangedFiles"))
      (->