* Add a shell script to get the list of KCL samples into the app * Add support for overwriting current file with sample * Move these KCL commands down into FileMachineProvider * Add support for creating a new file on desktop * Make it so these files aren't set to "renaming mode" right away * Add support for initializing default values that are functions * Add E2E tests * Add a code menu item to load a sample * Fix tsc issues * Remove `yarn fetch:samples` from `yarn postinstall` * Remove change to arg initialization logic, I was holding it wrong * Switch to use new manifest file from kcl-samples repo * Update tests now that we use proper sample titles * Remove double-load from units menu test * @jtran feedback * Don't encode `https://` that's silly * fmt * Update e2e/playwright/testing-samples-loading.spec.ts Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch> * Test feedback * Add a test step to actually check the file contents were written to (@Irev-Dev feedback) --------- Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
126 lines
3.8 KiB
TypeScript
126 lines
3.8 KiB
TypeScript
import { useSelector } from '@xstate/react'
|
|
import { useCommandsContext } from 'hooks/useCommandsContext'
|
|
import { useKclContext } from 'lang/KclProvider'
|
|
import { CommandArgument } from 'lib/commandTypes'
|
|
import {
|
|
Selection,
|
|
canSubmitSelectionArg,
|
|
getSelectionType,
|
|
getSelectionTypeDisplayText,
|
|
} from 'lib/selections'
|
|
import { modelingMachine } from 'machines/modelingMachine'
|
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
import { StateFrom } from 'xstate'
|
|
|
|
const semanticEntityNames: { [key: string]: Array<Selection['type']> } = {
|
|
face: ['extrude-wall', 'start-cap', 'end-cap'],
|
|
edge: ['edge', 'line', 'arc'],
|
|
point: ['point', 'line-end', 'line-mid'],
|
|
}
|
|
|
|
function getSemanticSelectionType(selectionType: Array<Selection['type']>) {
|
|
const semanticSelectionType = new Set()
|
|
selectionType.forEach((type) => {
|
|
Object.entries(semanticEntityNames).forEach(([entity, entityTypes]) => {
|
|
if (entityTypes.includes(type)) {
|
|
semanticSelectionType.add(entity)
|
|
}
|
|
})
|
|
})
|
|
|
|
return Array.from(semanticSelectionType)
|
|
}
|
|
|
|
const selectionSelector = (snapshot?: StateFrom<typeof modelingMachine>) =>
|
|
snapshot?.context.selectionRanges
|
|
|
|
function CommandBarSelectionInput({
|
|
arg,
|
|
stepBack,
|
|
onSubmit,
|
|
}: {
|
|
arg: CommandArgument<unknown> & { inputType: 'selection'; name: string }
|
|
stepBack: () => void
|
|
onSubmit: (data: unknown) => void
|
|
}) {
|
|
const { code } = useKclContext()
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
const { commandBarState, commandBarSend } = useCommandsContext()
|
|
const [hasSubmitted, setHasSubmitted] = useState(false)
|
|
const selection = useSelector(arg.machineActor, selectionSelector)
|
|
const selectionsByType = useMemo(() => {
|
|
const selectionRangeEnd = selection?.codeBasedSelections[0]?.range[1]
|
|
return !selectionRangeEnd || selectionRangeEnd === code.length
|
|
? 'none'
|
|
: getSelectionType(selection)
|
|
}, [selection, code])
|
|
const canSubmitSelection = useMemo<boolean>(
|
|
() => canSubmitSelectionArg(selectionsByType, arg),
|
|
[selectionsByType]
|
|
)
|
|
|
|
useEffect(() => {
|
|
inputRef.current?.focus()
|
|
}, [selection, inputRef])
|
|
|
|
// Fast-forward through this arg if it's marked as skippable
|
|
// and we have a valid selection already
|
|
useEffect(() => {
|
|
const argValue = commandBarState.context.argumentsToSubmit[arg.name]
|
|
if (canSubmitSelection && arg.skip && argValue === undefined) {
|
|
handleSubmit()
|
|
}
|
|
}, [canSubmitSelection])
|
|
|
|
function handleChange() {
|
|
inputRef.current?.focus()
|
|
}
|
|
|
|
function handleSubmit(e?: React.FormEvent<HTMLFormElement>) {
|
|
e?.preventDefault()
|
|
|
|
if (!canSubmitSelection) {
|
|
setHasSubmitted(true)
|
|
return
|
|
}
|
|
|
|
onSubmit(selection)
|
|
}
|
|
|
|
return (
|
|
<form id="arg-form" onSubmit={handleSubmit}>
|
|
<label
|
|
className={
|
|
'relative flex items-center mx-4 my-4 ' +
|
|
(!hasSubmitted || canSubmitSelection || 'text-destroy-50')
|
|
}
|
|
>
|
|
{canSubmitSelection
|
|
? getSelectionTypeDisplayText(selection) + ' selected'
|
|
: `Please select ${
|
|
arg.multiple ? 'one or more ' : 'one '
|
|
}${getSemanticSelectionType(arg.selectionTypes).join(' or ')}`}
|
|
<input
|
|
id="selection"
|
|
name="selection"
|
|
ref={inputRef}
|
|
required
|
|
placeholder="Select an entity with your mouse"
|
|
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer"
|
|
onKeyDown={(event) => {
|
|
if (event.key === 'Backspace') {
|
|
stepBack()
|
|
} else if (event.key === 'Escape') {
|
|
commandBarSend({ type: 'Close' })
|
|
}
|
|
}}
|
|
onChange={handleChange}
|
|
value={JSON.stringify(selection || {})}
|
|
/>
|
|
</label>
|
|
</form>
|
|
)
|
|
}
|
|
|
|
export default CommandBarSelectionInput
|