dev #320

Merged
ysCha merged 7 commits from dev into prd-deploy 2025-09-02 18:39:34 +09:00
2 changed files with 70 additions and 106 deletions
Showing only changes of commit f15ff10bf6 - Show all commits

View File

@ -4,34 +4,37 @@ import '@/styles/calc.scss'
export const CalculatorInput = ({ value, onChange, label, options = {}, id, className = 'calculator-input', readOnly = false }) => {
const [showKeypad, setShowKeypad] = useState(false)
const [displayValue, setDisplayValue] = useState(value || '0')
const [hasOperation, setHasOperation] = useState(false)
const calculatorRef = useRef(createCalculator(options))
const containerRef = useRef(null)
const inputRef = useRef(null) // input ref
const inputRef = useRef(null)
// Sync displayValue with value prop
useEffect(() => {
setDisplayValue(value || '0')
}, [value])
//
useEffect(() => {
const handleClickOutside = (event) => {
if (containerRef.current && !containerRef.current.contains(event.target)) {
setShowKeypad(false)
if (/[+\-×÷]/.test(value)) {
const newValue = calculatorRef.current.clear()
onChange(newValue)
if (hasOperation) {
// If there's an operation in progress, compute the result when losing focus
handleCompute()
}
}
}
document.addEventListener('mousedown', handleClickOutside)
return () => document.removeEventListener('mousedown', handleClickOutside)
}, [value, onChange])
// state
const [displayValue, setDisplayValue] = useState(value || '0')
const [hasOperation, setHasOperation] = useState(false)
}, [value, onChange, hasOperation])
//
const handleNumber = (num) => {
const calculator = calculatorRef.current
let newValue = ''
let newDisplayValue = ''
if (hasOperation) {
//
@ -41,30 +44,34 @@ export const CalculatorInput = ({ value, onChange, label, options = {}, id, clas
} else {
calculator.currentOperand = (calculator.currentOperand || '') + num
}
newValue = calculator.previousOperand + calculator.operation + calculator.currentOperand
setDisplayValue(newValue)
onChange(calculator.currentOperand)
newDisplayValue = calculator.previousOperand + calculator.operation + calculator.currentOperand
setDisplayValue(newDisplayValue)
} else {
//
if (value === '0' || calculator.shouldResetDisplay) {
if (displayValue === '0' || calculator.shouldResetDisplay) {
calculator.currentOperand = num.toString()
calculator.shouldResetDisplay = false
newDisplayValue = calculator.currentOperand
setDisplayValue(newDisplayValue)
if (!hasOperation) {
onChange(calculator.currentOperand)
}
} else {
calculator.currentOperand = (calculator.currentOperand || '') + num
newDisplayValue = calculator.currentOperand
setDisplayValue(newDisplayValue)
if (!hasOperation) {
onChange(newDisplayValue)
}
}
newValue = calculator.currentOperand
setDisplayValue(newValue)
onChange(newValue)
}
//
// ( )
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = newValue.length
const len = newDisplayValue.length
inputRef.current.setSelectionRange(len, len)
// Ensure focus is maintained
inputRef.current.focus()
}
})
}
@ -72,67 +79,42 @@ export const CalculatorInput = ({ value, onChange, label, options = {}, id, clas
//
const handleOperation = (operation) => {
const calculator = calculatorRef.current
let newDisplayValue = ''
// ( )
if (!calculator.currentOperand && calculator.previousOperand) {
calculator.operation = operation
const newValue = calculator.previousOperand + operation
setDisplayValue(newValue)
newDisplayValue = calculator.previousOperand + operation
setDisplayValue(newDisplayValue)
setHasOperation(true)
//
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = newValue.length
inputRef.current.setSelectionRange(len, len)
inputRef.current.focus()
}
})
return
}
if (hasOperation) {
} else if (hasOperation) {
// ,
const result = calculator.compute()
if (result !== undefined) {
calculator.previousOperand = result.toString()
calculator.operation = operation
calculator.currentOperand = ''
const newValue = result.toString() + operation
setDisplayValue(newValue)
setHasOperation(true)
onChange(result.toString())
//
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = newValue.length
inputRef.current.setSelectionRange(len, len)
inputRef.current.focus()
}
})
newDisplayValue = calculator.previousOperand + operation
setDisplayValue(newDisplayValue)
}
} else {
//
calculator.previousOperand = displayValue
//
calculator.previousOperand = calculator.currentOperand || '0'
calculator.operation = operation
calculator.currentOperand = ''
const newValue = displayValue + operation
setDisplayValue(newValue)
setHasOperation(true)
onChange(displayValue)
//
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = newValue.length
inputRef.current.setSelectionRange(len, len)
inputRef.current.focus()
}
})
newDisplayValue = calculator.previousOperand + operation
setDisplayValue(newDisplayValue)
}
// ( )
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = newDisplayValue.length
inputRef.current.setSelectionRange(len, len)
}
})
}
// AC
@ -157,44 +139,26 @@ export const CalculatorInput = ({ value, onChange, label, options = {}, id, clas
//
const handleCompute = () => {
if (!hasOperation) return
const calculator = calculatorRef.current
if (!hasOperation || !calculator.currentOperand) return
// 0
if (!calculator.currentOperand) {
calculator.currentOperand = '0'
}
//
const result = calculator.compute()
//
if (result === undefined || result === null) {
console.error('계산 결과가 유효하지 않습니다.')
return
if (result !== undefined) {
const resultStr = result.toString()
setDisplayValue(resultStr)
setHasOperation(false)
// Only call onChange with the final result
onChange(resultStr)
// ( )
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = resultStr.length
inputRef.current.setSelectionRange(len, len)
}
})
}
//
const resultStr = result.toString()
setDisplayValue(resultStr)
setHasOperation(false)
onChange(resultStr)
// ( )
calculator.clear()
calculator.previousOperand = resultStr
//
requestAnimationFrame(() => {
if (inputRef.current) {
inputRef.current.focus()
const len = resultStr.length
inputRef.current.setSelectionRange(len, len)
// Ensure focus is maintained
inputRef.current.focus()
}
})
}
// DEL

View File

@ -353,23 +353,23 @@ export default function PlacementShapeSetting({ id, pos = { x: 50, y: 180 }, pla
onChange={(value) => {
if (index === 0) {
const num = value === '' ? '' : Number(value)
setCurrentRoof({
...currentRoof,
setCurrentRoof(prev => ({
...prev,
pitch: num === '' ? '' : num,
angle: num === '' ? '' : getDegreeByChon(num),
})
}))
} else {
const num = value === '' ? '' : Number(value)
setCurrentRoof({
...currentRoof,
setCurrentRoof( prev => ({
...prev,
pitch: num === '' ? '' : getChonByDegree(num),
angle: num === '' ? '' : num,
})
}))
}
}}
options={{
allowNegative: false,
allowDecimal: (index !== 0),
allowDecimal: false //(index !== 0),
}}
/>
</div>