From ad3242b1e45ff4f49765cc5029af81998d84e265 Mon Sep 17 00:00:00 2001 From: rcmerci Date: Wed, 21 Jun 2023 13:37:37 +0800 Subject: [PATCH] update property handler --- src/main/frontend/handler/property.cljs | 52 ++++++++--------------- src/main/frontend/util/property_edit.cljs | 28 +----------- 2 files changed, 20 insertions(+), 60 deletions(-) diff --git a/src/main/frontend/handler/property.cljs b/src/main/frontend/handler/property.cljs index 4428c5dfc24..d28399bb6d5 100644 --- a/src/main/frontend/handler/property.cljs +++ b/src/main/frontend/handler/property.cljs @@ -10,45 +10,29 @@ [logseq.graph-parser.util :as gp-util] [logseq.graph-parser.util.page-ref :as page-ref] [frontend.modules.outliner.core :as outliner-core] - [frontend.modules.outliner.transaction :as outliner-tx] - [frontend.util.property-edit :as property-edit])) + [frontend.modules.outliner.transaction :as outliner-tx])) (defn add-property! - [repo block k v] - (let [tx-data (property-edit/insert-property-when-db-based repo block k v)] + [repo block k-name v] + (let [property-class (db/pull repo '[*] [:property/name k-name]) + property-class-uuid (or (:block/uuid property-class) (random-uuid)) + tx-data (cond-> [] + (nil? property-class) (conj {:property/schema {} + :property/name k-name + :block/uuid property-class-uuid + :block/type "property"}) + true (conj {:block/uuid (:block/uuid block) + :block/properties (assoc (:block/properties block) (str property-class-uuid) v)}))] (db/transact! repo tx-data))) (defn remove-property! - [repo block k] - (let [tx-data (property-edit/remove-property-when-db-based block k)] - (db/transact! repo tx-data))) - - -;; (defn add-property! -;; [block-db-id key] -;; (let [block (db/pull block-db-id) -;; key (string/trim key) -;; key-name (util/page-name-sanity-lc key) -;; property (db/entity [:block/name key-name])] -;; (when-not (or -;; (= (:block/name block) key-name) -;; (and property -;; (or -;; (= (:block/type property) "tag") -;; (= (:db/id property) (:db/id block))))) -;; (let [property-uuid (db/new-block-id)] -;; (db/transact! (state/get-current-repo) -;; [ -;; ;; property -;; {:block/uuid property-uuid -;; :block/type "property" -;; :block/property-schema {:type "any"} -;; :block/original-name key -;; :block/name key-name} - -;; {:block/uuid (:block/uuid block) -;; :block/properties (assoc (:block/properties block) -;; property-uuid "")}]))))) + [repo block k-uuid-or-builtin-k-name] + {:pre (string? k-uuid-or-builtin-k-name)} + (let [origin-properties (:block/properties block)] + (assert (contains? (set (keys origin-properties)) k-uuid-or-builtin-k-name)) + (db/transact! repo + [{:block/uuid (:block/uuid block) + :block/properties (dissoc origin-properties k-uuid-or-builtin-k-name)}]))) (defn- extract-refs [entity properties] diff --git a/src/main/frontend/util/property_edit.cljs b/src/main/frontend/util/property_edit.cljs index b0e6e359dfb..82ad36a9ce0 100644 --- a/src/main/frontend/util/property_edit.cljs +++ b/src/main/frontend/util/property_edit.cljs @@ -1,8 +1,7 @@ (ns frontend.util.property-edit - "Property related fns, both file-based and db-based version need to be considered." + "Wrappers for fns in `frontend.util.property`, do nothing when repo is db-based" (:require [frontend.util.property :as property] - [frontend.config :as config] - [frontend.db :as db])) + [frontend.config :as config])) ;; Why need these XXX-when-file-based fns? @@ -65,26 +64,3 @@ (def property-key-exist?-when-file-based property/property-key-exist?) (def goto-properties-end-when-file-based property/goto-properties-end) (def front-matter?-when-file-based property/front-matter?) - - -(defn insert-property-when-db-based - "return tx-data" - [repo block k-name v] - (let [property-class (db/pull repo '[*] [:property/name k-name]) - property-class-uuid (or (:block/uuid property-class) (random-uuid))] - (cond-> [] - (nil? property-class) (conj {:property/schema {} - :property/name k-name - :block/uuid property-class-uuid - :block/type "property"}) - true (conj {:block/uuid (:block/uuid block) - :block/properties (assoc (:block/properties block) (str property-class-uuid) v)})))) - -(defn remove-property-when-db-based - "return tx-data" - [block k-uuid-or-builtin-k-name] - {:pre (string? k-uuid-or-builtin-k-name)} - (let [origin-properties (:block/properties block)] - (assert (contains? (set (keys origin-properties)) k-uuid-or-builtin-k-name)) - [{:block/uuid (:block/uuid block) - :block/properties (dissoc origin-properties k-uuid-or-builtin-k-name)}]))