File size: 4,424 Bytes
2fcec7b | 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 | import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import styled from 'styled-components';
import { get, map, filter, size, isNaN, debounce } from 'lodash/fp';
import * as snippetActions from 'actions/snippets';
import Editor from 'components/common/controls/Editor';
import SnippetHeader from 'components/layout/content/snippet/SnippetHeader';
import 'github-markdown-css/github-markdown.css';
import Comments from 'components/layout/content/snippet/Comments';
import { toUnixTimeStamp } from 'utils/date';
import { getSetting } from 'utils/settings';
import { randomString } from 'utils/string';
import DropZone from 'components/common/DropZone';
import Icon from 'components/common/Icon';
const SnippetWrapper = styled.div`
background: #fff;
font-weight: 200;
width: auto;
display: flex;
flex-direction: column;
border-radius: 3px;
box-shadow: 0 0 10px ${(props) => props.theme.borderColor};
flex: 1;
margin-bottom: 20px;
`;
const EditArea = styled.div`
display: flex;
`;
const Or = styled.div`
margin-right: 20px;
line-height: 60px;
`;
const AddNewFile = styled.button`
margin: 0 20px 20px 0;
background: ${(props) => props.theme.baseAppColor};
border-radius: 10px;
width: 60px;
color: #fff;
cursor: pointer;
`;
const StyledDropZone = styled(DropZone)`
margin-bottom: 20px;
flex: 1;
`;
export class Snippet extends React.Component {
updateSnippet = debounce(300, this.props.updateTempSnippet);
componentDidMount() {
const id = this.props.match.params.id || this.props.snippet.id;
this.props.getSnippet(id);
}
componentDidUpdate(prevProps) {
/* istanbul ignore if */
if (prevProps.match.params.id !== this.props.match.params.id) {
const now = toUnixTimeStamp(new Date());
const viewed = get('viewed', this.props.snippet);
const shouldGetSnippet =
isNaN(get('lastModified', this.props.snippet)) ||
now - viewed > (getSetting('snippet-fetch-cache-in-seconds') || 100);
if (get('files', this.props.snippet) && shouldGetSnippet) {
this.props.getSnippet(this.props.match.params.id || this.props.snippet.id);
}
}
}
render() {
const { snippet, edit, tempSnippet, showComments, addTempFile } = this.props;
const currentSnippet = edit ? tempSnippet : snippet;
const files = filter((file) => !file.delete, get('files', currentSnippet));
if (!this.props.snippet) {
return null;
}
return (
<React.Fragment>
{showComments && <Comments snippetId={ this.props.match.params.id }/>}
{edit && (
<EditArea>
<AddNewFile title="Add new file" onClick={ () => addTempFile(randomString(5), '') }>
<Icon type="add" size={ 40 }/>
</AddNewFile>
<Or>Or</Or>
<StyledDropZone onAddFile={ addTempFile }/>
</EditArea>
)}
{map(
(file) => (
<SnippetWrapper key={ file.uuid || `${file.size}-${file.viewed}-${file.filename}` }>
<SnippetHeader file={ file } username={ snippet.username } snippetId={ snippet.id }/>
<Editor
file={ file }
edit={ edit }
filesCount={ size(files) }
height={ file.collapsed ? 0 : 400 }
onChange={ (value) => this.updateSnippet(['files', file.uuid, 'content'], value) }/>
</SnippetWrapper>
),
files
)}
</React.Fragment>
);
}
}
const mapStateToProps = (state, ownProps) => {
const snippetId = ownProps.match.params.id;
return {
snippet: get(['snippets', 'snippets', snippetId], state),
comments: get(['snippets', 'comments'], state),
tempSnippet: get(['snippets', 'edit'], state),
edit: get(['ui', 'snippets', 'edit'], state),
showComments: get(['ui', 'snippets', 'comments'], state)
};
};
Snippet.propTypes = {
match: PropTypes.object,
snippet: PropTypes.object,
getSnippet: PropTypes.func.isRequired,
edit: PropTypes.bool,
tempSnippet: PropTypes.object,
updateTempSnippet: PropTypes.func,
addTempFile: PropTypes.func,
showComments: PropTypes.any
};
export default connect(
mapStateToProps,
{
getSnippet: snippetActions.getSnippet,
updateTempSnippet: snippetActions.updateTempSnippet,
addTempFile: snippetActions.addTempFile
}
)(Snippet);
|