Skip to content

Commit

Permalink
enhance(plugin): block properties related apis
Browse files Browse the repository at this point in the history
  • Loading branch information
xyhp915 committed Nov 16, 2024
1 parent 94f0212 commit 897d18f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
38 changes: 5 additions & 33 deletions src/main/logseq/api.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,6 @@
(string/lower-case))
k))

(defn- encode-user-property-name
[k]
(when (string? k)
(-> k (string/trim)
(string/replace "/" "")
(string/replace " " ""))))

;; helpers
(defn ^:export install-plugin-hook
[pid hook ^js opts]
Expand Down Expand Up @@ -850,32 +843,11 @@
(editor-handler/expand-block! block-uuid))
nil)))))

(defn convert?to-built-in-property-name
[property-name]
(if (and (not (qualified-keyword? property-name))
(contains? #{:background-color} property-name))
(keyword :logseq.property property-name)
property-name))

;; FIXME: This ns should not be creating idents. This allows for ident conflicts
;; and assumes that names directly map to idents which is incorrect and breaks for multiple
;; cases e.g. a property that has been renamed or sanitized. Instead it should
;; find a property's ident by looking up the property in the db by its title
(defn get-db-ident-for-property-name
"Finds a property :db/ident for a given property name"
[property-name]
(let [property-name' (if (string? property-name)
(keyword property-name) property-name)
property-name' (convert?to-built-in-property-name property-name')]
(if (qualified-keyword? property-name')
property-name'
(keyword "plugin.property" (encode-user-property-name property-name)))))

;; properties (db only)
(defn ^:export get_property
[k]
(when-let [k' (and (string? k) (some-> k (sanitize-user-property-name) (keyword)))]
(p/let [k (if (qualified-keyword? k') k' (get-db-ident-for-property-name k))
(p/let [k (if (qualified-keyword? k') k' (api-block/get-db-ident-for-user-property-name k))
p (db-utils/pull k)]
(bean/->js (sdk-utils/normalize-keyword-for-json p)))))

Expand All @@ -892,7 +864,7 @@
(p/let [opts (or (some-> opts (bean/->clj)) {})
name (or (:name opts) (some-> (str k) (string/trim)))
k (if (qualified-keyword? k') k'
(get-db-ident-for-property-name k))
(api-block/get-db-ident-for-user-property-name k))
schema (or (some-> schema (bean/->clj)
(update-keys #(if (contains? #{:hide :public} %)
(keyword (str (name %) "?")) %))) {})
Expand All @@ -915,7 +887,7 @@
_ (db-async/<get-block repo block-uuid :children? false)
db? (config/db-based-graph? repo)
key (-> (if (keyword? key) (name keyname) keyname) (util/safe-lower-case))
key (if db? (get-db-ident-for-property-name key) key)
key (if db? (api-block/get-db-ident-for-user-property-name key) key)
_ (when (and db? (not (db-utils/entity key)))
(db-property-handler/upsert-property! key {} {:property-name keyname}))]
(property-handler/set-block-property! repo block-uuid key value))))
Expand All @@ -928,7 +900,7 @@
db? (config/db-based-graph? (state/get-current-repo))
key-ns? (and (keyword? key) (namespace key))
key (if key-ns? key (-> (if (keyword? key) (name key) key) (util/safe-lower-case)))
key (if (and db? (not key-ns?)) (get-db-ident-for-property-name key) key)]
key (if (and db? (not key-ns?)) (api-block/get-db-ident-for-user-property-name key) key)]
(property-handler/remove-block-property!
(state/get-current-repo)
block-uuid key))))
Expand All @@ -943,7 +915,7 @@
property-name (-> (if (keyword? key) (name key) key) (util/safe-lower-case))
property-value (or (get properties key)
(get properties (keyword property-name))
(get properties (get-db-ident-for-property-name property-name)))
(get properties (api-block/get-db-ident-for-user-property-name property-name)))
property-value (if-let [property-id (:db/id property-value)]
(db/pull property-id) property-value)
ret (sdk-utils/normalize-keyword-for-json property-value)]
Expand Down
33 changes: 30 additions & 3 deletions src/main/logseq/api/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@
[logseq.db :as ldb]
[logseq.sdk.utils :as sdk-utils]))

(defn- encode-user-property-name
[k]
(if (string? k)
(-> k (string/trim)
(string/replace "/" "")
(string/replace " " ""))
k))

(defn convert?to-built-in-property-name
[property-name]
(if (and (not (qualified-keyword? property-name))
(contains? #{:background-color} property-name))
(keyword :logseq.property property-name)
property-name))

;; FIXME: This ns should not be creating idents. This allows for ident conflicts
;; and assumes that names directly map to idents which is incorrect and breaks for multiple
;; cases e.g. a property that has been renamed or sanitized. Instead it should
;; find a property's ident by looking up the property in the db by its title
(defn get-db-ident-for-user-property-name
"Finds a property :db/ident for a given property name"
[property-name]
(let [property-name' (if (string? property-name)
(keyword property-name) property-name)
property-name' (convert?to-built-in-property-name property-name')]
(if (qualified-keyword? property-name')
property-name'
(keyword "plugin.property" (encode-user-property-name property-name)))))

(defn into-readable-db-properties
[properties]
(some-> properties
Expand Down Expand Up @@ -71,9 +100,7 @@
[block properties]
(when-let [block-id (and (seq properties) (:db/id block))]
(let [properties (update-keys properties
(fn [k]
(if (qualified-keyword? k) k
(db-property/create-user-property-ident-from-name (name k)))))
(fn [k] (get-db-ident-for-user-property-name k)))
*properties-page-refs (volatile! {})]
(-> (for [ident (keys properties)]
(p/let [ret (infer-property-value-type-to-save! ident (get properties ident))] ret))
Expand Down

0 comments on commit 897d18f

Please sign in to comment.