From c97a3c8836c615c7f1aaf282792973de57c7c77d Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Thu, 23 Jun 2022 15:31:32 +0800 Subject: [PATCH] feat: add pages extension points Signed-off-by: Ryan Wang --- packages/shared/src/index.ts | 1 + packages/shared/src/states/pages.ts | 9 +++ packages/shared/src/types/plugin.ts | 19 +++--- src/composables/usePlugins.ts | 21 +++++++ .../SheetList.vue => pages/PageList.vue} | 62 +++++++------------ .../contents/{sheets => pages}/module.ts | 10 +-- src/modules/index.ts | 4 +- src/types/extension.d.ts | 3 + 8 files changed, 71 insertions(+), 58 deletions(-) create mode 100644 packages/shared/src/states/pages.ts create mode 100644 src/composables/usePlugins.ts rename src/modules/contents/{sheets/SheetList.vue => pages/PageList.vue} (92%) rename src/modules/contents/{sheets => pages}/module.ts (78%) diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index c46da2c2..9b97d135 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -1,3 +1,4 @@ export * from "./types/plugin"; export * from "./types/menus"; export * from "./core/plugins"; +export * from "./states/pages"; diff --git a/packages/shared/src/states/pages.ts b/packages/shared/src/states/pages.ts new file mode 100644 index 00000000..fcefa7e3 --- /dev/null +++ b/packages/shared/src/states/pages.ts @@ -0,0 +1,9 @@ +export interface PagesPublicState { + functionalPages: FunctionalPagesState[]; +} + +export interface FunctionalPagesState { + name: string; + path: string; + url?: string; +} diff --git a/packages/shared/src/types/plugin.ts b/packages/shared/src/types/plugin.ts index 052e6cec..f176d80c 100644 --- a/packages/shared/src/types/plugin.ts +++ b/packages/shared/src/types/plugin.ts @@ -1,15 +1,11 @@ -import type { Component } from "vue"; +import type { Component, Ref } from "vue"; import type { RouteRecordRaw } from "vue-router"; import type { MenuGroupType } from "./menus"; +import type { PagesPublicState } from "@/states/pages"; -export type ExtensionPointName = - | "POSTS" - | "POST_EDITOR" - | "DASHBOARD" - | "USER_SETTINGS"; +export type ExtensionPointName = "PAGES" | "POSTS"; -// TODO define extension point state -export type ExtensionPointState = Record; +export type ExtensionPointState = PagesPublicState; export interface Plugin { name: string; @@ -33,8 +29,7 @@ export interface Plugin { menus?: MenuGroupType[]; - extensionPoints?: Record< - ExtensionPointName, - (state: ExtensionPointState) => void - >; + extensionPoints?: { + [key in ExtensionPointName]?: (state: Ref) => void; + }; } diff --git a/src/composables/usePlugins.ts b/src/composables/usePlugins.ts new file mode 100644 index 00000000..3324977d --- /dev/null +++ b/src/composables/usePlugins.ts @@ -0,0 +1,21 @@ +import { usePluginStore } from "@/stores/plugin"; +import type { + ExtensionPointName, + ExtensionPointState, +} from "@halo-dev/admin-shared"; +import type { Plugin } from "@/types/extension"; +import type { Ref } from "vue"; + +export function useExtensionPointsState( + point: ExtensionPointName, + state: Ref +) { + const { plugins } = usePluginStore(); + + plugins.forEach((plugin: Plugin) => { + if (!plugin.spec.module?.extensionPoints?.[point]) { + return; + } + plugin.spec.module.extensionPoints[point]?.(state); + }); +} diff --git a/src/modules/contents/sheets/SheetList.vue b/src/modules/contents/pages/PageList.vue similarity index 92% rename from src/modules/contents/sheets/SheetList.vue rename to src/modules/contents/pages/PageList.vue index 4f28aeed..e951e56f 100644 --- a/src/modules/contents/sheets/SheetList.vue +++ b/src/modules/contents/pages/PageList.vue @@ -14,9 +14,10 @@ import { } from "@halo-dev/components"; import { ref } from "vue"; import { users } from "@/modules/system/users/users-mock"; -import halo from "@/assets/logo.svg"; +import type { PagesPublicState } from "@halo-dev/admin-shared"; +import { useExtensionPointsState } from "@/composables/usePlugins"; -const sheetsRef = ref([ +const pagesRef = ref([ { title: "关于我们", url: "/about", @@ -37,29 +38,13 @@ const sheetsRef = ref([ }, ]); -const advancedSheets = ref([ - { - name: "友情链接", - author: "halo-dev", - logo: halo, - url: "/links", - }, - { - name: "图库", - author: "halo-dev", - logo: halo, - url: "/photos", - }, - { - name: "社区", - author: "halo-dev", - logo: halo, - url: "/community", - }, -]); - +const activeId = ref("functional"); const checkAll = ref(false); -const activeId = ref("advanced"); +const pagesPublicState = ref({ + functionalPages: [], +}); + +useExtensionPointsState("PAGES", pagesPublicState); -
+
    -
  • +
  • @@ -104,20 +93,15 @@ const activeId = ref("advanced"); - {{ sheet.name }} + {{ page.name }} - {{ sheet.url }} + {{ page.url }}
