Enhance/api storages for graph assets (#6488)

* improve(api): assets storage for plugin

Co-authored-by: charlie <xyhp915@qq.com>
This commit is contained in:
Tienson Qin
2022-10-04 12:30:19 +08:00
committed by GitHub
parent 3bf9e1a262
commit f4262cf919
11 changed files with 208 additions and 82 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@logseq/libs",
"version": "0.0.8",
"version": "0.0.10",
"description": "Logseq SDK libraries",
"main": "dist/lsplugin.user.js",
"typings": "index.d.ts",

View File

@@ -3,7 +3,7 @@ import * as CSS from 'csstype'
import EventEmitter from 'eventemitter3'
import { LSPluginCaller } from './LSPlugin.caller'
import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
import { IAsyncStorage, LSPluginFileStorage } from './modules/LSPlugin.Storage'
import { LSPluginRequest } from './modules/LSPlugin.Request'
export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -169,6 +169,7 @@ export interface BlockEntity {
unordered: boolean
content: string
page: IEntityID
properties?: Record<string, any>
// optional fields in dummy page
anchor?: string
@@ -196,9 +197,12 @@ export interface PageEntity {
file?: IEntityID
namespace?: IEntityID
children?: Array<PageEntity>
properties?: Record<string, any>
format?: 'markdown' | 'org'
journalDay?: number
updatedAt?: number
[key: string]: any
}
export type BlockIdentity = BlockUUID | Pick<BlockEntity, 'uuid'>
@@ -412,9 +416,9 @@ export interface IAppProxy {
// hook events
onCurrentGraphChanged: IUserHook
onGraphAfterIndexed: IUserHook<{repo: string}>
onGraphAfterIndexed: IUserHook<{ repo: string }>
onThemeModeChanged: IUserHook<{ mode: 'dark' | 'light' }>
onThemeChanged: IUserHook<Partial<{name: string, mode: string, pid: string, url: string}>>
onThemeChanged: IUserHook<Partial<{ name: string, mode: string, pid: string, url: string }>>
onBlockRendererSlotted: IUserSlotHook<{ uuid: BlockUUID }>
/**
@@ -786,7 +790,7 @@ export interface IAssetsProxy {
* @added 0.0.2
* @param exts
*/
listFilesOfCurrentGraph(exts: string | string[]): Promise<{
listFilesOfCurrentGraph(exts?: string | string[]): Promise<{
path: string
size: number
accessTime: number
@@ -794,6 +798,12 @@ export interface IAssetsProxy {
changeTime: number
birthTime: number
}>
/**
* @example https://github.com/logseq/logseq/pull/6488
* @added 0.0.10
*/
makeSandboxStorage(): IAsyncStorage
}
export interface ILSPluginThemeManager {
@@ -963,6 +973,7 @@ export interface ILSPluginUser extends EventEmitter<LSPluginUserEvents> {
DB: IDBProxy
Git: IGitProxy
UI: IUIProxy
Assets: IAssetsProxy
Request: LSPluginRequest
FileStorage: LSPluginFileStorage

View File

@@ -38,7 +38,7 @@ import {
import Debug from 'debug'
import * as CSS from 'csstype'
import EventEmitter from 'eventemitter3'
import { LSPluginFileStorage } from './modules/LSPlugin.Storage'
import { IAsyncStorage, LSPluginFileStorage } from './modules/LSPlugin.Storage'
import { LSPluginExperiments } from './modules/LSPlugin.Experiments'
import { LSPluginRequest } from './modules/LSPlugin.Request'
@@ -311,8 +311,18 @@ const db: Partial<IDBProxy> = {
}
const git: Partial<IGitProxy> = {}
const ui: Partial<IUIProxy> = {}
const assets: Partial<IAssetsProxy> = {}
const assets: Partial<IAssetsProxy> = {
makeSandboxStorage(
this: LSPluginUser
): IAsyncStorage {
return new LSPluginFileStorage(
this, { assets: true }
)
}
}
type uiState = {
key?: number

View File

@@ -9,6 +9,8 @@ export interface IAsyncStorage {
hasItem(key: string): Promise<boolean>
allKeys(): Promise<Array<string>>
clear(): Promise<void>
}
@@ -18,8 +20,14 @@ export interface IAsyncStorage {
class LSPluginFileStorage implements IAsyncStorage {
/**
* @param ctx
* @param opts
*/
constructor(private ctx: LSPluginUser) {}
constructor(
private ctx: LSPluginUser,
private opts?: {
assets: boolean
}
) {}
/**
* plugin id
@@ -32,20 +40,20 @@ class LSPluginFileStorage implements IAsyncStorage {
* @param key A string as file name that support nested directory
* @param value Storage value
*/
setItem(key: string, value: string): Promise<void> {
setItem(key: string, value: string | any): Promise<void> {
return this.ctx.caller.callAsync(`api:call`, {
method: 'write-plugin-storage-file',
args: [this.ctxId, key, value],
args: [this.ctxId, key, value, this.opts?.assets],
})
}
/**
* @param key
*/
getItem(key: string): Promise<string | undefined> {
getItem(key: string): Promise<string | any> {
return this.ctx.caller.callAsync(`api:call`, {
method: 'read-plugin-storage-file',
args: [this.ctxId, key],
args: [this.ctxId, key, this.opts?.assets],
})
}
@@ -55,7 +63,17 @@ class LSPluginFileStorage implements IAsyncStorage {
removeItem(key: string): Promise<void> {
return this.ctx.caller.call(`api:call`, {
method: 'unlink-plugin-storage-file',
args: [this.ctxId, key],
args: [this.ctxId, key, this.opts?.assets],
})
}
/**
* Get all path file keys
*/
allKeys(): Promise<Array<string>> {
return this.ctx.caller.callAsync(`api:call`, {
method: 'list-plugin-storage-files',
args: [this.ctxId, this.opts?.assets]
})
}
@@ -65,7 +83,7 @@ class LSPluginFileStorage implements IAsyncStorage {
clear(): Promise<void> {
return this.ctx.caller.call(`api:call`, {
method: 'clear-plugin-storage-files',
args: [this.ctxId],
args: [this.ctxId, this.opts?.assets],
})
}
@@ -75,7 +93,7 @@ class LSPluginFileStorage implements IAsyncStorage {
hasItem(key: string): Promise<boolean> {
return this.ctx.caller.callAsync(`api:call`, {
method: 'exist-plugin-storage-file',
args: [this.ctxId, key],
args: [this.ctxId, key, this.opts?.assets],
})
}
}