Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Image download #1931

Merged
merged 33 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
615c1dc
chore: Add html-to-image
bprusinowski Dec 3, 2024
85e46a4
feat: Add screenshot-making logic
bprusinowski Dec 3, 2024
2ef7911
fix: Making screenshots of canvas elements
bprusinowski Dec 3, 2024
90c905f
feat: Allow the MenuActionItem to be disabled
bprusinowski Dec 3, 2024
ebdd3c6
feat: Disable certain elements from being screenshotted
bprusinowski Dec 3, 2024
53a9efa
feat: Add a way to screenshot charts
bprusinowski Dec 3, 2024
5864301
docs: Update CHANGELOG
bprusinowski Dec 3, 2024
dd3c3d0
feat: Disable interactive filters panel from screenshots
bprusinowski Dec 3, 2024
7668511
chore: Add meta-png
bprusinowski Dec 3, 2024
98b957c
feat: Add a way to attach file metadata to PNG screenshots
bprusinowski Dec 3, 2024
1723945
refactor: Extract
bprusinowski Dec 3, 2024
3506fac
fix: Enable screenshots in published mode
bprusinowski Dec 3, 2024
f484bf7
feat: Disable more interactive elements from screenshots
bprusinowski Dec 4, 2024
9810604
feat: Add better labels
bprusinowski Dec 4, 2024
1aacf85
feat: Improve the Accent sync command
bprusinowski Dec 4, 2024
246bf0c
feat: Remove SVG image export for now
bprusinowski Dec 4, 2024
59e2be3
feat: Make all texts dark-grey in PNGs
bprusinowski Dec 4, 2024
af03396
refactor: Pass createdWith to VisualizeLink
bprusinowski Dec 4, 2024
d6ad38a
feat: Add Created with Visualize link to screenshot footnotes
bprusinowski Dec 4, 2024
7571dc9
chore: Update react-table types
bprusinowski Dec 5, 2024
cd3eb7c
fix: Use React 18's createRoot to render additional screenshot elements
bprusinowski Dec 5, 2024
e65ec30
refactor: Move modifyNode closer to useScreenshot
bprusinowski Dec 5, 2024
d38201c
feat: Use better names for downloaded images
bprusinowski Dec 5, 2024
506de01
feat: Add file metadata to downloaded PNG images
bprusinowski Dec 5, 2024
75fd1fb
fix: Remove diacritics
bprusinowski Dec 5, 2024
b1cc7fc
feat: Improve publisher definition
bprusinowski Dec 5, 2024
d6e9c49
refactor: Extract TablePreviewWrapper
bprusinowski Dec 5, 2024
1a8aa2c
refactor: Make it possible to access original node when making screen…
bprusinowski Dec 5, 2024
0c4c21c
fix: Screenshotting of free canvas layout charts
bprusinowski Dec 5, 2024
ae9492d
feat: Disable map control elements
bprusinowski Dec 5, 2024
e9dba7b
feat: Remove attribution from map screenshots
bprusinowski Dec 5, 2024
d66cec2
fix: Make modifyNode async and await animation frame where needed
bprusinowski Dec 5, 2024
c8df4a7
fix: Typos
bprusinowski Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/charts/map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ export const MapComponent = () => {
initialViewState={defaultViewState}
mapLib={maplibregl}
mapStyle={mapStyle}
// Important so we can take a screenshot of the map
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this comment?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say yes, as preserveDrawingBuffer is false by default and can decrease the performance of the map, the comment should make sure that we don't remove it and unknowingly break screenshotting of the maps :)

preserveDrawingBuffer
style={{
position: "absolute",
left: 0,
Expand Down
18 changes: 18 additions & 0 deletions app/utils/use-screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { select } from "d3-selection";
import { toPng, toSvg } from "html-to-image";
import { useCallback, useState } from "react";

Expand Down Expand Up @@ -65,7 +66,24 @@ const makeScreenshot = async ({
const wrapperNode = document.createElement("div");
wrapperNode.style.width = `${node.offsetWidth}px`;
document.body.appendChild(wrapperNode);

const clonedNode = node.cloneNode(true) as HTMLElement;

const canvasNodes = select(node)
.selectAll<HTMLCanvasElement, unknown>("canvas")
.nodes();
const clonedCanvasNodes = select(clonedNode)
.selectAll<HTMLCanvasElement, unknown>("canvas")
.nodes();

for (const canvasNode of canvasNodes) {
const clonedCanvasNode = clonedCanvasNodes[canvasNodes.indexOf(canvasNode)];
// Cloning the canvas element does not copy the content, so we need to
// manually copy it.
const ctx = clonedCanvasNode.getContext("2d") as CanvasRenderingContext2D;
ctx.drawImage(canvasNode, 0, 0);
}

wrapperNode.appendChild(clonedNode);
modifyNode?.(clonedNode);

Expand Down