Skip to content

Commit

Permalink
增加缓存机制,增加插件启用状态显示
Browse files Browse the repository at this point in the history
  • Loading branch information
cumany committed Nov 10, 2023
1 parent d51f1e6 commit b92b967
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 46 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "pkmer",
"name": "PKMer",
"version": "0.7.3",
"version": "0.7.4",
"minAppVersion": "1.3.5",
"description": "A marketplace of third-party plugins and themes developed by Obsidian enthusiasts in China to enhance the experience of using Obsidian in China.",
"author": "PKMer(windily-cloud&cumany)",
Expand Down
92 changes: 73 additions & 19 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: cumany [email protected]
* @Date: 2023-07-25 23:58:28
* @LastEditors: cumany [email protected]
* @LastEditTime: 2023-08-18 17:25:28
* @LastEditTime: 2023-11-10 14:03:07
* @Description:
*/
import { requestUrl } from "obsidian";
Expand Down Expand Up @@ -133,10 +133,22 @@ export class PkmerApi {
}

async getTop20Plugins(): Promise<ObsidianPluginInfo[]> {
const response = await this.fetchWithToken(BASE_API_URL + '/getTop20Plugins')
return await response.json() as ObsidianPluginInfo[];
}

const cachedData = localStorage.getItem('top20Plugins');
const cachedExpiry = localStorage.getItem('top20PluginsExpiry');
const currentTime = new Date().getTime();

