Skip to content

Commit 44468dd

Browse files
committed
enhance(apis): simplify tag resolution and enhance tag handling functions
1 parent 5f42c51 commit 44468dd

1 file changed

Lines changed: 43 additions & 32 deletions

File tree

src/main/logseq/api/db_based.cljs

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,27 @@
258258
:logseq.property.class/extends
259259
(:db/id extend))))
260260

261+
(defn- resolve-tag-eid [this class-uuid-or-ident-or-title]
262+
(let [eid (if (number? class-uuid-or-ident-or-title)
263+
class-uuid-or-ident-or-title
264+
(let [title-or-ident (-> (if-not (string? class-uuid-or-ident-or-title)
265+
(str class-uuid-or-ident-or-title)
266+
class-uuid-or-ident-or-title)
267+
(string/replace #"^:+" ""))]
268+
(if (text/namespace-page? title-or-ident)
269+
(keyword title-or-ident)
270+
(if (util/uuid-string? title-or-ident)
271+
(when-let [id (sdk-utils/uuid-or-throw-error title-or-ident)]
272+
[:block/uuid id])
273+
(keyword (api-block/resolve-class-prefix-for-db this) title-or-ident)))))]
274+
eid))
275+
261276
(defn get-tag [class-uuid-or-ident-or-title]
262277
(this-as this
263-
(let [eid (if (number? class-uuid-or-ident-or-title)
264-
class-uuid-or-ident-or-title
265-
(let [title-or-ident (-> (if-not (string? class-uuid-or-ident-or-title)
266-
(str class-uuid-or-ident-or-title)
267-
class-uuid-or-ident-or-title)
268-
(string/replace #"^:+" ""))]
269-
(if (text/namespace-page? title-or-ident)
270-
(keyword title-or-ident)
271-
(if (util/uuid-string? title-or-ident)
272-
(when-let [id (sdk-utils/uuid-or-throw-error title-or-ident)]
273-
[:block/uuid id])
274-
(keyword (api-block/resolve-class-prefix-for-db this) title-or-ident)))))
275-
tag (db/entity eid)]
276-
(when (ldb/class? tag)
277-
(sdk-utils/result->js tag)))))
278+
(let [eid (resolve-tag-eid this class-uuid-or-ident-or-title)
279+
tag (db/entity eid)]
280+
(when (ldb/class? tag)
281+
(sdk-utils/result->js tag)))))
278282

279283
(defn get-tags-by-name [name]
280284
(when-let [tags (some->> (entity-util/get-pages-by-name (db-conn/get-db) name)
@@ -304,25 +308,31 @@
304308
(sdk-utils/result->js (db/get-case-page tag-id)))))
305309

306310
(defn add-block-tag [id-or-name tag-id]
307-
(p/let [repo (state/get-current-repo)
308-
tag (db-async/<get-block repo tag-id)
309-
block (db-async/<get-block repo id-or-name)]
310-
(when-not (ldb/class? tag)
311-
(throw (ex-info (str "Not a tag: " tag-id)
312-
{:tag tag})))
313-
(when (and tag block)
314-
(db-page-handler/add-tag repo (:db/id block) tag))))
311+
(this-as this
312+
(p/let [repo (state/get-current-repo)
313+
block (db-async/<get-block repo id-or-name)
314+
tag-eid (resolve-tag-eid this tag-id)
315+
tag (or (db/entity tag-eid)
316+
(db-async/<get-block repo tag-id))]
317+
(when-not (ldb/class? tag)
318+
(throw (ex-info (str "Not a tag: " tag-id)
319+
{:tag (pr-str tag)})))
320+
(when (and tag block)
321+
(db-page-handler/add-tag repo (:db/id block) tag)))))
315322

316323
(defn remove-block-tag [id-or-name tag-id]
317-
(p/let [repo (state/get-current-repo)
318-
block (db-async/<get-block repo id-or-name)
319-
tag (db-async/<get-block repo tag-id)]
320-
(when-not (ldb/class? tag)
321-
(throw (ex-info (str "Not a tag: " tag-id)
322-
{:tag tag})))
323-
(when (and block tag)
324-
(db-property-handler/delete-property-value!
325-
(:db/id block) :block/tags (:db/id tag)))))
324+
(this-as this
325+
(p/let [repo (state/get-current-repo)
326+
block (db-async/<get-block repo id-or-name)
327+
tag-eid (resolve-tag-eid this tag-id)
328+
tag (or (db/entity tag-eid)
329+
(db-async/<get-block repo tag-id))]
330+
(when-not (ldb/class? tag)
331+
(throw (ex-info (str "Not a tag: " tag-id)
332+
{:tag tag})))
333+
(when (and block tag)
334+
(db-property-handler/delete-property-value!
335+
(:db/id block) :block/tags (:db/id tag))))))
326336

327337
(defn set-block-icon
328338
[block-id icon-type icon-name]
@@ -342,6 +352,7 @@
342352
:id (if (= icon-type "emoji")
343353
(:id (first (name->emoji icon-name)))
344354
icon-name)})))
355+
345356
(defn remove-block-icon
346357
[block-id]
347358
(p/let [repo (state/get-current-repo)

0 commit comments

Comments
 (0)