From 8ea2397f285aa40cf169c818dc396090cec2c9d3 Mon Sep 17 00:00:00 2001 From: Rajveer Singh Anand Date: Wed, 6 Nov 2024 03:36:08 -0500 Subject: [PATCH] Compromise solution (1) instead of draggable resizable node + selected edge button minor fix included (#113) * 1) Node width expands for entire text visibility. 2) Made selected edge button responsive and to design. 3) Minor node look changes * max node width decreased --- client/src/components/UMLClassNode.tsx | 123 +++++++++++++++++---- client/src/components/UMLInterfaceNode.tsx | 105 ++++++++++++++---- client/src/pages/Editor.tsx | 40 ++++++- 3 files changed, 224 insertions(+), 44 deletions(-) diff --git a/client/src/components/UMLClassNode.tsx b/client/src/components/UMLClassNode.tsx index 58ebb30..44315a6 100644 --- a/client/src/components/UMLClassNode.tsx +++ b/client/src/components/UMLClassNode.tsx @@ -22,8 +22,22 @@ const UMLClassNode: React.FC = ({ data, id }) => { const attributesRef = useRef(null); const methodsRef = useRef(null); + const labelRef = useRef(null); + + const [nodeWidth, setNodeWidth] = useState(200); // Initial minimum width + + // Function to measure the width of text using canvas + const measureTextWidth = (text: string, font: string): number => { + const canvas = document.createElement('canvas'); + const context = canvas.getContext('2d'); + if (!context) return 0; + context.font = font; + const metrics = context.measureText(text); + return metrics.width; + }; useEffect(() => { + // Adjust the height of text areas if (attributesRef.current) { attributesRef.current.style.height = 'auto'; attributesRef.current.style.height = `${attributesRef.current.scrollHeight}px`; @@ -32,8 +46,47 @@ const UMLClassNode: React.FC = ({ data, id }) => { methodsRef.current.style.height = 'auto'; methodsRef.current.style.height = `${methodsRef.current.scrollHeight}px`; } - }, [attributes, methods]); + // Initialize maxWidth with the minimum width + let maxWidth = 150; + + // Define font settings + const fontSize = 14; // Adjust font size if needed + const fontFamily = 'Arial, sans-serif'; + const font = `${fontSize}px ${fontFamily}`; + + // Measure width of the label + let labelWidth = 0; + if (labelRef.current) { + const labelText = labelRef.current.value; + labelWidth = measureTextWidth(labelText, font); + } + + // Measure the maximum width required by attributes + let attributesMaxWidth = 0; + const attributesLines = attributes.split('\n'); + attributesLines.forEach(line => { + const lineWidth = measureTextWidth(line, font); + attributesMaxWidth = Math.max(attributesMaxWidth, lineWidth); + }); + + // Measure the maximum width required by methods + let methodsMaxWidth = 0; + const methodsLines = methods.split('\n'); + methodsLines.forEach(line => { + const lineWidth = measureTextWidth(line, font); + methodsMaxWidth = Math.max(methodsMaxWidth, lineWidth); + }); + + // Determine the maximum width required + maxWidth = Math.max(maxWidth, labelWidth, attributesMaxWidth, methodsMaxWidth); + + // Add padding to the calculated width + const padding = 30; // Adjust padding as needed + setNodeWidth(maxWidth + padding); + }, [label, attributes, methods]); + + // Handlers for input changes const handleLabelChange = (e) => { const newLabel = e.target.value; setLabel(newLabel); @@ -53,31 +106,41 @@ const UMLClassNode: React.FC = ({ data, id }) => { }; return ( -
-
+
+
-
+