- @@ -269,7 +253,7 @@ const activeId = ref("advanced"); class="box-border h-full w-full divide-y divide-gray-100" role="list" > -
  • +
  • - {{ sheet.title }} + {{ page.title }} - {{ sheet.url }} + {{ page.url }}
    - 访问量 {{ sheet.views }} + 访问量 {{ page.views }} - 评论 {{ sheet.commentCount }} + 评论 {{ page.commentCount }}
    diff --git a/src/modules/contents/sheets/module.ts b/src/modules/contents/pages/module.ts similarity index 78% rename from src/modules/contents/sheets/module.ts rename to src/modules/contents/pages/module.ts index c8498eb4..baff69ef 100644 --- a/src/modules/contents/sheets/module.ts +++ b/src/modules/contents/pages/module.ts @@ -1,19 +1,19 @@ import { definePlugin } from "@halo-dev/admin-shared"; import { BasicLayout } from "@/layouts"; -import SheetList from "./SheetList.vue"; +import SheetList from "./PageList.vue"; import { IconPages } from "@halo-dev/components"; export default definePlugin({ - name: "sheetModule", + name: "pageModule", components: [], routes: [ { - path: "/sheets", + path: "/pages", component: BasicLayout, children: [ { path: "", - name: "Sheets", + name: "Pages", component: SheetList, }, ], @@ -25,7 +25,7 @@ export default definePlugin({ items: [ { name: "页面", - path: "/sheets", + path: "/pages", icon: IconPages, }, ], diff --git a/src/modules/index.ts b/src/modules/index.ts index afc15222..beea02b9 100644 --- a/src/modules/index.ts +++ b/src/modules/index.ts @@ -1,6 +1,6 @@ import dashboardModule from "./dashboard/module"; import postModule from "./contents/posts/module"; -import sheetModule from "./contents/sheets/module"; +import pageModule from "./contents/pages/module"; import commentModule from "./contents/comments/module"; import attachmentModule from "./contents/attachments/module"; import themeModule from "./interface/themes/module"; @@ -13,7 +13,7 @@ import settingModule from "./system/settings/module"; const coreModules = [ dashboardModule, postModule, - sheetModule, + pageModule, commentModule, attachmentModule, themeModule, diff --git a/src/types/extension.d.ts b/src/types/extension.d.ts index 8e405ee3..4a63ecf0 100644 --- a/src/types/extension.d.ts +++ b/src/types/extension.d.ts @@ -1,3 +1,5 @@ +import type { Plugin as PluginModule } from "@halo-dev/admin-shared"; + export interface License { name?: string; url?: string; @@ -44,6 +46,7 @@ export interface PluginSpec { requires?: string; pluginClass?: string; enabled?: boolean; + module?: PluginModule; } export interface PluginStatus {