@v-c/slider 1.0.2 → 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 +17 -5
  3. package/dist/Handles/Handle.js +191 -202
  4. package/dist/Handles/index.cjs +108 -1
  5. package/dist/Handles/index.d.ts +20 -4
  6. package/dist/Handles/index.js +105 -116
  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 +60 -263
  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 -97
  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 -60
  51. package/docs/editable.vue +0 -59
  52. package/docs/handle.vue +0 -45
  53. package/docs/marks.vue +0 -94
  54. package/docs/multiple.vue +0 -54
  55. package/docs/range.vue +0 -211
  56. package/docs/slider.stories.vue +0 -45
  57. package/docs/sliderDemo.vue +0 -267
  58. package/docs/vertical.vue +0 -122
  59. package/src/Handles/Handle.tsx +0 -223
  60. package/src/Handles/index.tsx +0 -124
  61. package/src/Marks/Mark.tsx +0 -40
  62. package/src/Marks/index.tsx +0 -40
  63. package/src/Slider.tsx +0 -593
  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 -264
  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 m=require("vue");function P(c,a,f,D,O,g){const w=m.computed(()=>n=>Math.max(c,Math.min(a,n))).value,V=m.computed(()=>n=>{if(f!==null){const l=c+Math.round((w(n)-c)/f)*f,u=r=>(String(r).split(".")[1]||"").length,o=Math.max(u(f),u(a),u(c)),t=Number(l.toFixed(o));return c<=t&&t<=a?t:null}return null}).value,y=m.computed(()=>n=>{const l=w(n),u=D.map(r=>r.value);f!==null&&u.push(V(n)),u.push(c,a);let o=u[0],t=a-c;return u.forEach(r=>{const h=Math.abs(l-r);h<=t&&(o=r,t=h)}),o}).value,b=(n,l,u,o="unit")=>{if(typeof l=="number"){let t;const r=n[u],h=r+l;let e=[];D.forEach(s=>{e.push(s.value)}),e.push(c,a),e.push(V(r));const i=l>0?1:-1;o==="unit"?e.push(V(r+i*f)):e.push(V(h)),e=e.filter(s=>s!==null).filter(s=>l<0?s<=r:s>=r),o==="unit"&&(e=e.filter(s=>s!==r));const S=o==="unit"?r:h;t=e[0];let E=Math.abs(t-S);if(e.forEach(s=>{const N=Math.abs(s-S);N<E&&(t=s,E=N)}),t===void 0)return l<0?c:a;if(o==="dist")return t;if(Math.abs(l)>1){const s=[...n];return s[u]=t,b(s,l-i,u,o)}return t}else{if(l==="min")return c;if(l==="max")return a}},M=(n,l,u,o="unit")=>{const t=n[u],r=b(n,l,u,o);return{value:r,changed:r!==t}},p=n=>g===null&&n===0||typeof g=="number"&&n<g;return[y,(n,l,u,o="unit")=>{const t=n.map(y),r=t[u],h=b(t,l,u,o);if(t[u]=h,O){if(typeof g=="number"||g===null){for(let e=u+1;e<t.length;e+=1){let i=!0;for(;p(t[e]-t[e-1])&&i;)({value:t[e],changed:i}=M(t,1,e))}for(let e=u;e>0;e-=1){let i=!0;for(;p(t[e]-t[e-1])&&i;)({value:t[e-1],changed:i}=M(t,-1,e-1))}for(let e=t.length-1;e>0;e-=1){let i=!0;for(;p(t[e]-t[e-1])&&i;)({value:t[e-1],changed:i}=M(t,-1,e-1))}for(let e=0;e<t.length-1;e+=1){let i=!0;for(;p(t[e+1]-t[e])&&i;)({value:t[e+1],changed:i}=M(t,1,e+1))}}}else{const e=g||0;u>0&&t[u-1]!==r&&(t[u]=Math.max(t[u],t[u-1]+e)),u<t.length-1&&t[u+1]!==r&&(t[u]=Math.min(t[u],t[u+1]-e))}return{value:t[u],values:t}}]}exports.default=P;
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, markList: InternalMarkObj[], allowCross: boolean, pushable: false | number): [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,98 +1,123 @@
1
- import { computed as m } from "vue";
2
- function P(c, a, f, w, C, V) {
3
- const b = m(() => (n) => Math.max(c, Math.min(a, n))).value, g = m(() => (n) => {
4
- if (f !== null) {
5
- const l = c + Math.round((b(n) - c) / f) * f, r = (u) => (String(u).split(".")[1] || "").length, i = Math.max(r(f), r(a), r(c)), t = Number(l.toFixed(i));
6
- return c <= t && t <= a ? t : null;
7
- }
8
- return null;
9
- }).value, y = m(() => (n) => {
10
- const l = b(n), r = w.map((u) => u.value);
11
- f !== null && r.push(g(n)), r.push(c, a);
12
- let i = r[0], t = a - c;
13
- return r.forEach((u) => {
14
- const h = Math.abs(l - u);
15
- h <= t && (i = u, t = h);
16
- }), i;
17
- }).value, D = (n, l, r, i = "unit") => {
18
- if (typeof l == "number") {
19
- let t;
20
- const u = n[r], h = u + l;
21
- let e = [];
22
- w.forEach((s) => {
23
- e.push(s.value);
24
- }), e.push(c, a), e.push(g(u));
25
- const o = l > 0 ? 1 : -1;
26
- i === "unit" ? e.push(g(u + o * f)) : e.push(g(h)), e = e.filter((s) => s !== null).filter((s) => l < 0 ? s <= u : s >= u), i === "unit" && (e = e.filter((s) => s !== u));
27
- const E = i === "unit" ? u : h;
28
- t = e[0];
29
- let N = Math.abs(t - E);
30
- if (e.forEach((s) => {
31
- const S = Math.abs(s - E);
32
- S < N && (t = s, N = S);
33
- }), t === void 0)
34
- return l < 0 ? c : a;
35
- if (i === "dist")
36
- return t;
37
- if (Math.abs(l) > 1) {
38
- const s = [...n];
39
- return s[r] = t, D(s, l - o, r, i);
40
- }
41
- return t;
42
- } else {
43
- if (l === "min")
44
- return c;
45
- if (l === "max")
46
- return a;
47
- }
48
- }, p = (n, l, r, i = "unit") => {
49
- const t = n[r], u = D(n, l, r, i);
50
- return {
51
- value: u,
52
- changed: u !== t
53
- };
54
- }, M = (n) => V === null && n === 0 || typeof V == "number" && n < V;
55
- return [y, (n, l, r, i = "unit") => {
56
- const t = n.map(y), u = t[r], h = D(t, l, r, i);
57
- if (t[r] = h, C) {
58
- if (typeof V == "number" || V === null) {
59
- for (let e = r + 1; e < t.length; e += 1) {
60
- let o = !0;
61
- for (; M(t[e] - t[e - 1]) && o; )
62
- ({ value: t[e], changed: o } = p(t, 1, e));
63
- }
64
- for (let e = r; e > 0; e -= 1) {
65
- let o = !0;
66
- for (; M(t[e] - t[e - 1]) && o; )
67
- ({ value: t[e - 1], changed: o } = p(t, -1, e - 1));
68
- }
69
- for (let e = t.length - 1; e > 0; e -= 1) {
70
- let o = !0;
71
- for (; M(t[e] - t[e - 1]) && o; )
72
- ({ value: t[e - 1], changed: o } = p(t, -1, e - 1));
73
- }
74
- for (let e = 0; e < t.length - 1; e += 1) {
75
- let o = !0;
76
- for (; M(t[e + 1] - t[e]) && o; )
77
- ({ value: t[e + 1], changed: o } = p(t, 1, e + 1));
78
- }
79
- }
80
- } else {
81
- const e = V || 0;
82
- r > 0 && t[r - 1] !== u && (t[r] = Math.max(
83
- t[r],
84
- t[r - 1] + e
85
- )), r < t.length - 1 && t[r + 1] !== u && (t[r] = Math.min(
86
- t[r],
87
- t[r + 1] - e
88
- ));
89
- }
90
- return {
91
- value: t[r],
92
- values: t
93
- };
94
- }];
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];
95
122
  }
