mirror of
https://github.com/PeWu/topola-viewer.git
synced 2026-03-17 13:03:46 +00:00
Added "Download PNG" option
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"main": "src/index.tsx",
|
"main": "src/index.tsx",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"canvg": "^1.5.3",
|
||||||
"d3": "^5.7.0",
|
"d3": "^5.7.0",
|
||||||
"detect-browser": "^4.1.0",
|
"detect-browser": "^4.1.0",
|
||||||
"history": "^4.7.2",
|
"history": "^4.7.2",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import * as d3 from 'd3';
|
import * as d3 from 'd3';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
|
import canvg from 'canvg';
|
||||||
import {intlShape} from 'react-intl';
|
import {intlShape} from 'react-intl';
|
||||||
import {
|
import {
|
||||||
JsonGedcomData,
|
JsonGedcomData,
|
||||||
@@ -169,4 +170,35 @@ export class Chart extends React.PureComponent<ChartProps, {}> {
|
|||||||
hiddenElement.download = 'topola.svg';
|
hiddenElement.download = 'topola.svg';
|
||||||
hiddenElement.click();
|
hiddenElement.click();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloadPng() {
|
||||||
|
const canvas = document.createElement('canvas');
|
||||||
|
|
||||||
|
// Scale image for better quality.
|
||||||
|
const svg = (document.getElementById('chart') as unknown) as SVGSVGElement;
|
||||||
|
canvas.width = svg.getBBox().width * 2;
|
||||||
|
canvas.height = svg.getBBox().height * 2;
|
||||||
|
|
||||||
|
// Fill canvas with white background.
|
||||||
|
const ctx = canvas.getContext('2d')!;
|
||||||
|
const oldFill = ctx.fillStyle;
|
||||||
|
ctx.fillStyle = 'white';
|
||||||
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
ctx.fillStyle = oldFill;
|
||||||
|
|
||||||
|
canvg(canvas, this.getSvgContents(), {
|
||||||
|
ignoreDimensions: true,
|
||||||
|
ignoreClear: true,
|
||||||
|
scaleWidth: canvas.width,
|
||||||
|
scaleHeight: canvas.height,
|
||||||
|
});
|
||||||
|
const onBlob = (blob: Blob | null) => {
|
||||||
|
const hiddenElement = document.createElement('a');
|
||||||
|
hiddenElement.href = URL.createObjectURL(blob);
|
||||||
|
hiddenElement.target = '_blank';
|
||||||
|
hiddenElement.download = 'topola.png';
|
||||||
|
hiddenElement.click();
|
||||||
|
};
|
||||||
|
canvas.toBlob(onBlob, 'image/png');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -261,4 +261,10 @@ export class ChartView extends React.Component<RouteComponentProps, State> {
|
|||||||
this.chartRef.downloadSvg();
|
this.chartRef.downloadSvg();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
downloadPng() {
|
||||||
|
if (this.chartRef) {
|
||||||
|
this.chartRef.downloadPng();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ if (browser && browser.name === 'ie') {
|
|||||||
{...props}
|
{...props}
|
||||||
onPrint={() => chartViewRef && chartViewRef.print()}
|
onPrint={() => chartViewRef && chartViewRef.print()}
|
||||||
onDownloadSvg={() => chartViewRef && chartViewRef.downloadSvg()}
|
onDownloadSvg={() => chartViewRef && chartViewRef.downloadSvg()}
|
||||||
|
onDownloadPng={() => chartViewRef && chartViewRef.downloadPng()}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ interface State {
|
|||||||
interface Props {
|
interface Props {
|
||||||
onPrint: () => void;
|
onPrint: () => void;
|
||||||
onDownloadSvg: () => void;
|
onDownloadSvg: () => void;
|
||||||
|
onDownloadPng: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TopBar extends React.Component<
|
export class TopBar extends React.Component<
|
||||||
@@ -187,6 +188,9 @@ export class TopBar extends React.Component<
|
|||||||
className="item"
|
className="item"
|
||||||
>
|
>
|
||||||
<Dropdown.Menu>
|
<Dropdown.Menu>
|
||||||
|
<Dropdown.Item onClick={() => this.props.onDownloadPng()}>
|
||||||
|
<FormattedMessage id="menu.png_file" defaultMessage="PNG file" />
|
||||||
|
</Dropdown.Item>
|
||||||
<Dropdown.Item onClick={() => this.props.onDownloadSvg()}>
|
<Dropdown.Item onClick={() => this.props.onDownloadSvg()}>
|
||||||
<FormattedMessage id="menu.svg_file" defaultMessage="SVG file" />
|
<FormattedMessage id="menu.svg_file" defaultMessage="SVG file" />
|
||||||
</Dropdown.Item>
|
</Dropdown.Item>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
"menu.load_from_file": "Otwórz plik",
|
"menu.load_from_file": "Otwórz plik",
|
||||||
"menu.print": "Drukuj",
|
"menu.print": "Drukuj",
|
||||||
"menu.download": "Pobierz",
|
"menu.download": "Pobierz",
|
||||||
|
"menu.png_file": "Plik PNG",
|
||||||
"menu.svg_file": "Plik SVG",
|
"menu.svg_file": "Plik SVG",
|
||||||
"menu.github": "Źródła na GitHub",
|
"menu.github": "Źródła na GitHub",
|
||||||
"intro.title": "Topola Genealogy",
|
"intro.title": "Topola Genealogy",
|
||||||
|
|||||||
Reference in New Issue
Block a user