This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from dfuzr/LIB-598-add-code-snippet
Lib 598 add code snippet
- Loading branch information
Showing
25 changed files
with
543 additions
and
103 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import React, {useEffect, useRef, useState} from "react"; | ||
import Clipboard from 'clipboard'; | ||
import Highlight, { defaultProps } from "prism-react-renderer"; | ||
import darkCodeTheme from "prism-react-renderer/themes/paleNight"; | ||
import lightCodeTheme from "prism-react-renderer/themes/github"; | ||
import PropTypes from 'prop-types'; | ||
|
||
import useThemeContext from '@theme/hooks/useThemeContext'; | ||
|
||
import WithBackgroundImage from 'components/WithBackgroundImage'; | ||
|
||
import classnames from 'classnames'; | ||
import styles from './styles.module.css'; | ||
|
||
const LINE_CONTENT_CLASS = 'line-content'; | ||
|
||
const getUniqueID = (() => { | ||
let id = 0; | ||
return () => id++; | ||
})(); | ||
|
||
const handleCopyCode = (setShowCopied) => { | ||
window.getSelection().empty(); | ||
setShowCopied(true); | ||
|
||
setTimeout(() => setShowCopied(false), 2000); | ||
}; | ||
|
||
const Header = ({ snippetID, target }) => { | ||
const [showCopied, setShowCopied] = useState(false); | ||
const button = useRef(null); | ||
|
||
useEffect(() => { | ||
let clipboard; | ||
|
||
if (button.current) { | ||
clipboard = new Clipboard(button.current, { | ||
text: () => | ||
Array.from(document.querySelectorAll(`#${snippetID} .${LINE_CONTENT_CLASS}`)) | ||
.map(({textContent}) => `${textContent}\n`) | ||
.join(''), | ||
}); | ||
} | ||
|
||
return () => { | ||
if (clipboard) { | ||
clipboard.destroy(); | ||
} | ||
}; | ||
}, [button.current, target.current]); | ||
|
||
return ( | ||
<div className={styles.header}> | ||
<div ref={button}> | ||
<WithBackgroundImage | ||
className={classnames(styles.copyWrapper, { | ||
[styles.isCopied]: showCopied, | ||
})} | ||
imageDark="img/copy-dark.svg" | ||
imageLight="img/copy.svg" | ||
onClick={() => handleCopyCode(setShowCopied)} | ||
> | ||
<button | ||
type="button" | ||
aria-label="Copy code to clipboard" | ||
> | ||
{showCopied ? 'Copied' : 'Copy'} | ||
</button> | ||
</WithBackgroundImage> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const Code = ({ children, className: languageClassName }) => { | ||
const {isDarkTheme} = useThemeContext(); | ||
const [snippetID, setSnippetID] = useState(null); | ||
const target = useRef(null); | ||
|
||
let code = children.replace(/\n$/, ''); | ||
const theme = isDarkTheme ? darkCodeTheme : lightCodeTheme; | ||
let language = | ||
languageClassName && languageClassName.replace(/language-/, ''); | ||
|
||
useEffect(() => setSnippetID(`snippet-${getUniqueID()}`), []); | ||
|
||
return ( | ||
<Highlight {...defaultProps} theme={theme} code={code} language={language}> | ||
{({ className,style, tokens, getLineProps, getTokenProps }) => ( | ||
<div className={styles.root}> | ||
<Header snippetID={snippetID} target={target} /> | ||
<pre className={className} id={snippetID} ref={target} style={style}> | ||
{tokens.map((line, i) => ( | ||
<div key={i} {...getLineProps({ line, key: i })}> | ||
<span className={styles.lineNumber}>{i + 1}</span> | ||
<span className={LINE_CONTENT_CLASS}> | ||
{line.map((token, key) => ( | ||
<span key={key} {...getTokenProps({ token, key })} /> | ||
))} | ||
</span> | ||
</div> | ||
))} | ||
</pre> | ||
</div> | ||
)} | ||
</Highlight> | ||
); | ||
}; | ||
|
||
Code.propTypes = { | ||
/* | ||
* For a list of available languages, see | ||
* https://github.com/FormidableLabs/prism-react-renderer/blob/master/src/vendor/prism/includeLangs.js | ||
*/ | ||
className: PropTypes.string.isRequired, | ||
children: PropTypes.element.isRequired, | ||
}; | ||
|
||
Code.defaultProps = { | ||
className: 'language-plaintext', | ||
}; | ||
|
||
export default Code; |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
.header { | ||
background-color: var(--snippet-header-background); | ||
border-top-left-radius: var(--ifm-pre-border-radius); | ||
border-top-right-radius: var(--ifm-pre-border-radius); | ||
display: flex; | ||
justify-content: flex-end; | ||
padding: 10px; | ||
} | ||
|
||
.copyWrapper { | ||
--icon-size: 12px; | ||
background-position: 99% 2.5px; | ||
background-repeat: no-repeat; | ||
background-size: var(--icon-size); | ||
display: flex; | ||
margin-right: 5px; | ||
padding: 3px calc(var(--icon-size) + 4px) 3px 0; | ||
} | ||
|
||
.copyWrapper:not(.isCopied), .copyWrapper:not(.isCopied) button { | ||
cursor: pointer; | ||
} | ||
|
||
.header button { | ||
background-color: transparent; | ||
border: none; | ||
color: var(--snippet-copy-color); | ||
font-size: 10px; | ||
} | ||
|
||
.header button:focus { | ||
outline: none; | ||
} | ||
|
||
.root pre { | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
} | ||
|
||
.root :global(.token-line) { | ||
display: table-row; | ||
} | ||
|
||
|
||
.lineNumber { | ||
display: table-cell; | ||
padding-right: 13px; | ||
text-align: right; | ||
user-select: none; | ||
-moz-user-select: none; | ||
word-break: keep-all; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
|
||
import styles from './styles.module.css'; | ||
|
||
const Excerpt = ({children, image}) => { | ||
return ( | ||
<div className={styles.root}> | ||
<div className={styles.content}> | ||
<div className={styles.imageContainer}> | ||
<img className={styles.image} src={useBaseUrl(image)} /> | ||
</div> | ||
<div className={styles.text}> | ||
{children} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
Excerpt.propTypes = { | ||
image: PropTypes.string.isRequired, | ||
text: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default Excerpt; |
Oops, something went wrong.