Drag and Drop Import (#95)
* Drag and drop files into collection first pass * Allow import of sub documents Fix up UI styles * Import Loading indicator * Drag onto document to import
This commit is contained in:
108
frontend/components/DropToImport/DropToImport.js
Normal file
108
frontend/components/DropToImport/DropToImport.js
Normal file
@@ -0,0 +1,108 @@
|
||||
// @flow
|
||||
import React, { Component } from 'react';
|
||||
import { inject } from 'mobx-react';
|
||||
import invariant from 'invariant';
|
||||
import _ from 'lodash';
|
||||
import Dropzone from 'react-dropzone';
|
||||
import Document from 'models/Document';
|
||||
import DocumentsStore from 'stores/DocumentsStore';
|
||||
import LoadingIndicator from 'components/LoadingIndicator';
|
||||
|
||||
class DropToImport extends Component {
|
||||
state: {
|
||||
isImporting: boolean,
|
||||
};
|
||||
props: {
|
||||
children?: React$Element<any>,
|
||||
collectionId: string,
|
||||
documentId?: string,
|
||||
activeClassName?: string,
|
||||
rejectClassName?: string,
|
||||
documents: DocumentsStore,
|
||||
history: Object,
|
||||
};
|
||||
state = {
|
||||
isImporting: false,
|
||||
};
|
||||
|
||||
importFile = async ({ file, documentId, collectionId, redirect }) => {
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = async ev => {
|
||||
const text = ev.target.result;
|
||||
let data = {
|
||||
parentDocument: undefined,
|
||||
collection: { id: collectionId },
|
||||
text,
|
||||
};
|
||||
|
||||
if (documentId) {
|
||||
data.parentDocument = {
|
||||
id: documentId,
|
||||
};
|
||||
}
|
||||
|
||||
let document = new Document(data);
|
||||
document = await document.save();
|
||||
this.props.documents.add(document);
|
||||
|
||||
if (redirect && this.props.history) {
|
||||
this.props.history.push(document.url);
|
||||
}
|
||||
};
|
||||
reader.readAsText(file);
|
||||
};
|
||||
|
||||
onDropAccepted = async (files = []) => {
|
||||
this.setState({ isImporting: true });
|
||||
|
||||
try {
|
||||
let collectionId = this.props.collectionId;
|
||||
const documentId = this.props.documentId;
|
||||
const redirect = files.length === 1;
|
||||
|
||||
if (documentId && !collectionId) {
|
||||
const document = await this.props.documents.fetch(documentId);
|
||||
invariant(document, 'Document not available');
|
||||
collectionId = document.collection.id;
|
||||
}
|
||||
|
||||
for (const file of files) {
|
||||
await this.importFile({ file, documentId, collectionId, redirect });
|
||||
}
|
||||
} catch (err) {
|
||||
// TODO: show error alert.
|
||||
} finally {
|
||||
this.setState({ isImporting: false });
|
||||
}
|
||||
};
|
||||
|
||||
render() {
|
||||
const props = _.omit(
|
||||
this.props,
|
||||
'history',
|
||||
'documentId',
|
||||
'collectionId',
|
||||
'documents'
|
||||
);
|
||||
|
||||
return (
|
||||
<Dropzone
|
||||
accept="text/markdown, text/plain"
|
||||
onDropAccepted={this.onDropAccepted}
|
||||
style={{}}
|
||||
disableClick
|
||||
disablePreview
|
||||
multiple
|
||||
{...props}
|
||||
>
|
||||
<span>
|
||||
{this.state.isImporting && <LoadingIndicator />}
|
||||
{this.props.children}
|
||||
</span>
|
||||
</Dropzone>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default inject('documents')(DropToImport);
|
||||
3
frontend/components/DropToImport/index.js
Normal file
3
frontend/components/DropToImport/index.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// @flow
|
||||
import DropToImport from './DropToImport';
|
||||
export default DropToImport;
|
||||
@@ -7,11 +7,12 @@ import { observer, inject } from 'mobx-react';
|
||||
import _ from 'lodash';
|
||||
import keydown from 'react-keydown';
|
||||
import Flex from 'components/Flex';
|
||||
import { textColor } from 'styles/constants.scss';
|
||||
import { color, layout } from 'styles/constants';
|
||||
|
||||
import DropdownMenu, { MenuItem } from 'components/DropdownMenu';
|
||||
import { LoadingIndicatorBar } from 'components/LoadingIndicator';
|
||||
import Scrollable from 'components/Scrollable';
|
||||
import Avatar from 'components/Avatar';
|
||||
|
||||
import SidebarCollection from './components/SidebarCollection';
|
||||
import SidebarCollectionList from './components/SidebarCollectionList';
|
||||
@@ -115,8 +116,9 @@ type Props = {
|
||||
? <SidebarCollection
|
||||
document={ui.activeDocument}
|
||||
collection={ui.activeCollection}
|
||||
history={this.props.history}
|
||||
/>
|
||||
: <SidebarCollectionList />}
|
||||
: <SidebarCollectionList history={this.props.history} />}
|
||||
</LinkSection>
|
||||
</Scrollable>
|
||||
</Flex>
|
||||
@@ -141,19 +143,13 @@ const LogoLink = styled(Link)`
|
||||
margin-top: 15px;
|
||||
font-family: 'Atlas Grotesk';
|
||||
font-weight: bold;
|
||||
color: ${textColor};
|
||||
color: ${color.text};
|
||||
text-decoration: none;
|
||||
font-size: 16px;
|
||||
`;
|
||||
|
||||
const Avatar = styled.img`
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
border-radius: 50%;
|
||||
`;
|
||||
|
||||
const MenuLink = styled(Link)`
|
||||
color: ${textColor};
|
||||
color: ${color.text};
|
||||
`;
|
||||
|
||||
const Content = styled(Flex)`
|
||||
@@ -162,13 +158,13 @@ const Content = styled(Flex)`
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
left: ${props => (props.editMode ? 0 : '250px')};
|
||||
left: ${props => (props.editMode ? 0 : layout.sidebarWidth)};
|
||||
transition: left 200ms ease-in-out;
|
||||
`;
|
||||
|
||||
const Sidebar = styled(Flex)`
|
||||
width: 250px;
|
||||
margin-left: ${props => (props.editMode ? '-250px' : 0)};
|
||||
width: ${layout.sidebarWidth};
|
||||
margin-left: ${props => (props.editMode ? `-${layout.sidebarWidth}` : 0)};
|
||||
background: rgba(250, 251, 252, 0.71);
|
||||
border-right: 1px solid #eceff3;
|
||||
transition: margin-left 200ms ease-in-out;
|
||||
@@ -176,12 +172,12 @@ const Sidebar = styled(Flex)`
|
||||
|
||||
const Header = styled(Flex)`
|
||||
flex-shrink: 0;
|
||||
padding: 10px 20px;
|
||||
padding: ${layout.padding};
|
||||
`;
|
||||
|
||||
const LinkSection = styled(Flex)`
|
||||
flex-direction: column;
|
||||
padding: 10px 20px;
|
||||
padding: 10px 0;
|
||||
`;
|
||||
|
||||
export default withRouter(inject('user', 'auth', 'ui', 'collections')(Layout));
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
import React from 'react';
|
||||
import Flex from 'components/Flex';
|
||||
import styled from 'styled-components';
|
||||
|
||||
import { layout } from 'styles/constants';
|
||||
import SidebarLink from '../SidebarLink';
|
||||
import DropToImport from 'components/DropToImport';
|
||||
|
||||
import Collection from 'models/Collection';
|
||||
import Document from 'models/Document';
|
||||
@@ -12,24 +13,39 @@ import type { NavigationNode } from 'types';
|
||||
type Props = {
|
||||
collection: ?Collection,
|
||||
document: ?Document,
|
||||
history: Object,
|
||||
};
|
||||
|
||||
const activeStyle = {
|
||||
color: '#000',
|
||||
background: '#E1E1E1',
|
||||
};
|
||||
|
||||
class SidebarCollection extends React.Component {
|
||||
props: Props;
|
||||
|
||||
renderDocuments(documentList: Array<NavigationNode>) {
|
||||
const { document } = this.props;
|
||||
renderDocuments(documentList: Array<NavigationNode>, depth = 0) {
|
||||
const { document, history } = this.props;
|
||||
const canDropToImport = depth === 0;
|
||||
|
||||
if (document) {
|
||||
return documentList.map(doc => (
|
||||
<Flex column key={doc.id}>
|
||||
<SidebarLink key={doc.id} to={doc.url}>
|
||||
{doc.title}
|
||||
</SidebarLink>
|
||||
{canDropToImport &&
|
||||
<DropToImport
|
||||
history={history}
|
||||
documentId={doc.id}
|
||||
activeStyle={activeStyle}
|
||||
>
|
||||
<SidebarLink to={doc.url}>{doc.title}</SidebarLink>
|
||||
</DropToImport>}
|
||||
{!canDropToImport &&
|
||||
<SidebarLink to={doc.url}>{doc.title}</SidebarLink>}
|
||||
|
||||
{(document.pathToDocument.includes(doc.id) ||
|
||||
document.id === doc.id) &&
|
||||
<Children column>
|
||||
{doc.children && this.renderDocuments(doc.children)}
|
||||
{doc.children && this.renderDocuments(doc.children, depth + 1)}
|
||||
</Children>}
|
||||
</Flex>
|
||||
));
|
||||
@@ -57,6 +73,7 @@ const Header = styled(Flex)`
|
||||
text-transform: uppercase;
|
||||
color: #9FA6AB;
|
||||
letter-spacing: 0.04em;
|
||||
padding: 0 ${layout.hpadding};
|
||||
`;
|
||||
|
||||
const Children = styled(Flex)`
|
||||
|
||||
@@ -3,23 +3,37 @@ import React from 'react';
|
||||
import { observer, inject } from 'mobx-react';
|
||||
import Flex from 'components/Flex';
|
||||
import styled from 'styled-components';
|
||||
import { layout } from 'styles/constants';
|
||||
|
||||
import SidebarLink from '../SidebarLink';
|
||||
import DropToImport from 'components/DropToImport';
|
||||
|
||||
import CollectionsStore from 'stores/CollectionsStore';
|
||||
|
||||
type Props = {
|
||||
history: Object,
|
||||
collections: CollectionsStore,
|
||||
};
|
||||
|
||||
const SidebarCollectionList = observer(({ collections }: Props) => {
|
||||
const activeStyle = {
|
||||
color: '#000',
|
||||
background: '#E1E1E1',
|
||||
};
|
||||
|
||||
const SidebarCollectionList = observer(({ history, collections }: Props) => {
|
||||
return (
|
||||
<Flex column>
|
||||
<Header>Collections</Header>
|
||||
{collections.data.map(collection => (
|
||||
<SidebarLink key={collection.id} to={collection.url}>
|
||||
{collection.name}
|
||||
</SidebarLink>
|
||||
<DropToImport
|
||||
history={history}
|
||||
collectionId={collection.id}
|
||||
activeStyle={activeStyle}
|
||||
>
|
||||
<SidebarLink key={collection.id} to={collection.url}>
|
||||
{collection.name}
|
||||
</SidebarLink>
|
||||
</DropToImport>
|
||||
))}
|
||||
</Flex>
|
||||
);
|
||||
@@ -31,6 +45,7 @@ const Header = styled(Flex)`
|
||||
text-transform: uppercase;
|
||||
color: #9FA6AB;
|
||||
letter-spacing: 0.04em;
|
||||
padding: 0 ${layout.hpadding};
|
||||
`;
|
||||
|
||||
export default inject('collections')(SidebarCollectionList);
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
// @flow
|
||||
import React from 'react';
|
||||
import { NavLink } from 'react-router-dom';
|
||||
import Flex from 'components/Flex';
|
||||
import { layout, color } from 'styles/constants';
|
||||
import { darken } from 'polished';
|
||||
import styled from 'styled-components';
|
||||
|
||||
const activeStyle = {
|
||||
@@ -9,18 +10,16 @@ const activeStyle = {
|
||||
};
|
||||
|
||||
function SidebarLink(props: Object) {
|
||||
return (
|
||||
<LinkContainer>
|
||||
<NavLink exact {...props} activeStyle={activeStyle} />
|
||||
</LinkContainer>
|
||||
);
|
||||
return <StyledNavLink exact {...props} activeStyle={activeStyle} />;
|
||||
}
|
||||
|
||||
const LinkContainer = styled(Flex)`
|
||||
padding: 5px 0;
|
||||
const StyledNavLink = styled(NavLink)`
|
||||
display: block;
|
||||
padding: 5px ${layout.hpadding};
|
||||
color: ${color.slateDark};
|
||||
|
||||
a {
|
||||
color: #848484;
|
||||
&:hover {
|
||||
color: ${darken(0.1, color.slateDark)};
|
||||
}
|
||||
`;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user