feat(wrapComponents): new chain configuration option (#7236)

This commit provides a backward compatible mechanism to chain wrap 
an individual component multiple times

`Chain` mode: allow chaining of plugins on a given component
`Legacy` mode: last plugin to wrap a given component will supercede others

* chore: Add unit test for wrapComponent wrapping

* doc: Add documentation about the new pluginsOptions configuration

* doc: Add a sidenote on plugin-api page

Co-authored-by: Tim Lai <timothy.lai@gmail.com>
This commit is contained in:
Damien
2021-05-21 00:41:11 +02:00
committed by GitHub
parent 60ac6d24ba
commit 516e666f1c
5 changed files with 99 additions and 11 deletions

View File

@@ -93,6 +93,13 @@ export default function SwaggerUI(opts) {
plugins: [
],
pluginsOptions: {
// Behavior during plugin registration. Can be :
// - legacy (default) : the current behavior for backward compatibility last plugin takes precedence over the others
// - chain : chain wrapComponents when targeting the same core component
pluginLoadType: "legacy"
},
// Initial state
initialState: { },
@@ -118,6 +125,7 @@ export default function SwaggerUI(opts) {
configs: constructorConfig.configs
},
plugins: constructorConfig.presets,
pluginsOptions: constructorConfig.pluginsOptions,
state: deepExtend({
layout: {
layout: constructorConfig.layout,

View File

@@ -35,6 +35,7 @@ export default class Store {
deepExtend(this, {
state: {},
plugins: [],
pluginsOptions: {},
system: {
configs: {},
fn: {},
@@ -63,7 +64,7 @@ export default class Store {
}
register(plugins, rebuild=true) {
var pluginSystem = combinePlugins(plugins, this.getSystem())
var pluginSystem = combinePlugins(plugins, this.getSystem(), this.pluginsOptions)
systemExtend(this.system, pluginSystem)
if(rebuild) {
this.buildSystem()
@@ -310,19 +311,21 @@ export default class Store {
}
function combinePlugins(plugins, toolbox) {
function combinePlugins(plugins, toolbox, pluginOptions) {
if(isObject(plugins) && !isArray(plugins)) {
return assignDeep({}, plugins)
}
if(isFunc(plugins)) {
return combinePlugins(plugins(toolbox), toolbox)
return combinePlugins(plugins(toolbox), toolbox, pluginOptions)
}
if(isArray(plugins)) {
const dest = pluginOptions.pluginLoadType === "chain" ? toolbox.getComponents() : {}
return plugins
.map(plugin => combinePlugins(plugin, toolbox))
.reduce(systemExtend, {})
.map(plugin => combinePlugins(plugin, toolbox, pluginOptions))
.reduce(systemExtend, dest)
}
return {}