* Start porting artifact graph creation to Rust * Add most of artifact graph creation * Add handling loft command from recent PR * Refactor artifact merge code so that it errors when a new artifact type is added * Add sweep subtype * Finish implementation of build artifact graph * Fix wasm.ts to use new combined generated ts-rs file * Fix Rust lints * Fix lints * Fix up replacement code * Add artifact graph to WASM outcome * Add artifact graph to simulation test output * Add new artifact graph output snapshots * Fix wall field and reduce unreachable code * Change field order for subtype * Change subtype to be determined from the request, like the TS * Fix plane sweep_id * Condense code * Change ID types to be properly optional * Change to favor the new ID, the same as TS * Fix to make error impossible * Rename artifact type tag values to match TS * Fix name of field on Cap * Update outputs * Change to use Rust source range * Update output snapshots * Add conversion to mermaid mind map and add to snapshot tests * Add new mermaid mind map output * Add flowchart * Remove raw artifact graph from tests * Remove JSON artifact graph output * Update output file with header * Update output after adding flowchart * Fix flowchart to not have duplicate edges, one in each direction * Fix not not output duplicate edges in flowcharts * Change flowchart edge style to be more obvious when a direction is missing * Update output after deduplication of edges * Fix not not skip sketch-on-face artifacts * Add docs * Fix edge iteration order to be stable * Update output after fixing order * Port TS artifactGraph.test.ts tests to simulation tests * Add grouping segments and solid2ds with their path * Update output flowcharts since grouping paths * Remove TS artifactGraph tests * Remove unused d3 dependencies * Fix to track loft ID on paths * Add command ID to error messages * Move artifact graph test code to a separate file since it's a large file * Reduce function visibility * Remove TS artifact graph code * Fix spelling error with serde * Add TODO for edge cut consumed ID * Add comment about mermaid edge rank * Fix mermaid flowchart edge cuts to appear as children of their edges * Update output since fixing flowchart order * Fix to always build the artifact graph even when there's a KCL error * Add artifact graph to error output * Change optional ID merge to match TS * Remove redundant SourceRange definition * Remove Rust-flavored default source range function * Add helper for source range creation * Update doc comment for the website * Update docs after doc comment change * Fix to save engine responses in execution cache * Remove unused import * Fix to not call WASM function before beforeAll callback is run * Remove more unused imports
160 lines
4.7 KiB
TypeScript
160 lines
4.7 KiB
TypeScript
import { useSelector } from '@xstate/react'
|
|
import { useCommandsContext } from 'hooks/useCommandsContext'
|
|
import { Artifact } from 'lang/std/artifactGraph'
|
|
import { CommandArgument } from 'lib/commandTypes'
|
|
import {
|
|
canSubmitSelectionArg,
|
|
getSelectionCountByType,
|
|
getSelectionTypeDisplayText,
|
|
} from 'lib/selections'
|
|
import { kclManager } from 'lib/singletons'
|
|
import { reportRejection } from 'lib/trap'
|
|
import { toSync } from 'lib/utils'
|
|
import { modelingMachine } from 'machines/modelingMachine'
|
|
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
import { StateFrom } from 'xstate'
|
|
|
|
const semanticEntityNames: {
|
|
[key: string]: Array<Artifact['type'] | 'defaultPlane'>
|
|
} = {
|
|
face: ['wall', 'cap', 'solid2d'],
|
|
edge: ['segment', 'sweepEdge', 'edgeCutEdge'],
|
|
point: [],
|
|
plane: ['defaultPlane'],
|
|
}
|
|
|
|
function getSemanticSelectionType(selectionType: Array<Artifact['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 inputRef = useRef<HTMLInputElement>(null)
|
|
const { commandBarState, commandBarSend } = useCommandsContext()
|
|
const [hasSubmitted, setHasSubmitted] = useState(false)
|
|
const selection = useSelector(arg.machineActor, selectionSelector)
|
|
const selectionsByType = useMemo(() => {
|
|
return getSelectionCountByType(selection)
|
|
}, [selection])
|
|
const canSubmitSelection = useMemo<boolean>(
|
|
() => canSubmitSelectionArg(selectionsByType, arg),
|
|
[selectionsByType]
|
|
)
|
|
|
|
useEffect(() => {
|
|
inputRef.current?.focus()
|
|
}, [selection, inputRef])
|
|
|
|
// Show the default planes if the selection type is 'plane'
|
|
useEffect(() => {
|
|
if (arg.selectionTypes.includes('plane') && !canSubmitSelection) {
|
|
toSync(() => {
|
|
return Promise.all([
|
|
kclManager.showPlanes(),
|
|
kclManager.setSelectionFilter(['plane', 'object']),
|
|
])
|
|
}, reportRejection)()
|
|
}
|
|
|
|
return () => {
|
|
toSync(() => {
|
|
const promises = [
|
|
new Promise(() => kclManager.defaultSelectionFilter(selection)),
|
|
]
|
|
if (!kclManager._isAstEmpty(kclManager.ast)) {
|
|
promises.push(kclManager.hidePlanes())
|
|
}
|
|
return Promise.all(promises)
|
|
}, reportRejection)()
|
|
}
|
|
}, [])
|
|
|
|
// 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 flex-col 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 ')}`}
|
|
{arg.warningMessage && (
|
|
<p className="text-warn-80 bg-warn-10 px-2 py-1 rounded-sm mt-3 mr-2 -mb-2 w-full text-sm cursor-default">
|
|
{arg.warningMessage}
|
|
</p>
|
|
)}
|
|
<span data-testid="cmd-bar-arg-name" className="sr-only">
|
|
{arg.name}
|
|
</span>
|
|
<input
|
|
id="selection"
|
|
name="selection"
|
|
ref={inputRef}
|
|
required
|
|
data-testid="cmd-bar-arg-value"
|
|
placeholder="Select an entity with your mouse"
|
|
className="absolute inset-0 w-full h-full opacity-0 cursor-default"
|
|
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
|