Browse Source

enhance(api): enhancements & types

charlie 1 year ago
parent
commit
73757c0f21
2 changed files with 21 additions and 7 deletions
  1. 3 2
      libs/src/LSPlugin.ts
  2. 18 5
      src/main/logseq/api.cljs

+ 3 - 2
libs/src/LSPlugin.ts

@@ -452,10 +452,11 @@ export interface IAppProxy {
 
   // graph
   getCurrentGraph: () => Promise<AppGraphInfo | null>
+  checkCurrentIsDbGraph: () => Promise<Boolean>
   getCurrentGraphConfigs: (...keys: string[]) => Promise<any>
   setCurrentGraphConfigs: (configs: {}) => Promise<void>
-  getCurrentGraphFavorites: () => Promise<Array<string> | null>
-  getCurrentGraphRecent: () => Promise<Array<string> | null>
+  getCurrentGraphFavorites: () => Promise<Array<string | PageEntity> | null>
+  getCurrentGraphRecent: () => Promise<Array<string | PageEntity> | null>
   getCurrentGraphTemplates: () => Promise<Record<string, BlockEntity> | null>
 
   // router

+ 18 - 5
src/main/logseq/api.cljs

@@ -71,6 +71,11 @@
                 :else [:block/name (util/page-name-sanity-lc id-or-name)])]
       (db-async/<pull (state/get-current-repo) eid))))
 
+(defn- db-graph?
+  []
+  (some-> (state/get-current-repo)
+    (config/db-based-graph?)))
+
 ;; helpers
 (defn ^:export install-plugin-hook
   [pid hook ^js opts]
@@ -146,15 +151,21 @@
 
 (def ^:export get_current_graph_favorites
   (fn []
-    (some->> (:favorites (state/get-config))
-             (remove string/blank?)
-             (filter string?)
-             (bean/->js))))
+    (if (db-graph?)
+      (-> (page-handler/get-favorites)
+        (p/then #(-> % (sdk-utils/normalize-keyword-for-json) (bean/->js))))
+      (some->> (:favorites (state/get-config))
+        (remove string/blank?)
+        (filter string?)
+        (bean/->js)))))
 
 (def ^:export get_current_graph_recent
   (fn []
     (some->> (recent-handler/get-recent-pages)
-             (bean/->js))))
+      (map #(db-utils/entity (:db/id %)))
+      (remove nil?)
+      (sdk-utils/normalize-keyword-for-json)
+      (bean/->js))))
 
 (def ^:export get_current_graph_templates
   (fn []
@@ -172,6 +183,8 @@
                     :name (util/node-path.basename repo)
                     :path (config/get-repo-dir repo)})))))
 
+(def ^:export check_current_is_db_graph db-graph?)
+
 (def ^:export show_themes
   (fn []
     (plugins/open-select-theme!)))