Skip to content

Commit

Permalink
update property handler
Browse files Browse the repository at this point in the history
  • Loading branch information
RCmerci committed Jun 21, 2023
1 parent 7a6972b commit ff57204
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
52 changes: 18 additions & 34 deletions src/main/frontend/handler/property.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
28 changes: 2 additions & 26 deletions src/main/frontend/util/property_edit.cljs
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -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)}]))

0 comments on commit ff57204

Please sign in to comment.