diff --git a/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts b/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts index 898325d..3d9491f 100644 --- a/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts +++ b/libs/Universal-PicGo-Core/src/core/UniversalPicGo.ts @@ -124,11 +124,11 @@ class UniversalPicGo extends EventEmitter implements IPicGo { // } // } - getConfig(name?: string): T { + getConfig(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) } } diff --git a/libs/Universal-PicGo-Core/src/index.ts b/libs/Universal-PicGo-Core/src/index.ts index bd0eaa4..1f1fc1a 100644 --- a/libs/Universal-PicGo-Core/src/index.ts +++ b/libs/Universal-PicGo-Core/src/index.ts @@ -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 } diff --git a/libs/Universal-PicGo-Core/src/types/index.d.ts b/libs/Universal-PicGo-Core/src/types/index.d.ts index 8e3ee6b..75bfb45 100644 --- a/libs/Universal-PicGo-Core/src/types/index.d.ts +++ b/libs/Universal-PicGo-Core/src/types/index.d.ts @@ -583,4 +583,13 @@ interface IPicgoDb { saveConfig(config: Partial): void removeConfig(config: T): void +} + +/** + * 图床类型定义 + */ +interface IPicBedType { + type: string + name: string + visible: boolean } \ No newline at end of file diff --git a/libs/zhi-siyuan-picgo/package.json b/libs/zhi-siyuan-picgo/package.json index 5d184dc..380a7d3 100644 --- a/libs/zhi-siyuan-picgo/package.json +++ b/libs/zhi-siyuan-picgo/package.json @@ -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", diff --git a/libs/zhi-siyuan-picgo/src/index.ts b/libs/zhi-siyuan-picgo/src/index.ts index 00b3768..0abe49d 100644 --- a/libs/zhi-siyuan-picgo/src/index.ts +++ b/libs/zhi-siyuan-picgo/src/index.ts @@ -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 } \ No newline at end of file +export { type IPicGo, type IImgInfo, type IPicgoDb, type IConfig, type IExternalPicgoConfig } diff --git a/libs/zhi-siyuan-picgo/src/lib/picgoHelper.ts b/libs/zhi-siyuan-picgo/src/lib/picgoHelper.ts new file mode 100644 index 0000000..2a8ea65 --- /dev/null +++ b/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. + * 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) { + 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 } diff --git a/packages/picgo-plugin-app/src/utils/browserClipboard.ts b/libs/zhi-siyuan-picgo/src/lib/utils/browserClipboard.ts similarity index 100% rename from packages/picgo-plugin-app/src/utils/browserClipboard.ts rename to libs/zhi-siyuan-picgo/src/lib/utils/browserClipboard.ts diff --git a/packages/picgo-plugin-app/src/utils/idUtil.ts b/libs/zhi-siyuan-picgo/src/lib/utils/idUtil.ts similarity index 100% rename from packages/picgo-plugin-app/src/utils/idUtil.ts rename to libs/zhi-siyuan-picgo/src/lib/utils/idUtil.ts diff --git a/packages/picgo-plugin-app/src/utils/utils.ts b/libs/zhi-siyuan-picgo/src/lib/utils/utils.ts similarity index 96% rename from packages/picgo-plugin-app/src/utils/utils.ts rename to libs/zhi-siyuan-picgo/src/lib/utils/utils.ts index 6a83ca7..b76b830 100644 --- a/packages/picgo-plugin-app/src/utils/utils.ts +++ b/libs/zhi-siyuan-picgo/src/lib/utils/utils.ts @@ -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] }) diff --git a/packages/picgo-plugin-app/package.json b/packages/picgo-plugin-app/package.json index d9d0c70..a5ce775 100644 --- a/packages/picgo-plugin-app/package.json +++ b/packages/picgo-plugin-app/package.json @@ -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", @@ -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", diff --git a/packages/picgo-plugin-app/src/components/home/DragUpload.vue b/packages/picgo-plugin-app/src/components/home/DragUpload.vue index 56acd4e..b4f1468 100644 --- a/packages/picgo-plugin-app/src/components/home/DragUpload.vue +++ b/packages/picgo-plugin-app/src/components/home/DragUpload.vue @@ -9,10 +9,10 @@