"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useOffset; var React = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /** Format the value in the range of [min, max] */ /** Format value align with step */ /** Format value align with step & marks */ function useOffset(min, max, step, markList, allowCross, pushable) { const formatRangeValue = React.useCallback(val => Math.max(min, Math.min(max, val)), [min, max]); const formatStepValue = React.useCallback(val => { if (step !== null) { const stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step; // Cut number in case to be like 0.30000000000000004 const getDecimal = num => (String(num).split('.')[1] || '').length; const maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min)); const fixedValue = Number(stepValue.toFixed(maxDecimal)); return min <= fixedValue && fixedValue <= max ? fixedValue : null; } return null; }, [step, min, max, formatRangeValue]); const formatValue = React.useCallback(val => { const formatNextValue = formatRangeValue(val); // List align values const alignValues = markList.map(mark => mark.value); if (step !== null) { alignValues.push(formatStepValue(val)); } // min & max alignValues.push(min, max); // Align with marks let closeValue = alignValues[0]; let closeDist = max - min; alignValues.forEach(alignValue => { const dist = Math.abs(formatNextValue - alignValue); if (dist <= closeDist) { closeValue = alignValue; closeDist = dist; } }); return closeValue; }, [min, max, markList, step, formatRangeValue, formatStepValue]); // ========================== Offset ========================== // Single Value const offsetValue = (values, offset, valueIndex, mode = 'unit') => { if (typeof offset === 'number') { let nextValue; const originValue = values[valueIndex]; // Only used for `dist` mode const targetDistValue = originValue + offset; // Compare next step value & mark value which is best match let potentialValues = []; markList.forEach(mark => { potentialValues.push(mark.value); }); // Min & Max potentialValues.push(min, max); // In case origin value is align with mark but not with step potentialValues.push(formatStepValue(originValue)); // Put offset step value also const sign = offset > 0 ? 1 : -1; if (mode === 'unit') { potentialValues.push(formatStepValue(originValue + sign * step)); } else { potentialValues.push(formatStepValue(targetDistValue)); } // Find close one potentialValues = potentialValues.filter(val => val !== null) // Remove reverse value .filter(val => offset < 0 ? val <= originValue : val >= originValue); if (mode === 'unit') { // `unit` mode can not contain itself potentialValues = potentialValues.filter(val => val !== originValue); } const compareValue = mode === 'unit' ? originValue : targetDistValue; nextValue = potentialValues[0]; let valueDist = Math.abs(nextValue - compareValue); potentialValues.forEach(potentialValue => { const dist = Math.abs(potentialValue - compareValue); if (dist < valueDist) { nextValue = potentialValue; valueDist = dist; } }); // Out of range will back to range if (nextValue === undefined) { return offset < 0 ? min : max; } // `dist` mode if (mode === 'dist') { return nextValue; } // `unit` mode may need another round if (Math.abs(offset) > 1) { const cloneValues = [...values]; cloneValues[valueIndex] = nextValue; return offsetValue(cloneValues, offset - sign, valueIndex, mode); } return nextValue; } else if (offset === 'min') { return min; } else if (offset === 'max') { return max; } }; /** Same as `offsetValue` but return `changed` mark to tell value changed */ const offsetChangedValue = (values, offset, valueIndex, mode = 'unit') => { const originValue = values[valueIndex]; const nextValue = offsetValue(values, offset, valueIndex, mode); return { value: nextValue, changed: nextValue !== originValue }; }; const needPush = dist => { return pushable === null && dist === 0 || typeof pushable === 'number' && dist < pushable; }; // Values const offsetValues = (values, offset, valueIndex, mode = 'unit') => { const nextValues = values.map(formatValue); const originValue = nextValues[valueIndex]; const nextValue = offsetValue(nextValues, offset, valueIndex, mode); nextValues[valueIndex] = nextValue; if (allowCross === false) { // >>>>> Allow Cross const pushNum = pushable || 0; // ============ AllowCross =============== if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) { nextValues[valueIndex] = Math.max(nextValues[valueIndex], nextValues[valueIndex - 1] + pushNum); } if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) { nextValues[valueIndex] = Math.min(nextValues[valueIndex], nextValues[valueIndex + 1] - pushNum); } } else if (typeof pushable === 'number' || pushable === null) { // >>>>> Pushable // =============== Push ================== // >>>>>> Basic push // End values for (let i = valueIndex + 1; i < nextValues.length; i += 1) { let changed = true; while (needPush(nextValues[i] - nextValues[i - 1]) && changed) { ({ value: nextValues[i], changed } = offsetChangedValue(nextValues, 1, i)); } } // Start values for (let i = valueIndex; i > 0; i -= 1) { let changed = true; while (needPush(nextValues[i] - nextValues[i - 1]) && changed) { ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1)); } } // >>>>> Revert back to safe push range // End to Start for (let i = nextValues.length - 1; i > 0; i -= 1) { let changed = true; while (needPush(nextValues[i] - nextValues[i - 1]) && changed) { ({ value: nextValues[i - 1], changed } = offsetChangedValue(nextValues, -1, i - 1)); } } // Start to End for (let i = 0; i < nextValues.length - 1; i += 1) { let changed = true; while (needPush(nextValues[i + 1] - nextValues[i]) && changed) { ({ value: nextValues[i + 1], changed } = offsetChangedValue(nextValues, 1, i + 1)); } } } return { value: nextValues[valueIndex], values: nextValues }; }; return [formatValue, offsetValues]; }