Skip to content

Commit 6e72275

Browse files
authored
feat: Try to use Share API (#53)
2 parents 4b0a1b2 + 11de221 commit 6e72275

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

index.html

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</aside>
2424
<aside class="download download--hidden">
2525
<a class="download__input download__input--action" download href="#" id="js-download">Download image</a>
26+
<button class="download__input download__input--share" id="js-share">Share image</a>
2627
<button class="download__input js-reset">Try another image</button>
2728
</aside>
2829
<aside class="map map--hidden map--center">

src/scripts/DownloadManager.ts

+21
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import { warn } from "./console.ts";
55

66
export default class DownloadManager extends ObserverPublisher(Publisher) {
77
private readonly downloadSelector: HTMLLinkElement;
8+
private readonly shareSelector: HTMLLinkElement;
89
private readonly resetSelector: NodeListOf<HTMLElement>;
910
private readonly container: HTMLElement | null;
1011

1112
constructor(
1213
$downloadElement: HTMLLinkElement,
14+
$shareElement: HTMLLinkElement,
1315
$resetElement: NodeListOf<HTMLElement>,
1416
) {
1517
super();
1618
this.downloadSelector = $downloadElement;
19+
this.shareSelector = $shareElement;
1720
this.resetSelector = $resetElement;
1821
this.container = $downloadElement.parentElement;
1922

@@ -24,6 +27,10 @@ export default class DownloadManager extends ObserverPublisher(Publisher) {
2427
});
2528
return element;
2629
});
30+
31+
if (!navigator.canShare()) {
32+
this.shareSelector.classList.add("download--hidden");
33+
}
2734
}
2835

2936
update(publication: Message) {
@@ -57,6 +64,20 @@ export default class DownloadManager extends ObserverPublisher(Publisher) {
5764
);
5865
this.downloadSelector.setAttribute("download", this.generateFilename());
5966
this.downloadSelector.setAttribute("href", dataURL);
67+
68+
if (navigator.canShare()) {
69+
this.shareSelector.addEventListener("click", (event) => {
70+
event.preventDefault();
71+
navigator.share({
72+
title: "See my photo with the embedded map",
73+
files: [
74+
new File([dataURL], this.generateFilename(), {
75+
type: "image/jpeg",
76+
}),
77+
],
78+
});
79+
});
80+
}
6081
}
6182

6283
hide() {

src/scripts/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ window.addEventListener("load", () => {
2525
["photo", $("#js-map")],
2626
["canvas", $("#js-main-canvas")],
2727
["download", $("#js-download")],
28+
["share", $("#js-share")],
2829
["reset", $$(".js-reset")], // At least 2 elements have this class
2930
["mapOptions", $("#js-map-options")],
3031
]);
@@ -39,6 +40,7 @@ window.addEventListener("load", () => {
3940
browser = new PhotoBrowser($elements.get("browser") as HTMLInputElement);
4041
downloadManager = new DownloadManager(
4142
$elements.get("download") as HTMLLinkElement,
43+
$elements.get("share") as HTMLLinkElement,
4244
$elements.get("reset") as NodeListOf<HTMLElement>,
4345
);
4446

0 commit comments

Comments
 (0)