UNPKG

4.64 kBJavaScriptView Raw
1function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
3function _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
5function _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
7function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
8
9function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
10
11function _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
13import "core-js/modules/es.object.to-string.js";
14import "core-js/modules/es.promise.js";
15import "core-js/modules/es.regexp.exec.js";
16import "core-js/modules/es.string.search.js";
17import "core-js/modules/es.array.concat.js";
18import "core-js/modules/es.string.split.js";
19import "core-js/modules/es.object.assign.js";
20import "core-js/modules/es.string.match.js";
21import "core-js/modules/es.symbol.js";
22import "core-js/modules/es.symbol.description.js";
23import "core-js/modules/es.symbol.iterator.js";
24import "core-js/modules/es.array.iterator.js";
25import "core-js/modules/es.string.iterator.js";
26import "core-js/modules/web.dom-collections.iterator.js";
27import "core-js/modules/es.array.from.js";
28import "core-js/modules/es.array.slice.js";
29import "core-js/modules/es.function.name.js";
30import global from 'global';
31import qs from 'qs';
32import { addons, makeDecorator } from '@storybook/addons';
33import { STORY_CHANGED, SELECT_STORY } from '@storybook/core-events';
34import { toId } from '@storybook/csf';
35import { PARAM_KEY } from './constants';
36var document = global.document,
37 HTMLElement = global.HTMLElement;
38export var navigate = function navigate(params) {
39 return addons.getChannel().emit(SELECT_STORY, params);
40};
41export 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
59var valueOrCall = function valueOrCall(args) {
60 return function (value) {
61 return typeof value === 'function' ? value.apply(void 0, _toConsumableArray(args)) : value;
62 };
63};
64
65export 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
88var 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
109var hasListener = false;
110
111var on = function on() {
112 if (!hasListener) {
113 hasListener = true;
114 document.addEventListener('click', linksListener);
115 }
116};
117
118var off = function off() {
119 if (hasListener) {
120 hasListener = false;
121 document.removeEventListener('click', linksListener);
122 }
123};
124
125export 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