@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.
- package/dist/Handles/Handle.cjs +195 -1
- package/dist/Handles/Handle.d.ts +17 -5
- package/dist/Handles/Handle.js +191 -202
- package/dist/Handles/index.cjs +108 -1
- package/dist/Handles/index.d.ts +20 -4
- package/dist/Handles/index.js +105 -116
- package/dist/Marks/Mark.cjs +51 -1
- package/dist/Marks/Mark.d.ts +2 -2
- package/dist/Marks/Mark.js +48 -39
- package/dist/Marks/index.cjs +39 -1
- package/dist/Marks/index.d.ts +2 -2
- package/dist/Marks/index.js +36 -32
- package/dist/Slider.cjs +569 -1
- package/dist/Slider.d.ts +60 -263
- package/dist/Slider.js +563 -352
- package/dist/Steps/Dot.cjs +52 -1
- package/dist/Steps/Dot.d.ts +2 -2
- package/dist/Steps/Dot.js +49 -38
- package/dist/Steps/index.cjs +64 -1
- package/dist/Steps/index.d.ts +2 -2
- package/dist/Steps/index.js +61 -41
- package/dist/Tracks/Track.cjs +74 -1
- package/dist/Tracks/Track.js +70 -81
- package/dist/Tracks/index.cjs +70 -1
- package/dist/Tracks/index.js +66 -82
- package/dist/_virtual/rolldown_runtime.cjs +21 -0
- package/dist/context.cjs +39 -1
- package/dist/context.d.ts +13 -7
- package/dist/context.js +29 -24
- package/dist/hooks/useDrag.cjs +160 -1
- package/dist/hooks/useDrag.d.ts +2 -2
- package/dist/hooks/useDrag.js +155 -86
- package/dist/hooks/useOffset.cjs +124 -1
- package/dist/hooks/useOffset.d.ts +2 -1
- package/dist/hooks/useOffset.js +122 -97
- package/dist/hooks/useRange.cjs +21 -1
- package/dist/hooks/useRange.js +18 -9
- package/dist/index.cjs +6 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -4
- package/dist/interface.cjs +0 -1
- package/dist/interface.js +0 -1
- package/dist/util.cjs +32 -1
- package/dist/util.js +27 -26
- package/package.json +18 -10
- package/docs/TooltipSlider.tsx +0 -94
- package/docs/assets/anim.less +0 -63
- package/docs/assets/bootstrap.less +0 -163
- package/docs/assets/index.less +0 -337
- package/docs/debug.vue +0 -60
- package/docs/editable.vue +0 -59
- package/docs/handle.vue +0 -45
- package/docs/marks.vue +0 -94
- package/docs/multiple.vue +0 -54
- package/docs/range.vue +0 -211
- package/docs/slider.stories.vue +0 -45
- package/docs/sliderDemo.vue +0 -267
- package/docs/vertical.vue +0 -122
- package/src/Handles/Handle.tsx +0 -223
- package/src/Handles/index.tsx +0 -124
- package/src/Marks/Mark.tsx +0 -40
- package/src/Marks/index.tsx +0 -40
- package/src/Slider.tsx +0 -593
- package/src/Steps/Dot.tsx +0 -40
- package/src/Steps/index.tsx +0 -54
- package/src/Tracks/Track.tsx +0 -89
- package/src/Tracks/index.tsx +0 -92
- package/src/context.ts +0 -65
- package/src/hooks/useDrag.ts +0 -243
- package/src/hooks/useOffset.ts +0 -264
- package/src/hooks/useRange.ts +0 -24
- package/src/index.ts +0 -8
- package/src/interface.ts +0 -17
- package/src/util.ts +0 -41
- package/vite.config.ts +0 -18
- package/vitest.config.ts +0 -11
package/dist/hooks/useDrag.js
CHANGED
|
@@ -1,88 +1,157 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
};
|
|
156
|
+
var useDrag_default = useDrag;
|
|
157
|
+
export { useDrag_default as default };
|
package/dist/hooks/useOffset.cjs
CHANGED
|
@@ -1 +1,124 @@
|
|
|
1
|
-
|
|
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
|
|
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 {};
|
package/dist/hooks/useOffset.js
CHANGED
|
@@ -1,98 +1,123 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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 };
|
package/dist/hooks/useRange.cjs
CHANGED
|
@@ -1 +1,21 @@
|
|
|
1
|
-
|
|
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;
|