-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
syntaxhighlighter.tsx
185 lines (161 loc) · 4.67 KB
/
syntaxhighlighter.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, { Component } from 'react';
import { styled } from '@storybook/theming';
import { document, window } from 'global';
import memoize from 'memoizerific';
import jsx from 'react-syntax-highlighter/languages/prism/jsx';
import bash from 'react-syntax-highlighter/languages/prism/bash';
import css from 'react-syntax-highlighter/languages/prism/css';
import html from 'react-syntax-highlighter/languages/prism/markup';
import ReactSyntaxHighlighter, { registerLanguage } from 'react-syntax-highlighter/prism-light';
import { js as beautify } from 'js-beautify';
import { ActionBar } from '../ActionBar/ActionBar';
import { ScrollArea } from '../ScrollArea/ScrollArea';
registerLanguage('jsx', jsx);
registerLanguage('bash', bash);
registerLanguage('css', css);
registerLanguage('html', html);
const themedSyntax = memoize(2)(theme =>
Object.entries(theme.code || {}).reduce((acc, [key, val]) => ({ ...acc, [`* .${key}`]: val }), {})
);
interface WrapperProps {
bordered?: boolean;
padded?: boolean;
}
const Wrapper = styled.div<WrapperProps>(
{
position: 'relative',
overflow: 'hidden',
},
({ theme, bordered }) =>
bordered
? {
border: `1px solid ${theme.appBorderColor}`,
borderRadius: theme.appBorderRadius,
background: theme.background.content,
}
: {}
);
const Scroller = styled(({ children, className }) => (
<ScrollArea horizontal vertical className={className}>
{children}
</ScrollArea>
))(
{
position: 'relative',
},
({ theme }) => ({
'& code': {
paddingRight: theme.layoutMargin,
},
}),
({ theme }) => themedSyntax(theme)
);
interface PreProps {
padded?: boolean;
}
const Pre = styled.pre<PreProps>(({ theme, padded }) => ({
display: 'flex',
justifyContent: 'flex-start',
margin: 0,
padding: padded ? theme.layoutMargin : 0,
}));
const Code = styled.code({
flex: 1,
paddingRight: 0,
opacity: 1,
});
export interface SyntaxHighlighterProps {
language: string;
copyable?: boolean;
bordered?: boolean;
padded?: boolean;
format?: boolean;
className?: string;
}
export interface SyntaxHighlighterState {
copied: boolean;
}
type ReactSyntaxHighlighterProps = React.ComponentProps<typeof ReactSyntaxHighlighter>;
export class SyntaxHighlighter extends Component<
SyntaxHighlighterProps & ReactSyntaxHighlighterProps,
SyntaxHighlighterState
> {
static defaultProps: SyntaxHighlighterProps = {
language: null,
copyable: false,
bordered: false,
padded: false,
format: true,
className: null,
};
state = { copied: false };
formatCode = memoize(2)((language: string, code: string) => {
let formattedCode = code;
if (language === 'jsx') {
try {
// eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
formattedCode = beautify(code, {
indent_size: 2,
brace_style: 'collapse-preserve-inline',
end_with_newline: true,
wrap_line_length: 80,
e4x: true, // e4x is not available in JsBeautify types for now
} as JsBeautifyOptions);
} catch (error) {
// eslint-disable-next-line no-console
console.warn("Couldn't format code", formattedCode);
}
}
return formattedCode;
});
onClick = (e: React.MouseEvent) => {
const { children } = this.props;
e.preventDefault();
const tmp = document.createElement('TEXTAREA');
const focus = document.activeElement;
tmp.value = children;
document.body.appendChild(tmp);
tmp.select();
document.execCommand('copy');
document.body.removeChild(tmp);
focus.focus();
this.setState({ copied: true }, () => {
window.setTimeout(() => this.setState({ copied: false }), 1500);
});
};
render() {
const {
children,
language = 'jsx',
copyable,
bordered,
padded,
format,
className,
...rest
} = this.props;
const { copied } = this.state;
return children ? (
<Wrapper bordered={bordered} padded={padded} className={className}>
<Scroller>
<ReactSyntaxHighlighter
padded={padded || bordered}
language={language}
useInlineStyles={false}
PreTag={Pre}
CodeTag={Code}
lineNumberContainerStyle={{}}
{...rest}
>
{format
? this.formatCode(language, (children as string).trim())
: (children as string).trim()}
</ReactSyntaxHighlighter>
</Scroller>
{copyable ? (
<ActionBar actionItems={[{ title: copied ? 'Copied' : 'Copy', onClick: this.onClick }]} />
) : null}
</Wrapper>
) : null;
}
}