-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.ts
280 lines (246 loc) · 7.46 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { RefObject } from 'react';
import builtin from '@/plugins/builtin';
import * as allPlugins from '@/plugins';
import type * as Type from '@/common/interface';
import { LOGGED_TOKEN_STORAGE_KEY } from '@/common/constants';
import { getPluginsStatus } from '@/services';
import Storage from '@/utils/storage';
import request from '@/utils/request';
import { initI18nResource } from './utils';
import { Plugin, PluginInfo, PluginType } from './interface';
/**
* This information is to be defined for all components.
* It may be used for feature upgrades or version compatibility processing.
*
* @field slug_name: Unique identity string for the plugin, usually configured in `info.yaml`
* @field type: The type of plugin is defined and a single type of plugin can have multiple implementations.
* For example, a plugin of type `connector` can have a `google` implementation and a `github` implementation.
* `PluginRender` automatically renders the plug-in types already included in `PluginType`.
* @field name: Plugin name, optionally configurable. Usually read from the `i18n` file
* @field description: Plugin description, optionally configurable. Usually read from the `i18n` file
*/
class Plugins {
plugins: Plugin[] = [];
registeredPlugins: Type.ActivatedPlugin[] = [];
initialization: Promise<void>;
constructor() {
this.initialization = this.init();
}
async init() {
this.registerBuiltin();
// Note: The /install stage does not allow access to the getPluginsStatus api, so an initial value needs to be given
const plugins = (await getPluginsStatus().catch(() => [])) || [];
this.registeredPlugins = plugins.filter((p) => p.enabled);
await this.registerPlugins();
}
refresh() {
this.plugins = [];
this.init();
}
validate(plugin: Plugin) {
if (!plugin) {
return false;
}
const { info } = plugin;
const { slug_name, type } = info;
if (!slug_name) {
return false;
}
if (!type) {
return false;
}
return true;
}
registerBuiltin() {
Object.keys(builtin).forEach((key) => {
const plugin = builtin[key];
// builttin plugins are always activated
// Use own internal rendering logic'
plugin.activated = true;
this.register(plugin);
});
}
registerPlugins() {
const plugins = this.registeredPlugins
.map((p) => {
const func = allPlugins[p.slug_name];
return func;
})
.filter((p) => p);
return Promise.all(plugins.map((p) => p())).then((resolvedPlugins) => {
resolvedPlugins.forEach((plugin) => this.register(plugin));
return true;
});
}
register(plugin: Plugin) {
const bool = this.validate(plugin);
if (!bool) {
return;
}
if (plugin.i18nConfig) {
initI18nResource(plugin.i18nConfig);
}
plugin.activated = true;
this.plugins.push(plugin);
}
getPlugin(slug_name: string) {
return this.plugins.find((p) => p.info.slug_name === slug_name);
}
getOnePluginHooks(slug_name: string) {
const plugin = this.getPlugin(slug_name);
return plugin?.hooks;
}
getPlugins() {
return this.plugins;
}
}
const plugins = new Plugins();
const getRoutePlugins = async () => {
await plugins.initialization;
return plugins
.getPlugins()
.filter((plugin) => plugin.info.type === PluginType.Route);
};
const defaultProps = () => {
const token = Storage.get(LOGGED_TOKEN_STORAGE_KEY) || '';
return {
key: token,
headers: {
Authorization: token,
},
};
};
const validateRoutePlugin = async (slugName) => {
let registeredPlugin;
if (plugins.registeredPlugins.length === 0) {
const pluginsStatus = await getPluginsStatus();
registeredPlugin = pluginsStatus.find((p) => p.slug_name === slugName);
} else {
registeredPlugin = plugins.registeredPlugins.find(
(p) => p.slug_name === slugName,
);
}
return Boolean(registeredPlugin?.enabled);
};
const mergeRoutePlugins = async (routes) => {
const routePlugins = await getRoutePlugins();
console.log('routePlugins', routePlugins);
if (routePlugins.length === 0) {
return routes;
}
routes.forEach((route) => {
if (route.page === 'pages/Layout') {
route.children?.forEach((child) => {
if (child.page === 'pages/SideNavLayout') {
routePlugins.forEach((plugin) => {
const { route: path, slug_name } = plugin.info;
child.children.push({
page: plugin.component,
path,
loader: async () => {
const bool = await validateRoutePlugin(slug_name);
return bool;
},
guard: (params) => {
if (params.loaderData) {
return {
ok: true,
};
}
return {
ok: false,
error: {
code: 404,
},
};
},
});
});
}
});
}
});
return routes;
};
/**
* Only used to enhance the capabilities of the markdown editor
* Add RefObject type to solve the problem of dom being null in hooks
*/
const useRenderHtmlPlugin = (
element: HTMLElement | RefObject<HTMLElement> | null,
) => {
plugins
.getPlugins()
.filter((plugin) => {
return (
plugin.activated &&
plugin.hooks?.useRender &&
(plugin.info.type === PluginType.Editor ||
plugin.info.type === PluginType.Render)
);
})
.forEach((plugin) => {
plugin.hooks?.useRender?.forEach((hook) => {
hook(element, request);
});
});
};
// Only for render type plugins
const useRenderPlugin = (
element: HTMLElement | RefObject<HTMLElement> | null,
) => {
return plugins
.getPlugins()
.filter((plugin) => {
return (
plugin.activated &&
plugin.hooks?.useRender &&
plugin.info.type === PluginType.Render
);
})
.forEach((plugin) => {
plugin.hooks?.useRender?.forEach((hook) => {
hook(element, request);
});
});
};
// Only one captcha type plug-in can be enabled at the same time
const useCaptchaPlugin = (key: Type.CaptchaKey) => {
const captcha = plugins
.getPlugins()
.filter(
(plugin) => plugin.info.type === PluginType.Captcha && plugin.activated,
);
const pluginHooks = plugins.getOnePluginHooks(captcha[0]?.info.slug_name);
return pluginHooks?.useCaptcha?.({
captchaKey: key,
commonProps: defaultProps(),
});
};
export type { Plugin, PluginInfo };
export {
useRenderHtmlPlugin,
mergeRoutePlugins,
useCaptchaPlugin,
useRenderPlugin,
PluginType,
};
export default plugins;