Skip to content

Commit

Permalink
feat: reactive config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 22, 2024
1 parent a4aaae1 commit 025d5f6
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 297 deletions.
4 changes: 2 additions & 2 deletions libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts
Expand Up @@ -124,11 +124,11 @@ class UniversalPicGo extends EventEmitter implements IPicGo {
// }
// }

getConfig<T>(name?: string): T {
getConfig<T>(name?: string, defaultValue?: any): T {
if (!name) {
return this._config as unknown as T
} else {
return _.get(this._config, name)
return _.get(this._config, name, defaultValue)
}
}

Expand Down
8 changes: 4 additions & 4 deletions libs/Universal-PicGo-Core/src/index.ts
Expand Up @@ -4,11 +4,11 @@ import ConfigDb from "./db/config"
import PluginLoaderDb from "./db/pluginLoder"
import ExternalPicgoConfigDb from "./db/externalPicGo"
import { currentWin, hasNodeEnv, parentWin, win } from "universal-picgo-store"
import { PicgoTypeEnum } from "./utils/enums"
import { IPicGo, IImgInfo, IPicgoDb, IConfig, IExternalPicgoConfig } from "./types"
import { PicgoTypeEnum, IBusEvent } from "./utils/enums"
import { IPicGo, IImgInfo, IPicgoDb, IConfig, IExternalPicgoConfig, IPicBedType } from "./types"

export { UniversalPicGo, ExternalPicgo }
export { ConfigDb, PluginLoaderDb, ExternalPicgoConfigDb }
export { PicgoTypeEnum }
export { PicgoTypeEnum, IBusEvent }
export { win, currentWin, parentWin, hasNodeEnv }
export { type IPicGo, type IImgInfo, type IPicgoDb, type IConfig, type IExternalPicgoConfig }
export { type IPicGo, type IImgInfo, type IPicgoDb, type IConfig, type IExternalPicgoConfig, type IPicBedType }
9 changes: 9 additions & 0 deletions libs/Universal-PicGo-Core/src/types/index.d.ts
Expand Up @@ -583,4 +583,13 @@ interface IPicgoDb<T> {
saveConfig(config: Partial<T>): void

removeConfig(config: T): void
}

/**
* 图床类型定义
*/
interface IPicBedType {
type: string
name: string
visible: boolean
}
4 changes: 3 additions & 1 deletion libs/zhi-siyuan-picgo/package.json
Expand Up @@ -26,11 +26,13 @@
},
"devDependencies": {
"@terwer/eslint-config-custom": "^1.3.6",
"@terwer/vite-config-custom": "^0.7.6"
"@terwer/vite-config-custom": "^0.7.6",
"@types/uuid": "^9.0.8"
},
"dependencies": {
"js-md5": "^0.8.3",
"universal-picgo": "workspace:*",
"uuid": "^9.0.1",
"zhi-common": "^1.31.0",
"zhi-device": "^2.11.0",
"zhi-lib-base": "^0.8.0",
Expand Down
8 changes: 6 additions & 2 deletions libs/zhi-siyuan-picgo/src/index.ts
Expand Up @@ -20,8 +20,12 @@ import {
PluginLoaderDb,
} from "universal-picgo"
import { SiyuanConfig as SiyuanPicgoConfig } from "zhi-siyuan-api"
import { PicgoHelper } from "./lib/picgoHelper"
import { retrieveImageFromClipboardAsBlob } from "./lib/utils/browserClipboard"
import { copyToClipboardInBrowser } from "./lib/utils/utils"

export { SiyuanPicgoConfig, SiyuanPicgoPostApi }
export { SiyuanPicgoConfig, SiyuanPicgoPostApi, PicgoHelper }
export { retrieveImageFromClipboardAsBlob, copyToClipboardInBrowser }
export { ConfigDb, PluginLoaderDb, ExternalPicgoConfigDb }
export { PicgoTypeEnum }
export { type IPicGo, type IImgInfo, type IPicgoDb, type IConfig, type IExternalPicgoConfig }
export { type IPicGo, type IImgInfo, type IPicgoDb, type IConfig, type IExternalPicgoConfig }
279 changes: 279 additions & 0 deletions libs/zhi-siyuan-picgo/src/lib/picgoHelper.ts
@@ -0,0 +1,279 @@
/*
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2024 Terwer, Inc. <https://terwer.space/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/

import _ from "lodash-es"
import { IBusEvent, IConfig, IPicBedType, IPicGo } from "universal-picgo"
import { getRawData } from "./utils/utils"
import { readonly } from "vue"

/**
* picgo 工具类
*
* @version 1.6.0
* @since 1.6.0
* @author terwer
*/
class PicgoHelper {
private readonly ctx: IPicGo
/**
* !!! 这个 cfg 是响应式的,修改这个会自动完成持久化
*
* !!! 这个 cfg 是响应式的,修改这个会自动完成持久化
*
* !!! 这个 cfg 是响应式的,修改这个会自动完成持久化
*
* @private
*/
private readonly reactiveCfg: IConfig
private readonly readonlyCfg: IConfig

/**
* 狗子 PicGo 帮组类
*
* @param ctx 上下文
* @param reactiveCfg 响应式配置对象
*/
constructor(ctx: IPicGo, reactiveCfg: IConfig) {
this.ctx = ctx
this.reactiveCfg = reactiveCfg
this.readonlyCfg = readonly(this.reactiveCfg)
}

/**
* 根据 key 获取配置项
*
* @param key
* @param defaultValue
*/
public getPicgoConfig2(key?: string, defaultValue?: any) {
if (!key) {
return this.readonlyCfg as unknown
}
return _.get(this.readonlyCfg, key, defaultValue)
}

/**
* 保存配置
*
* @param cfg
*/
public savePicgoConfig(cfg: Partial<IConfig>) {
if (!cfg) {
console.warn(`cfg can not be undefined `)
return
}
// 刷新
Object.keys(cfg).forEach((name: string) => {
const rawCfg = getRawData(cfg)
_.set(this.reactiveCfg, name, rawCfg[name])
this.ctx.emit(IBusEvent.CONFIG_CHANGE, {
configName: name,
value: rawCfg[name],
})
})
}

/**
* 获取所有的图床列表
*/
public getPicBeds(): IPicBedType[] {
const picBedTypes = this.ctx.helper.uploader.getIdList()
const picBedFromDB = this.getPicgoConfig2("picBed.list") || []

const picBeds = picBedTypes
.map((item: any) => {
const visible = picBedFromDB.find((i: any) => i.type === item) // object or undefined
return {
type: item,
name: this.ctx.helper.uploader.get(item).name || item,
visible: visible ? visible.visible : true,
}
})
.sort((a: any) => {
if (a.type === "smms") {
return -1
}
return 0
})

return picBeds
}

// /**
// * 获取启用的图床
// *
// * @param ctx
// */
// public static getVisiablePicBeds(ctx: IPicGo): IPicBedType[] {
// const picBeds = this.getPicBeds(ctx)
// const visiablePicBeds = picBeds
// .map((item: IPicBedType) => {
// if (item.visible) {
// return item
// }
// return null
// })
// .filter((item: any) => item) as IPicBedType[]
//
// // SM.MS是必选的
// if (visiablePicBeds.length == 0) {
// const defaultPicbed = {
// type: "smms",
// name: "SM.MS",
// } as IPicBedType
// visiablePicBeds.push(defaultPicbed)
// }
// return visiablePicBeds
// }
//
// /**
// * 获取可用的图床列表名称
// *
// * @param ctx
// */
// public static getVisiablePicBedNames(ctx: IPicGo): string[] {
// const picBeds = this.getPicBeds(ctx)
// return picBeds
// .map((item: IPicBedType) => {
// if (item.visible) {
// return item.name
// }
// return null
// })
// .filter((item: any) => item) as string[]
// }
//
/**
* 根据图床数据获取可用的图床列表名称
*
* @param picBeds
*/
public static getVisiablePicBedNamesByPicBeds(picBeds: IPicBedType[]): string[] {
return picBeds
.map((item: IPicBedType) => {
if (item.visible) {
return item.name
}
return null
})
.filter((item: any) => item) as string[]
}

// public static getUploaderConfigList(ctx: IPicGo, cfg: IConfig, type: string): IUploaderConfigItem {
// if (!type) {
// return {
// configList: [] as IUploaderConfigListItem[],
// defaultId: "",
// }
// }
// const currentUploaderConfig = this.getPicgoConfig(cfg, `uploader.${type}`) ?? {}
// let configList = currentUploaderConfig.configList
// let defaultId = currentUploaderConfig.defaultId || ""
// if (!configList) {
// const res = this.upgradeUploaderConfig(ctx, cfg, type)
// configList = res.configList
// defaultId = res.defaultId
// }
//
// const configItem = {
// configList,
// defaultId,
// }
// // console.warn("获取当前图床配置列表:", configItem)
// return configItem
// }
//
// /**
// * 选择当前图床
// *
// * @param ctx
// * @param cfg
// * @param type 当前图床类型
// * @param id 当前图床配置ID
// * @author terwer
// * @since 0.7.0
// */
// public static selectUploaderConfig = (ctx: IPicGo, cfg: IConfig, type: string, id: string) => {
// const { configList } = this.getUploaderConfigList(ctx, cfg, type)
// const config = configList.find((item) => item._id === id)
// if (config) {
// ctx.saveConfig({
// [`uploader.${type}.defaultId`]: id,
// [`picBed.${type}`]: config,
// })
// }
//
// return config
// }
//
// /**
// * 设置默认图床
// *
// * @param ctx
// * @param type
// */
// public static setDefaultPicBed(ctx: IPicGo, type: string) {
// this.savePicgoConfig(ctx, {
// "picBed.current": type,
// "picBed.uploader": type,
// })
// }
//
// // ===================================================================================================================
//
// /**
// * upgrade old uploader config to new format
// *
// * @param ctx
// * @param cfg
// * @param type type
// * @author terwer
// * @since 0.7.0
// */
// private static upgradeUploaderConfig = (ctx: IPicGo, cfg: IConfig, type: string) => {
// const uploaderConfig = this.getPicgoConfig(cfg, `picBed.${type}`) ?? {}
// if (!uploaderConfig._id) {
// Object.assign(uploaderConfig, this.completeUploaderMetaConfig(uploaderConfig))
// }
//
// const uploaderConfigList = [uploaderConfig]
// this.savePicgoConfig(ctx, {
// [`uploader.${type}`]: {
// configList: uploaderConfigList,
// defaultId: uploaderConfig._id,
// },
// [`picBed.${type}`]: uploaderConfig,
// })
// return {
// configList: uploaderConfigList as IUploaderConfigListItem[],
// defaultId: uploaderConfig._id as string,
// }
// }
//
// /**
// * 增加配置元数据
// *
// * @param originData 原始数据
// */
// private static completeUploaderMetaConfig(originData: any) {
// return Object.assign(
// {
// _configName: "Default",
// },
// trimValues(originData),
// {
// _id: IdUtil.newUuid(),
// _createdAt: Date.now(),
// _updatedAt: Date.now(),
// }
// )
// }
}

export { PicgoHelper }
Expand Up @@ -78,8 +78,8 @@ export const getRawData = (args: any): any => {
return args
}

export const trimValues = (obj: IStringKeyMap) => {
const newObj = {} as IStringKeyMap
export const trimValues = (obj: any) => {
const newObj = {} as any
Object.keys(obj).forEach((key) => {
newObj[key] = typeof obj[key] === "string" ? obj[key].trim() : obj[key]
})
Expand Down
2 changes: 0 additions & 2 deletions packages/picgo-plugin-app/package.json
Expand Up @@ -11,7 +11,6 @@
},
"devDependencies": {
"@terwer/eslint-config-custom": "^1.3.6",
"@types/uuid": "^9.0.8",
"@vitejs/plugin-vue": "^5.0.4",
"fast-glob": "^3.3.2",
"minimist": "^1.2.8",
Expand All @@ -31,7 +30,6 @@
"element-plus": "^2.6.1",
"lodash-es": "^4.17.21",
"unplugin-icons": "^0.18.5",
"uuid": "^9.0.1",
"vue": "^3.4.21",
"vue-i18n": "^9.10.2",
"vue-router": "^4.3.0",
Expand Down

0 comments on commit 025d5f6

Please sign in to comment.