function TextScroller(t, options = {}) {
const {
comma = false,
wheel = true,
keydown = true,
highlight = true,
step = 1,
formatting = (n) => {
return n;
},
constraints = (n) => {
return true;
}
} = options;
let action = (e) => {
if (e.which != 38 && e.which != 40 && e.type != "wheel") return;
if (!t.input)
t.input = t.querySelector("input") || t.querySelector("textarea");
if (!t.input) t.input = t;
if (t.input != document.activeElement) return;
e.preventDefault();
let delta = e.which == 40 || e.deltaY < 0 ? -step : step;
let deltaScale = 10;
if (e.altKey) delta *= deltaScale;
if (e.ctrlKey) delta *= deltaScale;
if (e.shiftKey) delta *= deltaScale;
let newValue = "";
let selection = { start: 0, end: 0 };
parseNumbers(
t.input.value,
t.input.selectionStart,
t.input.selectionEnd,
delta,
options
).forEach((n, i, a) => {
if (i == 0) selection.start = n.start;
newValue +=
t.input.value.substring(i == 0 ? 0 : a[i - 1].end, n.start) +
n.newNumber;
if (i == a.length - 1) {
selection.end = newValue.length;
newValue += t.input.value.substring(n.end, t.input.value.length);
}
});
if (newValue == "") return;
t.input.value = newValue;
t.input.dispatchEvent(new CustomEvent("input"));
t.dispatchEvent(new CustomEvent("input"));
if (highlight || selection.start != selection.end) {
t.input.setSelectionRange(selection.start, selection.end);
}
};
if (wheel) t.addEventListener("wheel", action);
if (keydown) t.addEventListener("keydown", action);
return t;
}