You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
3.6 KiB

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import { useMemo } from 'react';
import { invariant } from '@react-dnd/invariant';
import { registerSource } from '../../common/registration';
import { useDragDropManager } from './useDragDropManager';
import { DragSourceMonitorImpl } from '../../common/DragSourceMonitorImpl';
import { SourceConnector } from '../../common/SourceConnector';
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
export function useDragSourceMonitor() {
var manager = useDragDropManager();
var monitor = useMemo(function () {
return new DragSourceMonitorImpl(manager);
}, [manager]);
var connector = useMemo(function () {
return new SourceConnector(manager.getBackend());
}, [manager]);
return [monitor, connector];
}
export function useDragHandler(spec, monitor, connector) {
var manager = useDragDropManager();
var handler = useMemo(function () {
return {
beginDrag: function beginDrag() {
var _spec$current = spec.current,
begin = _spec$current.begin,
item = _spec$current.item;
if (begin) {
var beginResult = begin(monitor);
invariant(beginResult == null || _typeof(beginResult) === 'object', 'dragSpec.begin() must either return an object, undefined, or null');
return beginResult || item || {};
}
return item || {};
},
canDrag: function canDrag() {
if (typeof spec.current.canDrag === 'boolean') {
return spec.current.canDrag;
} else if (typeof spec.current.canDrag === 'function') {
return spec.current.canDrag(monitor);
} else {
return true;
}
},
isDragging: function isDragging(globalMonitor, target) {
var isDragging = spec.current.isDragging;
return isDragging ? isDragging(monitor) : target === globalMonitor.getSourceId();
},
endDrag: function endDrag() {
var end = spec.current.end;
if (end) {
end(monitor.getItem(), monitor);
}
connector.reconnect();
}
};
}, []);
useIsomorphicLayoutEffect(function registerHandler() {
var _registerSource = registerSource(spec.current.item.type, handler, manager),
_registerSource2 = _slicedToArray(_registerSource, 2),
handlerId = _registerSource2[0],
unregister = _registerSource2[1];
monitor.receiveHandlerId(handlerId);
connector.receiveHandlerId(handlerId);
return unregister;
}, []);
}