feat: improve share buttons functionality with hybrid approach
This commit is contained in:
@@ -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}"e=${encodedText}`;
|
return `https://www.facebook.com/sharer/sharer.php?u=${encodedUrl}"e=${encodedText}`;
|
||||||
@@ -226,29 +226,48 @@ 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;
|
||||||
|
|
||||||
|
const text = shareText.value;
|
||||||
|
const url = window.location.href;
|
||||||
|
const shareUrl = buildShareUrl(target, text, url);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const blob = await createShareBlob();
|
// Try native share first if available (supports images)
|
||||||
if (!blob) return;
|
if (navigator.share && navigator.canShare) {
|
||||||
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
|
const blob = await createShareBlob();
|
||||||
const text = shareText.value;
|
if (blob) {
|
||||||
const url = window.location.href;
|
const file = new File([blob], `nonogram-${store.size}x${store.size}.png`, { type: 'image/png' });
|
||||||
if (navigator.share && navigator.canShare && navigator.canShare({ files: [file] })) {
|
if (navigator.canShare({ files: [file] })) {
|
||||||
await navigator.share({
|
await navigator.share({
|
||||||
files: [file],
|
files: [file],
|
||||||
text,
|
text,
|
||||||
title: t('app.title'),
|
title: t('app.title'),
|
||||||
url
|
url
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
await downloadShareImage();
|
} catch (error) {
|
||||||
const shareUrl = buildShareUrl(target, text, url);
|
if (error.name === 'AbortError') {
|
||||||
if (shareUrl) {
|
return; // User cancelled native share, do nothing
|
||||||
window.open(shareUrl, '_blank', 'noopener');
|
|
||||||
}
|
}
|
||||||
|
// 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(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user