fix: get-block-and-children returns sorted blocks

Fix unit tests
experiment/tanstack-table
Tienson Qin 2024-06-20 07:56:30 +08:00
parent 77f79709dc
commit adae8ef5eb
4 changed files with 7 additions and 29 deletions

View File

@ -88,8 +88,8 @@
(defn- get-block-and-children-aux
[entity]
(if-let [children (:block/_parent entity)]
(cons (dissoc entity :block/_parent) (mapcat get-block-and-children-aux children))
(if-let [children (sort-by-order (:block/_parent entity))]
(cons entity (mapcat get-block-and-children-aux children))
[entity]))
(defn get-block-and-children

View File

@ -46,6 +46,7 @@
:else
[false root-id]))
;; TODO: entity can already be used as a tree
(defn blocks->vec-tree
"`blocks` need to be in the same page."
[repo db blocks root-id]
@ -107,27 +108,8 @@
parent->children (group-by :block/parent blocks)]
(map #(tree parent->children % (or default-level 1)) top-level-blocks'))))
(defn- sort-blocks-aux
[parents parent-groups]
(mapv (fn [parent]
(let [parent-id {:db/id (:db/id parent)}
children (ldb/sort-by-order (get @parent-groups parent-id))
_ (swap! parent-groups #(dissoc % parent-id))
sorted-nested-children (when (not-empty children) (sort-blocks-aux children parent-groups))]
(if sorted-nested-children [parent sorted-nested-children] [parent])))
parents))
;; Do we still need this?
(defn ^:api sort-blocks
"sort blocks by parent & order"
[blocks-exclude-root root]
(let [parent-groups (atom (group-by :block/parent blocks-exclude-root))]
(flatten (concat (sort-blocks-aux [root] parent-groups) (vals @parent-groups)))))
(defn get-sorted-block-and-children
[db db-id]
(when db-id
(when-let [root-block (d/entity db db-id)]
(let [blocks (ldb/get-block-and-children db (:block/uuid root-block))
blocks-exclude-root (remove (fn [b] (= (:db/id b) db-id)) blocks)]
(sort-blocks blocks-exclude-root root-block)))))
(ldb/get-block-and-children db (:block/uuid root-block)))))

View File

@ -2149,22 +2149,19 @@
(db-async/<get-block repo (:block/uuid block)
{:children? true
:nested-children? true}))]
(when-let [db-id (:db/id block)]
(when (:db/id block)
(let [journal? (ldb/journal-page? target)
target (or target (state/get-edit-block))
format (:block/format block)
block-uuid (:block/uuid block)
template-including-parent? (not (false? (:template-including-parent (:block/properties block))))
blocks (db/get-block-and-children repo block-uuid)
root-block (db/entity db-id)
blocks-exclude-root (remove (fn [b] (= (:db/id b) db-id)) blocks)
sorted-blocks (tree/sort-blocks blocks-exclude-root root-block)
sorted-blocks (cons
(-> (first sorted-blocks)
(-> (first blocks)
(update :block/properties-text-values dissoc :template)
(update :block/properties-order (fn [keys]
(vec (remove #{:template} keys)))))
(rest sorted-blocks))
(rest blocks))
blocks (if template-including-parent?
sorted-blocks
(drop 1 sorted-blocks))]

View File

@ -16,7 +16,6 @@
(def filter-top-level-blocks otree/filter-top-level-blocks)
(def non-consecutive-blocks->vec-tree otree/non-consecutive-blocks->vec-tree)
(def sort-blocks otree/sort-blocks)
(defn get-sorted-block-and-children
[repo db-id]