This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(prettier): add prettier support
- Loading branch information
1 parent
5d20026
commit 4052e02
Showing
29 changed files
with
755 additions
and
907 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,66 @@ | ||
import * as _ from 'lodash' | ||
import * as React from 'react' | ||
|
||
import formatCode from '../utils/formatCode' | ||
import Editor, { EDITOR_BACKGROUND_COLOR } from './Editor' | ||
|
||
export interface CodeSnippetProps { | ||
fitted?: boolean | ||
label?: string | ||
mode?: 'jsx' | 'html' | 'sh' | ||
value: string | ||
style?: React.CSSProperties | ||
} | ||
|
||
const CodeSnippet = ({ label, value, mode = 'jsx', style, ...rest }: CodeSnippetProps) => ( | ||
<div | ||
style={{ | ||
position: 'relative', | ||
padding: '1rem', | ||
marginBottom: '2rem', | ||
background: EDITOR_BACKGROUND_COLOR, | ||
...style, | ||
}} | ||
> | ||
const formatters = { | ||
sh: (val: string = ''): string => val.replace(/^/g, '$ '), | ||
html: (val: string = ''): string => formatCode(val, 'html'), | ||
jsx: (val: string = ''): string => formatCode(val, 'babylon'), | ||
} | ||
|
||
const CodeSnippet = ({ fitted, label, value, mode = 'jsx', ...rest }: CodeSnippetProps) => { | ||
const format = formatters[mode] | ||
const formattedValue = format(value) | ||
// remove eof line break, they are not helpful for snippets | ||
.replace(/\n$/, '') | ||
|
||
return ( | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
padding: '0.2rem 0.35rem', | ||
top: '1rem', | ||
right: '1rem', | ||
lineHeight: 1, | ||
color: '#899', | ||
fontFamily: 'monospace', | ||
fontSize: '0.8rem', | ||
border: '1px solid #566', | ||
zIndex: 100, | ||
position: 'relative', | ||
padding: '1rem', | ||
marginBottom: fitted ? 0 : '2rem', | ||
background: EDITOR_BACKGROUND_COLOR, | ||
...rest.style, | ||
}} | ||
> | ||
{label || mode} | ||
<div | ||
style={{ | ||
position: 'absolute', | ||
padding: '0.2rem 0.35rem', | ||
top: '1rem', | ||
right: '1rem', | ||
lineHeight: 1, | ||
color: '#899', | ||
fontFamily: 'monospace', | ||
fontSize: '0.8rem', | ||
border: '1px solid #566', | ||
zIndex: 100, | ||
}} | ||
> | ||
{label || mode} | ||
</div> | ||
<Editor | ||
highlightActiveLine={false} | ||
highlightGutterLine={false} | ||
mode={mode} | ||
readOnly | ||
showGutter={false} | ||
showCursor={false} | ||
value={formattedValue} | ||
{...rest} | ||
/> | ||
</div> | ||
<Editor | ||
id={btoa(value)} | ||
highlightActiveLine={false} | ||
highlightGutterLine={false} | ||
mode={mode} | ||
readOnly | ||
showGutter={false} | ||
showCursor={false} | ||
value={mode === 'sh' ? value.replace(/^/g, '$ ') : value} | ||
{...rest} | ||
/> | ||
</div> | ||
) | ||
) | ||
} | ||
export default CodeSnippet |
Oops, something went wrong.