2021-04-07 22:48:18 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
import React, {useRef} from 'react'
|
2021-04-13 15:33:25 +02:00
|
|
|
import {useDrag, useDrop, DragElementWrapper, DragSourceOptions, DragPreviewOptions} from 'react-dnd'
|
2021-04-07 22:48:18 +02:00
|
|
|
|
2021-10-05 19:55:55 -03:00
|
|
|
import {IContentBlockWithCords} from '../blocks/contentBlock'
|
|
|
|
import {Block} from '../blocks/block'
|
|
|
|
interface ISortableWithGripItem {
|
2022-09-14 15:11:50 +02:00
|
|
|
block: Block | Block[]
|
2021-10-05 19:55:55 -03:00
|
|
|
cords: {x: number, y?: number, z?: number}
|
|
|
|
}
|
|
|
|
|
|
|
|
function useSortableBase<T>(itemType: string, item: T, enabled: boolean, handler: (src: T, st: T) => void): [boolean, boolean, DragElementWrapper<DragSourceOptions>, DragElementWrapper<DragSourceOptions>, DragElementWrapper<DragPreviewOptions>] {
|
2021-04-12 18:23:11 +02:00
|
|
|
const [{isDragging}, drag, preview] = useDrag(() => ({
|
2021-04-07 22:48:18 +02:00
|
|
|
type: itemType,
|
|
|
|
item,
|
|
|
|
collect: (monitor) => ({
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}),
|
2021-04-08 09:55:16 +02:00
|
|
|
canDrag: () => enabled,
|
|
|
|
}), [itemType, item, enabled])
|
2021-04-07 22:48:18 +02:00
|
|
|
const [{isOver}, drop] = useDrop(() => ({
|
|
|
|
accept: itemType,
|
|
|
|
collect: (monitor) => ({
|
|
|
|
isOver: monitor.isOver(),
|
|
|
|
}),
|
2021-10-05 19:55:55 -03:00
|
|
|
drop: (dragItem: T) => {
|
2021-04-07 22:48:18 +02:00
|
|
|
handler(dragItem, item)
|
|
|
|
},
|
2021-04-08 09:55:16 +02:00
|
|
|
canDrop: () => enabled,
|
|
|
|
}), [item, handler, enabled])
|
2021-04-07 22:48:18 +02:00
|
|
|
|
2021-04-13 15:33:25 +02:00
|
|
|
return [isDragging, isOver, drag, drop, preview]
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:55:55 -03:00
|
|
|
export function useSortable<T>(itemType: string, item: T, enabled: boolean, handler: (src: T, st: T) => void): [boolean, boolean, React.RefObject<HTMLDivElement>] {
|
2021-04-13 15:33:25 +02:00
|
|
|
const ref = useRef<HTMLDivElement>(null)
|
|
|
|
const [isDragging, isOver, drag, drop] = useSortableBase(itemType, item, enabled, handler)
|
2021-04-07 22:48:18 +02:00
|
|
|
drop(drag(ref))
|
2021-04-13 15:33:25 +02:00
|
|
|
return [isDragging, isOver, ref]
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:55:55 -03:00
|
|
|
export function useSortableWithGrip(itemType: string, item: ISortableWithGripItem, enabled: boolean, handler: (src: IContentBlockWithCords, st: IContentBlockWithCords) => void): [boolean, boolean, React.RefObject<HTMLDivElement>, React.RefObject<HTMLDivElement>] {
|
2021-04-13 15:33:25 +02:00
|
|
|
const ref = useRef<HTMLDivElement>(null)
|
|
|
|
const previewRef = useRef<HTMLDivElement>(null)
|
2021-10-05 19:55:55 -03:00
|
|
|
const [isDragging, isOver, drag, drop, preview] = useSortableBase(itemType, item as IContentBlockWithCords, enabled, handler)
|
2021-04-13 15:33:25 +02:00
|
|
|
drag(ref)
|
2021-04-12 18:23:11 +02:00
|
|
|
drop(preview(previewRef))
|
|
|
|
return [isDragging, isOver, ref, previewRef]
|
2021-04-07 22:48:18 +02:00
|
|
|
}
|