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", "name": "bbctrl",
"version": "1.1.1b4", "version": "1.1.1b5",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "bbctrl", "name": "bbctrl",
"version": "1.1.1b4", "version": "1.1.1b5",
"hasInstallScript": true, "hasInstallScript": true,
"license": "GPL-3.0+", "license": "GPL-3.0+",
"devDependencies": { "devDependencies": {

View File

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

View File

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