@vtj/web 0.9.9 → 0.9.10

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/package.json CHANGED
@@ -1,17 +1,18 @@
1
1
  {
2
2
  "name": "@vtj/web",
3
3
  "private": false,
4
- "version": "0.9.9",
4
+ "version": "0.9.10",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "dependencies": {
8
8
  "core-js": "~3.39.0",
9
9
  "regenerator-runtime": "~0.14.1",
10
- "@vtj/charts": "~0.9.9",
11
- "@vtj/icons": "~0.9.9",
12
- "@vtj/renderer": "~0.9.9",
13
- "@vtj/utils": "~0.9.9",
14
- "@vtj/ui": "~0.9.9"
10
+ "@vtj/charts": "~0.9.10",
11
+ "@vtj/renderer": "~0.9.10",
12
+ "@vtj/core": "~0.9.10",
13
+ "@vtj/ui": "~0.9.10",
14
+ "@vtj/utils": "~0.9.10",
15
+ "@vtj/icons": "~0.9.10"
15
16
  },
16
17
  "devDependencies": {
17
18
  "@vtj/cli": "~0.9.6"
package/src/index.ts CHANGED
@@ -8,3 +8,4 @@ export * from './components';
8
8
  export * from './renderer';
9
9
  export * from './modules';
10
10
  export * from './apis';
11
+ export * from './integrations';
@@ -0,0 +1,179 @@
1
+ import type { App } from 'vue';
2
+ import type { Router, RouteRecordRaw } from 'vue-router';
3
+ import { kebabCase } from '@vtj/utils';
4
+ import { autoUpdate } from '@vtj/ui';
5
+ import type { ProjectSchema, PageFile } from '@vtj/core';
6
+ import {
7
+ createProvider,
8
+ LocalService,
9
+ NodeEnv,
10
+ ContextMode,
11
+ notify,
12
+ loading,
13
+ createAdapter,
14
+ createServiceRequest
15
+ } from '../renderer';
16
+ import { createModules } from '../modules';
17
+
18
+ export interface SetupElementAdminOptions {
19
+ id: string;
20
+ app: App;
21
+ ready?: () => void;
22
+ }
23
+
24
+ export interface setupElementAdminRoutesOptions {
25
+ id: string;
26
+ router: Router;
27
+ routes: any[];
28
+ layout: any;
29
+ }
30
+
31
+ function toElIcon(icon?: string) {
32
+ return icon ? `vi-ep:${kebabCase(icon)}` : undefined;
33
+ }
34
+
35
+ function createPageRoute(page: PageFile) {
36
+ const { id, title, icon, hidden, cache = false, meta = {} } = page;
37
+ return {
38
+ path: `page/${id}`,
39
+ name: `Page_${id}`,
40
+ component: () => import(`$vtj/vue/${id}.vue`),
41
+ meta: {
42
+ title,
43
+ hidden,
44
+ icon: toElIcon(icon),
45
+ noCache: !cache,
46
+ ...meta
47
+ }
48
+ };
49
+ }
50
+
51
+ function createNoMaskRoute(page: PageFile) {
52
+ const { id, title, icon, cache = false, meta = {} } = page;
53
+ return {
54
+ path: `/page/${id}`,
55
+ component: () => import(`$vtj/vue/${id}.vue`),
56
+ meta: {
57
+ title,
58
+ icon: toElIcon(icon),
59
+ hidden: true,
60
+ noCache: !cache,
61
+ ...meta
62
+ }
63
+ };
64
+ }
65
+
66
+ function childrenToRoutes(
67
+ children: PageFile[] = [],
68
+ noMask: any[]
69
+ ): RouteRecordRaw[] {
70
+ return children.map((child) => {
71
+ const { id, dir, mask = true, title, icon, hidden } = child;
72
+ if (dir) {
73
+ return {
74
+ path: '',
75
+ name: `Dir_${id}`,
76
+ redirect: `/page/${id}`,
77
+ meta: {
78
+ hidden,
79
+ title,
80
+ alwaysShow: true,
81
+ icon: toElIcon(icon)
82
+ },
83
+ children: childrenToRoutes(child.children, noMask)
84
+ };
85
+ }
86
+ if (!mask) {
87
+ noMask.push(createNoMaskRoute(child));
88
+ }
89
+ return createPageRoute(child);
90
+ });
91
+ }
92
+
93
+ function pagesToRoutes(pages: PageFile[], layout: any) {
94
+ const noMask: any[] = [];
95
+ const items: any[] = [];
96
+ for (const page of pages) {
97
+ const { id, dir, mask = true, title, icon, hidden, children = [] } = page;
98
+ if (!dir && !mask) {
99
+ noMask.push(createNoMaskRoute(page));
100
+ }
101
+
102
+ if (children.length) {
103
+ items.push({
104
+ path: '/',
105
+ name: `Root_${id}`,
106
+ component: layout,
107
+ meta: {
108
+ hidden,
109
+ title,
110
+ alwaysShow: true,
111
+ icon: toElIcon(icon)
112
+ },
113
+ children: childrenToRoutes(children, noMask)
114
+ });
115
+ } else {
116
+ items.push({
117
+ path: '/',
118
+ name: `Root_${id}`,
119
+ component: layout,
120
+ meta: {
121
+ hidden,
122
+ title,
123
+ alwaysShow: !!dir,
124
+ icon: toElIcon(icon)
125
+ },
126
+ children: dir ? [] : [createPageRoute(page)]
127
+ });
128
+ }
129
+ }
130
+ return [...noMask, ...items];
131
+ }
132
+
133
+ export function setupElementAdmin(options: SetupElementAdminOptions) {
134
+ const { id, app, ready } = options;
135
+ const adapter = createAdapter({ loading, notify });
136
+ const service = new LocalService(createServiceRequest(notify));
137
+ const modules = createModules();
138
+ const { provider, onReady } = createProvider({
139
+ nodeEnv: process.env.NODE_ENV as NodeEnv,
140
+ mode: ContextMode.Raw,
141
+ modules,
142
+ adapter,
143
+ service,
144
+ dependencies: {
145
+ Vue: () => import('vue'),
146
+ VueRouter: () => import('vue-router')
147
+ },
148
+ project: {
149
+ id
150
+ }
151
+ });
152
+
153
+ onReady(async () => {
154
+ app.use(provider);
155
+ ready && ready();
156
+ });
157
+
158
+ if (process.env.NODE_ENV === 'production') {
159
+ autoUpdate();
160
+ }
161
+ }
162
+
163
+ export async function setupElementAdminRoutes(
164
+ options: setupElementAdminRoutesOptions
165
+ ) {
166
+ const modules = createModules();
167
+ const { id, layout, routes, router } = options;
168
+ const loader = modules[`.vtj/projects/${id}.json`];
169
+ if (loader) {
170
+ const project = ((await loader()) as any).default as ProjectSchema;
171
+ const pages = project.pages || [];
172
+ const pageRoutes = pagesToRoutes(pages, layout);
173
+ console.log(pageRoutes);
174
+ routes.unshift(...pageRoutes);
175
+ routes.forEach((item) => {
176
+ router.addRoute(item);
177
+ });
178
+ }
179
+ }
@@ -0,0 +1 @@
1
+ export * from './element-admin';