1 | "use strict";
|
2 |
|
3 | require("core-js/modules/es.symbol");
|
4 |
|
5 | require("core-js/modules/es.symbol.description");
|
6 |
|
7 | require("core-js/modules/es.symbol.iterator");
|
8 |
|
9 | require("core-js/modules/es.array.concat");
|
10 |
|
11 | require("core-js/modules/es.array.for-each");
|
12 |
|
13 | require("core-js/modules/es.array.from");
|
14 |
|
15 | require("core-js/modules/es.array.index-of");
|
16 |
|
17 | require("core-js/modules/es.array.iterator");
|
18 |
|
19 | require("core-js/modules/es.array.map");
|
20 |
|
21 | require("core-js/modules/es.array.slice");
|
22 |
|
23 | require("core-js/modules/es.array.sort");
|
24 |
|
25 | require("core-js/modules/es.object.assign");
|
26 |
|
27 | require("core-js/modules/es.object.create");
|
28 |
|
29 | require("core-js/modules/es.object.define-property");
|
30 |
|
31 | require("core-js/modules/es.object.get-prototype-of");
|
32 |
|
33 | require("core-js/modules/es.object.keys");
|
34 |
|
35 | require("core-js/modules/es.object.set-prototype-of");
|
36 |
|
37 | require("core-js/modules/es.object.to-string");
|
38 |
|
39 | require("core-js/modules/es.string.iterator");
|
40 |
|
41 | require("core-js/modules/web.dom-collections.for-each");
|
42 |
|
43 | require("core-js/modules/web.dom-collections.iterator");
|
44 |
|
45 | Object.defineProperty(exports, "__esModule", {
|
46 | value: true
|
47 | });
|
48 | exports["default"] = void 0;
|
49 |
|
50 | var _react = _interopRequireWildcard(require("react"));
|
51 |
|
52 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
53 |
|
54 | var _theming = require("@storybook/theming");
|
55 |
|
56 | var _router = require("@storybook/router");
|
57 |
|
58 | var _components = require("@storybook/components");
|
59 |
|
60 | var _createElement = _interopRequireDefault(require("react-syntax-highlighter/dist/esm/create-element"));
|
61 |
|
62 | var _events = require("./events");
|
63 |
|
64 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
65 |
|
66 | function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
|
67 |
|
68 | function _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 |
|
70 | function _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 |
|
72 | function _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 |
|
74 | function _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 |
|
76 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
77 |
|
78 | function _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 |
|
80 | function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
81 |
|
82 | function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
|
83 |
|
84 | function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
|
85 |
|
86 | function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
|
87 |
|
88 | function _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 |
|
90 | function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
|
91 |
|
92 | var 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 |
|
105 | var 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 |
|
113 | var 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 |
|
120 | var 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 |
|
124 | var 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 |
|
130 | var StoryPanel =
|
131 |
|
132 | function (_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;
|
237 |
|
238 |
|
239 |
|
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 |
|
303 | exports["default"] = StoryPanel;
|
304 | StoryPanel.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 |