@brightspace-ui/core 2.161.1 → 2.162.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/helpers/README.md +39 -0
- package/helpers/plugins.js +46 -0
- package/package.json +1 -1
package/helpers/README.md
CHANGED
@@ -187,6 +187,45 @@ import { getLegacyOffsetParent } from '@brightspace-ui/core/helpers/offsetParent
|
|
187
187
|
const offsetParent = getLegacyOffsetParent(element);
|
188
188
|
```
|
189
189
|
|
190
|
+
## Plugins
|
191
|
+
|
192
|
+
Plugin helpers provide a way for modules to implement and register objects that can be plugged into other project's modules without requiring the plugin consumer to import those modules or objects directly. A higher order module (ex. in BSI) is responsible for importing the plugin registrations.
|
193
|
+
|
194
|
+
The plugin implementor uses the `registerPlugin` helper method to make its implementation available to interested consumers. The implementor provides a key for the set of plugins in which to register and the plugin implementation.
|
195
|
+
|
196
|
+
Optionally, an object to specify a `key` for the plugin and/or the `sort` value may be provided. The `key` is useful if consumers intend to request a specific plugin, while the `sort` is useful in cases where the order of plugins is important to consumers. If `sort` is not specified for at least one plugin, they will be provided to consumers in registration order.
|
197
|
+
|
198
|
+
**Important!** plugin registrations should defer loading their dependencies using dynamic imports. They should **not** be synchronously imported in the registration module.
|
199
|
+
|
200
|
+
```js
|
201
|
+
import { registerPlugin } from '@brightspace-ui/core/helpers/plugins.js';
|
202
|
+
|
203
|
+
// Provide plugin set key, plugin
|
204
|
+
registerPlugin('foo-plugins', { prop1: 'some value' });
|
205
|
+
registerPlugin('foo-plugins', { prop1: 'other value' });
|
206
|
+
|
207
|
+
// Optionally provide key and/or sort value
|
208
|
+
registerPlugin('foo-plugins', { prop1: 'some value' }, { key: 'key-1', sort: 1 });
|
209
|
+
registerPlugin('foo-plugins', { prop1: 'other value' }, { key: 'key-2', sort: 2 });
|
210
|
+
|
211
|
+
// Defer loading dependencies until needed
|
212
|
+
registerPlugin('foo-plugins', { getRenderer: async () => {
|
213
|
+
return (await import('./some-module.js')).renderer
|
214
|
+
}});
|
215
|
+
```
|
216
|
+
|
217
|
+
The plugin consumer uses the `getPlugins` helper method to get references to the registered plugins by providing a key for the set of plugins. If the consumer knows the key of the plugin it needs, it can request the plugin by using `tryGetPluginByKey` and specifying the plugin set key and plugin key.
|
218
|
+
|
219
|
+
```js
|
220
|
+
import { getPlugins, tryGetPluginByKey } from '@brightspace-ui/core/helpers/plugins.js';
|
221
|
+
|
222
|
+
// Call getPlugins to get plugins
|
223
|
+
const plugins = getPlugins('foo-plugins');
|
224
|
+
|
225
|
+
// Call tryGetPluginByKey to get a specific plugin by key
|
226
|
+
const plugin = tryGetPluginByKey('foo-plugins', 'key-1');
|
227
|
+
```
|
228
|
+
|
190
229
|
## queueMicrotask
|
191
230
|
|
192
231
|
A polyfill for [queueMicrotask](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/queueMicrotask). For more information on microtasks, read [this article from Mozilla](https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide).
|
@@ -0,0 +1,46 @@
|
|
1
|
+
const pluginSets = new Map();
|
2
|
+
|
3
|
+
function getPluginSet(setKey) {
|
4
|
+
let pluginSet = pluginSets.get(setKey);
|
5
|
+
if (pluginSet) return pluginSet;
|
6
|
+
|
7
|
+
pluginSet = { plugins: [], requested: false, requiresSorting: false };
|
8
|
+
pluginSets.set(setKey, pluginSet);
|
9
|
+
return pluginSet;
|
10
|
+
}
|
11
|
+
|
12
|
+
export function getPlugins(setKey) {
|
13
|
+
const pluginSet = getPluginSet(setKey);
|
14
|
+
pluginSet.requested = true;
|
15
|
+
if (pluginSet.requiresSorting) {
|
16
|
+
pluginSet.plugins.sort((item1, item2) => item1.options.sort - item2.options.sort);
|
17
|
+
pluginSet.requiresSorting = false;
|
18
|
+
}
|
19
|
+
return pluginSet.plugins.map(item => item.plugin);
|
20
|
+
}
|
21
|
+
|
22
|
+
export function registerPlugin(setKey, plugin, options) {
|
23
|
+
const pluginSet = getPluginSet(setKey);
|
24
|
+
|
25
|
+
if (pluginSet.requested) {
|
26
|
+
throw new Error(`Plugin Set "${setKey}" has already been requested. Additional plugin registrations would result in stale consumer plugins.`);
|
27
|
+
} else if (options?.key !== undefined) {
|
28
|
+
if (pluginSet.plugins.find(registeredPlugin => registeredPlugin.options.key === options?.key)) {
|
29
|
+
throw new Error(`Plugin Set "${setKey}" already has a plugin with the key "${options.key}".`);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
pluginSet.plugins.push({ plugin, options: Object.assign({ key: undefined, sort: 0 }, options) });
|
34
|
+
pluginSet.requiresSorting = pluginSet.requiresSorting || (options?.sort !== undefined);
|
35
|
+
}
|
36
|
+
|
37
|
+
// Do not import! Testing only!!
|
38
|
+
export function resetPlugins() {
|
39
|
+
pluginSets.clear();
|
40
|
+
}
|
41
|
+
|
42
|
+
export function tryGetPluginByKey(setKey, pluginKey) {
|
43
|
+
const pluginSet = pluginSets.get(setKey);
|
44
|
+
const plugin = pluginSet?.plugins.find(plugin => plugin.options.key === pluginKey)?.plugin;
|
45
|
+
return plugin || null;
|
46
|
+
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@brightspace-ui/core",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.162.0",
|
4
4
|
"description": "A collection of accessible, free, open-source web components for building Brightspace applications",
|
5
5
|
"type": "module",
|
6
6
|
"repository": "https://github.com/BrightspaceUI/core.git",
|