Skip to content

Commit 64db6a3

Browse files
committed
use html2canvas for maps and usingHtml2Image for other images
1 parent 688f5f9 commit 64db6a3

File tree

6 files changed

+4103
-10697
lines changed

6 files changed

+4103
-10697
lines changed

package-lock.json

Lines changed: 4053 additions & 10686 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
"react-share": "^4.4.0",
3838
"recharts": "^2.12.6",
3939
"tinycolor2": "^1.6.0",
40-
"typescript": "^4.3.5"
40+
"typescript": "^4.3.5",
41+
"html-to-image": "v1.11.11"
4142
},
4243
"scripts": {
4344
"start": "react-scripts start",

src/components/molecules/card/AnyWayCard.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { FC, useState } from 'react';
22
import {ColorScheme} from 'style'
33
import { Card, CardContent, Box } from '@material-ui/core';
44
import { makeStyles } from '@material-ui/core/styles';
5-
import widgetToImage from 'services/to-image.service';
5+
import {widgetToImageH2I, widgetToImageH2C} from 'services/to-image.service';
66

77
// TEXT BOX COMPONENT ADD FEATURE
88
import TextBox from 'components/organisms/TextBox'
@@ -15,7 +15,7 @@ import TitleIcon from '@material-ui/icons/Title';
1515
import { fontFamilyString } from 'style';
1616
import CardHeader from './CardHeader';
1717
import SocialShare from 'components/atoms/SocialShare';
18-
import { FooterVariant, getWidgetVariant, HeaderVariant } from 'services/widgets.style.service';
18+
import { FooterVariant, getWidgetVariant, HeaderVariant, getWidgetType, CardType } from 'services/widgets.style.service';
1919
import { getSizes } from './card.util';
2020
import CardFooter from './CardFooter';
2121
import CardEditor from 'components/organisms/CardEditorDialog';
@@ -103,15 +103,19 @@ const AnyWayCard: FC<IProps> = ({
103103
const variant = getWidgetVariant(widgetName);
104104
const factor = getSizeFactor(sizeOptions);
105105
const sizes = getSizes(variant, factor);
106+
const widgetType = getWidgetType(widgetName);
106107

107108
const classes = useStyles();
108109

109110
const imgDownloadHandler = () => {
110111
if (element && element instanceof HTMLElement) {
111-
widgetToImage(widgetName, element);
112+
if (widgetType === CardType.Map) {
113+
widgetToImageH2C(widgetName, element);
114+
} else {
115+
widgetToImageH2I(widgetName, element);
116+
}
112117
}
113118
};
114-
115119

116120
let Widget;
117121

src/components/organisms/CardEditorDialog.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import WidgetWrapper from 'components/molecules/widgets/WidgetWrapper';
55
import { Box, Checkbox, FormControlLabel, FormGroup } from '@material-ui/core';
66
import AnyWayCard, { CardSizeOptions } from 'components/molecules/card/AnyWayCard';
77
import { MetaTag, ErrorBoundary, Typography, Button, Slider } from 'components/atoms';
8-
import widgetToImage from 'services/to-image.service';
8+
import { getWidgetType, CardType } from 'services/widgets.style.service';
9+
import {widgetToImageH2I, widgetToImageH2C} from 'services/to-image.service';
910
import { useTranslation } from 'react-i18next';
1011
import { blueVioletColor } from 'style';
1112
import { logosSourceMap, OrgLogoData } from 'const/cards.const';
@@ -32,9 +33,14 @@ const CardEditor: FC<IProps> = ({ isOpen, onClose, widgetName, text }) => {
3233
const getCardRef = (element: HTMLElement) => setCardElement(element);
3334
const organizationName = userStore.userOrganizations ? userStore.userOrganizations[0] : '';
3435
const organizationData: OrgLogoData | undefined = logosSourceMap.find((p) => p.key === organizationName);
36+
const widgetType = getWidgetType(widgetName);
3537
const imgDownloadHandler = () => {
3638
if (cardElement && cardElement instanceof HTMLElement) {
37-
widgetToImage(widgetName, cardElement);
39+
if (widgetType === CardType.Map) {
40+
widgetToImageH2C(widgetName, cardElement);
41+
} else {
42+
widgetToImageH2I(widgetName, cardElement);
43+
}
3844
}
3945
};
4046

src/services/to-image.service.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { saveAs } from 'file-saver';
2+
import * as htmlToImage from 'html-to-image';
23
import html2canvas from 'html2canvas';
34

4-
const DEFAULT_SCALE = 1;
5+
const DEFAULT_SCALE = 3;
56

67
const removeMapControllers = (el: Document) => {
78
const elementList = el.querySelectorAll(
@@ -10,7 +11,20 @@ const removeMapControllers = (el: Document) => {
1011
Array.from(elementList).forEach((el) => el.remove());
1112
};
1213

13-
const widgetToImage = (fileName: string, widgetElement: HTMLElement, scale = DEFAULT_SCALE) => {
14+
export const widgetToImageH2I = (fileName: string, widgetElement: HTMLElement) => {
15+
usingHtml2Image(fileName, widgetElement);
16+
};
17+
18+
// https://github.com/bubkoo/html-to-image
19+
const usingHtml2Image = (fileName: string, widgetElement: HTMLElement) => {
20+
21+
htmlToImage.toPng(widgetElement)
22+
.then(function (blob: any) {
23+
saveAs(blob, `${fileName}.png`);
24+
});
25+
};
26+
27+
export const widgetToImageH2C = (fileName: string, widgetElement: HTMLElement, scale = DEFAULT_SCALE) => {
1428
usingHtml2Canvas(fileName, widgetElement, scale);
1529
};
1630

@@ -31,5 +45,3 @@ const usingHtml2Canvas = (fileName: string, widgetElement: HTMLElement, scale: n
3145
saveAs(blob, `${fileName}.png`);
3246
});
3347
};
34-
35-
export default widgetToImage;

src/services/widgets.style.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ export enum FooterVariant {
1414
LogoWithRange,
1515
}
1616

17+
export enum CardType {
18+
None,
19+
Map,
20+
}
1721
export interface CardVariant {
1822
header: HeaderVariant;
1923
footer: FooterVariant;
2024
}
2125

26+
2227
// === widgets variants === //
2328
// determine the footer and header style for each card
2429
// See `HeaderVariant` and `FooterVariant` enums
@@ -66,3 +71,14 @@ export function getWidgetVariant(widgetName: string) {
6671
const variant = widgetVariants[widgetName];
6772
return variant || widgetVariants.defaultVariant;
6873
}
74+
75+
const widgetTypes: { [index: string]: CardType } = {
76+
defaultType: CardType.None,
77+
[WidgetName.most_severe_accidents]: CardType.Map ,
78+
[WidgetName.accidents_heat_map]:CardType.Map,
79+
};
80+
81+
export function getWidgetType(widgetName: string) {
82+
const widgetType = widgetTypes[widgetName];
83+
return widgetType || widgetTypes.defaultType;
84+
}

0 commit comments

Comments
 (0)