-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9e0402
commit c26d42d
Showing
18 changed files
with
210 additions
and
306 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 27 additions & 65 deletions
92
client/src/components/GalleryItem/GalleryItemDragLayer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,38 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { DragLayer } from 'react-dnd'; | ||
import Badge from 'components/Badge/Badge'; | ||
import GalleryItem from './GalleryItem'; | ||
|
||
class GalleryItemDragLayer extends Component { | ||
getOffset() { | ||
const { | ||
offset, | ||
dragged, | ||
} = this.props; | ||
return { | ||
transform: offset && `translate(${offset.x + dragged.x}px, ${offset.y + dragged.y}px)`, | ||
}; | ||
} | ||
function GalleryItemDragLayer(props) { | ||
const { draggingItems, draggingItemProps } = props; | ||
const selectionCount = draggingItems.length; | ||
const shadows = [ | ||
selectionCount > 1 | ||
? <div key="1" className="gallery-item__drag-shadow" /> | ||
: null, | ||
selectionCount > 2 | ||
? <div key="2" className="gallery-item__drag-shadow gallery-item__drag-shadow--second" /> | ||
: null, | ||
]; | ||
|
||
render() { | ||
if (!this.props.isDragging) { | ||
return null; | ||
} | ||
const { item } = this.props; | ||
if (!item.selected) { | ||
return null; | ||
} | ||
const selectionCount = item.selected.length; | ||
const shadows = [ | ||
selectionCount > 1 | ||
? <div key="1" className="gallery-item__drag-shadow" /> | ||
: null, | ||
selectionCount > 2 | ||
? <div key="2" className="gallery-item__drag-shadow gallery-item__drag-shadow--second" /> | ||
: null, | ||
]; | ||
|
||
return ( | ||
<div className="gallery-item__drag-layer"> | ||
<div className="gallery-item__drag-layer-item" style={this.getOffset()}> | ||
<div className="gallery-item__drag-layer-preview"> | ||
{shadows} | ||
<GalleryItem {...item.props} isDragging /> | ||
</div> | ||
{selectionCount > 1 | ||
? ( | ||
<Badge | ||
className="gallery-item__drag-layer-count" | ||
status="info" | ||
message={`${selectionCount}`} | ||
/> | ||
) | ||
: null | ||
} | ||
</div> | ||
return ( | ||
<div className="gallery-item__drag-layer"> | ||
<div className="gallery-item__drag-layer-preview"> | ||
{shadows} | ||
<GalleryItem {...draggingItemProps} isDragging /> | ||
{selectionCount > 1 && <Badge | ||
className="gallery-item__drag-layer-count" | ||
status="info" | ||
message={`${selectionCount}`} | ||
/>} | ||
</div> | ||
); | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
GalleryItemDragLayer.propTypes = { | ||
item: PropTypes.object, | ||
offset: PropTypes.shape({ | ||
x: PropTypes.number.isRequired, | ||
y: PropTypes.number.isRequired, | ||
}), | ||
isDragging: PropTypes.bool.isRequired, | ||
draggingItems: PropTypes.arrayOf(PropTypes.number).isRequired, | ||
draggingItemProps: PropTypes.object.isRequired, | ||
}; | ||
|
||
const collect = (monitor) => ({ | ||
item: monitor.getItem(), | ||
offset: monitor.getInitialClientOffset(), | ||
dragged: monitor.getDifferenceFromInitialOffset(), | ||
isDragging: monitor.isDragging(), | ||
}); | ||
|
||
// eslint-disable-next-line new-cap | ||
export default DragLayer(collect)(GalleryItemDragLayer); | ||
export default GalleryItemDragLayer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,29 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { DragSource } from 'react-dnd'; | ||
import { getEmptyImage } from 'react-dnd-html5-backend'; | ||
|
||
export default function draggable(type) { | ||
const spec = { | ||
canDrag(props) { | ||
return props.canDrag; | ||
}, | ||
beginDrag(props) { | ||
const { id } = props.item; | ||
if (typeof props.onDrag === 'function') { | ||
props.onDrag(true, id); | ||
} | ||
const selected = props.selectedFiles.concat([]); | ||
if (!selected.includes(id)) { | ||
selected.push(id); | ||
} | ||
|
||
return { selected, props }; | ||
}, | ||
endDrag(props) { | ||
const { id } = props.item; | ||
if (typeof props.onDrag === 'function') { | ||
props.onDrag(false, id); | ||
} | ||
}, | ||
import { useDraggable } from '@dnd-kit/core'; | ||
|
||
export default function draggable(Item) { | ||
function DraggableItem(props) { | ||
const canDrag = typeof props.canDrag === 'boolean' ? props.canDrag : true; | ||
const { attributes, listeners, setNodeRef } = useDraggable({ disabled: !canDrag, id: props.item.id, data: { props } }); | ||
const item = <Item {...props} />; | ||
|
||
return <div | ||
className="gallery-item__draggable" | ||
// The `draggable` attribute is needed to tell react-selectable to not start selecting on mousedown | ||
draggable | ||
ref={setNodeRef} | ||
{...listeners} | ||
{...attributes} | ||
>{ item }</div>; | ||
} | ||
|
||
DraggableItem.propTypes = { | ||
item: PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
}).isRequired, | ||
canDrag: PropTypes.bool, | ||
}; | ||
|
||
const collect = (connect, monitor) => ({ | ||
connectDragPreview: connect.dragPreview(), | ||
connectDragSource: connect.dragSource(), | ||
isDragging: monitor.isDragging(), | ||
}); | ||
|
||
// eslint-disable-next-line new-cap | ||
const dragItem = DragSource(type, spec, collect); | ||
|
||
return (Item) => { | ||
class DraggableItem extends Component { | ||
componentDidMount() { | ||
// Use empty image as a drag preview so browsers don't draw it | ||
// and we can draw whatever we want on the custom drag layer instead. | ||
this.props.connectDragPreview(getEmptyImage(), { | ||
// IE fallback: specify that we'd rather screenshot the node | ||
// when it already knows it's being dragged so we can hide it with CSS. | ||
captureDraggingState: true, | ||
}); | ||
} | ||
|
||
render() { | ||
const { connectDragSource } = this.props; | ||
const item = <Item {...this.props} />; | ||
|
||
if (typeof item.type === 'string') { | ||
return connectDragSource(item); | ||
} | ||
return connectDragSource(<div className="gallery-item__draggable">{ item }</div>); | ||
} | ||
} | ||
|
||
DraggableItem.propTypes = { | ||
connectDragSource: PropTypes.func.isRequired, | ||
connectDragPreview: PropTypes.func.isRequired, | ||
item: PropTypes.shape({ | ||
id: PropTypes.number.isRequired, | ||
}).isRequired, | ||
onDrag: PropTypes.func, | ||
selectedFiles: PropTypes.arrayOf(PropTypes.number), | ||
}; | ||
|
||
return dragItem(DraggableItem); | ||
}; | ||
return DraggableItem; | ||
} |
Oops, something went wrong.