Selaa lähdekoodia

enhance(apis): enhance createTag function to accept options for custom UUID

charlie 1 kuukausi sitten
vanhempi
sitoutus
bd4b022a08
3 muutettua tiedostoa jossa 18 lisäystä ja 7 poistoa
  1. 1 1
      libs/src/LSPlugin.ts
  2. 11 6
      src/main/logseq/api/db_based.cljs
  3. 6 0
      src/main/logseq/sdk/utils.cljs

+ 1 - 1
libs/src/LSPlugin.ts

@@ -780,7 +780,7 @@ export interface IEditorProxy extends Record<string, any> {
   getAllTags: () => Promise<PageEntity[] | null>
   getAllProperties: () => Promise<PageEntity[] | null>
   getTagObjects: (PageIdentity) => Promise<BlockEntity[] | null>
-  createTag: (tagName: string) => Promise<PageEntity | null>
+  createTag: (tagName: string, opts?: Partial<{ uuid: string }>) => Promise<PageEntity | null>
   addTagProperty: (tagId: BlockIdentity, propertyIdOrName: BlockIdentity) => Promise<void>
   removeTagProperty: (tagId: BlockIdentity, propertyIdOrName: BlockIdentity) => Promise<void>
   addBlockTag: (blockId: BlockIdentity, tagId: BlockIdentity) => Promise<void>

+ 11 - 6
src/main/logseq/api/db_based.cljs

@@ -204,12 +204,17 @@
                                               (:db/id class))]
         (sdk-utils/result->js result)))))
 
-(defn create-tag [title]
-  (p/let [repo (state/get-current-repo)
-          tag (db-page-handler/<create-class! title {:redirect? false})
-          tag (db-async/<get-block repo (:db/id tag) {:children? false})]
-    (when tag
-      (sdk-utils/result->js tag))))
+(defn create-tag [title ^js opts]
+  (let [opts (bean/->clj opts)]
+    (p/let [repo (state/get-current-repo)
+            tag (db-page-handler/<create-class!
+                 title
+                 (-> opts
+                     (sdk-utils/with-custom-uuid)
+                     (assoc :redirect? false)))
+            tag (db-async/<get-block repo (:db/id tag) {:children? false})]
+      (when tag
+        (sdk-utils/result->js tag)))))
 
 (defn tag-add-property [tag-id property-id-or-name]
   (p/let [tag (db/get-page tag-id)

+ 6 - 0
src/main/logseq/sdk/utils.cljs

@@ -101,6 +101,12 @@
       normalize-keyword-for-json
       bean/->js))
 
+(defn with-custom-uuid [opts]
+  (let [custom-uuid (or (:customUUID opts) (:uuid opts))]
+    (cond-> opts
+            (util/uuid-string? custom-uuid)
+            (assoc :uuid (uuid custom-uuid)))))
+
 (def ^:export to-clj bean/->clj)
 (def ^:export jsx-to-clj jsx->clj)
 (def ^:export to-js bean/->js)