96
- export {
97
- P as default
98
- };
123
+ export { useOffset as default };
@@ -1 +1,21 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("@v-c/util/dist/warning");function i(e){if(e===!0||!e)return[!!e,!1,!1,0];const{editable:t=!1,draggableTrack:r=!1,minCount:a,maxCount:n}=e;return process.env.NODE_ENV!=="production"&&u.warning(!t||!r,"`editable` can not work with `draggableTrack`."),[!0,t,!t&&r,a||0,n]}exports.default=i;
1
+ Object.defineProperty(exports, "__esModule", { value: true });
2
+ const require_rolldown_runtime = require("../_virtual/rolldown_runtime.cjs");
3
+ let __v_c_util_dist_warning = require("@v-c/util/dist/warning");
4
+ function useRange(range) {
5
+ if (range === true || !range) return [
6
+ !!range,
7
+ false,
8
+ false,
9
+ 0
10
+ ];
11
+ const { editable = false, draggableTrack = false, minCount, maxCount } = range;
12
+ if (process.env.NODE_ENV !== "production") (0, __v_c_util_dist_warning.warning)(!editable || !draggableTrack, "`editable` can not work with `draggableTrack`.");
13
+ return [
14
+ true,
15
+ editable,
16
+ !editable && draggableTrack,
17
+ minCount || 0,
18
+ maxCount
19
+ ];
20
+ }
21
+ exports.default = useRange;