1
0
mirror of https://github.com/mattermost/focalboard.git synced 2025-01-26 18:48:15 +02:00
focalboard/webapp/src/hooks/sortable.tsx
Jesús Espino fd7d61b47c
Enabling again the eslint rules and applying the fixes (#3791)
* Enabling again the eslint rules and applying the fixes

* Restore , separators for types in single line mode

* fix new errors

* more lint fixes

Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
2022-09-14 07:11:50 -06:00

51 lines
2.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useRef} from 'react'
import {useDrag, useDrop, DragElementWrapper, DragSourceOptions, DragPreviewOptions} from 'react-dnd'
import {IContentBlockWithCords} from '../blocks/contentBlock'
import {Block} from '../blocks/block'
interface ISortableWithGripItem {
block: Block | Block[]
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>] {
const [{isDragging}, drag, preview] = useDrag(() => ({
type: itemType,
item,
collect: (monitor) => ({
isDragging: monitor.isDragging(),
}),
canDrag: () => enabled,
}), [itemType, item, enabled])
const [{isOver}, drop] = useDrop(() => ({
accept: itemType,
collect: (monitor) => ({
isOver: monitor.isOver(),
}),
drop: (dragItem: T) => {
handler(dragItem, item)
},
canDrop: () => enabled,
}), [item, handler, enabled])
return [isDragging, isOver, drag, drop, preview]
}
export function useSortable<T>(itemType: string, item: T, enabled: boolean, handler: (src: T, st: T) => void): [boolean, boolean, React.RefObject<HTMLDivElement>] {
const ref = useRef<HTMLDivElement>(null)
const [isDragging, isOver, drag, drop] = useSortableBase(itemType, item, enabled, handler)
drop(drag(ref))
return [isDragging, isOver, ref]
}
export function useSortableWithGrip(itemType: string, item: ISortableWithGripItem, enabled: boolean, handler: (src: IContentBlockWithCords, st: IContentBlockWithCords) => void): [boolean, boolean, React.RefObject<HTMLDivElement>, React.RefObject<HTMLDivElement>] {
const ref = useRef<HTMLDivElement>(null)
const previewRef = useRef<HTMLDivElement>(null)
const [isDragging, isOver, drag, drop, preview] = useSortableBase(itemType, item as IContentBlockWithCords, enabled, handler)
drag(ref)
drop(preview(previewRef))
return [isDragging, isOver, ref, previewRef]
}