Skip to content

Commit

Permalink
feat(dnd): add preview of an item inside cell while dragging (jquense…
Browse files Browse the repository at this point in the history
  • Loading branch information
asakalou-exadel authored and jquense committed Jun 26, 2019
1 parent dae79f5 commit ac715f8
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 31 deletions.
90 changes: 61 additions & 29 deletions examples/demos/dndOutsideSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ class Dnd extends React.Component {
item1: 0,
item2: 0,
},
displayDragItemInCell: true,
}
}

handleDragStart = name => {
this.setState({ draggedEvent: name })
handleDragStart = event => {
this.setState({ draggedEvent: event })
}

handleDisplayDragItemInCell = () => {
this.setState({
displayDragItemInCell: !this.state.displayDragItemInCell,
})
}

dragFromOutsideItem = () => {
return this.state.draggedEvent
}

customOnDragOver = event => {
Expand All @@ -43,14 +54,14 @@ class Dnd extends React.Component {
onDropFromOutside = ({ start, end, allDay }) => {
const { draggedEvent, counters } = this.state
const event = {
title: formatName(draggedEvent, counters[draggedEvent]),
title: formatName(draggedEvent.name, counters[draggedEvent.name]),
start,
end,
isAllDay: allDay,
}
const updatedCounters = {
...counters,
[draggedEvent]: counters[draggedEvent] + 1,
[draggedEvent.name]: counters[draggedEvent.name] + 1,
}
this.setState({ draggedEvent: null, counters: updatedCounters })
this.newEvent(event)
Expand Down Expand Up @@ -114,16 +125,35 @@ class Dnd extends React.Component {
render() {
return (
<div>
<Card
className="examples--header"
style={{
display: 'flex',
justifyContent: 'center',
flexWrap: 'wrap',
}}
>
<h4 style={{ color: 'gray', width: '100%' }}>Outside Drag Sources</h4>
{Object.entries(this.state.counters).map(([name, count]) => (
<Card className="examples--header" style={{ display: 'flex' }}>
<div
style={{
display: 'flex',
flex: 1,
justifyContent: 'center',
flexWrap: 'wrap',
}}
>
<h4 style={{ color: 'gray', width: '100%' }}>
Outside Drag Sources
</h4>
{Object.entries(this.state.counters).map(([name, count]) => (
<div
style={{
border: '2px solid gray',
borderRadius: '4px',
width: '100px',
margin: '10px',
}}
draggable="true"
key={name}
onDragStart={() =>
this.handleDragStart({ title: formatName(name, count), name })
}
>
{formatName(name, count)}
</div>
))}
<div
style={{
border: '2px solid gray',
Expand All @@ -133,30 +163,32 @@ class Dnd extends React.Component {
}}
draggable="true"
key={name}
onDragStart={() => this.handleDragStart(name)}
onDragStart={() => this.handleDragStart('undroppable')}
>
{formatName(name, count)}
Draggable but not for calendar.
</div>
))}
<div
style={{
border: '2px solid gray',
borderRadius: '4px',
width: '100px',
margin: '10px',
}}
draggable="true"
key={name}
onDragStart={() => this.handleDragStart('undroppable')}
>
Draggable but not for calendar.
</div>

<div>
<label>
<input
style={{ marginRight: 5 }}
type="checkbox"
checked={this.state.displayDragItemInCell}
onChange={this.handleDisplayDragItemInCell}
/>
Display dragged item in cell while dragging over
</label>
</div>
</Card>
<DragAndDropCalendar
selectable
localizer={this.props.localizer}
events={this.state.events}
onEventDrop={this.moveEvent}
dragFromOutsideItem={
this.state.displayDragItemInCell ? this.dragFromOutsideItem : null
}
onDropFromOutside={this.onDropFromOutside}
onDragOver={this.customOnDragOver}
resizable
Expand Down
21 changes: 21 additions & 0 deletions src/Selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class Selection {
this._handleTerminatingEvent = this._handleTerminatingEvent.bind(this)
this._keyListener = this._keyListener.bind(this)
this._dropFromOutsideListener = this._dropFromOutsideListener.bind(this)
this._dragOverFromOutsideListener = this._dragOverFromOutsideListener.bind(
this
)

// Fixes an iOS 10 bug where scrolling could not be prevented on the window.
// https://github.com/metafizzy/flickity/issues/457#issuecomment-254501356
Expand All @@ -70,6 +73,10 @@ class Selection {
'drop',
this._dropFromOutsideListener
)
this._onDragOverfromOutisde = addEventListener(
'dragover',
this._dragOverFromOutsideListener
)
this._addInitialEventListener()
}

Expand Down Expand Up @@ -105,6 +112,7 @@ class Selection {
this._onMoveListener && this._onMoveListener.remove()
this._onKeyUpListener && this._onKeyUpListener.remove()
this._onKeyDownListener && this._onKeyDownListener.remove()
this._onDropFromOutsideListener && this._onDragOverfromOutisde.remove()
}

isSelected(node) {
Expand Down Expand Up @@ -207,6 +215,19 @@ class Selection {
e.preventDefault()
}

_dragOverFromOutsideListener(e) {
const { pageX, pageY, clientX, clientY } = getEventCoordinates(e)

this.emit('dragOverFromOutside', {
x: pageX,
y: pageY,
clientX: clientX,
clientY: clientY,
})

e.preventDefault()
}

_handleInitialEvent(e) {
if (this.isDetached) {
return
Expand Down
9 changes: 9 additions & 0 deletions src/addons/dragAndDrop/EventContainerWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class EventContainerWrapper extends React.Component {
onDropFromOutside: PropTypes.func,
onBeginAction: PropTypes.func,
dragAndDropAction: PropTypes.object,
dragFromOutsideItem: PropTypes.func,
}),
}

Expand Down Expand Up @@ -168,6 +169,14 @@ class EventContainerWrapper extends React.Component {
this.handleDropFromOutside(point, bounds)
})

selector.on('dragOver', point => {
if (!this.context.draggable.dragFromOutsideItem) return

const bounds = getBoundsForNode(node)

this.handleDropFromOutside(point, bounds)
})

selector.on('selectStart', () => this.context.draggable.onStart())

selector.on('select', point => {
Expand Down
23 changes: 21 additions & 2 deletions src/addons/dragAndDrop/WeekWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class WeekWrapper extends React.Component {
dragAndDropAction: PropTypes.object,
onDropFromOutside: PropTypes.func,
onBeginAction: PropTypes.func,
dragFromOutsideItem: PropTypes.func,
}),
}

Expand Down Expand Up @@ -78,8 +79,8 @@ class WeekWrapper extends React.Component {
this.setState({ segment })
}

handleMove = ({ x, y }, node) => {
const { event } = this.context.draggable.dragAndDropAction
handleMove = ({ x, y }, node, draggedEvent) => {
const { event = draggedEvent } = this.context.draggable.dragAndDropAction
const metrics = this.props.slotMetrics
const { accessors } = this.props

Expand Down Expand Up @@ -122,6 +123,16 @@ class WeekWrapper extends React.Component {
})
}

handleDragOverFromOutside = ({ x, y }, node) => {
if (!this.context.draggable.dragFromOutsideItem) return

this.handleMove(
{ x, y },
node,
this.context.draggable.dragFromOutsideItem()
)
}

handleResize(point, node) {
const { event, direction } = this.context.draggable.dragAndDropAction
const { accessors, slotMetrics: metrics } = this.props
Expand Down Expand Up @@ -220,6 +231,14 @@ class WeekWrapper extends React.Component {
this.handleDropFromOutside(point, bounds)
})

selector.on('dragOverFromOutside', point => {
if (!this.context.draggable.dragFromOutsideItem) return

const bounds = getBoundsForNode(node)

this.handleDragOverFromOutside(point, bounds)
})

selector.on('click', () => this.context.draggable.onEnd(null))
}

Expand Down
4 changes: 4 additions & 0 deletions src/addons/dragAndDrop/withDragAndDrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export default function withDragAndDrop(Calendar) {
onDragOver: PropTypes.func,
onDropFromOutside: PropTypes.func,

dragFromOutsideItem: PropTypes.func,

draggableAccessor: accessor,
resizableAccessor: accessor,

Expand Down Expand Up @@ -100,6 +102,7 @@ export default function withDragAndDrop(Calendar) {
onEnd: PropTypes.func,
onBeginAction: PropTypes.func,
onDropFromOutside: PropTypes.fun,
dragFromOutsideItem: PropTypes.fun,
draggableAccessor: accessor,
resizableAccessor: accessor,
dragAndDropAction: PropTypes.object,
Expand Down Expand Up @@ -127,6 +130,7 @@ export default function withDragAndDrop(Calendar) {
onEnd: this.handleInteractionEnd,
onBeginAction: this.handleBeginAction,
onDropFromOutside: this.props.onDropFromOutside,
dragFromOutsideItem: this.props.dragFromOutsideItem,
draggableAccessor: this.props.draggableAccessor,
resizableAccessor: this.props.resizableAccessor,
dragAndDropAction: this.state,
Expand Down

0 comments on commit ac715f8

Please sign in to comment.