@v-c/slider 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/dist/Handles/Handle.cjs +195 -1
  2. package/dist/Handles/Handle.d.ts +4 -4
  3. package/dist/Handles/Handle.js +191 -204
  4. package/dist/Handles/index.cjs +108 -1
  5. package/dist/Handles/index.d.ts +1 -1
  6. package/dist/Handles/index.js +105 -120
  7. package/dist/Marks/Mark.cjs +51 -1
  8. package/dist/Marks/Mark.d.ts +2 -2
  9. package/dist/Marks/Mark.js +48 -39
  10. package/dist/Marks/index.cjs +39 -1
  11. package/dist/Marks/index.d.ts +2 -2
  12. package/dist/Marks/index.js +36 -32
  13. package/dist/Slider.cjs +569 -1
  14. package/dist/Slider.d.ts +53 -267
  15. package/dist/Slider.js +563 -352
  16. package/dist/Steps/Dot.cjs +52 -1
  17. package/dist/Steps/Dot.d.ts +2 -2
  18. package/dist/Steps/Dot.js +49 -38
  19. package/dist/Steps/index.cjs +64 -1
  20. package/dist/Steps/index.d.ts +2 -2
  21. package/dist/Steps/index.js +61 -41
  22. package/dist/Tracks/Track.cjs +74 -1
  23. package/dist/Tracks/Track.js +70 -81
  24. package/dist/Tracks/index.cjs +70 -1
  25. package/dist/Tracks/index.js +66 -82
  26. package/dist/_virtual/rolldown_runtime.cjs +21 -0
  27. package/dist/context.cjs +39 -1
  28. package/dist/context.d.ts +13 -7
  29. package/dist/context.js +29 -24
  30. package/dist/hooks/useDrag.cjs +160 -1
  31. package/dist/hooks/useDrag.d.ts +2 -2
  32. package/dist/hooks/useDrag.js +155 -86
  33. package/dist/hooks/useOffset.cjs +124 -1
  34. package/dist/hooks/useOffset.d.ts +2 -1
  35. package/dist/hooks/useOffset.js +122 -110
  36. package/dist/hooks/useRange.cjs +21 -1
  37. package/dist/hooks/useRange.js +18 -9
  38. package/dist/index.cjs +6 -1
  39. package/dist/index.d.ts +1 -0
  40. package/dist/index.js +4 -4
  41. package/dist/interface.cjs +0 -1
  42. package/dist/interface.js +0 -1
  43. package/dist/util.cjs +32 -1
  44. package/dist/util.js +27 -26
  45. package/package.json +18 -10
  46. package/docs/TooltipSlider.tsx +0 -94
  47. package/docs/assets/anim.less +0 -63
  48. package/docs/assets/bootstrap.less +0 -163
  49. package/docs/assets/index.less +0 -337
  50. package/docs/debug.vue +0 -59
  51. package/docs/editable.vue +0 -58
  52. package/docs/handle.vue +0 -51
  53. package/docs/marks.vue +0 -104
  54. package/docs/multiple.vue +0 -50
  55. package/docs/range.vue +0 -224
  56. package/docs/slider.stories.vue +0 -45
  57. package/docs/sliderDemo.vue +0 -283
  58. package/docs/vertical.vue +0 -135
  59. package/src/Handles/Handle.tsx +0 -232
  60. package/src/Handles/index.tsx +0 -132
  61. package/src/Marks/Mark.tsx +0 -40
  62. package/src/Marks/index.tsx +0 -40
  63. package/src/Slider.tsx +0 -602
  64. package/src/Steps/Dot.tsx +0 -40
  65. package/src/Steps/index.tsx +0 -54
  66. package/src/Tracks/Track.tsx +0 -89
  67. package/src/Tracks/index.tsx +0 -92
  68. package/src/context.ts +0 -65
  69. package/src/hooks/useDrag.ts +0 -243
  70. package/src/hooks/useOffset.ts +0 -272
  71. package/src/hooks/useRange.ts +0 -24
  72. package/src/index.ts +0 -8
  73. package/src/interface.ts +0 -17
  74. package/src/util.ts +0 -41
  75. package/vite.config.ts +0 -18
  76. package/vitest.config.ts +0 -11
