|
| 1 | +import React from 'react'; |
| 2 | +import {Modal, Button, Dropdown, Label} from 'semantic-ui-react'; |
| 3 | +import * as Tools from '../utils/Tools'; |
| 4 | +import Loading from "./Loading"; |
| 5 | + |
| 6 | +export default class SourceCode extends React.Component { |
| 7 | + code = React.createRef(); |
| 8 | + mark = null; |
| 9 | + visible = false; |
| 10 | + constructor(props) { |
| 11 | + super(props); |
| 12 | + } |
| 13 | + |
| 14 | + componentDidUpdate(prevProps, prevState, snapshot) { |
| 15 | + if (prevProps.sourceCode != this.props.sourceCode || (!this.visiable && this.props.sourceCodeVisible)) { |
| 16 | + this.renderPrettyPrint(); |
| 17 | + this.visible = true; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + componentDidMount() { |
| 22 | + this.renderPrettyPrint(); |
| 23 | + } |
| 24 | + |
| 25 | + handleClose = () => { |
| 26 | + this.visiable = false; |
| 27 | + this.props.onCloseSourceCode(); |
| 28 | + } |
| 29 | + renderPrettyPrint = () => { |
| 30 | + setTimeout(() => { |
| 31 | + if (this.code.current) { |
| 32 | + this.code.current.classList.remove('prettyprinted'); |
| 33 | + setTimeout(() => PR.prettyPrint( |
| 34 | + () => setTimeout(() =>this.renderHighLight(), 1000) |
| 35 | + ), 100); |
| 36 | + } |
| 37 | + }, this.code.current ? 0 : 1000); |
| 38 | + } |
| 39 | + |
| 40 | + renderHighLight = () => { |
| 41 | + if (this.mark) {this.mark.unmark()} |
| 42 | + this.mark = new Mark(this.code.current); |
| 43 | + let idx = 0; |
| 44 | + this.mark.mark(this.props.sourceCodeVariable.keyword, {each: el => { |
| 45 | + el.setAttribute('tabindex',idx++); |
| 46 | + }}); |
| 47 | + } |
| 48 | + |
| 49 | + renderDropdownItem() { |
| 50 | + return this.props.sourceCodeVariable.repoList.map(repo => { |
| 51 | + return ( |
| 52 | + <Dropdown.Item key={Tools.uuid()}> |
| 53 | + <Button size='mini' onClick={() => this.props.onRequestSourceCode(repo)}>Codes</Button> |
| 54 | + <Button size='mini' as='a' href={repo.repo} target='_blank'>Repo</Button> |
| 55 | + <Label size='mini' circular color={Tools.randomLabelColor()}>{repo.language}</Label> |
| 56 | + </Dropdown.Item> |
| 57 | + ) |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + render() { |
| 62 | + if (!this.props.sourceCodeVariable) { return null; } |
| 63 | + const sourceCodeVariable = this.props.sourceCodeVariable; |
| 64 | + return ( |
| 65 | + <Modal open={this.props.sourceCodeVisible} onClose={this.handleClose} |
| 66 | + centered={false} closeIcon className='source-code modal--fix'> |
| 67 | + <Modal.Header> |
| 68 | + <Dropdown floating labeled button blurring className='mini icon' |
| 69 | + text={`Matched results (${sourceCodeVariable.repoList.length})`}> |
| 70 | + <Dropdown.Menu> |
| 71 | + <Dropdown.Menu scrolling> |
| 72 | + {this.renderDropdownItem()} |
| 73 | + </Dropdown.Menu> |
| 74 | + </Dropdown.Menu> |
| 75 | + </Dropdown> |
| 76 | + <Button size='mini' as='a' href={this.props.sourceCodeRepo.repo} target='_blank'>Repo</Button> |
| 77 | + </Modal.Header> |
| 78 | + <Modal.Content> |
| 79 | + {this.props.requestingSourceCode ? <Loading/> : ''} |
| 80 | + <pre><code className='prettyprint linenums' ref={this.code}>{this.props.sourceCode}</code></pre> |
| 81 | + </Modal.Content> |
| 82 | + </Modal> |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
0 commit comments