Add math expression evaluation to NumberInput boxes (#1472)

* Add math expression parsing to NumberInput boxes

* Prevent NaN results

* Add support for implicit multiplication in expressions
This commit is contained in:
Keavon Chambers 2023-11-25 14:37:54 -08:00 committed by GitHub
parent ab3410cffe
commit 2515620a77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 16 deletions

View file

@ -2,6 +2,7 @@
import { createEventDispatcher, onMount, onDestroy } from "svelte";
import { type NumberInputMode, type NumberInputIncrementBehavior } from "@graphite/wasm-communication/messages";
import { evaluateMathExpression } from "@graphite-frontend/wasm/pkg/graphite_wasm.js";
import FieldInput from "@graphite/components/widgets/inputs/FieldInput.svelte";
@ -185,13 +186,11 @@
// The `unFocus()` call at the bottom of this function and in `onTextChangeCanceled()` causes this function to be run again, so this check skips a second run.
if (!editing) return;
const parsed = parseFloat(text);
const newValue = Number.isNaN(parsed) ? undefined : parsed;
let newValue = evaluateMathExpression(text);
if (newValue !== undefined && isNaN(newValue)) newValue = undefined; // Rejects `sqrt(-1)`
updateValue(newValue);
editing = false;
self?.unFocus();
}