UNPKG

12.4 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.symbol");
4
5require("core-js/modules/es.symbol.description");
6
7require("core-js/modules/es.symbol.iterator");
8
9require("core-js/modules/es.array.concat");
10
11require("core-js/modules/es.array.for-each");
12
13require("core-js/modules/es.array.from");
14
15require("core-js/modules/es.array.index-of");
16
17require("core-js/modules/es.array.iterator");
18
19require("core-js/modules/es.array.map");
20
21require("core-js/modules/es.array.slice");
22
23require("core-js/modules/es.array.sort");
24
25require("core-js/modules/es.object.assign");
26
27require("core-js/modules/es.object.create");
28
29require("core-js/modules/es.object.define-property");
30
31require("core-js/modules/es.object.get-prototype-of");
32
33require("core-js/modules/es.object.keys");
34
35require("core-js/modules/es.object.set-prototype-of");
36
37require("core-js/modules/es.object.to-string");
38
39require("core-js/modules/es.string.iterator");
40
41require("core-js/modules/web.dom-collections.for-each");
42
43require("core-js/modules/web.dom-collections.iterator");
44
45Object.defineProperty(exports, "__esModule", {
46 value: true
47});
48exports["default"] = void 0;
49
50var _react = _interopRequireWildcard(require("react"));
51
52var _propTypes = _interopRequireDefault(require("prop-types"));
53
54var _theming = require("@storybook/theming");
55
56var _router = require("@storybook/router");
57
58var _components = require("@storybook/components");
59
60var _createElement = _interopRequireDefault(require("react-syntax-highlighter/dist/esm/create-element"));
61
62var _events = require("./events");
63
64function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
65
66function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
67
68function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
69
70function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
71
72function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
73
74function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
75
76function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
77
78function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
79
80function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
81
82function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
83
84function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
85
86function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
87
88function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
89
90function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
91
92var StyledStoryLink = (0, _theming.styled)(_router.Link)(function (_ref) {
93 var theme = _ref.theme;
94 return {
95 display: 'block',
96 textDecoration: 'none',
97 borderRadius: theme.appBorderRadius,
98 color: 'inherit',
99 '&:hover': {
100 background: theme.background.hoverable
101 }
102 };
103});
104
105var SelectedStoryHighlight = _theming.styled.div(function (_ref2) {
106 var theme = _ref2.theme;
107 return {
108 background: theme.background.hoverable,
109 borderRadius: theme.appBorderRadius
110 };
111});
112
113var StyledSyntaxHighlighter = (0, _theming.styled)(_components.SyntaxHighlighter)(function (_ref3) {
114 var theme = _ref3.theme;
115 return {
116 fontSize: theme.typography.size.s2 - 1
117 };
118});
119
120var areLocationsEqual = function areLocationsEqual(a, b) {
121 return a.startLoc.line === b.startLoc.line && a.startLoc.col === b.startLoc.col && a.endLoc.line === b.endLoc.line && a.endLoc.col === b.endLoc.col;
122};
123
124var getLocationKeys = function getLocationKeys(locationsMap) {
125 return locationsMap ? Array.from(Object.keys(locationsMap)).sort(function (key1, key2) {
126 return locationsMap[key1].startLoc.line - locationsMap[key2].startLoc.line;
127 }) : [];
128};
129
130var StoryPanel =
131/*#__PURE__*/
132function (_Component) {
133 _inherits(StoryPanel, _Component);
134
135 function StoryPanel() {
136 var _getPrototypeOf2;
137
138 var _this;
139
140 _classCallCheck(this, StoryPanel);
141
142 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
143 args[_key] = arguments[_key];
144 }
145
146 _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(StoryPanel)).call.apply(_getPrototypeOf2, [this].concat(args)));
147 _this.state = {
148 source: 'loading source...'
149 };
150
151 _this.setSelectedStoryRef = function (ref) {
152 _this.selectedStoryRef = ref;
153 };
154
155 _this.listener = function (_ref4) {
156 var source = _ref4.edition.source,
157 _ref4$location = _ref4.location,
158 currentLocation = _ref4$location.currentLocation,
159 locationsMap = _ref4$location.locationsMap;
160 var locationsKeys = getLocationKeys(locationsMap);
161
162 _this.setState({
163 source: source,
164 currentLocation: currentLocation,
165 locationsMap: locationsMap,
166 locationsKeys: locationsKeys
167 });
168 };
169
170 _this.createPart = function (rows, stylesheet, useInlineStyles) {
171 return rows.map(function (node, i) {
172 return (0, _createElement["default"])({
173 node: node,
174 stylesheet: stylesheet,
175 useInlineStyles: useInlineStyles,
176 key: "code-segement".concat(i)
177 });
178 });
179 };
180
181 _this.createStoryPart = function (rows, stylesheet, useInlineStyles, location, id) {
182 var currentLocation = _this.state.currentLocation;
183 var first = location.startLoc.line - 1;
184 var last = location.endLoc.line;
185 var storyRows = rows.slice(first, last);
186
187 var story = _this.createPart(storyRows, stylesheet, useInlineStyles);
188
189 var storyKey = "".concat(first, "-").concat(last);
190
191 if (location && currentLocation && areLocationsEqual(location, currentLocation)) {
192 return _react["default"].createElement(SelectedStoryHighlight, {
193 key: storyKey,
194 ref: _this.setSelectedStoryRef
195 }, story);
196 }
197
198 return _react["default"].createElement(StyledStoryLink, {
199 to: "/story/".concat(id),
200 key: storyKey
201 }, story);
202 };
203
204 _this.createParts = function (rows, stylesheet, useInlineStyles) {
205 var _this$state = _this.state,
206 locationsMap = _this$state.locationsMap,
207 locationsKeys = _this$state.locationsKeys;
208 var parts = [];
209 var lastRow = 0;
210 locationsKeys.forEach(function (key) {
211 var location = locationsMap[key];
212 var first = location.startLoc.line - 1;
213 var last = location.endLoc.line;
214
215 var start = _this.createPart(rows.slice(lastRow, first), stylesheet, useInlineStyles);
216
217 var storyPart = _this.createStoryPart(rows, stylesheet, useInlineStyles, location, key);
218
219 parts.push(start);
220 parts.push(storyPart);
221 lastRow = last;
222 });
223
224 var lastPart = _this.createPart(rows.slice(lastRow), stylesheet, useInlineStyles);
225
226 parts.push(lastPart);
227 return parts;
228 };
229
230 _this.lineRenderer = function (_ref5) {
231 var rows = _ref5.rows,
232 stylesheet = _ref5.stylesheet,
233 useInlineStyles = _ref5.useInlineStyles;
234 var _this$state2 = _this.state,
235 locationsMap = _this$state2.locationsMap,
236 locationsKeys = _this$state2.locationsKeys; // because of the usage of lineRenderer, all lines will be wrapped in a span
237 // these spans will receive all classes on them for some reason
238 // which makes colours casecade incorrectly
239 // this removed that list of classnames
240
241 var myrows = rows.map(function (_ref6) {
242 var properties = _ref6.properties,
243 rest = _objectWithoutProperties(_ref6, ["properties"]);
244
245 return Object.assign({}, rest, {
246 properties: {
247 className: []
248 }
249 });
250 });
251
252 if (!locationsMap || !locationsKeys.length) {
253 return _this.createPart(myrows, stylesheet, useInlineStyles);
254 }
255
256 var parts = _this.createParts(myrows, stylesheet, useInlineStyles);
257
258 return _react["default"].createElement("span", null, parts);
259 };
260
261 return _this;
262 }
263
264 _createClass(StoryPanel, [{
265 key: "componentDidMount",
266 value: function componentDidMount() {
267 this.mounted = true;
268 var api = this.props.api;
269 api.on(_events.EVENT_ID, this.listener);
270 }
271 }, {
272 key: "componentDidUpdate",
273 value: function componentDidUpdate() {
274 if (this.selectedStoryRef) {
275 this.selectedStoryRef.scrollIntoView();
276 }
277 }
278 }, {
279 key: "componentWillUnmount",
280 value: function componentWillUnmount() {
281 var api = this.props.api;
282 api.off(_events.EVENT_ID, this.listener);
283 }
284 }, {
285 key: "render",
286 value: function render() {
287 var active = this.props.active;
288 var source = this.state.source;
289 return active ? _react["default"].createElement(StyledSyntaxHighlighter, {
290 language: "jsx",
291 showLineNumbers: "true",
292 renderer: this.lineRenderer,
293 format: false,
294 copyable: false,
295 padded: true
296 }, source) : null;
297 }
298 }]);
299
300 return StoryPanel;
301}(_react.Component);
302
303exports["default"] = StoryPanel;
304StoryPanel.propTypes = {
305 active: _propTypes["default"].bool.isRequired,
306 api: _propTypes["default"].shape({
307 selectStory: _propTypes["default"].func.isRequired,
308 emit: _propTypes["default"].func,
309 on: _propTypes["default"].func,
310 off: _propTypes["default"].func
311 }).isRequired
312};
\No newline at end of file