-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support re-ordering MIDI editor/CV output instances via DnD
- Loading branch information
Showing
10 changed files
with
342 additions
and
113 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -119,6 +119,7 @@ export default [ | |
}, | ||
], | ||
'react/react-in-jsx-scope': 0, | ||
curly: ['warn', 'all'], | ||
}, | ||
}, | ||
]; |
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
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { createContext, useCallback, useRef } from 'react'; | ||
import { useDrag, useDrop } from 'react-dnd'; | ||
|
||
const ItemTypes = { | ||
INSTANCE: 'instance', | ||
}; | ||
|
||
export const DragActivationContext = createContext<() => void>(() => {}); | ||
|
||
interface DragHandleProps { | ||
activateDrag: () => void; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
export const DragHandle: React.FC<DragHandleProps> = ({ style, activateDrag }) => ( | ||
<div | ||
className='drag-handle' | ||
style={style} | ||
onMouseDown={e => { | ||
e.stopPropagation(); | ||
activateDrag(); | ||
}} | ||
/> | ||
); | ||
|
||
interface DraggableInstanceProps { | ||
index: number; | ||
instanceKey: string; | ||
moveInstance: (dragIndex: number, hoverIndex: number) => void; | ||
children: React.ReactNode; | ||
} | ||
|
||
export const DraggableInstance: React.FC<DraggableInstanceProps> = ({ | ||
index, | ||
instanceKey, | ||
moveInstance, | ||
children, | ||
}) => { | ||
const ref = useRef<HTMLDivElement>(null); | ||
const dragAllowedRef = useRef(false); | ||
|
||
const [{ isDragging }, drag] = useDrag({ | ||
type: ItemTypes.INSTANCE, | ||
item: { index }, | ||
canDrag: () => dragAllowedRef.current, | ||
collect: monitor => ({ isDragging: monitor.isDragging() }), | ||
end: () => { | ||
dragAllowedRef.current = false; | ||
}, | ||
}); | ||
|
||
drag(ref); | ||
|
||
const [, drop] = useDrop({ | ||
accept: ItemTypes.INSTANCE, | ||
hover(item: { index: number }, monitor) { | ||
if (!ref.current) { | ||
return; | ||
} | ||
|
||
const dragIndex = item.index; | ||
const hoverIndex = index; | ||
if (dragIndex === hoverIndex) { | ||
return; | ||
} | ||
|
||
const hoverBoundingRect = ref.current.getBoundingClientRect(); | ||
const boundingHeight = hoverBoundingRect.bottom - hoverBoundingRect.top; | ||
const hoverCutoffY = boundingHeight < 100 ? boundingHeight / 2 : 50; | ||
const clientOffset = monitor.getClientOffset(); | ||
if (!clientOffset) { | ||
return; | ||
} | ||
const hoverClientY = clientOffset.y - hoverBoundingRect.top; | ||
|
||
if ( | ||
(dragIndex < hoverIndex && hoverClientY < hoverCutoffY) || | ||
(dragIndex > hoverIndex && hoverClientY > hoverCutoffY) | ||
) { | ||
return; | ||
} | ||
moveInstance(dragIndex, hoverIndex); | ||
item.index = hoverIndex; | ||
}, | ||
}); | ||
|
||
drop(ref); | ||
|
||
const activateDrag = useCallback(() => { | ||
dragAllowedRef.current = true; | ||
}, []); | ||
|
||
return ( | ||
<div ref={ref} key={instanceKey} style={{ opacity: isDragging ? 0.5 : 1 }}> | ||
<DragActivationContext.Provider value={activateDrag}> | ||
{children} | ||
</DragActivationContext.Provider> | ||
</div> | ||
); | ||
}; |
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
Oops, something went wrong.