The Settings page was saving numbers as strings.

This commit is contained in:
David Carley
2022-09-13 02:17:32 +00:00
parent 1a44fe3c8e
commit c6490fd7b2
3 changed files with 49 additions and 33 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "bbctrl",
"version": "1.1.1b4",
"version": "1.1.1b5",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "bbctrl",
"version": "1.1.1b4",
"version": "1.1.1b5",
"hasInstallScript": true,
"license": "GPL-3.0+",
"devDependencies": {

View File

@@ -1,6 +1,6 @@
{
"name": "bbctrl",
"version": "1.1.1b4",
"version": "1.1.1b5",
"homepage": "https://onefinitycnc.com/",
"repository": "https://github.com/OneFinityCNC/onefinity",
"license": "GPL-3.0+",

View File

@@ -4,13 +4,14 @@
import { ControllerMethods } from "$lib/RegisterControllerMethods";
import { onMount } from "svelte";
type ValueType =
| string
| number
| { title: string; value: string | number };
type Template = {
type?: string;
values?: (
| string
| number
| { title: string; value: string | number }
)[];
values?: Array<ValueType>;
unit?: "string";
iunit?: "string";
min?: number;
@@ -73,18 +74,15 @@
return value;
}
function onKeyup(event) {
value = event.target.value;
onChange();
}
function onChange() {
function onChange(event) {
Config.update((config) => {
let target = config;
for (const part of keyParts.slice(0, -1)) {
target = target[part];
}
const value = coerceValue(event.target.value);
console.log(value);
target[keyParts[keyParts.length - 1]] = value;
return config;
@@ -92,6 +90,37 @@
ControllerMethods.dispatch("config-changed");
}
function coerceValue(value) {
switch (template.type) {
case "float":
case "int":
return Number(value);
default:
return value;
}
}
function getOptionValue(opt: ValueType) {
switch (typeof opt) {
case "object":
return opt.value || opt;
default:
return opt;
}
}
function getOptionTitle(opt: ValueType) {
switch (typeof opt) {
case "object":
return opt.title || opt;
default:
return opt;
}
}
</script>
{#if template}
@@ -102,10 +131,10 @@
<select {name} bind:value on:change={onChange}>
{#each template.values as opt}
<option
value={opt?.value ?? opt}
value={getOptionValue(opt)}
disabled={opt === "-----"}
>
{opt?.title ?? opt}
{getOptionTitle(opt)}
</option>
{/each}
</select>
@@ -119,8 +148,7 @@
max={template.max}
step={template.step || "any"}
bind:value
on:input={onChange}
on:keyup={onKeyup}
on:keyup={onChange}
/>
{:else if template.type === "int"}
<input
@@ -129,24 +157,12 @@
min={template.min}
max={template.max}
bind:value
on:input={onChange}
on:keyup={onKeyup}
on:keyup={onChange}
/>
{:else if template.type === "string"}
<input
{name}
type="text"
bind:value
on:input={onChange}
on:keyup={onKeyup}
/>
<input {name} type="text" bind:value on:keyup={onChange} />
{:else if template.type == "text"}
<textarea
{name}
bind:value
on:input={onChange}
on:keyup={onKeyup}
/>
<textarea {name} bind:value on:keyup={onChange} />
{/if}
<label for="" class="units">{units || ""}</label>