From ff87da523f15769c794ad1ffa04c7ff46fefcb71 Mon Sep 17 00:00:00 2001 From: Ante Primorac Date: Sat, 23 May 2020 00:11:20 +0200 Subject: [PATCH] add drag'n'drop to `CollectionLink` and `DocumentLink` sidebar components --- .../Sidebar/components/CollectionLink.js | 93 ++++++++++++++++--- .../Sidebar/components/DocumentLink.js | 86 ++++++++++++++--- 2 files changed, 151 insertions(+), 28 deletions(-) diff --git a/app/components/Sidebar/components/CollectionLink.js b/app/components/Sidebar/components/CollectionLink.js index ce76b78d39..7381a6e85d 100644 --- a/app/components/Sidebar/components/CollectionLink.js +++ b/app/components/Sidebar/components/CollectionLink.js @@ -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 { @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 { /> } > - - {collection.documents.map(node => ( - - ))} - + + + {(provided, snapshot) => ( +
+ + + {() => + collection.documents.map((node, index) => ( + + {(provided, snapshot) => ( +
+ +
+ )} +
+ )) + } +
+
+ {provided.placeholder} +
+ )} +
+
); diff --git a/app/components/Sidebar/components/DocumentLink.js b/app/components/Sidebar/components/DocumentLink.js index 36b1c589f7..3149e73c45 100644 --- a/app/components/Sidebar/components/DocumentLink.js +++ b/app/components/Sidebar/components/DocumentLink.js @@ -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 { 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 { } > {this.hasChildDocuments() && ( - - {node.children.map(childNode => ( - - ))} - + + + {(provided, snapshot) => ( +
+ + + {() => + node.children.map((childNode, index) => ( + + {(provided, snapshot) => ( +
+ +
+ )} +
+ )) + } +
+
+ {provided.placeholder} +
+ )} +
+
)}