diff --git a/library/math/numbersLine/src/components/ShowElements.tsx b/library/math/numbersLine/src/components/ShowElements.tsx index 3e70c52..7308d84 100644 --- a/library/math/numbersLine/src/components/ShowElements.tsx +++ b/library/math/numbersLine/src/components/ShowElements.tsx @@ -13,7 +13,7 @@ import { unitAmount } from "@/type/ruler"; const ShowElements = () => { const { windowSize, rulerType, unit, setUnit, dragElements, setIdDraggElementClick } = useNumbersLineContext(); - const { rulerWidth, unitsAmount, rulerPosition } = useHelpers(); + const { rulerWidth, unitsAmount, calcRulerPosition } = useHelpers(); const { updateDragElements } = useDraggableElementAction(); const [windowResizing, setWindowResizing] = useState(false); @@ -26,9 +26,9 @@ const ShowElements = () => { } let translateY; if (element.yRatio < 0) { // Element is below the ruler - translateY = (-1 * element.yRatio * (windowSize.height - rulerPosition())) + rulerPosition(); + translateY = (-1 * element.yRatio * (windowSize.height - calcRulerPosition())) + calcRulerPosition(); } else { - translateY = element.yRatio * rulerPosition(); + translateY = element.yRatio * calcRulerPosition(); } const newTransform = `translate(${translateX.toFixed(2)}px, ${translateY.toFixed(2)}px)`; let documentElement = document.getElementById(`dragElement-${element.id}`); diff --git a/library/math/numbersLine/src/components/showElements/MoveableElement.tsx b/library/math/numbersLine/src/components/showElements/MoveableElement.tsx index a106d49..f91949e 100644 --- a/library/math/numbersLine/src/components/showElements/MoveableElement.tsx +++ b/library/math/numbersLine/src/components/showElements/MoveableElement.tsx @@ -19,14 +19,14 @@ interface IProps { const MoveableElement = ({ moveableRef, element, dragging, setDragging }: IProps) => { const { windowSize, rulerType, unit, leftPosition, idDraggElementClick, setIdDraggElementClick, color } = useNumbersLineContext(); const { deleteDragElement, duplicateDragJump, updateDragElements, updateDragElementsLayers } = useDraggableElementAction(); - const { calculatScreenWidth, rulerPaddingSides, jumpHeightWithoutBase, calcYElementPosition, xAxisFactor, widthRatio, heightRatio, rulerPosition, xRatio, yRatio } = useHelpers(); + const { calculatScreenWidth, rulerPaddingSides, jumpHeightWithoutBase, calcYElementPosition, xAxisFactor, widthRatio, heightRatio, calcRulerPosition, calcXRatio, calcYRatio } = useHelpers(); const [rightStartPosition, setRightStartPosition] = useState(0); const [boundScale, setBoundScale] = useState(0); const [changeDragState, setChangeDragState] = useState(false); - const [rulerPosition, setRulerPosition] = useState(rulerPosition()); + const [rulerPosition, setRulerPosition] = useState(calcRulerPosition()); useEffect(() => { - setRulerPosition(rulerPosition()); + setRulerPosition(calcRulerPosition()); }, [windowSize]); const ableProps = { @@ -65,8 +65,8 @@ const MoveableElement = ({ moveableRef, element, dragging, setDragging }: IProps const onDragEnd = (e: OnDragEnd | OnResizeEnd, resizeElement?: IElement) => { if (element.type == ActionTypes.text) { - const xRatio = xRatio(calcXTransform(e.target.style.transform)); - const yRatio = yRatio(calcYTransform(e.target.style.transform)); + const xRatio = calcXRatio(calcXTransform(e.target.style.transform)); + const yRatio = calcYRatio(calcYTransform(e.target.style.transform)); updateDragElements(element.id, { ...element, transform: e.target.style.transform, xRatio, yRatio }); setDragging!(false); setIdDraggElementClick(""); @@ -78,8 +78,8 @@ const MoveableElement = ({ moveableRef, element, dragging, setDragging }: IProps if (Math.abs(rulerPosition - elementPsition) < DistanceRulerForUpdatePositioning) updateXLocation(e); // Change the type of jump if its position has changed relative to the ruler. if (!element?.jump) { - const xRatio = xRatio(calcXTransform(e.target.style.transform)); - const yRatio = yRatio(calcYTransform(e.target.style.transform)); + const xRatio = calcXRatio(calcXTransform(e.target.style.transform)); + const yRatio = calcYRatio(calcYTransform(e.target.style.transform)); updateDragElements(element.id, { ...element, transform: e.target.style.transform, widthRatio: widthRatio(e.target.style.transform), heightRatio: heightRatio(e.target.style.transform), xRatio, yRatio }); return; } @@ -95,8 +95,8 @@ const MoveableElement = ({ moveableRef, element, dragging, setDragging }: IProps } e.target.style.transform = e.target.style.transform.replace(yTransform + "px)", newYPositionString); } - const xRatio = xRatio(calcXTransform(e.target.style.transform)); - const yRatio = yRatio(calcYTransform(e.target.style.transform)); + const xRatio = calcXRatio(calcXTransform(e.target.style.transform)); + const yRatio = calcYRatio(calcYTransform(e.target.style.transform)); updateDragElements(element.id, { ...element, transform: e.target.style.transform, @@ -173,7 +173,7 @@ const MoveableElement = ({ moveableRef, element, dragging, setDragging }: IProps newTransform = e.target.style.transform.replace("(" + xPosition, newXPosition); e.target.style.transform = newTransform; } - const xRatio = xRatio(calcXTransform(e.target.style.transform)); + const xRatio = calcXRatio(calcXTransform(e.target.style.transform)); updateDragElements(element.id, { ...element, transform: newTransform, widthRatio: widthRatio(e.target.style.transform), heightRatio: heightRatio(e.target.style.transform), jump: { ...element.jump, width: newWidth, value: newValue }, xRatio }); }; diff --git a/library/math/numbersLine/src/hooks/useDraggableElementAction.ts b/library/math/numbersLine/src/hooks/useDraggableElementAction.ts index 684f9fe..9793a92 100644 --- a/library/math/numbersLine/src/hooks/useDraggableElementAction.ts +++ b/library/math/numbersLine/src/hooks/useDraggableElementAction.ts @@ -34,14 +34,14 @@ export const useDraggableElementAction = () => { zIndexCounter, setZIndexCounter, } = useNumbersLineContext(); - const { rulerWidth, unitsAmount, rulerPaddingSides, xRatio, yRatio } = useHelpers(); + const { rulerWidth, unitsAmount, rulerPaddingSides, calcXRatio, calcYRatio } = useHelpers(); const addDraggableElement = (typeAction: ActionTypes, type?: NaviKeniIconsTypes) => { const elementWidth = typeAction == ActionTypes.jump ? rulerWidth() / unitsAmount(): typeAction == ActionTypes.naviAndKeni? rulerWidth() / unitAmount.ten: textBoxWidth; const xTranslate = (windowSize.width - elementWidth) / 2 + duplicateElementSpace; const yTranslate = windowSize.height / 4 + duplicateElementSpace; - const xRatio = xRatio(xTranslate) - const yRatio = yRatio(yTranslate) + const xRatio = calcXRatio(xTranslate) + const yRatio = calcYRatio(yTranslate) let newElement: IElement = { id: uuidv4(), type: typeAction, @@ -93,7 +93,7 @@ export const useDraggableElementAction = () => { setLeftPosition((prev) => Math.round((prev - outOfRange) / unit) * unit); } newTransform = element.transform.replace("(" + startPosition, "(" + newPosition); - const xRatio = xRatio(newPosition) + const xRatio = calcXRatio(newPosition) const newElement = { ...element, id, diff --git a/library/math/numbersLine/src/hooks/useHelpers.ts b/library/math/numbersLine/src/hooks/useHelpers.ts index b8792c5..c3cef33 100644 --- a/library/math/numbersLine/src/hooks/useHelpers.ts +++ b/library/math/numbersLine/src/hooks/useHelpers.ts @@ -61,20 +61,20 @@ export const useHelpers = () => { return calcYTransform(transform)/windowSize.height; }; - const rulerPosition = (): number => { + const calcRulerPosition = (): number => { return windowSize.height - (windowSize.height * rulerLocation) - ruleHeight; }; - const xRatio = (xPosition:number): number => { + const calcXRatio = (xPosition:number): number => { if(unitsAmount() == unitAmount.twenty) return (xPosition- 0.5*unit) / (windowSize.width - unit) return (xPosition- RulerPaddingSides) / (windowSize.width - 2 * RulerPaddingSides) }; - const yRatio = (yPosition: number): number => { - if(rulerPosition() < yPosition) - return (yPosition - rulerPosition()) / (windowSize.height - rulerPosition()) * -1 - return yPosition / rulerPosition() + const calcYRatio = (yPosition: number): number => { + if(calcRulerPosition() < yPosition) + return (yPosition - calcRulerPosition()) / (windowSize.height - calcRulerPosition()) * -1 + return yPosition / calcRulerPosition() }; const restart = () => { @@ -96,10 +96,10 @@ export const useHelpers = () => { widthRatio, jumpHeightWithoutBase, calcYElementPosition, - rulerPosition, + calcRulerPosition, xAxisFactor, - xRatio, - yRatio, + calcXRatio, + calcYRatio, restart, }; };