Skip to content

Commit 897d18f

Browse files
committed
enhance(plugin): block properties related apis
1 parent 94f0212 commit 897d18f

File tree

2 files changed

+35
-36
lines changed

2 files changed

+35
-36
lines changed

src/main/logseq/api.cljs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,6 @@
8686
(string/lower-case))
8787
k))
8888

89-
(defn- encode-user-property-name
90-
[k]
91-
(when (string? k)
92-
(-> k (string/trim)
93-
(string/replace "/" "")
94-
(string/replace " " ""))))
95-
9689
;; helpers
9790
(defn ^:export install-plugin-hook
9891
[pid hook ^js opts]
@@ -850,32 +843,11 @@
850843
(editor-handler/expand-block! block-uuid))
851844
nil)))))
852845

853-
(defn convert?to-built-in-property-name
854-
[property-name]
855-
(if (and (not (qualified-keyword? property-name))
856-
(contains? #{:background-color} property-name))
857-
(keyword :logseq.property property-name)
858-
property-name))
859-
860-
;; FIXME: This ns should not be creating idents. This allows for ident conflicts
861-
;; and assumes that names directly map to idents which is incorrect and breaks for multiple
862-
;; cases e.g. a property that has been renamed or sanitized. Instead it should
863-
;; find a property's ident by looking up the property in the db by its title
864-
(defn get-db-ident-for-property-name
865-
"Finds a property :db/ident for a given property name"
866-
[property-name]
867-
(let [property-name' (if (string? property-name)
868-
(keyword property-name) property-name)
869-
property-name' (convert?to-built-in-property-name property-name')]
870-
(if (qualified-keyword? property-name')
871-
property-name'
872-
(keyword "plugin.property" (encode-user-property-name property-name)))))
873-
874846
;; properties (db only)
875847
(defn ^:export get_property
876848
[k]
877849
(when-let [k' (and (string? k) (some-> k (sanitize-user-property-name) (keyword)))]
878-
(p/let [k (if (qualified-keyword? k') k' (get-db-ident-for-property-name k))
850+
(p/let [k (if (qualified-keyword? k') k' (api-block/get-db-ident-for-user-property-name k))
879851
p (db-utils/pull k)]
880852
(bean/->js (sdk-utils/normalize-keyword-for-json p)))))
881853

@@ -892,7 +864,7 @@
892864
(p/let [opts (or (some-> opts (bean/->clj)) {})
893865
name (or (:name opts) (some-> (str k) (string/trim)))
894866
k (if (qualified-keyword? k') k'
895-
(get-db-ident-for-property-name k))
867+
(api-block/get-db-ident-for-user-property-name k))
896868
schema (or (some-> schema (bean/->clj)
897869
(update-keys #(if (contains? #{:hide :public} %)
898870
(keyword (str (name %) "?")) %))) {})
@@ -915,7 +887,7 @@
915887
_ (db-async/<get-block repo block-uuid :children? false)
916888
db? (config/db-based-graph? repo)
917889
key (-> (if (keyword? key) (name keyname) keyname) (util/safe-lower-case))
918-
key (if db? (get-db-ident-for-property-name key) key)
890+
key (if db? (api-block/get-db-ident-for-user-property-name key) key)
919891
_ (when (and db? (not (db-utils/entity key)))
920892
(db-property-handler/upsert-property! key {} {:property-name keyname}))]
921893
(property-handler/set-block-property! repo block-uuid key value))))
@@ -928,7 +900,7 @@
928900
db? (config/db-based-graph? (state/get-current-repo))
929901
key-ns? (and (keyword? key) (namespace key))
930902
key (if key-ns? key (-> (if (keyword? key) (name key) key) (util/safe-lower-case)))
931-
key (if (and db? (not key-ns?)) (get-db-ident-for-property-name key) key)]
903+
key (if (and db? (not key-ns?)) (api-block/get-db-ident-for-user-property-name key) key)]
932904
(property-handler/remove-block-property!
933905
(state/get-current-repo)
934906
block-uuid key))))
@@ -943,7 +915,7 @@
943915
property-name (-> (if (keyword? key) (name key) key) (util/safe-lower-case))
944916
property-value (or (get properties key)
945917
(get properties (keyword property-name))
946-
(get properties (get-db-ident-for-property-name property-name)))
918+
(get properties (api-block/get-db-ident-for-user-property-name property-name)))
947919
property-value (if-let [property-id (:db/id property-value)]
948920
(db/pull property-id) property-value)
949921
ret (sdk-utils/normalize-keyword-for-json property-value)]

src/main/logseq/api/block.cljs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@
2020
[logseq.db :as ldb]
2121
[logseq.sdk.utils :as sdk-utils]))
2222

23+
(defn- encode-user-property-name
24+
[k]
25+
(if (string? k)
26+
(-> k (string/trim)
27+
(string/replace "/" "")
28+
(string/replace " " ""))
29+
k))
30+
31+
(defn convert?to-built-in-property-name
32+
[property-name]
33+
(if (and (not (qualified-keyword? property-name))
34+
(contains? #{:background-color} property-name))
35+
(keyword :logseq.property property-name)
36+
property-name))
37+
38+
;; FIXME: This ns should not be creating idents. This allows for ident conflicts
39+
;; and assumes that names directly map to idents which is incorrect and breaks for multiple
40+
;; cases e.g. a property that has been renamed or sanitized. Instead it should
41+
;; find a property's ident by looking up the property in the db by its title
42+
(defn get-db-ident-for-user-property-name
43+
"Finds a property :db/ident for a given property name"
44+
[property-name]
45+
(let [property-name' (if (string? property-name)
46+
(keyword property-name) property-name)
47+
property-name' (convert?to-built-in-property-name property-name')]
48+
(if (qualified-keyword? property-name')
49+
property-name'
50+
(keyword "plugin.property" (encode-user-property-name property-name)))))
51+
2352
(defn into-readable-db-properties
2453
[properties]
2554
(some-> properties
@@ -71,9 +100,7 @@
71100
[block properties]
72101
(when-let [block-id (and (seq properties) (:db/id block))]
73102
(let [properties (update-keys properties
74-
(fn [k]
75-
(if (qualified-keyword? k) k
76-
(db-property/create-user-property-ident-from-name (name k)))))
103+
(fn [k] (get-db-ident-for-user-property-name k)))
77104
*properties-page-refs (volatile! {})]
78105
(-> (for [ident (keys properties)]
79106
(p/let [ret (infer-property-value-type-to-save! ident (get properties ident))] ret))

0 commit comments

Comments
 (0)