Skip to content

Commit 7f4d8ad

Browse files
committed
add tag-add-property && tag-remove-property
1 parent 0fe1715 commit 7f4d8ad

File tree

2 files changed

+69
-74
lines changed

2 files changed

+69
-74
lines changed

src/main/logseq/api.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
(def ^:export create_tag db-based-api/create-tag)
204204
(def ^:export add_block_tag db-based-api/add-block-tag)
205205
(def ^:export remove_block_tag db-based-api/remove-block-tag)
206+
(def ^:export tag-add-property db-based-api/tag-add-property)
207+
(def ^:export tag-remove-property db-based-api/tag-remove-property)
206208

207209
;; Internal db-based CLI APIs
208210
(def ^:export list_tags cli-based-api/list-tags)

src/main/logseq/api/db_based.cljs

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
[clojure.string :as string]
66
[clojure.walk :as walk]
77
[datascript.core :as d]
8-
[frontend.components.property.value :as pv]
98
[frontend.db :as db]
9+
[frontend.db.async :as db-async]
1010
[frontend.db.model :as db-model]
1111
[frontend.handler.common.page :as page-common-handler]
12-
[logseq.db.frontend.property :as db-property]
12+
[frontend.handler.db-based.page :as db-page-handler]
1313
[frontend.handler.db-based.property :as db-property-handler]
1414
[frontend.handler.editor :as editor-handler]
1515
[frontend.handler.page :as page-handler]
1616
[frontend.modules.layout.core]
17-
[frontend.handler.db-based.page :as db-page-handler]
18-
[frontend.db.async :as db-async]
1917
[frontend.state :as state]
2018
[frontend.util :as util]
2119
[logseq.api.block :as api-block]
@@ -60,12 +58,12 @@
6058
blocks (:blocks result)]
6159
(when (seq blocks)
6260
(p/doseq [block blocks]
63-
(let [id (:block/uuid block)
64-
b (db/entity [:block/uuid id])
65-
properties (when uuid->properties (uuid->properties id))]
66-
(when (seq properties)
67-
(api-block/db-based-save-block-properties! b properties {:plugin this
68-
:schema schema})))))
61+
(let [id (:block/uuid block)
62+
b (db/entity [:block/uuid id])
63+
properties (when uuid->properties (uuid->properties id))]
64+
(when (seq properties)
65+
(api-block/db-based-save-block-properties! b properties {:plugin this
66+
:schema schema})))))
6967
(let [blocks' (map (fn [b] (db/entity [:block/uuid (:block/uuid b)])) blocks)]
7068
(sdk-utils/result->js blocks')))))
7169

@@ -95,10 +93,10 @@
9593
(defn get-property
9694
[k]
9795
(this-as this
98-
(p/let [prop (-get-property this k)
99-
prop' (some-> prop
100-
(assoc :type (:logseq.property/type prop)))]
101-
(sdk-utils/result->js prop'))))
96+
(p/let [prop (-get-property this k)
97+
prop' (some-> prop
98+
(assoc :type (:logseq.property/type prop)))]
99+
(sdk-utils/result->js prop'))))
102100

103101
(defn ->cardinality
104102
[input]
@@ -117,6 +115,32 @@
117115
(when-not (contains? valid-types type)
118116
(throw (ex-info (str "Invalid type, type should be one of: " valid-types) {:type type})))))
119117

118+
(defn- upsert-property-aux
119+
[this k schema opts]
120+
(p/let [k' (api-block/sanitize-user-property-name k)
121+
property-ident (api-block/get-db-ident-from-property-name k' this)
122+
property (db/entity property-ident)]
123+
(if property
124+
property
125+
(p/let [_ (api-block/ensure-property-upsert-control this property-ident k')
126+
schema (or (some-> schema bean/->clj
127+
(update-keys #(if (contains? #{:hide :public} %)
128+
(keyword (str (name %) "?")) %)))
129+
{})
130+
_ (when (:type schema)
131+
(schema-type-check! (keyword (:type schema))))
132+
schema (cond-> schema
133+
(string? (:cardinality schema))
134+
(-> (assoc :db/cardinality (->cardinality (:cardinality schema)))
135+
(dissoc :cardinality))
136+
137+
(string? (:type schema))
138+
(-> (assoc :logseq.property/type (keyword (:type schema)))
139+
(dissoc :type)))
140+
p (db-property-handler/upsert-property! property-ident schema
141+
(assoc opts :property-name k'))]
142+
(db/entity (:db/id p))))))
143+
120144
(defn upsert-property
121145
"schema:
122146
{:type :default | :number | :date | :datetime | :checkbox | :url | :node | :json | :string
@@ -128,37 +152,20 @@
128152
[k ^js schema ^js opts]
129153
(this-as
130154
this
131-
(when-not (string/blank? k)
132-
(p/let [opts (or (some-> opts bean/->clj) {})
133-
k' (api-block/sanitize-user-property-name k)
134-
property-ident (api-block/get-db-ident-from-property-name k' this)
135-
_ (api-block/ensure-property-upsert-control this property-ident k')
136-
schema (or (some-> schema (bean/->clj)
137-
(update-keys #(if (contains? #{:hide :public} %)
138-
(keyword (str (name %) "?")) %))) {})
139-
_ (when (:type schema)
140-
(schema-type-check! (keyword (:type schema))))
141-
schema (cond-> schema
142-
(string? (:cardinality schema))
143-
(-> (assoc :db/cardinality (->cardinality (:cardinality schema)))
144-
(dissoc :cardinality))
145-
146-
(string? (:type schema))
147-
(-> (assoc :logseq.property/type (keyword (:type schema)))
148-
(dissoc :type)))
149-
p (db-property-handler/upsert-property! property-ident schema
150-
(assoc opts :property-name k'))
151-
p (db/entity (:db/id p))]
152-
(sdk-utils/result->js p)))))
155+
(when-not (string/blank? k)
156+
(p/let [opts' (or (some-> opts bean/->clj) {})
157+
schema' (or (some-> schema bean/->clj) {})
158+
property (upsert-property-aux this k schema' opts')]
159+
(sdk-utils/result->js property)))))
153160

154161
(defn remove-property
155162
[k]
156163
(this-as
157164
this
158-
(p/let [property (-get-property this k)]
159-
(when-let [uuid (and (api-block/plugin-property-key? (:db/ident property))
160-
(:block/uuid property))]
161-
(page-common-handler/<delete! uuid nil nil)))))
165+
(p/let [property (-get-property this k)]
166+
(when-let [uuid (and (api-block/plugin-property-key? (:db/ident property))
167+
(:block/uuid property))]
168+
(page-common-handler/<delete! uuid nil nil)))))
162169

163170
(defn upsert-block-property
164171
[this block key' value schema]
@@ -204,40 +211,26 @@
204211
(when tag
205212
(sdk-utils/result->js tag))))
206213

207-
(defn add-tag-class-property [tag-id property-id-or-name & {:keys [schema]}]
208-
(p/let [repo (state/get-current-repo)
209-
tag-entity (db-async/<get-block repo tag-id {:children? false})
210-
class-tag? (some-> tag-entity (ldb/class?))
211-
_ (when-not class-tag? (throw (ex-info (str tag-id " should be class tag") {:value tag-entity})))
212-
property (db-async/<get-block repo property-id-or-name {:children? false})
213-
property? (some-> property (ldb/property?))
214-
property-title (or (:block/title property) property-id-or-name)]
215-
(p/let [property'
216-
(if property?
217-
(do
218-
(when (and (not (ldb/public-built-in-property? property))
219-
(ldb/built-in? property))
220-
(throw (ex-info "This is a private built-in property that can't be used." {:value property})))
221-
property)
222-
;; new property entered or converting page to property
223-
(if (db-property/valid-property-name? property-title)
224-
(p/let [type (or (:type schema) :default)
225-
schema (assoc schema :logseq.property/type type)
226-
opts (cond-> {:property-name property-title}
227-
(and (not property?) (ldb/internal-page? property))
228-
(assoc :properties {:db/id (:db/id property)}))
229-
result (db-property-handler/upsert-property! nil schema opts)
230-
property (db/entity (:db/id result))
231-
_ (when class-tag?
232-
(pv/<add-property! tag-entity (:db/ident property)
233-
"" {:class-schema? true :exit-edit? false}))]
234-
property)
235-
(throw (ex-info "This is an invalid property name." {:value property-title}))))]
236-
(sdk-utils/result->js property')
237-
)))
238-
239-
(defn remove-tag-class-property [tag-id property-id-or-name]
240-
(throw (ex-info "TODO: implement remove-tag-class-property" {tag-id property-id-or-name})))
214+
(defn tag-add-property [tag-id property-id-or-name]
215+
(p/let [tag (db/get-page tag-id)
216+
property (db/get-page property-id-or-name)]
217+
(when-not (ldb/class? tag) (throw (ex-info "Not a valid tag" {:tag tag-id})))
218+
(when-not (ldb/property? property) (throw (ex-info "Not a valid property" {:property property-id-or-name})))
219+
(when (and (not (ldb/public-built-in-property? property))
220+
(ldb/built-in? property))
221+
(throw (ex-info "This is a private built-in property that can't be used." {:value property})))
222+
(p/do!
223+
(db-property-handler/class-add-property! (:db/id tag) (:db/ident property))
224+
(sdk-utils/result->js (db/get-page tag-id)))))
225+
226+
(defn tag-remove-property [tag-id property-id-or-name]
227+
(p/let [tag (db/get-page tag-id)
228+
property (db/get-page property-id-or-name)]
229+
(when-not (ldb/class? tag) (throw (ex-info "Not a valid tag" {:tag tag-id})))
230+
(when-not (ldb/property? property) (throw (ex-info "Not a valid property" {:property property-id-or-name})))
231+
(p/do!
232+
(db-property-handler/class-remove-property! (:db/id tag) (:db/ident property))
233+
(sdk-utils/result->js (db/get-page tag-id)))))
241234

242235
(defn add-block-tag [id-or-name tag-id]
243236
(p/let [repo (state/get-current-repo)

0 commit comments

Comments
 (0)