useOffset.js 7.7 KB

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