File size: 1,090 Bytes
8d88d9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { browser } from "$app/environment";
import { isDesktop } from "./isDesktop";

export async function share(url: string, title: string, appendLeafId: boolean = false) {
	if (!browser) return;

	// Retrieve the leafId from localStorage
	const leafId = localStorage.getItem("leafId");

	if (appendLeafId && leafId) {
		// Use URL and URLSearchParams to add the leafId parameter
		const shareUrl = new URL(url);
		shareUrl.searchParams.append("leafId", leafId);
		url = shareUrl.toString();
	}

	if (navigator.share && !isDesktop(window)) {
		navigator.share({ url, title });
	} else {
		// this is really ugly
		// but on chrome the clipboard write doesn't work if the window isn't focused
		// and after we use confirm() to ask the user if they want to share, the window is no longer focused
		// for a few ms until the confirm dialog closes. tried await tick(), tried window.focus(), didnt work
		// bug doesnt occur in firefox, if you can find a better fix for it please do
		await new Promise((resolve) => setTimeout(resolve, 250));
		await navigator.clipboard.writeText(url);
	}
}