Skip to content

Commit e4e8388

Browse files
committed
fix: revert disallowed built-in block updates
1. built-in pages (non-classes) shouldn't be used as tags 2. some protected properties for built-in nodes shouldn't be updated, e.g. db/ident, block/title, block/name, logseq.property/type
1 parent 79f670f commit e4e8388

File tree

8 files changed

+97
-32
lines changed

8 files changed

+97
-32
lines changed

deps/outliner/src/logseq/outliner/core.cljs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,6 @@
327327
(:block/title m*)
328328
(not= (:block/title m*) (:block/title block-entity)))
329329
(outliner-validate/validate-block-title db (:block/title m*) block-entity))
330-
_ (when (and db-based? (seq (:block/tags m*)))
331-
;; Add built-in? b/c it's not available here
332-
(doseq [tag (map #(assoc % :logseq.property/built-in?
333-
(contains? sqlite-create-graph/built-in-pages-names (:block/title %))) (:block/tags m*))]
334-
(outliner-validate/validate-built-in-pages tag {:message "Built-in page can't be a tag"})))
335330
m (cond-> m*
336331
db-based?
337332
(dissoc :block/format :block/pre-block? :block/priority :block/marker :block/properties-order))]

deps/outliner/src/logseq/outliner/validate.cljs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@
3939

4040
(def ^:api uneditable-page? ldb/built-in?)
4141

42-
(defn ^:api validate-built-in-pages
43-
"Validates built-in pages shouldn't be modified"
44-
[entity & {:keys [message]}]
45-
(when (uneditable-page? entity)
46-
(throw (ex-info "Rename built-in pages"
47-
{:type :notification
48-
:payload {:message (or message "Built-in pages can't be edited")
49-
:type :warning}}))))
50-
5142
(defn- find-other-ids-with-title-and-tags
5243
"Query that finds other ids given the id to ignore, title to look up and tags to consider"
5344
[entity]
@@ -138,7 +129,6 @@
138129
(defn validate-block-title
139130
"Validates a block title when it has changed for a entity-util/page? or tagged node"
140131
[db new-title existing-block-entity]
141-
(validate-built-in-pages existing-block-entity)
142132
(validate-unique-by-name-and-tags db new-title existing-block-entity)
143133
(validate-disallow-page-with-journal-name new-title existing-block-entity))
144134

src/main/frontend/components/editor.cljs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,13 @@
161161
result (if db-tag?
162162
(let [classes (editor-handler/get-matched-classes q)]
163163
(if (and (ldb/internal-page? block)
164-
(= (:block/title block) q))
165-
(cons {:block/title (util/format "Convert \"%s\" to tag" q)
164+
(= (:block/title block) q)
165+
(not (ldb/built-in? block)))
166+
(cons {:block/title q
166167
:db/id (:db/id block)
167168
:block/uuid (:block/uuid block)
168-
:convert-page-to-tag? true} classes)
169+
:convert-page-to-tag? true
170+
:friendly-title (util/format "Convert \"%s\" to tag" q)} classes)
169171
classes))
170172
(editor-handler/<get-matched-blocks q {:nlp-pages? true
171173
:page-only? (not db-based?)}))]
@@ -203,7 +205,7 @@
203205
(let [block' (if-let [id (:block/uuid block)]
204206
(if-let [e (db/entity [:block/uuid id])]
205207
(assoc e
206-
:block/title (or (:block/title e) (:block/title block))
208+
:block/title (or (:friendly-title block) (:block/title e) (:block/title block))
207209
:alias (:alias block))
208210
block)
209211
block)]

