Skip to content

Commit

Permalink
feat: maps gl download (#432)
Browse files Browse the repository at this point in the history
* fix: map download

* Remove unused dependency
  • Loading branch information
turban committed Jan 30, 2020
1 parent c155f2a commit 57cb9fb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 889 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -70,6 +70,7 @@
"d3-selection": "^1.4.0",
"d3-time": "^1.0.11",
"d3-time-format": "^2.1.3",
"dom-to-image": "^2.6.0",
"file-saver": "^1.3.3",
"lodash": "^4.17.5",
"loglevel": "^1.6.1",
Expand Down
22 changes: 22 additions & 0 deletions src/components/download/DownloadDialog.js
Expand Up @@ -139,14 +139,36 @@ export class DownloadDialog extends Component {
.toString(36)
.substring(7)}.png`;

// Skip map controls in download except attribution and scale
// Mapbox map controls contains inline SVG for CSS background-image, which
// is not accepted by dom-to-image
const skipControls = el =>
!el.classList ||
el.classList.contains('mapboxgl-ctrl-scale') ||
el.classList.contains('mapboxgl-ctrl-attrib') ||
!el.classList.contains('mapboxgl-ctrl');

const options = {
width: mapEl.offsetWidth,
height: mapEl.offsetHeight,
filter: skipControls,
};

// Adding 1 to the computed width of the title element avoids text
// wrapping outside the box in the generated image
const titleEl = mapEl.getElementsByClassName('dhis2-maps-title')[0];
const width = window
.getComputedStyle(titleEl, null)
.getPropertyValue('width');

titleEl.style.width = parseInt(width, 10) + 1 + 'px';

convertToPng(mapEl, options)
.then(dataUri => {
downloadFile(dataURItoBlob(dataUri), filename);

titleEl.style.width = 'auto';

this.onClose();
})
.catch(this.onError);
Expand Down
4 changes: 2 additions & 2 deletions src/components/download/DownloadLegend.js
Expand Up @@ -42,11 +42,11 @@ const styles = theme => ({
right: theme.spacing.unit,
},
bottomleft: {
bottom: 30,
bottom: 40,
left: theme.spacing.unit,
},
bottomright: {
bottom: 20,
bottom: 30,
right: theme.spacing.unit,
},
name: {
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/MapContainer.js
Expand Up @@ -77,7 +77,7 @@ const MapContainer = ({
coordinatePopup={coordinatePopup}
closeCoordinatePopup={closeCoordinatePopup}
/>
{isDownload && legendPosition && (
{isDownload && legendPosition && layers.length && (
<DownloadLegend
position={legendPosition}
layers={layers}
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/MapName.js
Expand Up @@ -46,7 +46,7 @@ const styles = theme => ({
const MapName = ({ showName, name, interpretationDate, uiLocale, classes }) =>
showName && name ? (
<div className={classes.root}>
<div className={classes.name}>{name}</div>
<div className={`${classes.name} dhis2-maps-title`}>{name}</div>
{interpretationDate && (
<div className={classes.interpretation}>
<div className={classes.interpretationIcon}>
Expand Down

0 comments on commit 57cb9fb

Please sign in to comment.