Anki/ts/lib/components/SpinBox.svelte
Damien Elmes 9f55cf26fc
Switch to SvelteKit (#3077)
* Update to latest Node LTS

* Add sveltekit

* Split tslib into separate @generated and @tslib components

SvelteKit's path aliases don't support multiple locations, so our old
approach of using @tslib to refer to both ts/lib and out/ts/lib will no
longer work. Instead, all generated sources and their includes are
placed in a separate out/ts/generated folder, and imported via @generated
instead. This also allows us to generate .ts files, instead of needing
to output separate .d.ts and .js files.

* Switch package.json to module type

* Avoid usage of baseUrl

Incompatible with SvelteKit

* Move sass into ts; use relative links

SvelteKit's default sass support doesn't allow overriding loadPaths

* jest->vitest, graphs example working with yarn dev

* most pages working in dev mode

* Some fixes after rebasing

* Fix/silence some svelte-check errors

* Get image-occlusion working with Fabric types

* Post-rebase lock changes

* Editor is now checked

* SvelteKit build integrated into ninja

* Use the new SvelteKit entrypoint for pages like congrats/deck options/etc

* Run eslint once for ts/**; fix some tests

* Fix a bunch of issues introduced when rebasing over latest main

* Run eslint fix

* Fix remaining eslint+pylint issues; tests now all pass

* Fix some issues with a clean build

* Latest bufbuild no longer requires @__PURE__ hack

* Add a few missed dependencies

* Add yarn.bat to fix Windows build

* Fix pages failing to show when ANKI_API_PORT not defined

* Fix svelte-check and vitest on Windows

* Set node path in ./yarn

* Move svelte-kit output to ts/.svelte-kit

Sadly, I couldn't figure out a way to store it in out/ if out/ is
a symlink, as it breaks module resolution when SvelteKit is run.

* Allow HMR inside Anki

* Skip SvelteKit build when HMR is defined

* Fix some post-rebase issues

I should have done a normal merge instead.
2024-03-31 09:16:31 +01:00

199 lines
5.5 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import * as tr from "@generated/ftl";
import { isDesktop } from "@tslib/platform";
import { tick } from "svelte";
import IconConstrain from "./IconConstrain.svelte";
import { chevronDown, chevronUp } from "./icons";
export let value: number;
export let step = 1;
export let min = 1;
export let max = 9999;
let input: HTMLInputElement;
let focused = false;
/** Set value to a new number, clamping it to a valid range, and
leaving it unchanged if `newValue` is NaN. */
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));
}
function decimalPlaces(value: number) {
if (Math.floor(value) === value) {
return 0;
}
return value.toString().split(".")[1].length || 0;
}
let stringValue: string;
$: stringValue = value.toFixed(decimalPlaces(step));
function update(this: HTMLInputElement): void {
updateValue(parseFloat(this.value));
}
function handleWheel(event: WheelEvent) {
if (focused) {
updateValue(value + (event.deltaY < 0 ? step : -step));
event.preventDefault();
}
}
function change(step: number): void {
updateValue(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;
</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)}
/>
{#if isDesktop()}
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="spinner decrement"
class:active={value > min}
tabindex="-1"
title={tr.actionsDecrementValue()}
role="button"
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>
</div>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div
class="spinner increment"
class:active={value < max}
tabindex="-1"
title={tr.actionsIncrementValue()}
role="button"
on:click={() => {
input.focus();
if (value < max) {
change(step);
}
}}
on:mousedown={() =>
longPress(() => {
if (value < max) {
change(step);
}
})}
on:mouseup={() => {
clearTimeout(pressTimer);
pressed = false;
}}
>
<IconConstrain>
{@html chevronUp}
</IconConstrain>
</div>
{/if}
</div>
<style lang="scss">
.spin-box {
width: 100%;
background: var(--canvas-inset);
border: 1px solid var(--border);
border-radius: var(--border-radius);
overflow: hidden;
position: relative;
display: flex;
justify-content: space-between;
input {
flex-grow: 1;
border: none;
outline: none;
background: transparent;
&::-webkit-inner-spin-button {
display: none;
}
padding-left: 0.5em;
padding-right: 0.5em;
}
&:hover,
&:focus-within {
.spinner {
opacity: 0.1;
&.active {
opacity: 0.4;
cursor: pointer;
&:hover {
opacity: 1;
}
}
}
}
}
.spinner {
opacity: 0;
height: 100%;
}
</style>