2022-09-27 04:16:45 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2022-12-08 13:32:35 +01:00
|
|
|
import * as tr from "@tslib/ftl";
|
2022-12-04 04:18:49 +01:00
|
|
|
import { isDesktop } from "@tslib/platform";
|
|
|
|
|
2022-09-27 04:16:45 +02:00
|
|
|
import IconConstrain from "./IconConstrain.svelte";
|
2022-12-04 04:18:49 +01:00
|
|
|
import { chevronDown, chevronUp } from "./icons";
|
2022-09-27 04:16:45 +02:00
|
|
|
|
2022-10-12 06:31:54 +02:00
|
|
|
export let value: number;
|
2022-09-27 04:16:45 +02:00
|
|
|
export let step = 1;
|
|
|
|
export let min = 1;
|
|
|
|
export let max = 9999;
|
|
|
|
|
|
|
|
let input: HTMLInputElement;
|
|
|
|
let focused = false;
|
|
|
|
|
|
|
|
function decimalPlaces(value: number) {
|
|
|
|
if (Math.floor(value) === value) return 0;
|
|
|
|
return value.toString().split(".")[1].length || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stringValue: string;
|
2022-10-12 06:31:54 +02:00
|
|
|
$: if (value) stringValue = value.toFixed(decimalPlaces(step));
|
2022-09-27 04:16:45 +02:00
|
|
|
|
|
|
|
function update(this: HTMLInputElement): void {
|
|
|
|
value = Math.min(max, Math.max(min, parseFloat(this.value)));
|
|
|
|
if (value > max) {
|
|
|
|
value = max;
|
|
|
|
} else if (value < min) {
|
|
|
|
value = min;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleWheel(event: WheelEvent) {
|
|
|
|
if (focused) {
|
|
|
|
value += event.deltaY < 0 ? step : -step;
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 11:41:15 +01:00
|
|
|
|
|
|
|
function change(step: number): void {
|
|
|
|
value += step;
|
|
|
|
if (pressed) {
|
|
|
|
setTimeout(() => change(step), timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const progression = [1500, 1250, 1000, 750, 500, 250];
|
|
|
|
|
|
|
|
async function longPress(func: Function): Promise<void> {
|
|
|
|
pressed = true;
|
|
|
|
timeout = 128;
|
|
|
|
pressTimer = setTimeout(func, 250);
|
|
|
|
|
|
|
|
for (const delay of progression) {
|
|
|
|
timeout = await new Promise((resolve) =>
|
|
|
|
setTimeout(() => resolve(pressed ? timeout / 2 : 128), delay),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let pressed = false;
|
|
|
|
let timeout: number;
|
|
|
|
let pressTimer: any;
|
2022-09-27 04:16:45 +02:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<div class="spin-box" on:wheel={handleWheel}>
|
|
|
|
<input
|
|
|
|
type="number"
|
|
|
|
pattern="[0-9]*"
|
|
|
|
inputmode="numeric"
|
|
|
|
{min}
|
|
|
|
{max}
|
|
|
|
{step}
|
|
|
|
value={stringValue}
|
|
|
|
bind:this={input}
|
|
|
|
on:blur={update}
|
|
|
|
on:focusin={() => (focused = true)}
|
|
|
|
on:focusout={() => (focused = false)}
|
|
|
|
/>
|
2022-12-04 04:18:49 +01:00
|
|
|
{#if isDesktop()}
|
2022-12-08 13:32:35 +01:00
|
|
|
<div
|
|
|
|
class="spinner decrement"
|
|
|
|
class:active={value > min}
|
2022-12-04 04:18:49 +01:00
|
|
|
tabindex="-1"
|
2022-12-08 13:32:35 +01:00
|
|
|
title={tr.actionsDecrementValue()}
|
2022-12-04 04:18:49 +01:00
|
|
|
on:click={() => {
|
|
|
|
input.focus();
|
|
|
|
if (value > min) {
|
|
|
|
change(-step);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
on:mousedown={() =>
|
|
|
|
longPress(() => {
|
|
|
|
if (value > min) {
|
|
|
|
change(-step);
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
on:mouseup={() => {
|
|
|
|
clearTimeout(pressTimer);
|
|
|
|
pressed = false;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<IconConstrain>
|
|
|
|
{@html chevronDown}
|
|
|
|
</IconConstrain>
|
2022-12-08 13:32:35 +01:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
class="spinner increment"
|
|
|
|
class:active={value < max}
|
2022-12-04 04:18:49 +01:00
|
|
|
tabindex="-1"
|
2022-12-08 13:32:35 +01:00
|
|
|
title={tr.actionsIncrementValue()}
|
2022-12-04 04:18:49 +01:00
|
|
|
on:click={() => {
|
|
|
|
input.focus();
|
2022-12-01 06:34:54 +01:00
|
|
|
if (value < max) {
|
2022-11-02 11:41:15 +01:00
|
|
|
change(step);
|
|
|
|
}
|
2022-12-04 04:18:49 +01:00
|
|
|
}}
|
|
|
|
on:mousedown={() =>
|
|
|
|
longPress(() => {
|
|
|
|
if (value < max) {
|
|
|
|
change(step);
|
|
|
|
}
|
|
|
|
})}
|
|
|
|
on:mouseup={() => {
|
|
|
|
clearTimeout(pressTimer);
|
|
|
|
pressed = false;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<IconConstrain>
|
|
|
|
{@html chevronUp}
|
|
|
|
</IconConstrain>
|
2022-12-08 13:32:35 +01:00
|
|
|
</div>
|
2022-12-04 04:18:49 +01:00
|
|
|
{/if}
|
2022-09-27 04:16:45 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
.spin-box {
|
|
|
|
width: 100%;
|
2022-12-01 06:34:54 +01:00
|
|
|
background: var(--canvas-inset);
|
2022-09-27 04:16:45 +02:00
|
|
|
border: 1px solid var(--border);
|
|
|
|
border-radius: var(--border-radius);
|
|
|
|
overflow: hidden;
|
|
|
|
position: relative;
|
2022-12-01 06:34:54 +01:00
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
2022-09-27 04:16:45 +02:00
|
|
|
|
|
|
|
input {
|
2022-12-01 06:34:54 +01:00
|
|
|
flex-grow: 1;
|
2022-09-27 04:16:45 +02:00
|
|
|
border: none;
|
|
|
|
outline: none;
|
2022-12-01 06:34:54 +01:00
|
|
|
background: transparent;
|
2022-09-27 04:16:45 +02:00
|
|
|
&::-webkit-inner-spin-button {
|
|
|
|
display: none;
|
|
|
|
}
|
2022-12-04 04:18:49 +01:00
|
|
|
padding-left: 0.5em;
|
|
|
|
padding-right: 0.5em;
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
|
|
|
|
2022-12-08 13:32:35 +01:00
|
|
|
&:hover,
|
|
|
|
&:focus-within {
|
|
|
|
.spinner {
|
|
|
|
opacity: 0.1;
|
|
|
|
&.active {
|
|
|
|
opacity: 0.4;
|
|
|
|
cursor: pointer;
|
|
|
|
&:hover {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 06:34:54 +01:00
|
|
|
}
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-12-08 13:32:35 +01:00
|
|
|
.spinner {
|
|
|
|
opacity: 0;
|
2022-12-01 06:34:54 +01:00
|
|
|
height: 100%;
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
|
|
|
</style>
|