Browse Source

enhance(plugin): api types for the property entity

charlie 1 year ago
parent
commit
fccef5e904
2 changed files with 32 additions and 3 deletions
  1. 21 1
      libs/src/LSPlugin.ts
  2. 11 2
      src/main/logseq/api.cljs

+ 21 - 1
libs/src/LSPlugin.ts

@@ -798,6 +798,26 @@ export interface IEditorProxy extends Record<string, any> {
 
   saveFocusedCodeEditorContent: () => Promise<void>
 
+  // property entity related APIs (DB only)
+  getProperty: (key: string) => Promise<BlockEntity | null>
+
+  /**
+   * insert or update property entity
+   * @param key
+   * @param schema
+   * @param opts
+   */
+  upsertProperty: (
+    key: string,
+    schema?: Partial<{
+      type: 'default' | 'map' | 'keyword' | 'date' | 'checkbox' | string,
+      cardinality: 'many' | 'one',
+      hide: boolean
+      public: boolean
+    }>,
+    opts?: {}) => Promise<IEntityID>
+
+  // block property related APIs
   upsertBlockProperty: (
     block: BlockIdentity,
     key: string,
@@ -806,7 +826,7 @@ export interface IEditorProxy extends Record<string, any> {
 
   removeBlockProperty: (block: BlockIdentity, key: string) => Promise<void>
 
-  getBlockProperty: (block: BlockIdentity, key: string) => Promise<BlockEntity | string| null>
+  getBlockProperty: (block: BlockIdentity, key: string) => Promise<BlockEntity | string | null>
 
   getBlockProperties: (block: BlockIdentity) => Promise<Record<string, any> | null>
 

+ 11 - 2
src/main/logseq/api.cljs

@@ -875,11 +875,20 @@
       (bean/->js (sdk-utils/normalize-keyword-for-json p)))))
 
 (defn ^:export upsert_property
+  "schema:
+    {:type :default | :keyword | :map | :date | :checkbox
+     :cardinality :many | :one
+     :hide? true
+     :view-context :page
+     :public? false}
+  "
   [k ^js schema ^js opts]
   (when-let [k' (and (string? k) (keyword k))]
     (p/let [k (if (qualified-keyword? k') k'
-                  (get-db-ident-for-property-name k))
-            schema (or (and schema (bean/->clj schema)) {})
+                (get-db-ident-for-property-name k))
+            schema (or (some-> schema (bean/->clj)
+                         (update-keys #(if (contains? #{:hide :public} %)
+                                         (keyword (str (name %) "?")) %))) {})
             schema (cond-> schema
                      (string? (:cardinality schema))
                      (update :cardinality keyword)