FIX: more reliable native sharing (#7039)

This commit is contained in:
Joffrey JAFFEUX 2019-02-20 20:43:48 +01:00 committed by GitHub
parent 9618300d69
commit 18296b4211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 44 deletions

View File

@ -6,34 +6,6 @@ export default {
name: "topic-footer-buttons", name: "topic-footer-buttons",
initialize() { initialize() {
registerTopicFooterButton({
id: "native-share",
icon: "link",
priority: 999,
label: "topic.share.title",
title: "topic.share.help",
action() {
nativeShare({ url: this.get("topic.shareUrl") }).catch(() =>
showModal("share-and-invite", {
modalClass: "share-and-invite",
panels: [
{
id: "share",
title: "topic.share.title",
model: { topic: this.get("topic") }
}
]
})
);
},
dropdown: true,
classNames: ["native-share"],
dependentKeys: ["topic.shareUrl", "topic.isPrivateMessage"],
displayed() {
return window.navigator.share;
}
});
registerTopicFooterButton({ registerTopicFooterButton({
id: "share-and-invite", id: "share-and-invite",
icon: "link", icon: "link",
@ -79,13 +51,7 @@ export default {
}); });
}; };
if (window.navigator.share) { nativeShare({ url: this.get("topic.shareUrl") }).then(null, modal);
window.navigator
.share({ url: this.get("topic.shareUrl") })
.catch(() => modal());
} else {
modal();
}
}, },
dropdown() { dropdown() {
return this.site.mobileView; return this.site.mobileView;
@ -98,10 +64,7 @@ export default {
"inviteDisabled", "inviteDisabled",
"isPM", "isPM",
"invitingToTopic" "invitingToTopic"
], ]
displayed() {
return !(this.site.mobileView && window.navigator.share);
}
}); });
registerTopicFooterButton({ registerTopicFooterButton({

View File

@ -6,8 +6,14 @@ export function nativeShare(data) {
) { ) {
window.navigator window.navigator
.share(data) .share(data)
.catch(reject) .then(resolve)
.then(resolve); .catch(e => {
if (e.message === "Share canceled") {
// closing share panel do nothing
} else {
reject();
}
});
} else { } else {
reject(); reject();
} }

View File

@ -280,9 +280,7 @@ createWidget("post-date", {
}); });
}; };
// use native webshare when available nativeShare({ url: this.attrs.shareUrl }).then(null, modalFallback);
// navigator.share needs HTTPS, returns undefined on HTTP
nativeShare({ url: this.attrs.shareUrl }).catch(modalFallback);
} }
}); });