1 line
25 KiB
JSON
1 line
25 KiB
JSON
|
{"ast":null,"code":"import _toConsumableArray from \"@babel/runtime/helpers/esm/toConsumableArray\";\nimport _slicedToArray from \"@babel/runtime/helpers/esm/slicedToArray\";\nimport { useEvent } from 'rc-util';\nimport * as React from 'react';\nimport { UnstableContext } from \"../context\";\n/** Drag to delete offset. It's a user experience number for dragging out */\nvar REMOVE_DIST = 130;\nfunction getPosition(e) {\n var obj = 'touches' in e ? e.touches[0] : e;\n return {\n pageX: obj.pageX,\n pageY: obj.pageY\n };\n}\nfunction useDrag(containerRef, direction, rawValues, min, max, formatValue, triggerChange, finishChange, offsetValues, editable, minCount) {\n var _React$useState = React.useState(null),\n _React$useState2 = _slicedToArray(_React$useState, 2),\n draggingValue = _React$useState2[0],\n setDraggingValue = _React$useState2[1];\n var _React$useState3 = React.useState(-1),\n _React$useState4 = _slicedToArray(_React$useState3, 2),\n draggingIndex = _React$useState4[0],\n setDraggingIndex = _React$useState4[1];\n var _React$useState5 = React.useState(false),\n _React$useState6 = _slicedToArray(_React$useState5, 2),\n draggingDelete = _React$useState6[0],\n setDraggingDelete = _React$useState6[1];\n var _React$useState7 = React.useState(rawValues),\n _React$useState8 = _slicedToArray(_React$useState7, 2),\n cacheValues = _React$useState8[0],\n setCacheValues = _React$useState8[1];\n var _React$useState9 = React.useState(rawValues),\n _React$useState10 = _slicedToArray(_React$useState9, 2),\n originValues = _React$useState10[0],\n setOriginValues = _React$useState10[1];\n var mouseMoveEventRef = React.useRef(null);\n var mouseUpEventRef = React.useRef(null);\n var _React$useContext = React.useContext(UnstableContext),\n onDragStart = _React$useContext.onDragStart,\n onDragChange = _React$useContext.onDragChange;\n React.useLayoutEffect(function () {\n if (draggingIndex === -1) {\n setCacheValues(rawValues);\n }\n }, [rawValues, draggingIndex]);\n\n // Clean up event\n React.useEffect(function () {\n return function () {\n document.removeEventListener('mousemove', mouseMoveEventRef.current);\n document.removeEventListener('mouseup', mouseUpEventRef.current);\n document.removeEventListener('touchmove', mouseMoveEventRef.current);\n document.removeEventListener('touchend', mouseUpEventRef.current);\n };\n }, []);\n var flushValues = function flushValues(nextValues, nextValue, deleteMark) {\n // Perf: Only update state when value changed\n if (nextValue !== undefined) {\n setDraggingValue(nextValue);\n }\n setCacheValues(nextValues);\n var changeValues = nextValues;\n if (deleteMark) {\n changeValues = nextValues.filter(function (_, i) {\n return i !== draggingIndex;\n });\n }\n triggerChange(changeValues);\n if (onDragChange) {\n onDragChange({\n rawValues: nextValues,\n deleteIndex: deleteMark ? draggingIndex : -1,\n draggingIndex: draggingIndex,\n draggingValue: nextValue\n });\n }\n };\n var updateCacheValue = useEvent(function (valueIndex, offsetPercent, deleteMark) {\n if (valueIndex === -1) {\n // >>>> Dragging on the track\n var startValue = originValues[0];\n var endValue = originValues[originValues.length - 1];\n var maxStartOffset = min - startValue;\n var maxEndOffset = max - endValue;\n\n // Get valid offset\n var offset = offsetPercent * (max - min);\n offset = Math.max(offset, maxStartOffset);\n offset = Math.min(offset, maxEndOffset);\n\n // Use first value to revert back of valid offset (like steps marks)\n var formatStartValue = formatValue(startValue + offset);\n offset = formatStartValue - startValue;\n var cloneCacheValues = originValues.map(function (val) {\n return val + offset;\n });\n flushValues(cloneCacheValues);\n } else {\n // >>>> Dragging on the handle\n var offsetDist = (max - min) * offs
|