import { createLiteral, createLocalName, createUnaryExpression, } from '@src/lang/create' import type { PrevVariable } from '@src/lang/queryAst' import type { BinaryPart } from '@src/lang/wasm' export const AvailableVars = ({ onVarClick, prevVariables, }: { onVarClick: (a: string) => void prevVariables: PrevVariable[] }) => { return ( ) } export const addToInputHelper = ( inputRef: React.RefObject, setValue: (a: string) => void ) => (varName: string) => { const selectionStart = inputRef.current?.selectionStart let selectionEnd = inputRef.current?.selectionEnd let newValue = '' if ( typeof selectionStart === 'number' && typeof selectionEnd === 'number' ) { newValue = stringSplice( inputRef.current?.value || '', selectionStart, selectionEnd, varName ) selectionEnd = selectionStart + varName.length } else { newValue = inputRef.current?.value + varName } setValue(newValue) inputRef.current?.focus() setTimeout(() => { // run in the next render cycle const _selectionEnd = typeof selectionEnd === 'number' ? selectionEnd : newValue.length inputRef.current?.setSelectionRange(_selectionEnd, _selectionEnd) }) } function stringSplice(str: string, index: number, count: number, add: string) { return str.slice(0, index) + (add || '') + str.slice(index + count) } export const CalcResult = ({ calcResult }: { calcResult: string }) => { return (
= {calcResult}
) } export const CreateNewVariable = ({ newVariableName, isNewVariableNameUnique, setNewVariableName, shouldCreateVariable, setShouldCreateVariable = () => {}, showCheckbox = true, }: { isNewVariableNameUnique: boolean newVariableName: string setNewVariableName: (a: string) => void shouldCreateVariable?: boolean setShouldCreateVariable?: (a: boolean) => void showCheckbox?: boolean }) => { return ( <>
{showCheckbox && ( { setShouldCreateVariable(e.target.checked) }} className="bg-chalkboard-10 dark:bg-chalkboard-80" /> )} { setNewVariableName(e.target.value) }} />
{!isNewVariableNameUnique && (
Sorry, that's not a unique variable name. Please try something else
)} ) } export function removeDoubleNegatives( valueNode: BinaryPart, sign: number, variableName?: string ): BinaryPart { let finValue: BinaryPart = variableName ? createLocalName(variableName) : valueNode if (sign === -1) finValue = createUnaryExpression(finValue) if ( finValue.type === 'UnaryExpression' && finValue.operator === '-' && finValue.argument.type === 'UnaryExpression' && finValue.argument.operator === '-' ) { finValue = finValue.argument.argument } if ( finValue.type === 'UnaryExpression' && finValue.operator === '-' && finValue.argument.type === 'Literal' && typeof finValue.argument.value === 'number' && finValue.argument.value < 0 ) { finValue = createLiteral(-finValue.argument.value) } return finValue }