Browse Source

fix: revert :block/children because it doesn't work

We need to resolve those tempids after db transacted which makes it slow
Tienson Qin 5 years ago
parent
commit
5987cc9742

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

@@ -1315,7 +1315,7 @@
                 (:block/uuid child)))))])
 
      (when ref?
-       (let [children (-> (db/get-block-children-unsafe repo uuid)
+       (let [children (-> (db/get-block-immediate-children repo uuid)
                           db/sort-by-pos)]
          (when (seq children)
            [:div.ref-children.ml-12

+ 24 - 26
src/main/frontend/db.cljs

@@ -1173,11 +1173,11 @@
                [?e2 :block/children ?e1]]
               [(parent ?e2 ?e1)
                [?t :block/children ?e1]
-               (parent ?e2 ?t)]])
-           (seq-flatten)))))
+               [?t :block/uuid ?tid]
+               (parent ?e2 ?tid)]])
+           (apply concat)))))
 
-;; FIXME: :block/children should contain all
-(defn get-block-children-unsafe
+(defn get-block-immediate-children
   [repo block-uuid]
   (when-let [conn (get-conn repo)]
     (let [ids (:block/children (entity repo [:block/uuid block-uuid]))]
@@ -1189,8 +1189,7 @@
   (when-let [conn (get-conn repo)]
     (let [ids (get-block-children-ids repo block-uuid)]
       (when (seq ids)
-        (pull-many repo '[*]
-                   (map (fn [id] [:block/uuid id]) ids))))))
+        (pull-many repo '[*] ids)))))
 
 (defn get-block-and-children
   ([repo block-uuid]
@@ -2247,18 +2246,18 @@
 (defn get-block-parent
   [repo block-id]
   (when-let [conn (get-conn repo)]
-    (when-let [id (:db/id (d/entity conn [:block/uuid block-id]))]
-      (d/entity conn [:block/children id]))))
+    (d/entity conn [:block/children [:block/uuid block-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]))]
+    [repo block-id depth]
+    (when-let [conn (get-conn repo)]
       (d/pull conn
               '[:db/id :block/uuid :block/title :block/content
                 {:block/_children ...}]
-              id))))
+              [:block/uuid block-id])))
 
 ;; non recursive query
 (defn get-block-parents
@@ -2402,7 +2401,6 @@
            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)
@@ -2412,20 +2410,20 @@
                                            tx
                                            (map
                                             (fn [child]
-                                              [:db/add (:db/id block) :block/children child])
+                                              [:db/add (:db/id block) :block/children [:block/uuid child]])
                                             cur-children)))
                                          tx)
                                     children (-> children
                                                  (dissoc last-level)
-                                                 (update level conj db-id))]
+                                                 (update level conj uuid))]
                                 [tx children])
 
                               (> level last-level)        ; child of sibling
-                              (let [children (update children level conj db-id)]
+                              (let [children (update children level conj uuid)]
                                 [tx children])
 
                               :else                       ; sibling
-                              (let [children (update children last-level conj db-id)]
+                              (let [children (update children last-level conj uuid)]
                                 [tx children]))]
           (recur others tx children level))
         ;; TODO: add top-level children to the "Page" block (we might remove the Page from db schema)
@@ -2506,15 +2504,15 @@
   (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)
+      '[: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

+ 5 - 5
src/main/frontend/format/block.cljs

@@ -272,23 +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 [temp-id level])
+                        [(conj children [id level])
                          #{}]
 
                         (< level last-level)
                         (let [current-block-children (set (->> (filter #(< level (second %)) children)
-                                                               (map first)))
+                                                               (map first)
+                                                               (map (fn [id]
+                                                                      [:block/uuid id]))))
                               others (vec (remove #(< level (second %)) children))]
-                          [(conj others [temp-id level])
+                          [(conj others [id level])
                            current-block-children]))
                       block (-> (assoc block
-                                       :db/id temp-id
                                        :uuid id
                                        :body (vec (reverse block-body))
                                        :properties (:properties properties)