feat: improve share buttons functionality with hybrid approach

This commit is contained in:
2026-02-10 23:59:32 +01:00
parent 1eceec1595
commit bec5ef33dd

View File

@@ -212,7 +212,7 @@ const buildShareUrl = (target, text, url) => {
const encodedText = encodeURIComponent(text); const encodedText = encodeURIComponent(text);
const encodedUrl = encodeURIComponent(url); const encodedUrl = encodeURIComponent(url);
if (target === 'x') { if (target === 'x') {
return `https://twitter.com/intent/tweet?text=${encodedText}&url=${encodedUrl}`; return `https://x.com/intent/tweet?text=${encodedText}&url=${encodedUrl}`;
} }
if (target === 'facebook') { if (target === 'facebook') {
return `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}&quote=${encodedText}`; return `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}&quote=${encodedText}`;
@@ -226,13 +226,18 @@ const buildShareUrl = (target, text, url) => {
const shareTo = async (target) => { const shareTo = async (target) => {
if (shareInProgress.value) return; if (shareInProgress.value) return;
shareInProgress.value = true; shareInProgress.value = true;
try {
const blob = await createShareBlob();
if (!blob) return;
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
const text = shareText.value; const text = shareText.value;
const url = window.location.href; const url = window.location.href;
if (navigator.share && navigator.canShare && navigator.canShare({ files: [file] })) { const shareUrl = buildShareUrl(target, text, url);
try {
// Try native share first if available (supports images)
if (navigator.share && navigator.canShare) {
const blob = await createShareBlob();
if (blob) {
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
if (navigator.canShare({ files: [file] })) {
await navigator.share({ await navigator.share({
files: [file], files: [file],
text, text,
@@ -241,14 +246,28 @@ const shareTo = async (target) => {
}); });
return; return;
} }
await downloadShareImage();
const shareUrl = buildShareUrl(target, text, url);
if (shareUrl) {
window.open(shareUrl, '_blank', 'noopener');
} }
}
} catch (error) {
if (error.name === 'AbortError') {
return; // User cancelled native share, do nothing
}
// Other errors -> fall through to fallback
} finally { } finally {
shareInProgress.value = false; shareInProgress.value = false;
} }
// Fallback: Direct Link + Download
// Open window immediately if possible (though we awaited above, so it might be blocked,
// but we can't do much about it if we want to try native share first).
// Ideally, for Desktop, navigator.share is undefined so we skip the await above.
if (shareUrl) {
window.open(shareUrl, '_blank', 'noopener');
}
// Trigger download as "screenshot support"
downloadShareImage();
}; };
onMounted(() => { onMounted(() => {