UNPKG

7.33 kBJavaScriptView Raw
1import "core-js/modules/es.symbol.js";
2var _excluded = ["properties"];
3
4function _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; }
5
6function _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; }
7
8import "core-js/modules/es.array.find.js";
9import "core-js/modules/es.object.to-string.js";
10import "core-js/modules/es.object.keys.js";
11import "core-js/modules/es.regexp.exec.js";
12import "core-js/modules/es.string.split.js";
13import "core-js/modules/es.string.ends-with.js";
14import "core-js/modules/es.array.map.js";
15import "core-js/modules/es.array.slice.js";
16import "core-js/modules/es.array.concat.js";
17import "core-js/modules/web.dom-collections.for-each.js";
18import "core-js/modules/es.object.assign.js";
19import React from 'react';
20import { useParameter } from '@storybook/api';
21import { styled } from '@storybook/theming';
22import { Link } from '@storybook/router';
23import { SyntaxHighlighter } from '@storybook/components'; // @ts-expect-error Typedefs don't currently expose `createElement` even though it exists
24
25import { createElement as createSyntaxHighlighterElement } from 'react-syntax-highlighter';
26var StyledStoryLink = styled(Link)(function (_ref) {
27 var theme = _ref.theme;
28 return {
29 display: 'block',
30 textDecoration: 'none',
31 borderRadius: theme.appBorderRadius,
32 color: 'inherit',
33 '&:hover': {
34 background: theme.background.hoverable
35 }
36 };
37});
38var SelectedStoryHighlight = styled.div(function (_ref2) {
39 var theme = _ref2.theme;
40 return {
41 background: theme.background.hoverable,
42 borderRadius: theme.appBorderRadius
43 };
44});
45var StyledSyntaxHighlighter = styled(SyntaxHighlighter)(function (_ref3) {
46 var theme = _ref3.theme;
47 return {
48 fontSize: theme.typography.size.s2 - 1
49 };
50});
51
52var areLocationsEqual = function areLocationsEqual(a, b) {
53 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;
54};
55
56export var StoryPanel = function StoryPanel(_ref4) {
57 var api = _ref4.api;
58 var story = api.getCurrentStoryData();
59 var selectedStoryRef = React.useRef(null);
60
61 var _useParameter = useParameter('storySource', {
62 source: 'loading source...'
63 }),
64 source = _useParameter.source,
65 locationsMap = _useParameter.locationsMap;
66
67 var currentLocation = locationsMap ? locationsMap[Object.keys(locationsMap).find(function (key) {
68 var sourceLoaderId = key.split('--');
69 return story.id.endsWith(sourceLoaderId[sourceLoaderId.length - 1]);
70 })] : undefined;
71 React.useEffect(function () {
72 if (selectedStoryRef.current) {
73 selectedStoryRef.current.scrollIntoView();
74 }
75 }, [selectedStoryRef.current]);
76
77 var createPart = function createPart(_ref5) {
78 var rows = _ref5.rows,
79 stylesheet = _ref5.stylesheet,
80 useInlineStyles = _ref5.useInlineStyles;
81 return rows.map(function (node, i) {
82 return createSyntaxHighlighterElement({
83 node: node,
84 stylesheet: stylesheet,
85 useInlineStyles: useInlineStyles,
86 key: "code-segment".concat(i)
87 });
88 });
89 };
90
91 var createStoryPart = function createStoryPart(_ref6) {
92 var rows = _ref6.rows,
93 stylesheet = _ref6.stylesheet,
94 useInlineStyles = _ref6.useInlineStyles,
95 location = _ref6.location,
96 id = _ref6.id,
97 refId = _ref6.refId;
98 var first = location.startLoc.line - 1;
99 var last = location.endLoc.line;
100 var storyRows = rows.slice(first, last);
101 var storySource = createPart({
102 rows: storyRows,
103 stylesheet: stylesheet,
104 useInlineStyles: useInlineStyles
105 });
106 var storyKey = "".concat(first, "-").concat(last);
107
108 if (currentLocation && areLocationsEqual(location, currentLocation)) {
109 return /*#__PURE__*/React.createElement(SelectedStoryHighlight, {
110 key: storyKey,
111 ref: selectedStoryRef
112 }, storySource);
113 }
114
115 return /*#__PURE__*/React.createElement(StyledStoryLink, {
116 to: refId ? "/story/".concat(refId, "_").concat(id) : "/story/".concat(id),
117 key: storyKey
118 }, storySource);
119 };
120
121 var createParts = function createParts(_ref7) {
122 var rows = _ref7.rows,
123 stylesheet = _ref7.stylesheet,
124 useInlineStyles = _ref7.useInlineStyles;
125 var parts = [];
126 var lastRow = 0;
127 Object.keys(locationsMap).forEach(function (key) {
128 var location = locationsMap[key];
129 var first = location.startLoc.line - 1;
130 var last = location.endLoc.line;
131 var kind = story.kind,
132 refId = story.refId; // source loader ids are different from story id
133
134 var sourceIdParts = key.split('--');
135 var id = api.storyId(kind, sourceIdParts[sourceIdParts.length - 1]);
136 var start = createPart({
137 rows: rows.slice(lastRow, first),
138 stylesheet: stylesheet,
139 useInlineStyles: useInlineStyles
140 });
141 var storyPart = createStoryPart({
142 rows: rows,
143 stylesheet: stylesheet,
144 useInlineStyles: useInlineStyles,
145 location: location,
146 id: id,
147 refId: refId
148 });
149 parts.push(start);
150 parts.push(storyPart);
151 lastRow = last;
152 });
153 var lastPart = createPart({
154 rows: rows.slice(lastRow),
155 stylesheet: stylesheet,
156 useInlineStyles: useInlineStyles
157 });
158 parts.push(lastPart);
159 return parts;
160 };
161
162 var lineRenderer = function lineRenderer(_ref8) {
163 var rows = _ref8.rows,
164 stylesheet = _ref8.stylesheet,
165 useInlineStyles = _ref8.useInlineStyles;
166 // because of the usage of lineRenderer, all lines will be wrapped in a span
167 // these spans will receive all classes on them for some reason
168 // which makes colours cascade incorrectly
169 // this removed that list of classnames
170 var myrows = rows.map(function (_ref9) {
171 var properties = _ref9.properties,
172 rest = _objectWithoutProperties(_ref9, _excluded);
173
174 return Object.assign({}, rest, {
175 properties: {
176 className: []
177 }
178 });
179 });
180
181 if (!locationsMap || !Object.keys(locationsMap).length) {
182 return createPart({
183 rows: myrows,
184 stylesheet: stylesheet,
185 useInlineStyles: useInlineStyles
186 });
187 }
188
189 var parts = createParts({
190 rows: myrows,
191 stylesheet: stylesheet,
192 useInlineStyles: useInlineStyles
193 });
194 return /*#__PURE__*/React.createElement("span", null, parts);
195 };
196
197 return story ? /*#__PURE__*/React.createElement(StyledSyntaxHighlighter, {
198 language: "jsx",
199 showLineNumbers: true,
200 renderer: lineRenderer,
201 format: false,
202 copyable: false,
203 padded: true
204 }, source) : null;
205};
\No newline at end of file