useOffset.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. var _typeof = require("@babel/runtime/helpers/typeof");
  4. Object.defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = useOffset;
  8. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  9. var React = _interopRequireWildcard(require("react"));
  10. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  11. 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; }
  12. /** Format the value in the range of [min, max] */
  13. /** Format value align with step */
  14. /** Format value align with step & marks */
  15. function useOffset(min, max, step, markList, allowCross, pushable) {
  16. var formatRangeValue = React.useCallback(function (val) {
  17. return Math.max(min, Math.min(max, val));
  18. }, [min, max]);
  19. var formatStepValue = React.useCallback(function (val) {
  20. if (step !== null) {
  21. var stepValue = min + Math.round((formatRangeValue(val) - min) / step) * step;
  22. // Cut number in case to be like 0.30000000000000004
  23. var getDecimal = function getDecimal(num) {
  24. return (String(num).split('.')[1] || '').length;
  25. };
  26. var maxDecimal = Math.max(getDecimal(step), getDecimal(max), getDecimal(min));
  27. var fixedValue = Number(stepValue.toFixed(maxDecimal));
  28. return min <= fixedValue && fixedValue <= max ? fixedValue : null;
  29. }
  30. return null;
  31. }, [step, min, max, formatRangeValue]);
  32. var formatValue = React.useCallback(function (val) {
  33. var formatNextValue = formatRangeValue(val);
  34. // List align values
  35. var alignValues = markList.map(function (mark) {
  36. return mark.value;
  37. });
  38. if (step !== null) {
  39. alignValues.push(formatStepValue(val));
  40. }
  41. // min & max
  42. alignValues.push(min, max);
  43. // Align with marks
  44. var closeValue = alignValues[0];
  45. var closeDist = max - min;
  46. alignValues.forEach(function (alignValue) {
  47. var dist = Math.abs(formatNextValue - alignValue);
  48. if (dist <= closeDist) {
  49. closeValue = alignValue;
  50. closeDist = dist;
  51. }
  52. });
  53. return closeValue;
  54. }, [min, max, markList, step, formatRangeValue, formatStepValue]);
  55. // ========================== Offset ==========================
  56. // Single Value
  57. var offsetValue = function offsetValue(values, offset, valueIndex) {
  58. var mode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'unit';
  59. if (typeof offset === 'number') {
  60. var nextValue;
  61. var originValue = values[valueIndex];
  62. // Only used for `dist` mode
  63. var targetDistValue = originValue + offset;
  64. // Compare next step value & mark value which is best match
  65. var potentialValues = [];
  66. markList.forEach(function (mark) {
  67. potentialValues.push(mark.value);
  68. });
  69. // Min & Max
  70. potentialValues.push(min, max);
  71. // In case origin value is align with mark but not with step
  72. potentialValues.push(formatStepValue(originValue));
  73. // Put offset step value also
  74. var sign = offset > 0 ? 1 : -1;
  75. if (mode === 'unit') {
  76. potentialValues.push(formatStepValue(originValue + sign * step));
  77. } else {
  78. potentialValues.push(formatStepValue(targetDistValue));
  79. }
  80. // Find close one
  81. potentialValues = potentialValues.filter(function (val) {
  82. return val !== null;
  83. })
  84. // Remove reverse value
  85. .filter(function (val) {
  86. return offset < 0 ? val <= originValue : val >= originValue;
  87. });
  88. if (mode === 'unit') {
  89. // `unit` mode can not contain itself
  90. potentialValues = potentialValues.filter(function (val) {
  91. return val !== originValue;
  92. });
  93. }
  94. var compareValue = mode === 'unit' ? originValue : targetDistValue;
  95. nextValue = potentialValues[0];
  96. var valueDist = Math.abs(nextValue - compareValue);
  97. potentialValues.forEach(function (potentialValue) {
  98. var dist = Math.abs(potentialValue - compareValue);
  99. if (dist < valueDist) {
  100. nextValue = potentialValue;
  101. valueDist = dist;
  102. }
  103. });
  104. // Out of range will back to range
  105. if (nextValue === undefined) {
  106. return offset < 0 ? min : max;
  107. }
  108. // `dist` mode
  109. if (mode === 'dist') {
  110. return nextValue;
  111. }
  112. // `unit` mode may need another round
  113. if (Math.abs(offset) > 1) {
  114. var cloneValues = (0, _toConsumableArray2.default)(values);
  115. cloneValues[valueIndex] = nextValue;
  116. return offsetValue(cloneValues, offset - sign, valueIndex, mode);
  117. }
  118. return nextValue;
  119. } else if (offset === 'min') {
  120. return min;
  121. } else if (offset === 'max') {
  122. return max;
  123. }
  124. };
  125. /** Same as `offsetValue` but return `changed` mark to tell value changed */
  126. var offsetChangedValue = function offsetChangedValue(values, offset, valueIndex) {
  127. var mode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'unit';
  128. var originValue = values[valueIndex];
  129. var nextValue = offsetValue(values, offset, valueIndex, mode);
  130. return {
  131. value: nextValue,
  132. changed: nextValue !== originValue
  133. };
  134. };
  135. var needPush = function needPush(dist) {
  136. return pushable === null && dist === 0 || typeof pushable === 'number' && dist < pushable;
  137. };
  138. // Values
  139. var offsetValues = function offsetValues(values, offset, valueIndex) {
  140. var mode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'unit';
  141. var nextValues = values.map(formatValue);
  142. var originValue = nextValues[valueIndex];
  143. var nextValue = offsetValue(nextValues, offset, valueIndex, mode);
  144. nextValues[valueIndex] = nextValue;
  145. if (allowCross === false) {
  146. // >>>>> Allow Cross
  147. var pushNum = pushable || 0;
  148. // ============ AllowCross ===============
  149. if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) {
  150. nextValues[valueIndex] = Math.max(nextValues[valueIndex], nextValues[valueIndex - 1] + pushNum);
  151. }
  152. if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) {
  153. nextValues[valueIndex] = Math.min(nextValues[valueIndex], nextValues[valueIndex + 1] - pushNum);
  154. }
  155. } else if (typeof pushable === 'number' || pushable === null) {
  156. // >>>>> Pushable
  157. // =============== Push ==================
  158. // >>>>>> Basic push
  159. // End values
  160. for (var i = valueIndex + 1; i < nextValues.length; i += 1) {
  161. var changed = true;
  162. while (needPush(nextValues[i] - nextValues[i - 1]) && changed) {
  163. var _offsetChangedValue = offsetChangedValue(nextValues, 1, i);
  164. nextValues[i] = _offsetChangedValue.value;
  165. changed = _offsetChangedValue.changed;
  166. }
  167. }
  168. // Start values
  169. for (var _i = valueIndex; _i > 0; _i -= 1) {
  170. var _changed = true;
  171. while (needPush(nextValues[_i] - nextValues[_i - 1]) && _changed) {
  172. var _offsetChangedValue2 = offsetChangedValue(nextValues, -1, _i - 1);
  173. nextValues[_i - 1] = _offsetChangedValue2.value;
  174. _changed = _offsetChangedValue2.changed;
  175. }
  176. }
  177. // >>>>> Revert back to safe push range
  178. // End to Start
  179. for (var _i2 = nextValues.length - 1; _i2 > 0; _i2 -= 1) {
  180. var _changed2 = true;
  181. while (needPush(nextValues[_i2] - nextValues[_i2 - 1]) && _changed2) {
  182. var _offsetChangedValue3 = offsetChangedValue(nextValues, -1, _i2 - 1);
  183. nextValues[_i2 - 1] = _offsetChangedValue3.value;
  184. _changed2 = _offsetChangedValue3.changed;
  185. }
  186. }
  187. // Start to End
  188. for (var _i3 = 0; _i3 < nextValues.length - 1; _i3 += 1) {
  189. var _changed3 = true;
  190. while (needPush(nextValues[_i3 + 1] - nextValues[_i3]) && _changed3) {
  191. var _offsetChangedValue4 = offsetChangedValue(nextValues, 1, _i3 + 1);
  192. nextValues[_i3 + 1] = _offsetChangedValue4.value;
  193. _changed3 = _offsetChangedValue4.changed;
  194. }
  195. }
  196. }
  197. return {
  198. value: nextValues[valueIndex],
  199. values: nextValues
  200. };
  201. };
  202. return [formatValue, offsetValues];
  203. }