2024-10-31 18:07:51 +09:00

142 lines
5.6 KiB
JavaScript

import { useEffect, useRef, useState } from 'react'
import WithDraggable from '@/components/common/draggable/WithDraggable'
import { usePopup } from '@/hooks/usePopup'
import { useMessage } from '@/hooks/useMessage'
import { useRecoilState, useRecoilValue } from 'recoil'
import { contextPopupPositionState } from '@/store/popupAtom'
import QSelectBox from '@/components/common/select/QSelectBox'
import { canvasState, pitchTextSelector } from '@/store/canvasAtom'
import { defaultSlope } from '@/store/commonAtom'
export default function DimensionLineSetting(props) {
const contextPopupPosition = useRecoilValue(contextPopupPositionState)
let { id, setIsShow, pos = contextPopupPosition } = props
const { getMessage } = useMessage()
const { closePopup } = usePopup()
const canvas = useRecoilValue(canvasState)
const pitchText = useRecoilState(pitchTextSelector)
const SelectOption01 = defaultSlope
const [basicLength, setBasicLength] = useState(0)
const [slopeAble, setSlopeAble] = useState(false)
const changeSlopeRef = useRef()
let slopeInput1, slopeInput2
useEffect(() => {
if (canvas) {
const dimensionObject = canvas.getActiveObject()
const id = dimensionObject.groupId
const textObj = dimensionObject._objects.filter((obj) => obj.name === 'dimensionLineText' && obj.id === id)[0]
setBasicLength(parseInt(textObj.text))
}
return () => {
setBasicLength(0)
}
}, [])
const handleChangeLength = () => {
const changeLength = changeSlopeRef.current
if (canvas) {
const dimensionObject = canvas.getActiveObject()
const id = dimensionObject.groupId
const textObj = dimensionObject._objects.filter((obj) => obj.name === 'dimensionLineText' && obj.id === id)[0]
let resultText = changeLength.value > 0 ? changeLength.value : '0'
if (slopeAble) {
if (slopeInput1) {
resultText = calculateLength(basicLength, slopeInput1.angleValue).toFixed(0)
if (slopeInput2) {
const angle = slopeInput1 + slopeInput2
const length = calculateLength(basicLength, angle)
resultText = length.toFixed(2)
}
}
}
textObj.set({
text: String(resultText),
})
canvas.renderAll()
}
}
const handleSelectbox = (option, params) => {
const index = params.index
if (index === 1) slopeInput1 = option
if (index === 2) slopeInput2 = option
}
function calculateLength(originalLength, angle) {
const angleInRadians = angle * (Math.PI / 180) // 각도를 라디안으로 변환
const result = Math.sqrt(Math.pow(originalLength * Math.tan(angleInRadians), 2) + Math.pow(originalLength, 2))
return result
}
return (
<WithDraggable isShow={true} pos={pos}>
<div className={`modal-pop-wrap xm mount`}>
<div className="modal-head">
<h1 className="title">{getMessage('contextmenu.display.edit')} </h1>
<button className="modal-close" onClick={() => closePopup(id)}>
닫기
</button>
</div>
<div className="modal-body">
<div className="guide">{getMessage('modal.display.edit.info')}</div>
<div className="mb-box">
<div className="slope-wrap">
<div className="outline-form mb15">
<span className="mr10">{getMessage('modal.display.edit.before.length')}</span>
<div className="input-grid mr5">
<input type="text" className="input-origin block" value={basicLength} readOnly />
</div>
</div>
<div className="mb-box">
<div className="outline-form">
<span className="mr10">{getMessage('modal.display.edit.after.length')}</span>
<div className="input-grid mr5">
<input type="text" className="input-origin block" ref={changeSlopeRef} />
</div>
</div>
</div>
<div className="d-check-box pop">
<input type="checkbox" id="ch99" onChange={() => setSlopeAble(!slopeAble)} />
<label htmlFor="ch99">{getMessage('modal.display.edit.input.slope')}</label>
</div>
</div>
</div>
<div className="slope-wrap">
<div className="warning">{getMessage('modal.display.edit.input.slope')}</div>
<div className="display-change-wrap">
<div className="outline-form mb15">
<span className="mr10">{getMessage('slope')}</span>
<div className="grid-select mr10">
<QSelectBox title={''} options={SelectOption01} disabled={!slopeAble} params={{ index: 1 }} onChange={handleSelectbox} />
</div>
<span className="thin">{pitchText}</span>
</div>
<div className="outline-form">
<span className="mr10">{getMessage('slope')}</span>
<div className="grid-select mr10">
<QSelectBox title={''} options={SelectOption01} disabled={!slopeAble} params={{ index: 2 }} onChange={handleSelectbox} />
</div>
<span className="thin">{pitchText}</span>
</div>
</div>
<div className="warning">{getMessage('modal.display.edit.input.slope.info')}</div>
</div>
<div className="grid-btn-wrap">
<button className="btn-frame modal act" onClick={handleChangeLength}>
{getMessage('modal.common.save')}
</button>
</div>
</div>
</div>
</WithDraggable>
)
}