Files
modeling-app/src/components/SetHorVertDistanceModal.tsx
Frank Noirot ff38ae091e Replace number command bar arg input type with kcl expression input (#1474)
* Rename useCalc

* Move CommandBar so it has access to settings and kcl

* Create codemirror variable mention extension

* Make project path a dep of TextEditor useMemo

* Add incomplete KCL input for CommandBar
to replace current number arg type

* Add previous variables autocompletion to kcl input

* Fix missed typos from merge

* Working AST mods, not working variable additions

* Add ability to create a new variable

* Add icon and tooltip to command arg tag if a variable is added

* Polish variable naming logic, preserve when going back

* Allow stepping back from KCL input

* Don't prevent keydown of enter, it's used by autocomplete

* Round the variable value in cmd bar header

* Add Playwright test

* Formatting, polish TS types

* More type wrangling

* Needed to fmt after above type wrangling

* Update snapshot tests to account for new variable name autogeneration

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* Merge branch 'main' into cmd-bar-make-variable

* Update all test instances of var name with number index after merge with main

* Partial revert of "Polish variable naming logic, preserve when going back"

This reverts commit dddcb13c36.

* Revert "Update all test instances of var name with number index after merge with main"

This reverts commit 8c4b63b523.

* Revert "Update snapshot tests to account for new variable name autogeneration"

This reverts commit 11bfce3832.

* Retry a refactoring of findUniqueName

* minor feedback from @jgomez720
- better highlighting of kcl input
- consistent hotkeys
- disallow invalid var names

* Polish stepping back state logic

* Fix tests now that keyboard shortcut changed

* Remove unused imports

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* Fix tests

* Trigger CI

* Update src/components/ProjectSidebarMenu.test.tsx

* A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu)

* re-trigger CI

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
2024-02-23 11:24:22 -05:00

186 lines
6.4 KiB
TypeScript

import { Dialog, Transition } from '@headlessui/react'
import { Fragment, useState } from 'react'
import { type InstanceProps, create } from 'react-modal-promise'
import { Value } from '../lang/wasm'
import {
AvailableVars,
addToInputHelper,
CalcResult,
CreateNewVariable,
} from './AvailableVarsHelpers'
import { useCalculateKclExpression } from 'lib/useCalculateKclExpression'
type ModalResolve = {
value: string
segName: string
valueNode: Value
variableName?: string
newVariableInsertIndex: number
sign: number
}
type ModalReject = boolean
type GetInfoModalProps = InstanceProps<ModalResolve, ModalReject> & {
segName: string
isSegNameEditable: boolean
value?: number
initialVariableName: string
}
export const createInfoModal = create<
GetInfoModalProps,
ModalResolve,
ModalReject
>
export const GetInfoModal = ({
isOpen,
onResolve,
onReject,
segName: initialSegName,
isSegNameEditable,
value: initialValue,
initialVariableName,
}: GetInfoModalProps) => {
const [sign, setSign] = useState(Math.sign(Number(initialValue)))
const [segName, setSegName] = useState(initialSegName)
const [value, setValue] = useState(
initialValue === undefined ? '' : String(Math.abs(initialValue))
)
const [shouldCreateVariable, setShouldCreateVariable] = useState(false)
const {
prevVariables,
inputRef,
calcResult,
valueNode,
setNewVariableName,
newVariableName,
isNewVariableNameUnique,
newVariableInsertIndex,
} = useCalculateKclExpression({ value: value, initialVariableName })
return (
<Transition appear show={isOpen} as={Fragment}>
<Dialog as="div" className="relative z-10" onClose={onReject}>
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="ease-in duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="fixed inset-0 bg-black bg-opacity-25" />
</Transition.Child>
<div className="fixed inset-0 overflow-y-auto">
<div className="flex min-h-full items-center justify-center p-4 text-center">
<Transition.Child
as={Fragment}
enter="ease-out duration-300"
enterFrom="opacity-0 scale-95"
enterTo="opacity-100 scale-100"
leave="ease-in duration-200"
leaveFrom="opacity-100 scale-100"
leaveTo="opacity-0 scale-95"
>
<Dialog.Panel className="w-full max-w-md transform overflow-hidden rounded-2xl bg-white/90 p-6 text-left align-middle shadow-xl transition-all">
<Dialog.Title
as="h3"
className="text-lg font-medium leading-6 text-gray-900"
>
Constraint details
</Dialog.Title>
<div className="block text-sm font-medium text-gray-700 mt-3 font-mono capitalize">
Available Variables
</div>
<AvailableVars
prevVariables={prevVariables}
onVarClick={addToInputHelper(inputRef, setValue)}
/>
<label
htmlFor="val"
className="block text-sm font-medium text-gray-700 mt-3 font-mono"
>
Distance
</label>
<div className="mt-1 flex">
<button
className="border border-gray-400 px-2 mr-1 text-gray-900"
onClick={() => setSign(-sign)}
>
{sign > 0 ? '+' : '-'}
</button>
<input
type="text"
name="val"
id="val"
ref={inputRef}
className="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm text-gray-900 border-gray-300 rounded-md font-mono"
value={value}
onChange={(e) => {
setValue(e.target.value)
}}
/>
</div>
<CalcResult calcResult={calcResult} />
<label
htmlFor="segName"
className="block text-sm font-medium text-gray-700 mt-3 font-mono"
>
Segment Name
</label>
<div className="mt-1">
<input
type="text"
name="segName"
id="segName"
disabled={!isSegNameEditable}
className="shadow-sm focus:ring-blue-500 focus:border-blue-500 block w-full sm:text-sm text-gray-900 border-gray-300 rounded-md font-mono"
value={segName}
onChange={(e) => {
setSegName(e.target.value)
}}
/>
</div>
<CreateNewVariable
setNewVariableName={setNewVariableName}
newVariableName={newVariableName}
isNewVariableNameUnique={isNewVariableNameUnique}
shouldCreateVariable={shouldCreateVariable}
setShouldCreateVariable={setShouldCreateVariable}
/>
<div className="mt-4">
<button
type="button"
className="inline-flex justify-center rounded-md border border-transparent bg-blue-100 px-4 py-2 text-sm font-medium text-blue-900 hover:bg-blue-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2"
onClick={() =>
valueNode &&
onResolve({
segName,
value,
valueNode,
newVariableInsertIndex,
sign,
variableName: shouldCreateVariable
? newVariableName
: undefined,
})
}
>
Add constraining value
</button>
</div>
</Dialog.Panel>
</Transition.Child>
</div>
</div>
</Dialog>
</Transition>
)
}