fix: prefer existing page when creating a new class

fixes
https://linear.app/logseq/issue/LOG-3240/inline-tag-for-an-existing-page-creates-a-duplicate-tag

`get-case-page` was removed because it's confusing to have multiple
tags with the same name (case-sensitive).
fix/use-existing-page-for-new-tag
Tienson Qin 2024-09-23 02:24:39 +08:00
parent 3bd3d6b125
commit dc8814304d
12 changed files with 58 additions and 67 deletions

View File

@ -244,15 +244,6 @@
(d/entity db [:block/uuid id])
(d/entity db (get-first-page-by-name db (name page-name-or-uuid))))))
(defn get-case-page
"Case sensitive version of get-page. For use with DB graphs"
[db page-name-or-uuid]
(when db
(if-let [id (if (uuid? page-name-or-uuid) page-name-or-uuid
(parse-uuid page-name-or-uuid))]
(d/entity db [:block/uuid id])
(d/entity db (sqlite-common-db/get-first-page-by-title db page-name-or-uuid)))))
(defn page-empty?
"Whether a page is empty. Does it has a non-page block?
`page-id` could be either a string or a db/id."

View File

@ -307,9 +307,7 @@
(not (boolean (text/get-nested-page-name original-page-name')))
(text/namespace-page? original-page-name'))
page-entity (when (and db (not skip-existing-page-check?))
(if class?
(ldb/get-case-page db original-page-name')
(ldb/get-page db original-page-name')))
(ldb/get-page db original-page-name'))
original-page-name' (or from-page (:block/title page-entity) original-page-name')
page (merge
{:block/name page-name
@ -449,7 +447,7 @@
refs (->> (ref->map-fn *refs false)
(remove nil?)
(map (fn [ref]
(let [ref' (if-let [entity (ldb/get-case-page db (:block/title ref))]
(let [ref' (if-let [entity (ldb/get-page db (:block/title ref))]
(if (= (:db/id parse-block) (:db/id entity))
ref
(select-keys entity [:block/uuid :block/title :block/name]))

View File

@ -42,7 +42,7 @@
(defn- create-view!
[class view-title views set-view-entity! set-views!]
(when-let [page (db/get-case-page common-config/views-page-name)]
(when-let [page (db/get-page common-config/views-page-name)]
(p/let [result (editor-handler/api-insert-new-block! view-title {:page (:block/uuid page)
:properties {:logseq.property/view-for (:db/id class)}})
view (db/entity [:block/uuid (:block/uuid result)])]

View File

@ -12,7 +12,7 @@
(let [k (:property-key (last (:rum/args state)))]
(assoc state
::property-key (atom k)
::property (atom (when k (db/get-case-page k))))))}
::property (atom (when k (db/get-page k))))))}
[state blocks opts]
(when (seq blocks)
(let [*property-key (::property-key state)

View File

@ -322,7 +322,7 @@
(or (seq (map string/trim (rest (re-find #"(.*)#(.*)$" page*))))
[page* nil])
[page* nil])
page-entity (ldb/get-case-page (db/get-db) page)
page-entity (ldb/get-page (db/get-db) page)
id (:db/id page-entity)
class? (or (= :block/tags (:db/ident property))
(and (= :logseq.property/parent (:db/ident property))
@ -334,7 +334,7 @@
(or (nil? id) (and class? (not page?)))
(let [inline-class-uuid
(when inline-class
(or (:block/uuid (ldb/get-case-page (db/get-db) inline-class))
(or (:block/uuid (ldb/get-page (db/get-db) inline-class))
(do (log/error :msg "Given inline class does not exist" :inline-class inline-class)
nil)))
create-options {:redirect? false

View File

@ -33,7 +33,7 @@
get-custom-css
get-file-last-modified-at get-file get-file-page get-file-page-id file-exists?
get-files-blocks get-files-full get-journals-length
get-latest-journals get-page get-case-page get-page-alias-names
get-latest-journals get-page get-page-alias-names
get-page-blocks-count get-page-blocks-no-cache get-page-file get-page-format
get-referenced-blocks get-page-referenced-blocks-full get-page-referenced-pages
get-all-pages get-pages-relation get-pages-that-mentioned-page

View File

@ -455,11 +455,6 @@ independent of format as format specific heading characters are stripped"
(when page-name-or-uuid
(ldb/get-page (conn/get-db) page-name-or-uuid)))
(defn get-case-page
[page-name-or-uuid]
(when page-name-or-uuid
(ldb/get-case-page (conn/get-db) page-name-or-uuid)))
(defn get-journal-page
[page-title]
(when-let [journal-day (date/journal-title->int page-title)]

View File

@ -82,6 +82,6 @@
add-tag-to-nearest-node? (= page-ref/right-brackets (common-util/safe-subs edit-content (- hash-idx 2) hash-idx))
nearest-node (some-> (editor-handler/get-nearest-page) string/trim)]
(if (and add-tag-to-nearest-node? (not (string/blank? nearest-node)))
(when-let [e (db/get-case-page nearest-node)]
(when-let [e (db/get-page nearest-node)]
(add-tag (state/get-current-repo) (:block/uuid e) tag-entity))
(add-tag (state/get-current-repo) (:block/uuid edit-block) tag-entity))))))))

View File

@ -184,8 +184,8 @@
fix-data (keep
(fn [d]
(if-let [id (if (= :all-pages (:v d))
(:db/id (ldb/get-case-page db common-config/views-page-name))
(:db/id (d/entity db (:v d))))]
(:db/id (ldb/get-page db common-config/views-page-name))
(:db/id (d/entity db (:v d))))]
[:db/add (:e d) :logseq.property/view-for id]
[:db/retract (:e d) :logseq.property/view-for (:v d)]))
datoms)]

View File

@ -86,35 +86,42 @@
today-journal?
"journal"
:else
"page")]
(when-not (ldb/page-exists? db title type)
(let [format :markdown
page (-> (gp-block/page-name->map title @conn true date-formatter
{:class? class?
:page-uuid (when (uuid? uuid) uuid)
:skip-existing-page-check? true})
(assoc :block/format format))
[page parents] (if (and (text/namespace-page? title) split-namespace?)
(let [pages (outliner-core/split-namespace-pages db page nil date-formatter)]
[(last pages) (butlast pages)])
[page nil])]
(when page
(let [page-uuid (:block/uuid page)
page-txs (build-page-tx conn properties page (select-keys options [:whiteboard? :class? :tags]))
first-block-tx (when (and
(nil? (d/entity @conn [:block/uuid page-uuid]))
create-first-block?
(not (or whiteboard? class?))
page-txs)
(build-first-block-tx (:block/uuid (first page-txs)) format))
txs (concat
parents
page-txs
first-block-tx)]
(when (seq txs)
(ldb/transact! conn txs (cond-> {:persist-op? persist-op?
:outliner-op :create-page}
today-journal?
(assoc :create-today-journal? true
:today-journal-name title))))
[title page-uuid]))))))
"page")
page-exists? (ldb/page-exists? db title type)]
(when-not page-exists?
(let [page-with-same-name (ldb/get-page db title)]
(if (and class? (= (:block/type page-with-same-name) "page")) ; convert existing page to tag
(let [class (db-class/build-new-class db (select-keys page-with-same-name
[:db/id :block/title :block/created-at]))]
(ldb/transact! conn [class] {:outliner-op :save-block})
[title (:block/uuid page-with-same-name)])
(let [format :markdown
page (-> (gp-block/page-name->map title @conn true date-formatter
{:class? class?
:page-uuid (when (uuid? uuid) uuid)
:skip-existing-page-check? true})
(assoc :block/format format))
[page parents] (if (and (text/namespace-page? title) split-namespace?)
(let [pages (outliner-core/split-namespace-pages db page nil date-formatter)]
[(last pages) (butlast pages)])
[page nil])]
(when page
(let [page-uuid (:block/uuid page)
page-txs (build-page-tx conn properties page (select-keys options [:whiteboard? :class? :tags]))
first-block-tx (when (and
(nil? (d/entity @conn [:block/uuid page-uuid]))
create-first-block?
(not (or whiteboard? class?))
page-txs)
(build-first-block-tx (:block/uuid (first page-txs)) format))
txs (concat
parents
page-txs
first-block-tx)]
(when (seq txs)
(ldb/transact! conn txs (cond-> {:persist-op? persist-op?
:outliner-op :create-page}
today-journal?
(assoc :create-today-journal? true
:today-journal-name title))))
[title page-uuid]))))))))

View File

@ -47,7 +47,7 @@
(if multi?
(-> (for [v value]
(when-let [page (some-> v (str) (string/trim))]
(let [id (:db/id (ldb/get-case-page (conn/get-db) page))]
(let [id (:db/id (ldb/get-page (conn/get-db) page))]
(if (nil? id)
(-> (page-handler/<create! page {:redirect? false :create-first-block? false})
(p/then #(:db/id %)))

View File

@ -29,7 +29,7 @@
(deftest ^:fix-me get-class-objects-test
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (test-helper/create-page! "class1" opts)
class (db/get-case-page "class1")
class (db/get-page "class1")
_ (test-helper/save-block! repo fbid "Block 1" {:tags ["class1"]})]
(is (= (map :db/id (model/get-class-objects repo (:db/id class)))
[(:db/id (db/entity [:block/uuid fbid]))]))
@ -37,7 +37,7 @@
(testing "classes parent"
(test-helper/create-page! "class2" opts)
;; set class2's parent to class1
(let [class2 (db/get-case-page "class2")]
(let [class2 (db/get-page "class2")]
(db/transact! [{:db/id (:db/id class2)
:logseq.property/parent (:db/id class)}]))
(test-helper/save-block! repo sbid "Block 2" {:tags ["class2"]})
@ -49,8 +49,8 @@
(let [opts {:redirect? false :create-first-block? false :class? true}
_ (test-helper/create-page! "class1" opts)
_ (test-helper/create-page! "class2" opts)
class1 (db/get-case-page "class1")
class2 (db/get-case-page "class2")
class1 (db/get-page "class1")
class2 (db/get-page "class2")
conn (db/get-db false)]
(outliner-property/upsert-property! conn :user.property/property-1 {:type :node} {})
(outliner-property/class-add-property! conn (:db/id class1) :user.property/property-1)
@ -72,13 +72,13 @@
_ (test-helper/create-page! "class1" opts)
_ (test-helper/create-page! "class2" opts)
_ (test-helper/create-page! "class3" opts)
class1 (db/get-case-page "class1")
class2 (db/get-case-page "class2")
class3 (db/get-case-page "class3")
class1 (db/get-page "class1")
class2 (db/get-page "class2")
class3 (db/get-page "class3")
_ (db/transact! [{:db/id (:db/id class2)
:logseq.property/parent (:db/id class1)}
{:db/id (:db/id class3)
:logseq.property/parent (:db/id class2)}])]
(is
(= (model/get-structured-children repo (:db/id (db/get-case-page "class1")))
(= (model/get-structured-children repo (:db/id (db/get-page "class1")))
[(:db/id class2) (:db/id class3)]))))