import { useState, useEffect } from 'react' import { create } from 'react-modal-promise' import { toolTips, useStore } from '../../useStore' import { Value } from '../../lang/wasm' import { getNodePathFromSourceRange, getNodeFromPath, } from '../../lang/queryAst' import { TransformInfo, getTransformInfos, transformAstSketchLines, ConstraintType, } from '../../lang/std/sketchcombos' import { SetAngleLengthModal } from '../SetAngleLengthModal' import { createIdentifier, createVariableDeclaration, } from '../../lang/modifyAst' import { removeDoubleNegatives } from '../AvailableVarsHelpers' import { updateCursors } from '../../lang/util' const getModalInfo = create(SetAngleLengthModal as any) type ButtonType = 'xAbs' | 'yAbs' | 'snapToYAxis' | 'snapToXAxis' const buttonLabels: Record = { xAbs: 'Set distance from X Axis', yAbs: 'Set distance from Y Axis', snapToYAxis: 'Snap To Y Axis', snapToXAxis: 'Snap To X Axis', } export const SetAbsDistance = ({ buttonType }: { buttonType: ButtonType }) => { const { guiMode, selectionRanges, ast, programMemory, updateAst, setCursor } = useStore((s) => ({ guiMode: s.guiMode, ast: s.ast, updateAst: s.updateAst, selectionRanges: s.selectionRanges, programMemory: s.programMemory, setCursor: s.setCursor, })) const disType: ConstraintType = buttonType === 'xAbs' || buttonType === 'yAbs' ? buttonType : buttonType === 'snapToYAxis' ? 'xAbs' : 'yAbs' const [enableAngLen, setEnableAngLen] = useState(false) const [transformInfos, setTransformInfos] = useState() useEffect(() => { if (!ast) return const paths = selectionRanges.codeBasedSelections.map(({ range }) => getNodePathFromSourceRange(ast, range) ) const nodes = paths.map( (pathToNode) => getNodeFromPath(ast, pathToNode, 'CallExpression').node ) const isAllTooltips = nodes.every( (node) => node?.type === 'CallExpression' && toolTips.includes(node.callee.name as any) ) const theTransforms = getTransformInfos(selectionRanges, ast, disType) setTransformInfos(theTransforms) const enableY = disType === 'yAbs' && selectionRanges.otherSelections.length === 1 && selectionRanges.otherSelections[0] === 'x-axis' // select the x axis to set the distance from it i.e. y const enableX = disType === 'xAbs' && selectionRanges.otherSelections.length === 1 && selectionRanges.otherSelections[0] === 'y-axis' // select the y axis to set the distance from it i.e. x const _enableHorz = isAllTooltips && theTransforms.every(Boolean) && selectionRanges.codeBasedSelections.length === 1 && (enableX || enableY) setEnableAngLen(_enableHorz) }, [guiMode, selectionRanges]) if (guiMode.mode !== 'sketch') return null const isAlign = buttonType === 'snapToYAxis' || buttonType === 'snapToXAxis' return ( ) }