if (cachedData && cachedExpiry && currentTime < +cachedExpiry) {
return JSON.parse(cachedData) as ObsidianPluginInfo[];
} else {
const response = await this.fetchWithToken(BASE_API_URL + '/getTop20Plugins');
const data = await response.json() as ObsidianPluginInfo[];

localStorage.setItem('top20Plugins', JSON.stringify(data));
localStorage.setItem('top20PluginsExpiry', String(currentTime + 3 * 24 * 60 * 60 * 1000));

return data;
}
}
async searchPlugins(keyword: string): Promise<ObsidianPluginInfo[]> {
const response = await this.fetchWithToken(BASE_API_URL + '/searchPlugins' + '?keyword=' + keyword + '&limit=10', {
method: 'GET',
Expand All @@ -145,23 +157,65 @@ export class PkmerApi {
}

async getPluginList(): Promise<ObsidianPluginInfo[]> {
const response = await this.fetchWithToken(BASE_API_URL + '/getAllPlugins', {
const cachedData = localStorage.getItem('pluginList');
const cachedExpiry = localStorage.getItem('pluginListExpiry');
const currentTime = new Date().getTime();

if (cachedData && cachedExpiry && currentTime < +cachedExpiry) {
return JSON.parse(cachedData) as ObsidianPluginInfo[];
} else {
const response = await this.fetchWithToken(BASE_API_URL + '/getAllPlugins', {
method: 'GET',
})
return await response.json() as ObsidianPluginInfo[];
}

async getTop20Themes(): Promise<ThemeInfo[]> {
const response = await this.fetchWithToken(BASE_API_URL + '/getTop20Themes')
return await response.json() as ThemeInfo[];
}
async getThemeList(): Promise<ThemeInfo[]> {
const response = await this.fetchWithToken(BASE_API_URL + '/getAllThemes', {
});
const data = await response.json() as ObsidianPluginInfo[];

localStorage.setItem('pluginList', JSON.stringify(data));
localStorage.setItem('pluginListExpiry', String(currentTime + 4 * 60 * 60 * 1000)); // 4 hours

return data;
}
}
async getTop20Themes(): Promise<ThemeInfo[]> {
const cachedData = localStorage.getItem('top20Themes');
const cachedExpiry = localStorage.getItem('top20ThemesExpiry');
const currentTime = new Date().getTime();

if (cachedData && cachedExpiry && currentTime < +cachedExpiry) {
return JSON.parse(cachedData) as ThemeInfo[];
} else {
const response = await this.fetchWithToken(BASE_API_URL + '/getTop20Themes');
const data = await response.json() as ThemeInfo[];

localStorage.setItem('top20Themes', JSON.stringify(data));
localStorage.setItem('top20ThemesExpiry', String(currentTime + 3 * 24 * 60 * 60 * 1000)); // Set the expiry time according to your requirements

return data;
}
}

async getThemeList(): Promise<ThemeInfo[]> {
const cachedData = localStorage.getItem('themeList');
const cachedExpiry = localStorage.getItem('themeListExpiry');
const currentTime = new Date().getTime();

if (cachedData && cachedExpiry && currentTime < +cachedExpiry) {
return JSON.parse(cachedData) as ThemeInfo[];
} else {
const response = await this.fetchWithToken(BASE_API_URL + '/getAllThemes', {
method: 'GET',
})
return await response.json() as ThemeInfo[];
}
});
const data = await response.json() as ThemeInfo[];

localStorage.setItem('themeList', JSON.stringify(data));
localStorage.setItem('themeListExpiry', String(currentTime +4 * 60 * 60 * 1000)); // 4 hours

return data;
}
}




async getThemeDownloadUrl(themeName: string, version: string): Promise<string> {
const response = await this.fetchWithToken(BASE_API_URL + '/getThemeDownloadUrl/' + themeName + '/' + version, {
method: 'GET',
Expand Down
72 changes: 48 additions & 24 deletions src/components/plugin/PluginCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: cumany cuman@qq.com
* @Date: 2023-02-23 17:17:12
* @LastEditors: cumany cuman@qq.com
* @LastEditTime: 2023-09-09 11:37:11
* @LastEditTime: 2023-11-10 16:20:52
* @FilePath: \pkmer-docs\src\components\Widget\WidgetCard.vue
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
Expand All @@ -20,7 +20,8 @@ const prop = defineProps<Props>()
const isUserLogin = prop.isLogin

const showImage = ref(false)

const PluginStatus =ref('');
const statusColor=ref('rgba(255, 51, 68, 0.8)');
defineEmits(["download-update-plugin"])
function getUsernameFromRepo() {
if (
Expand Down Expand Up @@ -91,6 +92,47 @@ function diffDays(timestamp: string) {
return `未知`
}
}
const getTooltip=()=>
{
if(PluginStatus.value=='未启用')
return '当前插件未启用,点击启用';
if(PluginStatus.value=='已启用')
return '当前插件已启动,点击禁用';
else
return '未成功启用。请查看控制台错误信息。';
}
const enablePlugin=async(id: string)=>{
if(PluginStatus.value== "已启用")
{ //@ts-ignore
await prop.app.plugins.disablePluginAndSave(id);
PluginStatus.value= "未启用";
statusColor.value="rgba(255, 51, 68, 0.8)";
//@ts-ignore
}else
{
//@ts-ignore
if(await prop.app.plugins.enablePluginAndSave(id))
{
PluginStatus.value= "已启用";
statusColor.value="#2aa330";
}else
{
PluginStatus.value= "未成功";
}

}

}
//@ts-ignore
if((Array.from(app.plugins.enabledPlugins).indexOf(prop.pluginInfo.id) != -1) && prop.pluginInfo.isInstalled) {
PluginStatus.value= "已启用";
statusColor.value="#2aa330"
}
//@ts-ignore
if((Array.from(app.plugins.enabledPlugins).indexOf(prop.pluginInfo.id) == -1) && prop.pluginInfo.isInstalled) {
PluginStatus.value= "未启用";
statusColor.value="rgba(255, 51, 68, 0.8)"
}
//名称的首字母大写
function getInitials(name: string) {
let initials = name.match(/\b\w/g) || []
Expand Down Expand Up @@ -136,7 +178,7 @@ const handleOpenSettings = () => {
<div class="relative">
<!--Badge-->
<span
class="absolute top-3 left-3 inline-block font-sans text-xs py-1.5 px-3 m-1 rounded-lg bg-primary-500 text-white shadow-xl shadow-primary-500/20">
class="absolute z-1 top-3 left-3 inline-block font-sans text-xs py-1.5 px-3 m-1 rounded-lg bg-primary-500 text-white shadow-xl shadow-primary-500/20">
{{ tags[0] }}
</span>

Expand Down Expand Up @@ -373,28 +415,10 @@ const handleOpenSettings = () => {
</button>
<button
v-else
@click="enablePlugin(pluginInfo.id)"
:tooltip="getTooltip()"
class="inline-flex items-center h-8 px-2 py-1 text-white transition-colors duration-300 border-0 rounded shadow-xl whitespace-nowrap bg-primary-500 hover:bg-primary-600 shadow-primary-500/20 tw-accessibility">
<svg
xmlns="http://www.w3.org/2000/svg"
aria-hidden="true"
role="img"
width="1em"
height="1em"
viewBox="0 0 48 48"
data-v-5ade68da=""
data-icon="icon-park-outline:link-cloud-sucess"
class="block w-4 h-4 mx-auto iconify iconify--icon-park-outline">
<g
fill="none"
stroke="currentColor"
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="4">
<path
d="M12 33c-3.333 0-8-1.5-8-7.5c0-7 7-8.5 9-8.5c1-3.5 3-9 11-9c7 0 10 4 11 7.5c0 0 9 1 9 9.5c0 6-4 8-8 8"></path>
<path d="m18 33l6 5l8-10"></path>
</g>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path :fill="statusColor" d="M8 4c.367 0 .721.048 1.063.145a3.943 3.943 0 0 1 1.762 1.031a3.944 3.944 0 0 1 1.03 1.762c.097.34.145.695.145 1.062c0 .367-.048.721-.145 1.063a3.94 3.94 0 0 1-1.03 1.765a4.017 4.017 0 0 1-1.762 1.031C8.72 11.953 8.367 12 8 12s-.721-.047-1.063-.14a4.056 4.056 0 0 1-1.765-1.032A4.055 4.055 0 0 1 4.14 9.062A3.992 3.992 0 0 1 4 8c0-.367.047-.721.14-1.063a4.02 4.02 0 0 1 .407-.953A4.089 4.089 0 0 1 5.98 4.546a3.94 3.94 0 0 1 .957-.401A3.89 3.89 0 0 1 8 4z"/></svg>
已安装
</button>
</div>
Expand Down
11 changes: 9 additions & 2 deletions src/components/theme/ThemeCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: cumany cuman@qq.com
* @Date: 2023-02-23 17:17:12
* @LastEditors: cumany cuman@qq.com
* @LastEditTime: 2023-10-09 22:49:06
* @LastEditTime: 2023-11-10 14:32:40
* @FilePath: \pkmer-docs\src\components\Widget\WidgetCard.vue
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
Expand Down Expand Up @@ -71,7 +71,11 @@ function getDefaultAvata(name: string) {
} else url = `https://cdn.pkmer.cn/covers/logo.png!nomark`
return url
}
const changeTheme=async(themeName: string)=>{
//@ts-ignore
await prop.app.customCss.setTheme(themeName);

}
const getRadomImage = () => {
const url = `https://pkmer.cn/img/cover/${Math.floor(
Math.random() * 11
Expand Down Expand Up @@ -307,6 +311,9 @@ const handleOpenSettings = () => {
</button>
<button
v-else
@click="changeTheme(themeInfo.name)"
tooltip="主题已安装,应用当前主题"
flow="down"
class="inline-flex items-center h-8 px-2 py-1 text-white transition-colors duration-300 border-0 rounded shadow-xl whitespace-nowrap bg-primary-500 hover:bg-primary-600 shadow-primary-500/20 tw-accessibility">
<svg
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -329,7 +336,7 @@ const handleOpenSettings = () => {
<path d="m18 33l6 5l8-10"></path>
</g>
</svg>
已安装
应用
</button>
</div>
</div>
Expand Down

0 comments on commit b92b967

Please sign in to comment.