@@ -1,88 +1,157 @@
1
- import N from "@v-c/util/dist/hooks/useEvent";
2
- import { ref as r, inject as Q, onUnmounted as W, computed as Z } from "vue";
3
- import { UnstableContextKey as $, defaultUnstableContextValue as w } from "../context.js";
4
- const I = 130;
5
- function _(h) {
6
- const C = "targetTouches" in h ? h.targetTouches[0] : h;
7
- return { pageX: C.pageX, pageY: C.pageY };
1
+ import { UnstableContextKey, defaultUnstableContextValue } from "../context.js";
2
+ import { computed, inject, onUnmounted, ref, watch } from "vue";
3
+ import useEvent from "@v-c/util/dist/hooks/useEvent";
4
+ var REMOVE_DIST = 130;
5
+ function getPosition(e) {
6
+ const obj = "targetTouches" in e ? e.targetTouches[0] : e;
7
+ return {
8
+ pageX: obj.pageX,
9
+ pageY: obj.pageY
10
+ };
8
11
  }
9
- function ne(h, C, f, T, X, y, B, K, q, R, x) {
10
- const Y = r(null), c = r(-1), D = r(!1), i = r(f.value), g = r(f.value), p = r(null), E = r(null), v = r(null), z = Q($, w), { onDragStart: U, onDragChange: O } = z;
11
- c.value === -1 && (i.value = f.value), W(() => {
12
- document.removeEventListener("mousemove", p.value), document.removeEventListener("mouseup", E.value), v.value && (v.value.removeEventListener("touchmove", p.value), v.value.removeEventListener("touchend", E.value));
13
- });
14
- const j = (t, n, o) => {
15
- n !== void 0 && (Y.value = n), i.value = t;
16
- let a = t;
17
- o && (a = t.filter((l, e) => e !== c.value)), B(a), O && O({
18
- rawValues: t,
19
- deleteIndex: o ? c.value : -1,
20
- draggingIndex: c.value,
21
- draggingValue: n
22
- });
23
- }, A = N(
24
- (t, n, o) => {
25
- if (t === -1) {
26
- const a = g.value[0], l = g.value[g.value.length - 1], e = T.value - a, s = X.value - l;
27
- let u = n * (X.value - T.value);
28
- u = Math.max(u, e), u = Math.min(u, s), u = y(a + u) - a;
29
- const m = g.value.map((d) => d + u);
30
- j(m);
31
- } else {
32
- const a = (X.value - T.value) * n, l = [...i.value];
33
- l[t] = g.value[t];
34
- const e = q(l, a, t, "dist");
35
- j(e.values, e.value, o);
36
- }
37
- }
38
- ), F = (t, n, o) => {
39
- t.stopPropagation();
40
- const a = o || f.value, l = a[n];
41
- c.value = n, Y.value = l, g.value = a, i.value = a, D.value = !1;
42
- const { pageX: e, pageY: s } = _(t);
43
- let u = !1;
44
- U && U({
45
- rawValues: a,
46
- draggingIndex: n,
47
- draggingValue: l
48
- });
49
- const V = (d) => {
50
- d.preventDefault();
51
- const { pageX: H, pageY: J } = _(d), M = H - e, S = J - s, { width: k, height: P } = h.value.getBoundingClientRect();
52
- let L, b;
53
- switch (C.value) {
54
- case "btt":
55
- L = -S / P, b = M;
56
- break;
57
- case "ttb":
58
- L = S / P, b = M;
59
- break;
60
- case "rtl":
61
- L = -M / k, b = S;
62
- break;
63
- default:
64
- L = M / k, b = S;
65
- }
66
- u = R ? Math.abs(b) > I && x < i.value.length : !1, D.value = u, A(n, L, u);
67
- }, m = (d) => {
68
- d.preventDefault(), document.removeEventListener("mouseup", m), document.removeEventListener("mousemove", V), v.value && (v.value.removeEventListener("touchmove", p.value), v.value.removeEventListener("touchend", E.value)), p.value = null, E.value = null, v.value = null, K(u), c.value = -1, D.value = !1;
69
- };
70
- document.addEventListener("mouseup", m), document.addEventListener("mousemove", V), t.currentTarget.addEventListener("touchend", m), t.currentTarget.addEventListener("touchmove", V), p.value = V, E.value = m, v.value = t.currentTarget;
71
- }, G = Z(() => {
72
- const t = [...f.value].sort((e, s) => e - s), n = [...i.value].sort((e, s) => e - s), o = {};
73
- n.forEach((e) => {
74
- o[e] = (o[e] || 0) + 1;
75
- }), t.forEach((e) => {
76
- o[e] = (o[e] || 0) - 1;
77
- });
78
- const a = R ? 1 : 0;
79
- return Object.values(o).reduce(
80
- (e, s) => e + Math.abs(s),
81
- 0
82
- ) <= a ? i.value : f.value;
83
- });
84
- return [c, Y, D, G, F];
12
+ function useDrag(containerRef, direction, rawValues, min, max, formatValue, triggerChange, finishChange, offsetValues, editable, minCount) {
13
+ const draggingValue = ref(null);
14
+ const draggingIndex = ref(-1);
15
+ const draggingDelete = ref(false);
16
+ const cacheValues = ref(rawValues.value);
17
+ const originValues = ref(rawValues.value);
18
+ const mouseMoveEventRef = ref(null);
19
+ const mouseUpEventRef = ref(null);
20
+ const touchEventTargetRef = ref(null);
21
+ const { onDragStart, onDragChange } = inject(UnstableContextKey, defaultUnstableContextValue);
22
+ watch(rawValues, (val) => {
23
+ if (draggingIndex.value === -1) {
24
+ cacheValues.value = [...val];
25
+ originValues.value = [...val];
26
+ }
27
+ }, { immediate: true });
28
+ onUnmounted(() => {
29
+ document.removeEventListener("mousemove", mouseMoveEventRef.value);
30
+ document.removeEventListener("mouseup", mouseUpEventRef.value);
31
+ if (touchEventTargetRef.value) {
32
+ touchEventTargetRef.value.removeEventListener("touchmove", mouseMoveEventRef.value);
33
+ touchEventTargetRef.value.removeEventListener("touchend", mouseUpEventRef.value);
34
+ }
35
+ });
36
+ const flushValues = (nextValues, nextValue, deleteMark) => {
37
+ if (nextValue !== void 0) draggingValue.value = nextValue;
38
+ cacheValues.value = nextValues;
39
+ let changeValues = nextValues;
40
+ if (deleteMark) changeValues = nextValues.filter((_, i) => i !== draggingIndex.value);
41
+ triggerChange(changeValues);
42
+ if (onDragChange) onDragChange({
43
+ rawValues: nextValues,
44
+ deleteIndex: deleteMark ? draggingIndex.value : -1,
45
+ draggingIndex: draggingIndex.value,
46
+ draggingValue: nextValue
47
+ });
48
+ };
49
+ const updateCacheValue = useEvent((valueIndex, offsetPercent, deleteMark) => {
50
+ if (valueIndex === -1) {
51
+ const startValue = originValues.value[0];
52
+ const endValue = originValues.value[originValues.value.length - 1];
53
+ const maxStartOffset = min.value - startValue;
54
+ const maxEndOffset = max.value - endValue;
55
+ let offset = offsetPercent * (max.value - min.value);
56
+ offset = Math.max(offset, maxStartOffset);
57
+ offset = Math.min(offset, maxEndOffset);
58
+ offset = formatValue.value(startValue + offset) - startValue;
59
+ flushValues(originValues.value.map((val) => val + offset));
60
+ } else {
61
+ const offsetDist = (max.value - min.value) * offsetPercent;
62
+ const cloneValues = [...cacheValues.value];
63
+ cloneValues[valueIndex] = originValues.value[valueIndex];
64
+ const next = offsetValues.value(cloneValues, offsetDist, valueIndex, "dist");
65
+ flushValues(next.values, next.value, deleteMark);
66
+ }
67
+ });
68
+ const onStartMove = (e, valueIndex, startValues) => {
69
+ e.stopPropagation();
70
+ const initialValues = startValues || rawValues.value;
71
+ const originValue = initialValues[valueIndex];
72
+ draggingIndex.value = valueIndex;
73
+ draggingValue.value = originValue;
74
+ originValues.value = initialValues;
75
+ cacheValues.value = initialValues;
76
+ draggingDelete.value = false;
77
+ const { pageX: startX, pageY: startY } = getPosition(e);
78
+ let deleteMark = false;
79
+ if (onDragStart) onDragStart({
80
+ rawValues: initialValues,
81
+ draggingIndex: valueIndex,
82
+ draggingValue: originValue
83
+ });
84
+ const onMouseMove = (event) => {
85
+ event.preventDefault();
86
+ const { pageX: moveX, pageY: moveY } = getPosition(event);
87
+ const offsetX = moveX - startX;
88
+ const offsetY = moveY - startY;
89
+ const { width, height } = containerRef.value.getBoundingClientRect();
90
+ let offSetPercent;
91
+ let removeDist;
92
+ switch (direction.value) {
93
+ case "btt":
94
+ offSetPercent = -offsetY / height;
95
+ removeDist = offsetX;
96
+ break;
97
+ case "ttb":
98
+ offSetPercent = offsetY / height;
99
+ removeDist = offsetX;
100
+ break;
101
+ case "rtl":
102
+ offSetPercent = -offsetX / width;
103
+ removeDist = offsetY;
104
+ break;
105
+ default:
106
+ offSetPercent = offsetX / width;
107
+ removeDist = offsetY;
108
+ }
109
+ deleteMark = editable.value ? Math.abs(removeDist) > REMOVE_DIST && minCount.value < cacheValues.value.length : false;
110
+ draggingDelete.value = deleteMark;
111
+ updateCacheValue(valueIndex, offSetPercent, deleteMark);
112
+ };
113
+ const onMouseUp = (event) => {
114
+ event.preventDefault();
115
+ document.removeEventListener("mouseup", onMouseUp);
116
+ document.removeEventListener("mousemove", onMouseMove);
117
+ if (touchEventTargetRef.value) {
118
+ touchEventTargetRef.value.removeEventListener("touchmove", mouseMoveEventRef.value);
119
+ touchEventTargetRef.value.removeEventListener("touchend", mouseUpEventRef.value);
120
+ }
121
+ mouseMoveEventRef.value = null;
122
+ mouseUpEventRef.value = null;
123
+ touchEventTargetRef.value = null;
124
+ finishChange(deleteMark);
125
+ draggingIndex.value = -1;
126
+ draggingDelete.value = false;
127
+ };
128
+ document.addEventListener("mouseup", onMouseUp);
129
+ document.addEventListener("mousemove", onMouseMove);
130
+ e.currentTarget.addEventListener("touchend", onMouseUp);
131
+ e.currentTarget.addEventListener("touchmove", onMouseMove);
132
+ mouseMoveEventRef.value = onMouseMove;
133
+ mouseUpEventRef.value = onMouseUp;
134
+ touchEventTargetRef.value = e.currentTarget;
135
+ };
136
+ return [
137
+ draggingIndex,
138
+ draggingValue,
139
+ draggingDelete,
140
+ computed(() => {
141
+ const sourceValues = [...rawValues.value].sort((a, b) => a - b);
142
+ const targetValues = [...cacheValues.value].sort((a, b) => a - b);
143
+ const counts = {};
144
+ targetValues.forEach((val) => {
145
+ counts[val] = (counts[val] || 0) + 1;
146
+ });
147
+ sourceValues.forEach((val) => {
148
+ counts[val] = (counts[val] || 0) - 1;
149
+ });
150
+ const maxDiffCount = editable.value ? 1 : 0;
151
+ return Object.values(counts).reduce((prev, next) => prev + Math.abs(next), 0) <= maxDiffCount ? cacheValues.value : rawValues.value;
152
+ }),
153
+ onStartMove
154
+ ];
85
155
  }
86
- export {
87
- ne as default
88
- };
156
+ var useDrag_default = useDrag;
157
+ export { useDrag_default as default };
@@ -1 +1,124 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const P=require("vue");function _(c,a,h,b,O,V){const D=P.computed(()=>r=>Math.max(c,Math.min(a,r))).value,g=r=>{if(h!==null){const i=c+Math.round((D(r)-c)/h)*h,l=u=>(String(u).split(".")[1]||"").length,s=Math.max(l(h),l(a),l(c)),t=Number(i.toFixed(s));return c<=t&&t<=a?t:null}return null},w=r=>{const i=D(r),l=b.map(u=>u&&u.value);if(h!==null){const u=g(r);u!==null&&l.push(u)}l.push(c,a);let s=l[0],t=a-c;return l.forEach(u=>{const f=Math.abs(i-u);f<=t&&(s=u,t=f)}),s},S=(r,i,l,s="unit")=>{if(typeof i=="number"){let t;const u=r[l],f=u+i;let e=[];b.forEach(n=>{e.push(n.value)}),e.push(c,a);const o=g(u);o!==null&&e.push(o);const y=i>0?1:-1;if(s==="unit"){const n=g(u+y*h);n!==null&&e.push(n)}else{const n=g(f);n!==null&&e.push(n)}e=e.filter(n=>n!==null).filter(n=>i<0?n<=u:n>=u),s==="unit"&&(e=e.filter(n=>n!==u));const E=s==="unit"?u:f;t=e[0];let N=Math.abs(t-E);if(e.forEach(n=>{const m=Math.abs(n-E);m<N&&(t=n,N=m)}),t===void 0)return i<0?c:a;if(s==="dist")return t;if(Math.abs(i)>1){const n=[...r];return n[l]=t,S(n,i-y,l,s)}return t}else{if(i==="min")return c;if(i==="max")return a}},p=(r,i,l,s="unit")=>{const t=r[l],u=S(r,i,l,s);return{value:u,changed:u!==t}},M=r=>V===null&&r===0||typeof V=="number"&&r<V;return[w,(r,i,l,s="unit")=>{const t=r.map(w),u=t[l],f=S(t,i,l,s);if(t[l]=f,O){if(typeof V=="number"||V===null){for(let e=l+1;e<t.length;e+=1){let o=!0;for(;M(t[e]-t[e-1])&&o;)({value:t[e],changed:o}=p(t,1,e))}for(let e=l;e>0;e-=1){let o=!0;for(;M(t[e]-t[e-1])&&o;)({value:t[e-1],changed:o}=p(t,-1,e-1))}for(let e=t.length-1;e>0;e-=1){let o=!0;for(;M(t[e]-t[e-1])&&o;)({value:t[e-1],changed:o}=p(t,-1,e-1))}for(let e=0;e<t.length-1;e+=1){let o=!0;for(;M(t[e+1]-t[e])&&o;)({value:t[e+1],changed:o}=p(t,1,e+1))}}}else{const e=V||0;l>0&&t[l-1]!==u&&(t[l]=Math.max(t[l],t[l-1]+e)),l<t.length-1&&t[l+1]!==u&&(t[l]=Math.min(t[l],t[l+1]-e))}return{value:t[l],values:t}}]}exports.default=_;
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ function useOffset(min, max, step, markList, allowCross, pushable) {
3
+ const formatRangeValue = (val) => Math.max(min.value, Math.min(max.value, val));
4
+ const formatStepValue = (val) => {
5
+ if (step.value !== null) {
6
+ const stepValue = min.value + Math.round((formatRangeValue(val) - min.value) / step.value) * step.value;
7
+ const getDecimal = (num) => (String(num).split(".")[1] || "").length;
8
+ const maxDecimal = Math.max(getDecimal(step.value), getDecimal(max.value), getDecimal(min.value));
9
+ const fixedValue = Number(stepValue.toFixed(maxDecimal));
10
+ return min.value <= fixedValue && fixedValue <= max.value ? fixedValue : null;
11
+ }
12
+ return null;
13
+ };
14
+ const formatValue = (val) => {
15
+ const formatNextValue = formatRangeValue(val);
16
+ const alignValues = markList.value.map((mark) => mark && mark.value);
17
+ if (step.value !== null) {
18
+ const stepValue = formatStepValue(val);
19
+ if (stepValue !== null) alignValues.push(stepValue);
20
+ }
21
+ alignValues.push(min.value, max.value);
22
+ let closeValue = alignValues[0];
23
+ let closeDist = max.value - min.value;
24
+ alignValues.forEach((alignValue) => {
25
+ const dist = Math.abs(formatNextValue - alignValue);
26
+ if (dist <= closeDist) {
27
+ closeValue = alignValue;
28
+ closeDist = dist;
29
+ }
30
+ });
31
+ return closeValue;
32
+ };
33
+ const offsetValue = (values, offset, valueIndex, mode = "unit") => {
34
+ if (typeof offset === "number") {
35
+ let nextValue;
36
+ const originValue = values[valueIndex];
37
+ const targetDistValue = originValue + offset;
38
+ let potentialValues = [];
39
+ markList.value.forEach((mark) => {
40
+ potentialValues.push(mark.value);
41
+ });
42
+ potentialValues.push(min.value, max.value);
43
+ const originStepValue = formatStepValue(originValue);
44
+ if (originStepValue !== null) potentialValues.push(originStepValue);
45
+ const sign = offset > 0 ? 1 : -1;
46
+ if (mode === "unit") {
47
+ if (step.value !== null) {
48
+ const allStepValues = formatStepValue(originValue + sign * step.value);
49
+ if (allStepValues !== null) potentialValues.push(allStepValues);
50
+ }
51
+ } else if (step.value !== null) {
52
+ const targetStepValue = formatStepValue(targetDistValue);
53
+ if (targetStepValue !== null) potentialValues.push(targetStepValue);
54
+ }
55
+ potentialValues = potentialValues.filter((val) => val !== null).filter((val) => offset < 0 ? val <= originValue : val >= originValue);
56
+ if (mode === "unit") potentialValues = potentialValues.filter((val) => val !== originValue);
57
+ const compareValue = mode === "unit" ? originValue : targetDistValue;
58
+ nextValue = potentialValues[0];
59
+ let valueDist = Math.abs(nextValue - compareValue);
60
+ potentialValues.forEach((potentialValue) => {
61
+ const dist = Math.abs(potentialValue - compareValue);
62
+ if (dist < valueDist) {
63
+ nextValue = potentialValue;
64
+ valueDist = dist;
65
+ }
66
+ });
67
+ if (nextValue === void 0) return offset < 0 ? min.value : max.value;
68
+ if (mode === "dist") return nextValue;
69
+ if (Math.abs(offset) > 1) {
70
+ const cloneValues = [...values];
71
+ cloneValues[valueIndex] = nextValue;
72
+ return offsetValue(cloneValues, offset - sign, valueIndex, mode);
73
+ }
74
+ return nextValue;
75
+ }
76
+ if (offset === "min") return min.value;
77
+ if (offset === "max") return max.value;
78
+ return values[valueIndex];
79
+ };
80
+ const offsetChangedValue = (values, offset, valueIndex, mode = "unit") => {
81
+ const originValue = values[valueIndex];
82
+ const nextValue = offsetValue(values, offset, valueIndex, mode);
83
+ return {
84
+ value: nextValue,
85
+ changed: nextValue !== originValue
86
+ };
87
+ };
88
+ const needPush = (dist) => {
89
+ return pushable.value === null && dist === 0 || typeof pushable.value === "number" && dist < pushable.value;
90
+ };
91
+ const offsetValues = (values, offset, valueIndex, mode = "unit") => {
92
+ const nextValues = values.map(formatValue);
93
+ const originValue = nextValues[valueIndex];
94
+ nextValues[valueIndex] = offsetValue(nextValues, offset, valueIndex, mode);
95
+ if (!allowCross.value) {
96
+ const pushNum = pushable.value || 0;
97
+ if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) nextValues[valueIndex] = Math.max(nextValues[valueIndex], nextValues[valueIndex - 1] + pushNum);
98
+ if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) nextValues[valueIndex] = Math.min(nextValues[valueIndex], nextValues[valueIndex + 1] - pushNum);
99
+ } else if (typeof pushable.value === "number" || pushable.value === null) {
100
+ for (let i = valueIndex + 1; i < nextValues.length; i += 1) {
101
+ let changed = true;
102
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i], changed} = offsetChangedValue(nextValues, 1, i));
103
+ }
104
+ for (let i = valueIndex; i > 0; i -= 1) {
105
+ let changed = true;
106
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i - 1], changed} = offsetChangedValue(nextValues, -1, i - 1));
107
+ }
108
+ for (let i = nextValues.length - 1; i > 0; i -= 1) {
109
+ let changed = true;
110
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i - 1], changed} = offsetChangedValue(nextValues, -1, i - 1));
111
+ }
112
+ for (let i = 0; i < nextValues.length - 1; i += 1) {
113
+ let changed = true;
114
+ while (needPush(nextValues[i + 1] - nextValues[i]) && changed) ({value: nextValues[i + 1], changed} = offsetChangedValue(nextValues, 1, i + 1));
115
+ }
116
+ }
117
+ return {
118
+ value: nextValues[valueIndex],
119
+ values: nextValues
120
+ };
121
+ };
122
+ return [formatValue, offsetValues];
123
+ }
124
+ exports.default = useOffset;
@@ -1,3 +1,4 @@
1
+ import { Ref } from 'vue';
1
2
  import { InternalMarkObj } from '../Marks';
