fix: convert tag to page can break a tag branch

Converting a tag parent causes the graph to be invalid and buggy as tag children
are then extended by non-tags
This commit is contained in:
Gabriel Horner
2025-05-22 12:05:23 -04:00
parent 78a89f67e1
commit 4b39ce213f

View File

@@ -56,31 +56,33 @@
(db/transact! (state/get-current-repo) txs {:outliner-op :save-block}))))
(defn convert-tag-to-page!
[page-entity]
(if (db/page-exists? (:block/title page-entity) #{:logseq.class/Page})
(notification/show! (str "A page with the name \"" (:block/title page-entity) "\" already exists.") :warning false)
(when-not (:logseq.property/built-in? page-entity)
(p/let [objects (db-async/<get-tag-objects (state/get-current-repo) (:db/id page-entity))]
(let [convert-fn
(fn convert-fn []
(let [page-txs [[:db/retract (:db/id page-entity) :db/ident]
[:db/retract (:db/id page-entity) :block/tags :logseq.class/Tag]
[:db/retract (:db/id page-entity) :logseq.property.class/extends]
[:db/retract (:db/id page-entity) :logseq.property.class/properties]
[:db/add (:db/id page-entity) :block/tags :logseq.class/Page]]
obj-txs (mapcat (fn [obj]
(let [tags (map #(db/entity (state/get-current-repo) (:db/id %)) (:block/tags obj))]
[{:db/id (:db/id obj)
:block/title (db-content/replace-tag-refs-with-page-refs (:block/title obj) tags)}
[:db/retract (:db/id obj) :block/tags (:db/id page-entity)]]))
objects)
txs (concat page-txs obj-txs)]
(db/transact! (state/get-current-repo) txs {:outliner-op :save-block})))]
(-> (shui/dialog-confirm!
"Converting a tag to page also removes its tag properties and its tag from all nodes tagged with it. Are you ok with that?"
{:id :convert-tag-to-page
:data-reminder :ok})
(p/then convert-fn)))))))
[entity]
(if (db/page-exists? (:block/title entity) #{:logseq.class/Page})
(notification/show! (str "A page with the name \"" (:block/title entity) "\" already exists.") :warning false)
(when-not (:logseq.property/built-in? entity)
(if (seq (:logseq.property.class/_extends entity))
(notification/show! "This tag cannot be converted because it has tag children. All tag children must be removed or converted before converting this tag." :warning false)
(p/let [objects (db-async/<get-tag-objects (state/get-current-repo) (:db/id entity))]
(let [convert-fn
(fn convert-fn []
(let [page-txs [[:db/retract (:db/id entity) :db/ident]
[:db/retract (:db/id entity) :block/tags :logseq.class/Tag]
[:db/retract (:db/id entity) :logseq.property.class/extends]
[:db/retract (:db/id entity) :logseq.property.class/properties]
[:db/add (:db/id entity) :block/tags :logseq.class/Page]]
obj-txs (mapcat (fn [obj]
(let [tags (map #(db/entity (state/get-current-repo) (:db/id %)) (:block/tags obj))]
[{:db/id (:db/id obj)
:block/title (db-content/replace-tag-refs-with-page-refs (:block/title obj) tags)}
[:db/retract (:db/id obj) :block/tags (:db/id entity)]]))
objects)
txs (concat page-txs obj-txs)]
(db/transact! (state/get-current-repo) txs {:outliner-op :save-block})))]
(-> (shui/dialog-confirm!
"Converting a tag to page also removes its tag properties and its tag from all nodes tagged with it. Are you ok with that?"
{:id :convert-tag-to-page
:data-reminder :ok})
(p/then convert-fn))))))))
(defn <create-class!
"Creates a class page and provides class-specific error handling"