1 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
2 |
|
3 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
4 |
|
5 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
6 |
|
7 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
8 |
|
9 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
10 |
|
11 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
12 |
|
13 | import "core-js/modules/es.object.to-string.js";
|
14 | import "core-js/modules/es.promise.js";
|
15 | import "core-js/modules/es.regexp.exec.js";
|
16 | import "core-js/modules/es.string.search.js";
|
17 | import "core-js/modules/es.array.concat.js";
|
18 | import "core-js/modules/es.string.split.js";
|
19 | import "core-js/modules/es.object.assign.js";
|
20 | import "core-js/modules/es.string.match.js";
|
21 | import "core-js/modules/es.symbol.js";
|
22 | import "core-js/modules/es.symbol.description.js";
|
23 | import "core-js/modules/es.symbol.iterator.js";
|
24 | import "core-js/modules/es.array.iterator.js";
|
25 | import "core-js/modules/es.string.iterator.js";
|
26 | import "core-js/modules/web.dom-collections.iterator.js";
|
27 | import "core-js/modules/es.array.from.js";
|
28 | import "core-js/modules/es.array.slice.js";
|
29 | import "core-js/modules/es.function.name.js";
|
30 | import global from 'global';
|
31 | import qs from 'qs';
|
32 | import { addons, makeDecorator } from '@storybook/addons';
|
33 | import { STORY_CHANGED, SELECT_STORY } from '@storybook/core-events';
|
34 | import { toId } from '@storybook/csf';
|
35 | import { PARAM_KEY } from './constants';
|
36 | var document = global.document,
|
37 | HTMLElement = global.HTMLElement;
|
38 | export var navigate = function navigate(params) {
|
39 | return addons.getChannel().emit(SELECT_STORY, params);
|
40 | };
|
41 | export var hrefTo = function hrefTo(title, name) {
|
42 | return new Promise(function (resolve) {
|
43 | var location = document.location;
|
44 | var query = qs.parse(location.search, {
|
45 | ignoreQueryPrefix: true
|
46 | });
|
47 | var existingId = [].concat(query.id)[0];
|
48 | var titleToLink = title || existingId.split('--', 2)[0];
|
49 | var id = toId(titleToLink, name);
|
50 | var url = "".concat(location.origin + location.pathname, "?").concat(qs.stringify(Object.assign({}, query, {
|
51 | id: id
|
52 | }), {
|
53 | encode: false
|
54 | }));
|
55 | resolve(url);
|
56 | });
|
57 | };
|
58 |
|
59 | var valueOrCall = function valueOrCall(args) {
|
60 | return function (value) {
|
61 | return typeof value === 'function' ? value.apply(void 0, _toConsumableArray(args)) : value;
|
62 | };
|
63 | };
|
64 |
|
65 | export var linkTo = function linkTo(idOrTitle, nameInput) {
|
66 | return function () {
|
67 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
68 | args[_key] = arguments[_key];
|
69 | }
|
70 |
|
71 | var resolver = valueOrCall(args);
|
72 | var title = resolver(idOrTitle);
|
73 | var name = resolver(nameInput);
|
74 |
|
75 | if (title !== null && title !== void 0 && title.match(/--/) && !name) {
|
76 | navigate({
|
77 | storyId: title
|
78 | });
|
79 | } else {
|
80 | navigate({
|
81 | kind: title,
|
82 | story: name
|
83 | });
|
84 | }
|
85 | };
|
86 | };
|
87 |
|
88 | var linksListener = function linksListener(e) {
|
89 | var target = e.target;
|
90 |
|
91 | if (!(target instanceof HTMLElement)) {
|
92 | return;
|
93 | }
|
94 |
|
95 | var element = target;
|
96 | var _element$dataset = element.dataset,
|
97 | kind = _element$dataset.sbKind,
|
98 | story = _element$dataset.sbStory;
|
99 |
|
100 | if (kind || story) {
|
101 | e.preventDefault();
|
102 | navigate({
|
103 | kind: kind,
|
104 | story: story
|
105 | });
|
106 | }
|
107 | };
|
108 |
|
109 | var hasListener = false;
|
110 |
|
111 | var on = function on() {
|
112 | if (!hasListener) {
|
113 | hasListener = true;
|
114 | document.addEventListener('click', linksListener);
|
115 | }
|
116 | };
|
117 |
|
118 | var off = function off() {
|
119 | if (hasListener) {
|
120 | hasListener = false;
|
121 | document.removeEventListener('click', linksListener);
|
122 | }
|
123 | };
|
124 |
|
125 | export var withLinks = makeDecorator({
|
126 | name: 'withLinks',
|
127 | parameterName: PARAM_KEY,
|
128 | wrapper: function wrapper(getStory, context) {
|
129 | on();
|
130 | addons.getChannel().once(STORY_CHANGED, off);
|
131 | return getStory(context);
|
132 | }
|
133 | }); |
\ | No newline at end of file |