src/main/frontend/handler/db_based/page.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
(:block/parent page-entity)
5151
(notification/show! "Namespaced pages can't be tags" :error false)
5252
(outliner-validate/uneditable-page? page-entity)
53-
(notification/show! "Built-in pages can't be edited" :error)
53+
(notification/show! "Built-in pages can't be used as tags" :error)
5454
:else
5555
(let [txs [(db-class/build-new-class (db/get-db)
5656
{:db/id (:db/id page-entity)
@@ -65,7 +65,7 @@
6565
(cond (db/page-exists? (:block/title entity) #{:logseq.class/Page})
6666
(notification/show! (str "A page with the name \"" (:block/title entity) "\" already exists.") :warning false)
6767
(outliner-validate/uneditable-page? entity)
68-
(notification/show! "Built-in tags can't be edited" :error)
68+
(notification/show! "Built-in tags can't be converted to pages" :error)
6969
:else
7070
(if (seq (:logseq.property.class/_extends entity))
7171
(notification/show! "This tag cannot be converted because it has tag children. All tag children must be removed or converted before converting this tag." :error false)

src/main/frontend/handler/editor.cljs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,13 +3630,10 @@
36303630
(save-current-block!) ;; Save the input contents before collapsing
36313631
(ui-outliner-tx/transact! ;; Save the new collapsed state as an undo transaction (if it changed)
36323632
{:outliner-op :collapse-expand-blocks}
3633-
(doseq [block-id block-ids]
3634-
(when-let [block (db/entity [:block/uuid block-id])]
3635-
(let [current-value (boolean (:block/collapsed? block))]
3636-
(when-not (= current-value value)
3637-
(let [block {:block/uuid block-id
3638-
:block/collapsed? value}]
3639-
(outliner-save-block! block {:outliner-op :collapse-expand-blocks})))))))
3633+
(let [tx-data (map (fn [block-id]
3634+
{:block/uuid block-id
3635+
:block/collapsed? value}) block-ids)]
3636+
(outliner-op/transact! tx-data {})))
36403637
(doseq [block-id block-ids]
36413638
(state/set-collapsed-block! block-id value)))))
36423639

src/main/frontend/worker/db/migrate.cljs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,6 @@
510510

511511
r (d/transact! conn data' {:fix-db? true
512512
:db-migrate? true})]
513-
(when (seq (:tx-data r))
514-
(prn :debug :ensure-built-in-data-exists? :tx-data (:tx-data r)))
515513
(assoc r :migrate-updates
516514
;; fake it as a normal :fix type migration
517515
{:fix (constantly :ensure-built-in-data-exists!)})))

