enhance(libs): utils apis

This commit is contained in:
charlie
2025-01-09 17:35:55 +08:00
parent 72c09e5c30
commit 99998e25e9
3 changed files with 32 additions and 9 deletions

View File

@@ -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
*/

View File

@@ -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')
}

View File

@@ -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
}
}