enhance: allow existing page and property to be used as tags

This commit is contained in:
Tienson Qin
2024-12-06 18:51:39 +08:00
parent 57912bda15
commit a73606155b
4 changed files with 32 additions and 20 deletions

View File

@@ -33,15 +33,16 @@
(defn add-tag [repo block-id tag-entity]
(let [opts {:outliner-op :save-block}]
(ui-outliner-tx/transact! opts
(p/do!
(editor-handler/save-current-block!)
;; Check after save-current-block to get most up to date block content
(when (valid-tag? repo (db/entity repo [:block/uuid block-id]) tag-entity)
(let [tx-data [[:db/add [:block/uuid block-id] :block/tags (:db/id tag-entity)]
;; TODO: Move this to outliner.core to consistently add refs for tags
[:db/add [:block/uuid block-id] :block/refs (:db/id tag-entity)]]]
(db/transact! repo tx-data {:outliner-op :save-block})))))))
(ui-outliner-tx/transact!
opts
(p/do!
(editor-handler/save-current-block!)
;; Check after save-current-block to get most up to date block content
(when (valid-tag? repo (db/entity repo [:block/uuid block-id]) tag-entity)
(let [tx-data [[:db/add [:block/uuid block-id] :block/tags (:db/id tag-entity)]
;; TODO: Move this to outliner.core to consistently add refs for tags
[:db/add [:block/uuid block-id] :block/refs (:db/id tag-entity)]]]
(db/transact! repo tx-data {:outliner-op :save-block})))))))
(defn convert-to-tag!
[page-entity]

View File

@@ -4,7 +4,8 @@
[frontend.db.transact]
[frontend.db.conn]
[logseq.outliner.op]
[frontend.modules.outliner.op])))
[frontend.modules.outliner.op]
[logseq.db])))
(defmacro transact!
[opts & body]

View File

@@ -169,15 +169,24 @@
(let [db @conn
date-formatter (:logseq.property.journal/title-format (d/entity db :logseq.class/Journal))
title (sanitize-title title*)
type (cond class?
:logseq.class/Tag
whiteboard?
:logseq.class/Whiteboard
today-journal?
:logseq.class/Journal
:else
:logseq.class/Page)]
(when-not (ldb/page-exists? db title #{type})
types (cond class?
#{:logseq.class/Tag :logseq.class/Property :logseq.class/Page}
whiteboard?
#{:logseq.class/Whiteboard}
today-journal?
#{:logseq.class/Journal}
:else
#{:logseq.class/Page})]
(if-let [existing-page-id (first (ldb/page-exists? db title types))]
(let [existing-page (d/entity db existing-page-id)
tx-meta {:persist-op? persist-op?
:outliner-op :save-block}]
(when (and class?
(not (ldb/class? existing-page))
(or (ldb/property? existing-page) (ldb/internal-page? existing-page)))
;; convert existing property or page to class
(let [tx-data (db-class/build-new-class db (select-keys existing-page [:block/title :block/uuid :db/ident :block/created-at]))]
(ldb/transact! conn tx-data tx-meta))))
(let [format :markdown
page (-> (gp-block/page-name->map title @conn true date-formatter
{:class? class?