2
3
  /** Format value align with step & marks */
3
4
  type FormatValue = (value: number) => number;
@@ -6,5 +7,5 @@ export type OffsetValues = (values: number[], offset: number | 'min' | 'max', va
6
7
  value: number;
7
8
  values: number[];
8
9
  };
9
- export default function useOffset(min: number, max: number, step: number | null, markList: InternalMarkObj[], allowCross: boolean, pushable: false | number | null): [FormatValue, OffsetValues];
10
+ export default function useOffset(min: Ref<number>, max: Ref<number>, step: Ref<number | null>, markList: Ref<InternalMarkObj[]>, allowCross: Ref<boolean>, pushable: Ref<false | number | null>): [FormatValue, OffsetValues];
10
11
  export {};
@@ -1,111 +1,123 @@
1
- import { computed as F } from "vue";
2
- function R(c, a, h, S, C, V) {
3
- const w = F(() => (r) => Math.max(c, Math.min(a, r))).value, g = (r) => {
4
- if (h !== null) {
5
- const i = c + Math.round((w(r) - c) / h) * h, l = (n) => (String(n).split(".")[1] || "").length, o = Math.max(l(h), l(a), l(c)), t = Number(i.toFixed(o));
6
- return c <= t && t <= a ? t : null;
7
- }
8
- return null;
9
- }, b = (r) => {
10
- const i = w(r), l = S.map((n) => n && n.value);
11
- if (h !== null) {
12
- const n = g(r);
13
- n !== null && l.push(n);
14
- }
15
- l.push(c, a);
16
- let o = l[0], t = a - c;
17
- return l.forEach((n) => {
18
- const f = Math.abs(i - n);
19
- f <= t && (o = n, t = f);
20
- }), o;
21
- }, D = (r, i, l, o = "unit") => {
22
- if (typeof i == "number") {
23
- let t;
24
- const n = r[l], f = n + i;
25
- let e = [];
26
- S.forEach((u) => {
27
- e.push(u.value);
28
- }), e.push(c, a);
29
- const s = g(n);
30
- s !== null && e.push(s);
31
- const m = i > 0 ? 1 : -1;
32
- if (o === "unit") {
33
- const u = g(n + m * h);
34
- u !== null && e.push(u);
35
- } else {
36
- const u = g(f);
37
- u !== null && e.push(u);
38
- }
39
- e = e.filter((u) => u !== null).filter((u) => i < 0 ? u <= n : u >= n), o === "unit" && (e = e.filter((u) => u !== n));
40
- const y = o === "unit" ? n : f;
41
- t = e[0];
42
- let E = Math.abs(t - y);
43
- if (e.forEach((u) => {
44
- const N = Math.abs(u - y);
45
- N < E && (t = u, E = N);
46
- }), t === void 0)
47
- return i < 0 ? c : a;
48
- if (o === "dist")
49
- return t;
50
- if (Math.abs(i) > 1) {
51
- const u = [...r];
52
- return u[l] = t, D(u, i - m, l, o);
53
- }
54
- return t;
55
- } else {
56
- if (i === "min")
57
- return c;
58
- if (i === "max")
59
- return a;
60
- }
61
- }, p = (r, i, l, o = "unit") => {
62
- const t = r[l], n = D(r, i, l, o);
63
- return {
64
- value: n,
65
- changed: n !== t
66
- };
67
- }, M = (r) => V === null && r === 0 || typeof V == "number" && r < V;
68
- return [b, (r, i, l, o = "unit") => {
69
- const t = r.map(b), n = t[l], f = D(t, i, l, o);
70
- if (t[l] = f, C) {
71
- if (typeof V == "number" || V === null) {
72
- for (let e = l + 1; e < t.length; e += 1) {
73
- let s = !0;
74
- for (; M(t[e] - t[e - 1]) && s; )
75
- ({ value: t[e], changed: s } = p(t, 1, e));
76
- }
77
- for (let e = l; e > 0; e -= 1) {
78
- let s = !0;
79
- for (; M(t[e] - t[e - 1]) && s; )
80
- ({ value: t[e - 1], changed: s } = p(t, -1, e - 1));
81
- }
82
- for (let e = t.length - 1; e > 0; e -= 1) {
83
- let s = !0;
84
- for (; M(t[e] - t[e - 1]) && s; )
85
- ({ value: t[e - 1], changed: s } = p(t, -1, e - 1));
86
- }
87
- for (let e = 0; e < t.length - 1; e += 1) {
88
- let s = !0;
89
- for (; M(t[e + 1] - t[e]) && s; )
90
- ({ value: t[e + 1], changed: s } = p(t, 1, e + 1));
91
- }
92
- }
93
- } else {
94
- const e = V || 0;
95
- l > 0 && t[l - 1] !== n && (t[l] = Math.max(
96
- t[l],
97
- t[l - 1] + e
98
- )), l < t.length - 1 && t[l + 1] !== n && (t[l] = Math.min(
99
- t[l],
100
- t[l + 1] - e
101
- ));
102
- }
103
- return {
104
- value: t[l],
105
- values: t
106
- };
107
- }];
1
+ function useOffset(min, max, step, markList, allowCross, pushable) {
2
+ const formatRangeValue = (val) => Math.max(min.value, Math.min(max.value, val));
3
+ const formatStepValue = (val) => {
4
+ if (step.value !== null) {
5
+ const stepValue = min.value + Math.round((formatRangeValue(val) - min.value) / step.value) * step.value;
6
+ const getDecimal = (num) => (String(num).split(".")[1] || "").length;
7
+ const maxDecimal = Math.max(getDecimal(step.value), getDecimal(max.value), getDecimal(min.value));
8
+ const fixedValue = Number(stepValue.toFixed(maxDecimal));
9
+ return min.value <= fixedValue && fixedValue <= max.value ? fixedValue : null;
10
+ }
11
+ return null;
12
+ };
13
+ const formatValue = (val) => {
14
+ const formatNextValue = formatRangeValue(val);
15
+ const alignValues = markList.value.map((mark) => mark && mark.value);
16
+ if (step.value !== null) {
17
+ const stepValue = formatStepValue(val);
18
+ if (stepValue !== null) alignValues.push(stepValue);
19
+ }
20
+ alignValues.push(min.value, max.value);
21
+ let closeValue = alignValues[0];
22
+ let closeDist = max.value - min.value;
23
+ alignValues.forEach((alignValue) => {
24
+ const dist = Math.abs(formatNextValue - alignValue);
25
+ if (dist <= closeDist) {
26
+ closeValue = alignValue;
27
+ closeDist = dist;
28
+ }
29
+ });
30
+ return closeValue;
31
+ };
32
+ const offsetValue = (values, offset, valueIndex, mode = "unit") => {
33
+ if (typeof offset === "number") {
34
+ let nextValue;
35
+ const originValue = values[valueIndex];
36
+ const targetDistValue = originValue + offset;
37
+ let potentialValues = [];
38
+ markList.value.forEach((mark) => {
39
+ potentialValues.push(mark.value);
40
+ });
41
+ potentialValues.push(min.value, max.value);
42
+ const originStepValue = formatStepValue(originValue);
43
+ if (originStepValue !== null) potentialValues.push(originStepValue);
44
+ const sign = offset > 0 ? 1 : -1;
45
+ if (mode === "unit") {
46
+ if (step.value !== null) {
47
+ const allStepValues = formatStepValue(originValue + sign * step.value);
48
+ if (allStepValues !== null) potentialValues.push(allStepValues);
49
+ }
50
+ } else if (step.value !== null) {
51
+ const targetStepValue = formatStepValue(targetDistValue);
52
+ if (targetStepValue !== null) potentialValues.push(targetStepValue);
53
+ }
54
+ potentialValues = potentialValues.filter((val) => val !== null).filter((val) => offset < 0 ? val <= originValue : val >= originValue);
55
+ if (mode === "unit") potentialValues = potentialValues.filter((val) => val !== originValue);
56
+ const compareValue = mode === "unit" ? originValue : targetDistValue;
57
+ nextValue = potentialValues[0];
58
+ let valueDist = Math.abs(nextValue - compareValue);
59
+ potentialValues.forEach((potentialValue) => {
60
+ const dist = Math.abs(potentialValue - compareValue);
61
+ if (dist < valueDist) {
62
+ nextValue = potentialValue;
63
+ valueDist = dist;
64
+ }
65
+ });
66
+ if (nextValue === void 0) return offset < 0 ? min.value : max.value;
67
+ if (mode === "dist") return nextValue;
68
+ if (Math.abs(offset) > 1) {
69
+ const cloneValues = [...values];
70
+ cloneValues[valueIndex] = nextValue;
71
+ return offsetValue(cloneValues, offset - sign, valueIndex, mode);
72
+ }
73
+ return nextValue;
74
+ }
75
+ if (offset === "min") return min.value;
76
+ if (offset === "max") return max.value;
77
+ return values[valueIndex];
78
+ };
79
+ const offsetChangedValue = (values, offset, valueIndex, mode = "unit") => {
80
+ const originValue = values[valueIndex];
81
+ const nextValue = offsetValue(values, offset, valueIndex, mode);
82
+ return {
83
+ value: nextValue,
84
+ changed: nextValue !== originValue
85
+ };
86
+ };
87
+ const needPush = (dist) => {
88
+ return pushable.value === null && dist === 0 || typeof pushable.value === "number" && dist < pushable.value;
89
+ };
90
+ const offsetValues = (values, offset, valueIndex, mode = "unit") => {
91
+ const nextValues = values.map(formatValue);
92
+ const originValue = nextValues[valueIndex];
93
+ nextValues[valueIndex] = offsetValue(nextValues, offset, valueIndex, mode);
94
+ if (!allowCross.value) {
95
+ const pushNum = pushable.value || 0;
96
+ if (valueIndex > 0 && nextValues[valueIndex - 1] !== originValue) nextValues[valueIndex] = Math.max(nextValues[valueIndex], nextValues[valueIndex - 1] + pushNum);
97
+ if (valueIndex < nextValues.length - 1 && nextValues[valueIndex + 1] !== originValue) nextValues[valueIndex] = Math.min(nextValues[valueIndex], nextValues[valueIndex + 1] - pushNum);
98
+ } else if (typeof pushable.value === "number" || pushable.value === null) {
99
+ for (let i = valueIndex + 1; i < nextValues.length; i += 1) {
100
+ let changed = true;
101
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i], changed} = offsetChangedValue(nextValues, 1, i));
102
+ }
103
+ for (let i = valueIndex; i > 0; i -= 1) {
104
+ let changed = true;
105
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i - 1], changed} = offsetChangedValue(nextValues, -1, i - 1));
106
+ }
107
+ for (let i = nextValues.length - 1; i > 0; i -= 1) {
108
+ let changed = true;
109
+ while (needPush(nextValues[i] - nextValues[i - 1]) && changed) ({value: nextValues[i - 1], changed} = offsetChangedValue(nextValues, -1, i - 1));
110
+ }
111
+ for (let i = 0; i < nextValues.length - 1; i += 1) {
112
+ let changed = true;
113
+ while (needPush(nextValues[i + 1] - nextValues[i]) && changed) ({value: nextValues[i + 1], changed} = offsetChangedValue(nextValues, 1, i + 1));
114
+ }
115
+ }
116
+ return {
117
+ value: nextValues[valueIndex],
118
+ values: nextValues
119
+ };
120
+ };
121
+ return [formatValue, offsetValues];
108
122
  }
109
- export {
110
- R as default
111
- };
123
+ export { useOffset as default };