diff --git a/src/main/frontend/handler/page.cljs b/src/main/frontend/handler/page.cljs index e8600573e2..ecea316b02 100644 --- a/src/main/frontend/handler/page.cljs +++ b/src/main/frontend/handler/page.cljs @@ -11,7 +11,6 @@ [frontend.fs :as fs] [frontend.handler.common :as common-handler] [frontend.handler.common.page :as page-common-handler] - [frontend.handler.config :as config-handler] [frontend.handler.editor :as editor-handler] [frontend.handler.plugin :as plugin-handler] [frontend.handler.notification :as notification] @@ -248,6 +247,14 @@ (let [current-selected (util/get-selected-text)] (cursor/move-cursor-forward input (+ 2 (count current-selected)))))) +(defn add-tag [repo block-id tag & {:keys [tag-entity]}] + (let [tag-entity (or tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)])) + tx-data [[:db/add (:db/id tag-entity) :block/type "class"] + [:db/add [:block/uuid block-id] :block/tags (:db/id tag-entity)] + ;; TODO: Should classes counted as refs + [:db/add [:block/uuid block-id] :block/refs (:db/id tag-entity)]]] + (db/transact! repo tx-data {:outliner-op :save-block}))) + (defn on-chosen-handler [input id _q pos format] (let [current-pos (cursor/pos input) @@ -292,13 +299,7 @@ :class? class?})) tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)])] (when class? - (let [repo (state/get-current-repo) - tag-entity (or tag-entity (db/entity [:block/name (util/page-name-sanity-lc tag)])) - tx-data [[:db/add (:db/id tag-entity) :block/type "class"] - [:db/add [:block/uuid (:block/uuid edit-block)] :block/tags (:db/id tag-entity)] - ;; TODO: Should classes counted as refs - [:db/add [:block/uuid (:block/uuid edit-block)] :block/refs (:db/id tag-entity)]]] - (db/transact! repo tx-data {:outliner-op :save-block}))))))) + (add-tag (state/get-current-repo) (:block/uuid edit-block) tag {:tag-entity tag-entity})))))) (editor-handler/insert-command! id (str "#" wrapped-tag) format diff --git a/src/test/frontend/db/db_based_model_test.cljs b/src/test/frontend/db/db_based_model_test.cljs index cbc8029e70..32a1797c28 100644 --- a/src/test/frontend/db/db_based_model_test.cljs +++ b/src/test/frontend/db/db_based_model_test.cljs @@ -5,8 +5,7 @@ [frontend.test.helper :as test-helper] [datascript.core :as d] [frontend.handler.db-based.property :as db-property-handler] - [frontend.handler.page :as page-handler] - [frontend.handler.editor :as editor-handler])) + [frontend.handler.page :as page-handler])) (def repo test-helper/test-db-name-db-version) @@ -52,23 +51,23 @@ (is (= ["class1" "class2"] (map first (model/get-all-classes repo)))))) (deftest get-class-objects-test - (let [opts {:redirect? false :create-first-block? false :class? true} - _ (page-handler/create! "class1" opts) - class (db/entity [:block/name "class1"]) - _ (editor-handler/save-block! repo fbid "Block 1 #class1")] - (is (= (model/get-class-objects repo (:db/id class)) - [(:db/id (db/entity [:block/uuid fbid]))])) - - (testing "namespace classes" - (page-handler/create! "class2" opts) - ;; set class2's parent to class1 - (let [class2 (db/entity [:block/name "class2"])] - (db/transact! [{:db/id (:db/id class2) - :block/namespace (:db/id class)}])) - (editor-handler/save-block! repo sbid "Block 2 #class2") + (let [opts {:redirect? false :create-first-block? false :class? true} + _ (page-handler/create! "class1" opts) + class (db/entity [:block/name "class1"]) + _ (test-helper/save-block! repo fbid "Block 1" {:tags ["class1"]})] (is (= (model/get-class-objects repo (:db/id class)) - [(:db/id (db/entity [:block/uuid fbid])) - (:db/id (db/entity [:block/uuid sbid]))]))))) + [(:db/id (db/entity [:block/uuid fbid]))])) + + (testing "namespace classes" + (page-handler/create! "class2" opts) + ;; set class2's parent to class1 + (let [class2 (db/entity [:block/name "class2"])] + (db/transact! [{:db/id (:db/id class2) + :block/namespace (:db/id class)}])) + (test-helper/save-block! repo sbid "Block 2" {:tags ["class2"]}) + (is (= (model/get-class-objects repo (:db/id class)) + [(:db/id (db/entity [:block/uuid fbid])) + (:db/id (db/entity [:block/uuid sbid]))]))))) (deftest get-classes-with-property-test (let [opts {:redirect? false :create-first-block? false :class? true} @@ -86,8 +85,8 @@ (deftest get-tag-blocks-test (let [opts {:redirect? false :create-first-block? false :class? true} _ (page-handler/create! "class1" opts) - _ (editor-handler/save-block! repo fbid "Block 1 #class1") - _ (editor-handler/save-block! repo sbid "Block 2 #class1")] + _ (test-helper/save-block! repo fbid "Block 1" {:tags ["class1"]}) + _ (test-helper/save-block! repo sbid "Block 2" {:tags ["class1"]})] (is (= (model/get-tag-blocks repo "class1") [(:db/id (db/entity [:block/uuid fbid])) diff --git a/src/test/frontend/handler/db_based/property_test.cljs b/src/test/frontend/handler/db_based/property_test.cljs index 77cc307ac3..51085a8826 100644 --- a/src/test/frontend/handler/db_based/property_test.cljs +++ b/src/test/frontend/handler/db_based/property_test.cljs @@ -202,8 +202,8 @@ (db-property-handler/class-remove-property! repo c1id (:block/uuid (db/entity [:block/name "property-1"]))) (is (= 1 (count (:properties (:block/schema (db/entity (:db/id c1)))))))) (testing "Add classes to a block" - (editor-handler/save-block! repo fbid "Block 1 #class1 #class2 #class3") - (is (= 3 (count (:block/tags (db/entity [:block/uuid fbid])))))) + (test-helper/save-block! repo fbid "Block 1" {:tags ["class1" "class2" "class3"]}) + (is (= 3 (count (:block/tags (db/entity [:block/uuid fbid])))))) ;; FIXME: @tiensonqin https://github.com/logseq/logseq/commit/575624c650b2b7e919033a79aa5d14b97507d86f #_(testing "Remove a class from a block" ;; make sure class2 will not be deleted when removing it from the first block diff --git a/src/test/frontend/test/helper.cljs b/src/test/frontend/test/helper.cljs index b56cee2459..6ea5a4b641 100644 --- a/src/test/frontend/test/helper.cljs +++ b/src/test/frontend/test/helper.cljs @@ -8,6 +8,8 @@ [logseq.db.sqlite.util :as sqlite-util] [frontend.db :as db] [frontend.date :as date] + [frontend.handler.editor :as editor-handler] + [frontend.handler.page :as page-handler] [datascript.core :as d] [logseq.graph-parser.text :as text])) @@ -47,8 +49,8 @@ [property-lines] (->> property-lines (keep #(let [[k v] (string/split % #"::\s*" 2)] - (when (string/includes? % "::") - [(keyword k) (parse-property-value v)]))) + (when (string/includes? % "::") + [(keyword k) (parse-property-value v)]))) (into {}))) (defn- build-block-properties @@ -256,3 +258,10 @@ This can be called in synchronous contexts as no async fns should be invoked" (start-test-db!)) :after #(do (state/set-current-repo! nil) (destroy-test-db!))}) + +(defn save-block! + "Wrapper around editor-handler/save-block! that also adds tags" + [repo block-uuid content {:keys [tags]}] + (editor-handler/save-block! repo block-uuid content) + (doseq [tag tags] + (page-handler/add-tag repo block-uuid tag))) \ No newline at end of file