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";
|
2023-01-28 12:21:49 +01:00
|
|
|
import { tick } from "svelte";
|
2022-12-04 04:18:49 +01:00
|
|
|
|
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;
|
|
|
|
|
2023-05-26 04:49:44 +02:00
|
|
|
/** Set value to a new number, clamping it to a valid range, and
|
|
|
|
leaving it unchanged if `newValue` is NaN. */
|
2023-01-28 12:21:49 +01:00
|
|
|
function updateValue(newValue: number) {
|
|
|
|
if (Number.isNaN(newValue)) {
|
|
|
|
// avoid updating the value
|
|
|
|
} else {
|
|
|
|
value = Math.min(max, Math.max(min, newValue));
|
|
|
|
}
|
|
|
|
// Assigning to `value` will trigger the stringValue reactive statement below,
|
|
|
|
// but Svelte may not redraw the UI. For example, if '1' was shown, and the user
|
|
|
|
// enters '0', if the value gets clamped back to '1', Svelte will think the value hasn't
|
|
|
|
// changed, and will skip the UI update. So we manually update the DOM to ensure it stays
|
|
|
|
// in sync.
|
|
|
|
tick().then(() => (input.value = stringValue));
|
|
|
|
}
|
|
|
|
|
2022-09-27 04:16:45 +02:00
|
|
|
function decimalPlaces(value: number) {
|
2023-10-23 01:19:23 +02:00
|
|
|
if (Math.floor(value) === value) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-09-27 04:16:45 +02:00
|
|
|
return value.toString().split(".")[1].length || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let stringValue: string;
|
2023-01-28 11:58:04 +01:00
|
|
|
$: stringValue = value.toFixed(decimalPlaces(step));
|
2022-09-27 04:16:45 +02:00
|
|
|
|
|
|
|
function update(this: HTMLInputElement): void {
|
2023-01-28 12:21:49 +01:00
|
|
|
updateValue(parseFloat(this.value));
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function handleWheel(event: WheelEvent) {
|
|
|
|
if (focused) {
|
2023-01-28 12:21:49 +01:00
|
|
|
updateValue(value + (event.deltaY < 0 ? step : -step));
|
2022-09-27 04:16:45 +02:00
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
2022-11-02 11:41:15 +01:00
|
|
|
|
|
|
|
function change(step: number): void {
|
2023-01-28 12:21:49 +01:00
|
|
|
updateValue(value + step);
|
2022-11-02 11:41:15 +01:00
|
|
|
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()}
|
2023-11-14 02:40:04 +01:00
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
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()}
|
2023-11-14 02:40:04 +01:00
|
|
|
role="button"
|
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>
|
2023-11-14 02:40:04 +01:00
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
2022-12-08 13:32:35 +01:00
|
|
|
<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()}
|
2023-11-14 02:40:04 +01:00
|
|
|
role="button"
|
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>
|