UNPKG

3.31 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.array.for-each");
4
5require("core-js/modules/es.object.assign");
6
7require("core-js/modules/es.object.to-string");
8
9require("core-js/modules/es.object.values");
10
11require("core-js/modules/es.promise");
12
13require("core-js/modules/web.dom-collections.for-each");
14
15Object.defineProperty(exports, "__esModule", {
16 value: true
17});
18exports.addons = exports.AddonStore = void 0;
19
20var _global = _interopRequireDefault(require("global"));
21
22var _clientLogger = require("@storybook/client-logger");
23
24var _types = require("./types");
25
26function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
27
28function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29
30var AddonStore = function AddonStore() {
31 var _this = this;
32
33 _classCallCheck(this, AddonStore);
34
35 this.loaders = {};
36 this.elements = {};
37 this.config = {};
38 this.channel = void 0;
39 this.promise = void 0;
40 this.resolve = void 0;
41
42 this.getChannel = function () {
43 // this.channel should get overwritten by setChannel. If it wasn't called (e.g. in non-browser environment), throw.
44 if (!_this.channel) {
45 throw new Error('Accessing non-existent addons channel, see https://storybook.js.org/basics/faq/#why-is-there-no-addons-channel');
46 }
47
48 return _this.channel;
49 };
50
51 this.ready = function () {
52 return _this.promise;
53 };
54
55 this.hasChannel = function () {
56 return !!_this.channel;
57 };
58
59 this.setChannel = function (channel) {
60 _this.channel = channel;
61
62 _this.resolve();
63 };
64
65 this.getElements = function (type) {
66 if (!_this.elements[type]) {
67 _this.elements[type] = {};
68 }
69
70 return _this.elements[type];
71 };
72
73 this.addPanel = function (name, options) {
74 _this.add(name, Object.assign({
75 type: _types.types.PANEL
76 }, options));
77 };
78
79 this.add = function (name, addon) {
80 var type = addon.type;
81
82 var collection = _this.getElements(type);
83
84 collection[name] = Object.assign({
85 id: name
86 }, addon);
87 };
88
89 this.setConfig = function (value) {
90 Object.assign(_this.config, value);
91 };
92
93 this.getConfig = function () {
94 return _this.config;
95 };
96
97 this.register = function (name, registerCallback) {
98 if (_this.loaders[name]) {
99 _clientLogger.logger.warn("".concat(name, " was loaded twice, this could have bad side-effects"));
100 }
101
102 _this.loaders[name] = registerCallback;
103 };
104
105 this.loadAddons = function (api) {
106 Object.values(_this.loaders).forEach(function (value) {
107 return value(api);
108 });
109 };
110
111 this.promise = new Promise(function (res) {
112 _this.resolve = function () {
113 return res(_this.getChannel());
114 };
115 });
116}; // Enforce addons store to be a singleton
117
118
119exports.AddonStore = AddonStore;
120var KEY = '__STORYBOOK_ADDONS';
121
122function getAddonsStore() {
123 if (!_global["default"][KEY]) {
124 _global["default"][KEY] = new AddonStore();
125 }
126
127 return _global["default"][KEY];
128} // Exporting this twice in order to to be able to import it like { addons } instead of 'addons'
129// prefer import { addons } from '@storybook/addons' over import addons from '@storybook/addons'
130//
131// See public_api.ts
132
133
134var addons = getAddonsStore();
135exports.addons = addons;
\No newline at end of file