瀏覽代碼

enhance(libs): utils apis

charlie 9 月之前
父節點
當前提交
99998e25e9
共有 3 個文件被更改,包括 32 次插入9 次删除
  1. 5 1
      libs/src/LSPlugin.ts
  2. 11 5
      libs/src/LSPlugin.user.ts
  3. 16 3
      libs/src/modules/LSPlugin.Experiments.ts

+ 5 - 1
libs/src/LSPlugin.ts

@@ -315,7 +315,7 @@ export type ExternalCommandType =
   | 'logseq.ui/toggle-theme'
   | 'logseq.ui/toggle-wide-mode'
 
-export type UserProxyTags = 'app' | 'editor' | 'db' | 'git' | 'ui' | 'assets'
+export type UserProxyTags = 'app' | 'editor' | 'db' | 'git' | 'ui' | 'assets' | 'utils'
 
 export type SearchIndiceInitStatus = boolean
 export type SearchBlockItem = {
@@ -937,6 +937,10 @@ export interface IUIProxy {
   resolveThemeCssPropsVals: (props: string | Array<string>) => Promise<Record<string, string | undefined> | null>
 }
 
+export interface IUtilsProxy {
+  toJs: <R = unknown>(obj: {}) => Promise<R>
+}
+
 /**
  * Assets related APIs
  */

+ 11 - 5
libs/src/LSPlugin.user.ts

@@ -37,7 +37,7 @@ import {
   IAssetsProxy,
   AppInfo,
   IPluginSearchServiceHooks,
-  PageEntity,
+  PageEntity, IUtilsProxy,
 } from './LSPlugin'
 import Debug from 'debug'
 import * as CSS from 'csstype'
@@ -454,6 +454,8 @@ const git: Partial<IGitProxy> = {}
 
 const ui: Partial<IUIProxy> = {}
 
+const utils: Partial<IUtilsProxy> = {}
+
 const assets: Partial<IAssetsProxy> = {
   makeSandboxStorage(this: LSPluginUser): IAsyncStorage {
     return new LSPluginFileStorage(this, { assets: true })
@@ -793,7 +795,8 @@ export class LSPluginUser
 
           let method = propKey as string
 
-          if ((['git', 'ui', 'assets'] as UserProxyTags[]).includes(tag)) {
+          // TODO: refactor api call with the explicit tag
+          if ((['git', 'ui', 'assets', 'utils'] as UserProxyTags[]).includes(tag)) {
             method = tag + '_' + method
           }
 
@@ -831,10 +834,8 @@ export class LSPluginUser
   #editorProxy: IEditorProxy
   #dbProxy: IDBProxy
   #uiProxy: IUIProxy
+  #utilsProxy: IUtilsProxy
 
-  /**
-   * The interface methods of {@link IAppProxy}
-   */
   get App(): IAppProxy {
     if (this.#appProxy) return this.#appProxy
     return (this.#appProxy = this._makeUserProxy(app, 'app'))
@@ -855,6 +856,11 @@ export class LSPluginUser
     return (this.#uiProxy = this._makeUserProxy(ui, 'ui'))
   }
 
+  get Utils(): IUtilsProxy {
+    if (this.#utilsProxy) return this.#utilsProxy
+    return (this.#utilsProxy = this._makeUserProxy(utils, 'utils'))
+  }
+
   get Git(): IGitProxy {
     return this._makeUserProxy(git, 'git')
   }

+ 16 - 3
libs/src/modules/LSPlugin.Experiments.ts

@@ -25,6 +25,18 @@ export class LSPluginExperiments {
     }
   }
 
+  get Utils() {
+    const utils = this.ensureHostScope().logseq.sdk.utils
+    const withCall = (name: string): (input: any) => any => utils[safeSnakeCase(name)]
+    return {
+      toClj: withCall('toClj'),
+      jsxToClj: withCall('jsxToClj'),
+      toJs: withCall('toJs'),
+      toKeyword: withCall('toKeyword'),
+      toSymbol: withCall('toSymbol')
+    }
+  }
+
   get pluginLocal(): PluginLocal {
     return this.ensureHostScope().LSPluginCore.ensurePlugin(
       this.ctx.baseInfo.id
@@ -124,11 +136,12 @@ export class LSPluginExperiments {
   }
 
   ensureHostScope(): any {
-    if (window === top) {
+    try {
+      const _ = window.top?.document
+    } catch (_e) {
       console.error('Can not access host scope!')
-      return {}
     }
 
-    return top
+    return window.top
   }
 }