Skip to content

Commit

Permalink
Adding in pan/scale and custom sized maps. Progresses #178
Browse files Browse the repository at this point in the history
  • Loading branch information
damonsk committed May 31, 2024
1 parent e68c29e commit e1c4e9b
Show file tree
Hide file tree
Showing 23 changed files with 602 additions and 404 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"react": "18.2.0",
"react-ace": "^10.1.0",
"react-dom": "18.2.0",
"react-svg-pan-zoom": "^3.12.1",
"sass": "^1.69.5",
"styled-components": "6.1.5"
},
Expand Down
77 changes: 56 additions & 21 deletions frontend/src/components/MapEnvironment.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ function convertToImage(base64Data, contentType) {
}

const getHeight = () => {
var winHeight = window.innerHeight;
var topNavHeight = document.getElementById('top-nav-wrapper').clientHeight;
var titleHeight = document.getElementById('title').clientHeight;
return winHeight - topNavHeight - titleHeight - 95;
const winHeight = window.innerHeight;
const topNavHeight = document.getElementById('top-nav-wrapper').clientHeight;
return winHeight - topNavHeight - 95;
};
const getWidth = () => {
return document.getElementById('map').clientWidth - 50;
Expand Down Expand Up @@ -116,6 +115,9 @@ function Environment(props) {
);
const [mapIterations, setMapIterations] = useState([]);
const [mapDimensions, setMapDimensions] = useState(Defaults.MapDimensions);
const [mapCanvasDimensions, setMapCanvasDimensions] = useState(
Defaults.MapDimensions
);
const [mapEvolutionStates, setMapEvolutionStates] = useState(
Defaults.EvolutionStages
);
Expand All @@ -137,7 +139,6 @@ function Environment(props) {
const [hideNav, setHideNav] = useState(false);

const mutateMapText = (newText) => {
console.log('mutate');
setMapText(newText);
setSaveOutstanding(true);
if (currentIteration !== null && currentIteration > -1) {
Expand Down Expand Up @@ -291,13 +292,25 @@ function Environment(props) {
}

function downloadMap() {
html2canvas(mapRef.current).then((canvas) => {
const base64image = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = mapTitle;
link.href = base64image;
link.click();
});
const svgMapText = mapRef.current.getElementsByTagName('svg')[0].outerHTML;
const tempElement = document.createElement('div');
tempElement.innerHTML = svgMapText;
tempElement.style.position = 'absolute';
tempElement.style.left = '-9999px';
document.body.appendChild(tempElement);
html2canvas(tempElement, { useCORS: true, allowTaint: true })
.then((canvas) => {
const base64image = canvas.toDataURL('image/png');
const link = document.createElement('a');
link.download = mapTitle;
link.href = base64image;
link.click();
tempElement.remove();
})
.catch((x) => {
console.log(x);
tempElement.remove();
});
}

function downloadMapAsSVG() {
Expand Down Expand Up @@ -389,7 +402,6 @@ function Environment(props) {
useEffect(() => {
document.title = mapTitle + ' - ' + Defaults.PageTitle;
}, [mapTitle]);

useEffect(() => {
setMapDimensions({
width: mapSize.width > 0 ? mapSize.width : getWidth(),
Expand Down Expand Up @@ -467,31 +479,53 @@ function Environment(props) {

useEffect(() => {
const debouncedHandleResize = debounce(() => {
setMapDimensions({
const dimensions = {
width: mapSize.width > 0 ? mapSize.width : getWidth(),
height: mapSize.height > 0 ? mapSize.height : getHeight(),
});
}, 0);
};
setMapDimensions(dimensions);
}, 1);

if (mapSize.width === 0 || mapSize.height === 0) {
window.addEventListener('resize', debouncedHandleResize);
debouncedHandleResize();
}
window.addEventListener('resize', debouncedHandleResize);
debouncedHandleResize();

return function cleanup() {
window.removeEventListener('resize', debouncedHandleResize);
};
}, [mapSize]);

useEffect(() => {
const newDimensions = {
width: mapSize.width > 0 ? mapSize.width : getWidth(),
height: mapSize.height > 0 ? mapSize.height : getHeight(),
};
setMapDimensions(newDimensions);
setMapCanvasDimensions({
width: getWidth(),
height: getHeight(),
});
}, [mapOnlyView, hideNav]);

useEffect(() => {
const initialLoad = () => {
setMapDimensions({
setMapCanvasDimensions({
width: getWidth(),
height: getHeight(),
});
};

const debouncedHandleCanvasResize = debounce(() => {
setMapCanvasDimensions({
width: getWidth(),
height: getHeight(),
});
}, 500);

window.addEventListener('load', initialLoad);
window.addEventListener('resize', debouncedHandleCanvasResize);

return function cleanup() {
window.removeEventListener('resize', debouncedHandleCanvasResize);
window.removeEventListener('load', initialLoad);
};
}, []);
Expand Down Expand Up @@ -616,6 +650,7 @@ function Environment(props) {
mapMethods={mapMethods}
mapStyleDefs={mapStyleDefs}
mapDimensions={mapDimensions}
mapCanvasDimensions={mapCanvasDimensions}
mapEvolutionStates={mapEvolutionStates}
mapRef={mapRef}
mapText={mapText}
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ function Editor(props) {
};

const getHeight = () => {
var winHeight = window.innerHeight;
var topNavHeight = document.getElementById('top-nav-wrapper').clientHeight;
var titleHeight = document.getElementById('title').clientHeight;
return winHeight - topNavHeight - titleHeight + 35;
const winHeight = window.innerHeight;
const topNavHeight =
document.getElementById('top-nav-wrapper').clientHeight;
return winHeight - topNavHeight + 35;
};

useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/Anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const Anchor = (props) => {
fixedY={false}
fixedX={false}
isModKeyPressed={isModKeyPressed}
scaleFactor={props.scaleFactor}
>
<ComponentTextSymbol
id={elementKey('text')}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/AnnotationBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function AnnotationElement(props) {
fixedX={false}
x={x()}
y={y()}
scaleFactor={props.scaleFactor}
>
<AnnotationBoxSymbol
id={'annotationsBoxTextContainer'}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/AnnotationElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function AnnotationElement(props) {
y={y()}
fixedY={false}
fixedX={false}
scaleFactor={props.scaleFactor}
>
<AnnotationElementSymbol
annotation={props.annotation}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/Attitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const Attitude = (props) => {
y={y}
fixedY={false}
fixedX={false}
scaleFactor={props.scaleFactor}
>
<AttitudeSymbol
id={`attitude_${type}`}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/ComponentLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function ComponentLink(props) {
setMetaText={props.setMetaText}
x={x2}
y={y2}
scaleFactor={props.scaleFactor}
/>
)}
{featureSwitches.enableLinkContext && link.context && (
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/ComponentText.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function ComponentText(props) {
onMove={overrideDrag ? overrideDrag : endDrag}
x={element.label.x}
y={element.label.y}
scaleFactor={props.scaleFactor}
>
{showTextField === false && (
<ComponentTextSymbol
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/FlowText.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const FlowText = (props) => {
onMove={flowLabelEndDrag}
y={flowLabelPosition.y}
x={flowLabelPosition.x}
scaleFactor={props.scaleFactor}
>
<ComponentTextSymbol
className="draggable label"
Expand Down
34 changes: 19 additions & 15 deletions frontend/src/components/map/FluidLink.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useCallback, useEffect } from 'react';
import PositionCalculator from './PositionCalculator';
import LinkSymbol from '../symbols/LinkSymbol';

Expand All @@ -17,20 +17,24 @@ function FluidLink(props) {
coords: {},
});

const handleMouseMove = (e) => {
setPosition((position) => {
const xDiff = position.coords.x - e.pageX;
const yDiff = position.coords.y - e.pageY;
return {
x: position.x - xDiff,
y: position.y - yDiff,
coords: {
x: e.pageX,
y: e.pageY,
},
};
});
};
const handleMouseMove = useCallback(
(e) => {
setPosition((position) => {
const scaleFactor = props.scaleFactor || 1; // Use scaleFactor from props, default to 1 if not provided
const xDiff = (position.coords.x - e.pageX) / scaleFactor;
const yDiff = (position.coords.y - e.pageY) / scaleFactor;
return {
x: position.x - xDiff,
y: position.y - yDiff,
coords: {
x: e.pageX,
y: e.pageY,
},
};
});
},
[props.scaleFactor]
);

useEffect(() => {
const pageX = origClick.pageX;
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/map/MapAccelerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function MapAccelerator(props) {
fixedX={false}
shouldShowMoving={false}
isModKeyPressed={false}
scaleFactor={props.scaleFactor}
>
<>{children}</>
</Movable>
Expand Down
Loading

0 comments on commit e1c4e9b

Please sign in to comment.