@zhin.js/client 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/router.ts ADDED
@@ -0,0 +1,44 @@
1
+ import { createRouter, createWebHistory } from 'vue-router';
2
+ import { useCommonStore } from './store.js';
3
+ import { MenuWithComponent, ToolInfo } from './types.js';
4
+ export const router = createRouter({
5
+ history: createWebHistory(),
6
+ routes: [],
7
+ });
8
+
9
+ router.beforeEach(async (to, from, next) => {
10
+ const commonStore = useCommonStore();
11
+ if (!commonStore.initialized) {
12
+ await commonStore.initial;
13
+ return next({ ...to, replace: true });
14
+ }
15
+ return next();
16
+ });
17
+ export function addPage(menu: MenuWithComponent) {
18
+ if (menu.parentName) {
19
+ router.addRoute(menu.parentName, {
20
+ path: menu.path,
21
+ name: menu.name,
22
+ component: menu.component,
23
+ children: [],
24
+ });
25
+ } else {
26
+ router.addRoute({
27
+ path: menu.path,
28
+ name: menu.name,
29
+ component: menu.component,
30
+ children: [],
31
+ });
32
+ }
33
+ useCommonStore().addData({
34
+ key: 'menus',
35
+ value: menu,
36
+ });
37
+ return () => router.removeRoute(menu.name);
38
+ }
39
+ export function addTool(tool: ToolInfo) {
40
+ useCommonStore().addData({
41
+ key: 'tools',
42
+ value: tool,
43
+ });
44
+ }
package/src/store.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { ref, watch } from 'vue';
2
+ import { defineStore } from 'pinia';
3
+ export const useCommonStore = defineStore('common', () => {
4
+ const store = ref<Record<string, any>>({});
5
+ const initialized = ref(false);
6
+ let resolve: (v: unknown) => any;
7
+ const initial = new Promise(res => (resolve = res));
8
+ const syncData = ({ key, value }: { key: string; value: any }) => {
9
+ store.value[key] = value;
10
+ if (resolve) {
11
+ initialized.value = true;
12
+ setTimeout(resolve, 300);
13
+ }
14
+ };
15
+ const addData = ({ key, value }: { key: string; value: any }) => {
16
+ const list = (store.value[key] ||= []);
17
+ list.push(value);
18
+ };
19
+ const deleteData = ({ key, value }: { key: string; value: any }) => {
20
+ const list = (store.value[key] ||= []);
21
+ list.splice(list.indexOf(value), 1);
22
+ };
23
+ let beforeElement: Node[] = [];
24
+ const createScript = (store: Record<string, any>) => {
25
+ if(!store.entries?.length) return;
26
+ const fragment = document.createDocumentFragment();
27
+ while (beforeElement.length) {
28
+ const element = beforeElement.shift()!;
29
+ document.body.removeChild(element);
30
+ }
31
+ const entries: string[] = (store.entries ||= []);
32
+ entries.forEach(entry => {
33
+ const el = document.createElement('script');
34
+ el.type = 'module';
35
+ el.src = entry;
36
+ fragment.appendChild(el);
37
+ beforeElement.push(el);
38
+ });
39
+ document.body.appendChild(fragment);
40
+ };
41
+ watch(store, createScript, {
42
+ immediate: true,
43
+ deep: true,
44
+ });
45
+ return {
46
+ store,
47
+ initialized,
48
+ initial,
49
+ syncData,
50
+ addData,
51
+ deleteData,
52
+ };
53
+ });
package/src/types.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { RouteComponent } from 'vue-router';
2
+
3
+ export interface MenuInfo {
4
+ icon?: string;
5
+ parentName?: string;
6
+ children?: MenuInfo[];
7
+ path: string;
8
+ name: string;
9
+ }
10
+ export type MenuWithComponent = {
11
+ component: RouteComponent;
12
+ children?: MenuWithComponent[];
13
+ } & MenuInfo;
14
+ export type ToolInfo = {
15
+ name?: string;
16
+ icon?: string;
17
+ component: RouteComponent;
18
+ };