add drag'n'drop to CollectionLink and DocumentLink sidebar components

This commit is contained in:
Ante Primorac committed 2020-05-23 00:11:20 +02:00
1 parent 633ccc0800
commit ff87da523f
2 files changed
+151 -28

No files matched your search

@@ -1,8 +1,14 @@
// @flow
import * as React from 'react';
import { observer } from 'mobx-react';
import { observer, Observer } from 'mobx-react';
import { observable } from 'mobx';
import { CollectionIcon, PrivateCollectionIcon } from 'outline-icons';
import {
DragDropContext,
Droppable,
Draggable,
DropResult,
} from 'react-beautiful-dnd';
import Collection from 'models/Collection';
import Document from 'models/Document';
import CollectionMenu from 'menus/CollectionMenu';
@@ -25,6 +31,38 @@ type Props = {
class CollectionLink extends React.Component<Props> {
@observable menuOpen = false;
reorder = (result: DropResult) => {
// Bail out early if result doesn't have a destination data
if (!result.destination) {
return;
}
// Bail out early if no changes
if (
result.destination.droppableId === result.source.droppableId &&
result.destination.index === result.source.index
) {
return;
}
const { collection, documents } = this.props;
const document = collection.documents.find(
({ id }) => id === result.draggableId
);
// Bail out if document doesn't exist
if (!document) {
return;
}
documents.move(
document,
collection.id,
undefined,
result.destination.index
);
};
render() {
const {
collection,
@@ -69,19 +107,46 @@ class CollectionLink extends React.Component<Props> {
/>
}
>
<Flex column>
{collection.documents.map(node => (
<DocumentLink
key={node.id}
node={node}
documents={documents}
collection={collection}
activeDocument={activeDocument}
prefetchDocument={prefetchDocument}
depth={1.5}
/>
))}
</Flex>
<DragDropContext onDragEnd={this.reorder}>
<Droppable droppableId={`droppable-collection-${collection.id}`}>
{(provided, snapshot) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
<Flex column>
<Observer>
{() =>
collection.documents.map((node, index) => (
<Draggable
key={node.id}
draggableId={node.id}
index={index}
>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
<DocumentLink
key={node.id}
node={node}
documents={documents}
collection={collection}
activeDocument={activeDocument}
prefetchDocument={prefetchDocument}
depth={1.5}
/>
</div>
)}
</Draggable>
))
}
</Observer>
</Flex>
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
</SidebarLink>
</DropToImport>
);
@@ -1,8 +1,14 @@
// @flow
import * as React from 'react';
import { observer } from 'mobx-react';
import { observer, Observer } from 'mobx-react';
import { observable } from 'mobx';
import styled from 'styled-components';
import {
DragDropContext,
Droppable,
Draggable,
DropResult,
} from 'react-beautiful-dnd';
import Document from 'models/Document';
import DocumentMenu from 'menus/DocumentMenu';
import SidebarLink from './SidebarLink';
@@ -60,6 +66,31 @@ class DocumentLink extends React.Component<Props> {
return !!this.props.node.children.length;
};
reorder = (result: DropResult) => {
// Bail out early if result doesn't have a destination data
if (!result.destination) {
return;
}
// Bail out early if no changes
if (
result.destination.droppableId === result.source.droppableId &&
result.destination.index === result.source.index
) {
return;
}
const { collection, node, documents } = this.props;
const document = node.children.find(({ id }) => id === result.draggableId);
// Bail out if document doesn't exist
if (!document) {
return;
}
documents.move(document, collection.id, node.id, result.destination.index);
};
render() {
const {
node,
@@ -116,19 +147,46 @@ class DocumentLink extends React.Component<Props> {
}
>
{this.hasChildDocuments() && (
<DocumentChildren column>
{node.children.map(childNode => (
<DocumentLink
key={childNode.id}
collection={collection}
node={childNode}
documents={documents}
activeDocument={activeDocument}
prefetchDocument={prefetchDocument}
depth={depth + 1}
/>
))}
</DocumentChildren>
<DragDropContext onDragEnd={this.reorder}>
<Droppable droppableId={`droppable-document-${node.id}`}>
{(provided, snapshot) => (
<div {...provided.droppableProps} ref={provided.innerRef}>
<DocumentChildren column>
<Observer>
{() =>
node.children.map((childNode, index) => (
<Draggable
key={childNode.id}
draggableId={childNode.id}
index={index}
>
{(provided, snapshot) => (
<div
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
>
<DocumentLink
key={childNode.id}
collection={collection}
node={childNode}
documents={documents}
activeDocument={activeDocument}
prefetchDocument={prefetchDocument}
depth={depth + 1}
/>
</div>
)}
</Draggable>
))
}
</Observer>
</DocumentChildren>
{provided.placeholder}
</div>
)}
</Droppable>
</DragDropContext>
)}
</SidebarLink>
</DropToImport>