src/main/frontend/worker/pipeline.cljs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
[logseq.db.common.order :as db-order]
1616
[logseq.db.common.sqlite :as common-sqlite]
1717
[logseq.db.frontend.class :as db-class]
18+
[logseq.db.sqlite.create-graph :as sqlite-create-graph]
1819
[logseq.db.sqlite.export :as sqlite-export]
1920
[logseq.graph-parser.exporter :as gp-exporter]
2021
[logseq.outliner.core :as outliner-core]
@@ -86,6 +87,7 @@
8687
(:block/uuid e)))))))]
8788
blocks))))]
8889
(when (seq template-blocks)
90+
;; FIXME: outliner core apis shouldn't use `repo`
8991
(let [result (outliner-core/insert-blocks
9092
repo db template-blocks object
9193
{:sibling? false
@@ -323,10 +325,49 @@
323325
(cond->> add-created-by-tx-data
324326
(nil? created-by-ent) (cons created-by-block))))))
325327

328+
(defn- revert-disallowed-changes
329+
[{:keys [tx-meta tx-data db-before db-after]}]
330+
(when-not (rtc-tx-or-download-graph? tx-meta)
331+
(let [built-in-page? (fn [id]
332+
(let [block (d/entity db-after id)]
333+
(and (contains? sqlite-create-graph/built-in-pages-names
334+
(:block/title block))
335+
(ldb/built-in? block))))
336+
tx-data' (mapcat
337+
(fn [[e a v _t added]]
338+
(when added
339+
(cond
340+
;; using built-in pages as tags
341+
(and (= a :block/tags) (built-in-page? v))
342+
[[:db/retract v :db/ident]
343+
[:db/retract v :logseq.property.class/extends]
344+
[:db/retract v :block/tags :logseq.class/Tag]
345+
[:db/add v :block/tags :logseq.class/Page]
346+
[:db/retract e a v]]
347+
348+
;; built-in block protected properties updated
349+
(and (contains? #{:db/ident :block/title :block/name :logseq.property/type
350+
:logseq.property/built-in? :logseq.property.class/extends} a)
351+
(some? (d/entity db-before e))
352+
(let [block (d/entity db-after e)]
353+
(and (ldb/built-in? block)
354+
(not= (get block a) (get (d/entity db-before e) a)))))
355+
(if-some [prev-v (get (d/entity db-before e) a)]
356+
[[:db/add e a prev-v]]
357+
[[:db/retract e a v]])
358+
359+
:else
360+
nil)))
361+
tx-data)]
362+
(when (seq tx-data')
363+
(prn :debug ::revert-built-in-block-updates :tx-data (distinct tx-data')))
364+
(distinct tx-data'))))
365+
326366
(defn- compute-extra-tx-data
327367
[repo tx-report]
328368
(let [{:keys [db-before db-after tx-data tx-meta]} tx-report
329369
db db-after
370+
revert-tx-data (revert-disallowed-changes tx-report)
330371
fix-page-tags-tx-data (fix-page-tags tx-report)
331372
fix-inline-page-tx-data (fix-inline-built-in-page-classes tx-report)
332373
toggle-page-and-block-tx-data (when (empty? fix-inline-page-tx-data)
@@ -337,7 +378,8 @@
337378
insert-templates-tx (when-not (rtc-tx-or-download-graph? tx-meta)
338379
(insert-tag-templates repo tx-report))
339380
created-by-tx (add-created-by-ref-hook db-before db-after tx-data tx-meta)]
340-
(concat toggle-page-and-block-tx-data
381+
(concat revert-tx-data
382+
toggle-page-and-block-tx-data
341383
display-blocks-tx-data
342384
commands-tx
343385
insert-templates-tx

src/test/frontend/worker/pipeline_test.cljs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
(ns frontend.worker.pipeline-test
22
(:require [cljs.test :refer [deftest is testing]]
3-
[frontend.worker.pipeline :as worker-pipeline]))
3+
[datascript.core :as d]
4+
[frontend.test.helper :as test-helper]
5+
[frontend.worker.pipeline :as worker-pipeline]
6+
[logseq.db :as ldb]
7+
[logseq.db.test.helper :as db-test]))
48

59
(deftest remove-conflict-datoms-test
610
(testing "remove-conflict-datoms (1)"
@@ -36,3 +40,40 @@
3640
[177 :block/refs 136 536871082 true]
3741
[177 :block/refs 21 536871082 true]])
3842
(set (#'worker-pipeline/remove-conflict-datoms datoms)))))))
43+
44+
(deftest test-built-in-page-updates-that-should-be-reverted
45+
(let [graph test-helper/test-db-name-db-version
46+
conn (db-test/create-conn-with-blocks
47+
[{:page {:block/title "page1"}
48+
:blocks [{:block/title "b1"}]}])
49+
library (ldb/get-built-in-page @conn "Library")]
50+
51+
(ldb/register-transact-pipeline-fn!
52+
(fn [tx-report]
53+
(worker-pipeline/transact-pipeline graph tx-report)))
54+
55+
(testing "Using built-in pages as tags"
56+
(let [page-1 (ldb/get-page @conn "page1")
57+
b1 (first (:block/_page page-1))]
58+
(ldb/transact! conn [{:db/id (:db/id b1)
59+
:block/title "b1 #Library"
60+
:block/tags [library]}])
61+
62+
(is (not (ldb/class? library)))
63+
(is (empty? (:block/tags (d/entity @conn (:db/id b1)))))))
64+
65+
(testing "Updating protected properties for built-in nodes"
66+
(ldb/transact! conn [{:db/id (:db/id library)
67+
:block/title "newlibrary"
68+
:db/ident :test/ident}])
69+
(let [library (ldb/get-built-in-page @conn "Library")]
70+
(is (nil? (:db/ident library)))
71+
(is (= "Library" (:block/title library))))
72+
73+
(let [task (d/entity @conn :logseq.class/Task)]
74+
(ldb/transact! conn [{:db/id (:db/id task)
75+
:db/ident :logseq.class/task-new-ident
76+
:block/title "task"}])
77+
(let [task (d/entity @conn (:db/id task))]
78+
(is (= :logseq.class/Task (:db/ident task)))
79+
(is (= "Task" (:block/title task))))))))

0 commit comments

Comments
 (0)