-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
syntaxhighlighter.tsx
148 lines (125 loc) · 3.82 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
import React, { Component, ComponentProps, FunctionComponent, MouseEvent, useState } from 'react';
import { styled } from '@storybook/theming';
import { document, window } from 'global';
import memoize from 'memoizerific';
import jsx from 'react-syntax-highlighter/dist/esm/languages/prism/jsx';
import bash from 'react-syntax-highlighter/dist/esm/languages/prism/bash';
import css from 'react-syntax-highlighter/dist/esm/languages/prism/css';
import html from 'react-syntax-highlighter/dist/esm/languages/prism/markup';
import { PrismLight as ReactSyntaxHighlighter } from 'react-syntax-highlighter';
import { ActionBar } from '../ActionBar/ActionBar';
import { ScrollArea } from '../ScrollArea/ScrollArea';
import { formatter } from './formatter';
ReactSyntaxHighlighter.registerLanguage('jsx', jsx);
ReactSyntaxHighlighter.registerLanguage('bash', bash);
ReactSyntaxHighlighter.registerLanguage('css', css);
ReactSyntaxHighlighter.registerLanguage('html', html);
const themedSyntax = memoize(2)(theme =>
Object.entries(theme.code || {}).reduce((acc, [key, val]) => ({ ...acc, [`* .${key}`]: val }), {})
);
export interface WrapperProps {
bordered?: boolean;
padded?: boolean;
}
const Wrapper = styled.div<WrapperProps>(
({ theme }) => ({
position: 'relative',
overflow: 'hidden',
color: theme.color.defaultText,
}),
({ theme, bordered }) =>
bordered
? {
border: `1px solid ${theme.appBorderColor}`,
borderRadius: theme.borderRadius,
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)
);
export 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 = ComponentProps<typeof ReactSyntaxHighlighter>;
type Props = SyntaxHighlighterProps & ReactSyntaxHighlighterProps;
export const SyntaxHighlighter: FunctionComponent<Props> = ({
children,
language = 'jsx',
copyable = false,
bordered = false,
padded = false,
format = true,
className = null,
...rest
}) => {
const [copied, setCopied] = useState(false);
const onClick = (e: MouseEvent<HTMLButtonElement>) => {
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();
setCopied(true);
window.setTimeout(() => setCopied(false), 1500);
};
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 ? formatter((children as string).trim()) : (children as string).trim()}
</ReactSyntaxHighlighter>
</Scroller>
{copyable ? (
<ActionBar actionItems={[{ title: copied ? 'Copied' : 'Copy', onClick }]} />
) : null}
</Wrapper>
) : null;
};