Răsfoiți Sursa

improve(plugin): support to get app info from api

charlie 3 ani în urmă
părinte
comite
80bc3f0337
4 a modificat fișierele cu 35 adăugiri și 4 ștergeri
  1. 1 1
      libs/src/LSPlugin.caller.ts
  2. 12 1
      libs/src/LSPlugin.ts
  3. 14 2
      libs/src/LSPlugin.user.ts
  4. 8 0
      src/main/logseq/api.cljs

+ 1 - 1
libs/src/LSPlugin.caller.ts

@@ -159,7 +159,7 @@ class LSPluginCaller extends EventEmitter {
             payload._sync = tag
 
             actor.setTag(`async call #${tag}`)
-            debug('async call #', tag)
+            debug(`async call #${tag}`)
           }
 
           refParent.emit(LSPMSGFn(model.baseInfo.id), { type, payload })

+ 12 - 1
libs/src/LSPlugin.ts

@@ -112,6 +112,12 @@ export interface AppUserInfo {
   [key: string]: any
 }
 
+export interface AppInfo {
+  version: string
+
+  [key: string]: any
+}
+
 /**
  * User's app configurations
  */
@@ -271,7 +277,12 @@ export type UserProxyTags = 'app' | 'editor' | 'db' | 'git' | 'ui' | 'assets'
  * App level APIs
  */
 export interface IAppProxy {
-  // base
+  /**
+   * @add 0.0.4
+   * @param key
+   */
+  getInfo: (key?: keyof AppInfo) => Promise<AppInfo | any>
+
   getUserInfo: () => Promise<AppUserInfo | null>
   getUserConfigs: () => Promise<AppUserConfigs>
 

+ 14 - 2
libs/src/LSPlugin.user.ts

@@ -32,6 +32,7 @@ import {
   BlockEntity,
   IDatom,
   IAssetsProxy,
+  AppInfo,
 } from './LSPlugin'
 import Debug from 'debug'
 import * as CSS from 'csstype'
@@ -85,7 +86,18 @@ function registerSimpleCommand(
   })
 }
 
+let _appBaseInfo: AppInfo = null
 const app: Partial<IAppProxy> = {
+  async getInfo(
+    this: LSPluginUser,
+    key
+  ) {
+    if (!_appBaseInfo) {
+      _appBaseInfo = await this._execCallableAPIAsync('get-app-info')
+    }
+    return typeof key === 'string' ? _appBaseInfo[key] : _appBaseInfo
+  },
+
   registerCommand: registerSimpleCommand,
 
   registerCommandPalette(
@@ -166,7 +178,7 @@ const app: Partial<IAppProxy> = {
     } else {
       flag ? sf(true) : sf()
     }
-  },
+  }
 }
 
 let registeredCmdUid = 0
@@ -556,7 +568,7 @@ export class LSPluginUser
         return function (this: any, ...args: any) {
           if (origMethod) {
             const ret = origMethod.apply(that, args.concat(tag))
-            if (ret !== PROXY_CONTINUE) return
+            if (ret !== PROXY_CONTINUE) return ret
           }
 
           // Handle hook

+ 8 - 0
src/main/logseq/api.cljs

@@ -36,6 +36,7 @@
             [promesa.core :as p]
             [reitit.frontend.easy :as rfe]
             [sci.core :as sci]
+            [frontend.version :as fv]
             [frontend.handler.shell :as shell]
             [frontend.modules.layout.core]))
 
@@ -82,6 +83,13 @@
                  (keyword %)))
          (get-in @state/state))))
 
+(defn ^:export get_app_info
+  ;; get app base info
+  []
+  (bean/->js
+    (normalize-keyword-for-json
+      {:version fv/version})))
+
 (def ^:export get_user_configs
   (fn []
     (bean/->js