-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
StoryPanel.tsx
185 lines (160 loc) · 5.82 KB
/
StoryPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import React from 'react';
import { type API, useParameter } from '@storybook/manager-api';
import { styled } from '@storybook/theming';
import { Link } from '@storybook/router';
import {
SyntaxHighlighter,
type SyntaxHighlighterProps,
type SyntaxHighlighterRendererProps,
} from '@storybook/components';
import invariant from 'tiny-invariant';
// @ts-expect-error Typedefs don't currently expose `createElement` even though it exists
import { createElement as createSyntaxHighlighterElement } from 'react-syntax-highlighter';
import type { SourceBlock, LocationsMap } from '@storybook/source-loader';
const StyledStoryLink = styled(Link)<{ to: string; key: string }>(({ theme }) => ({
display: 'block',
textDecoration: 'none',
borderRadius: theme.appBorderRadius,
color: 'inherit',
'&:hover': {
background: theme.background.hoverable,
},
}));
const SelectedStoryHighlight = styled.div(({ theme }) => ({
background: theme.background.hoverable,
borderRadius: theme.appBorderRadius,
}));
const StyledSyntaxHighlighter = styled(SyntaxHighlighter)<SyntaxHighlighterProps>(({ theme }) => ({
fontSize: theme.typography.size.s2 - 1,
}));
const areLocationsEqual = (a: SourceBlock, b: SourceBlock): boolean =>
a.startLoc.line === b.startLoc.line &&
a.startLoc.col === b.startLoc.col &&
a.endLoc.line === b.endLoc.line &&
a.endLoc.col === b.endLoc.col;
interface StoryPanelProps {
api: API;
}
interface SourceParams {
source?: string;
locationsMap?: LocationsMap;
}
interface DocsParams {
source?: { originalSource?: string };
}
export const StoryPanel: React.FC<StoryPanelProps> = ({ api }) => {
const story = api.getCurrentStoryData();
const selectedStoryRef = React.useRef<HTMLDivElement>(null);
const { source: loaderSource, locationsMap }: SourceParams = useParameter('storySource', {});
const { source: { originalSource: docsSource } = {} }: DocsParams = useParameter('docs', {});
// prefer to use the source from source-loader, but fallback to
// source provided by csf-plugin for vite usage
const source = loaderSource || docsSource || 'loading source...';
const currentLocationIndex = locationsMap
? Object.keys(locationsMap).find((key: string) => {
const sourceLoaderId = key.split('--');
return story.id.endsWith(sourceLoaderId[sourceLoaderId.length - 1]);
})
: undefined;
const currentLocation =
locationsMap && currentLocationIndex ? locationsMap[currentLocationIndex] : undefined;
React.useEffect(() => {
if (selectedStoryRef.current) {
selectedStoryRef.current.scrollIntoView();
}
}, [selectedStoryRef.current]);
const createPart = ({
rows,
stylesheet,
useInlineStyles,
}: SyntaxHighlighterRendererProps): React.ReactNode[] =>
rows.map((node, i) =>
createSyntaxHighlighterElement({
node,
stylesheet,
useInlineStyles,
key: `code-segment${i}`,
})
);
const createStoryPart = ({
rows,
stylesheet,
useInlineStyles,
location,
id,
refId,
}: SyntaxHighlighterRendererProps & { location: SourceBlock; id: string; refId?: string }) => {
const first = location.startLoc.line - 1;
const last = location.endLoc.line;
const storyRows = rows.slice(first, last);
const storySource = createPart({ rows: storyRows, stylesheet, useInlineStyles });
const storyKey = `${first}-${last}`;
if (currentLocation && areLocationsEqual(location, currentLocation)) {
return (
<SelectedStoryHighlight key={storyKey} ref={selectedStoryRef}>
{storySource}
</SelectedStoryHighlight>
);
}
return (
<StyledStoryLink to={refId ? `/story/${refId}_${id}` : `/story/${id}`} key={storyKey}>
{storySource}
</StyledStoryLink>
);
};
const createParts = ({ rows, stylesheet, useInlineStyles }: SyntaxHighlighterRendererProps) => {
const parts: React.ReactNode[] = [];
let lastRow = 0;
invariant(locationsMap, 'locationsMap should be defined while creating parts');
Object.keys(locationsMap).forEach((key) => {
const location = locationsMap[key];
const first = location.startLoc.line - 1;
const last = location.endLoc.line;
const { title, refId } = story;
// source loader ids are different from story id
const sourceIdParts = key.split('--');
const id = api.storyId(title, sourceIdParts[sourceIdParts.length - 1]);
const start = createPart({ rows: rows.slice(lastRow, first), stylesheet, useInlineStyles });
const storyPart = createStoryPart({ rows, stylesheet, useInlineStyles, location, id, refId });
parts.push(...start);
parts.push(storyPart);
lastRow = last;
});
const lastPart = createPart({ rows: rows.slice(lastRow), stylesheet, useInlineStyles });
parts.push(...lastPart);
return parts;
};
const lineRenderer = ({
rows,
stylesheet,
useInlineStyles,
}: SyntaxHighlighterRendererProps): React.ReactNode => {
// because of the usage of lineRenderer, all lines will be wrapped in a span
// these spans will receive all classes on them for some reason
// which makes colours cascade incorrectly
// this removed that list of classnames
const myrows = rows.map(({ properties, ...rest }) => ({
...rest,
properties: { className: [] },
}));
if (!locationsMap || !Object.keys(locationsMap).length) {
return createPart({ rows: myrows, stylesheet, useInlineStyles });
}
const parts = createParts({ rows: myrows, stylesheet, useInlineStyles });
return <span>{parts}</span>;
};
return story ? (
<StyledSyntaxHighlighter
language="jsx"
showLineNumbers
renderer={lineRenderer}
format={false}
copyable={false}
padded
wrapLongLines
lineProps={{ style: { whiteSpace: 'pre' } }}
>
{source}
</StyledSyntaxHighlighter>
) : null;
};