fix: fix :block/tags, reuse ref->eid

pull/11102/head
rcmerci 2024-03-05 19:46:12 +08:00
parent 201fc32d10
commit 92625679b3
1 changed files with 25 additions and 28 deletions

View File

@ -208,6 +208,26 @@
merge-tx))))))
(reset! (:editor/create-page? @state/state) false))))
(defn- ref->eid
"ref: entity, map, int, eid"
[ref]
(cond
(:db/id ref)
(:db/id ref)
(:block/uuid ref)
[:block/uuid (:block/uuid ref)]
(and (vector? ref)
(= (count ref) 2)
(= :block/uuid (first ref)))
[:block/uuid (second ref)]
(int? ref)
ref
:else (throw (js/Error. (str "invalid ref " ref)))))
(defn ^:api rebuild-block-refs
[repo conn date-formatter block new-properties]
(let [db @conn
@ -241,15 +261,7 @@
(gp-block/extract-refs-from-text repo db content date-formatter))]
(concat property-refs content-refs
(when (sqlite-util/db-based-graph? repo)
(map (fn [t]
(cond
(de/entity? t)
(:db/id t)
(and (vector? t) (= (count t) 2) (= :block/uuid (first t)))
[:block/uuid (second t)]
(map? t)
[:block/uuid (:block/uuid t)]))
(:block/tags block))))))
(map ref->eid (:block/tags block))))))
(defn- rebuild-refs
[repo conn date-formatter txs-state block m]
@ -260,18 +272,8 @@
(remove nil?))
add-tag-type (map
(fn [t]
(cond
(integer? t)
{:db/id t
:block/type "class"}
(and (vector? t) (= (count t) 2) (= :block/uuid (first t)))
{:block/uuid (second t)
:block/type "class"}
(map? t)
{:block/uuid (:block/uuid t)
:block/type "class"}
:else
(throw (js/Error. (str "Wrong tag: " t)))))
{:db/id (ref->eid t)
:block/type "class"})
(:block/tags m))]
(swap! txs-state (fn [txs] (concat txs [{:db/id (:db/id block)
:block/refs refs}]
@ -396,13 +398,8 @@
(db-marker-handle conn))
m (if db-based?
(update m :block/tags (fn [tags]
(->>
(concat (map :db/id (:block/tags block-entity))
(map (fn [t] (or (:db/id t)
(when-let [id (:block/uuid t)]
[:block/uuid id])))
tags))
(remove nil?))))
(concat (keep :db/id (:block/tags block-entity))
(keep ref->eid tags))))
m)]
;; Ensure block UUID never changes