From a13322dcee25172b5a03af4b7a6a4ea7ee6fece8 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 5 Aug 2021 18:30:44 +0200 Subject: [PATCH] Add share option to answers --- .../stylesheets/components/_answerbox.scss | 6 ++++++ .../retrospring/initializers/share.ts | 20 +++++++++++++++++++ app/views/answerbox/_actions.haml | 2 ++ 3 files changed, 28 insertions(+) create mode 100644 app/javascript/retrospring/initializers/share.ts diff --git a/app/assets/stylesheets/components/_answerbox.scss b/app/assets/stylesheets/components/_answerbox.scss index d13f672f..cc892099 100644 --- a/app/assets/stylesheets/components/_answerbox.scss +++ b/app/assets/stylesheets/components/_answerbox.scss @@ -61,4 +61,10 @@ } } } +} + +body:not(.cap-web-share) { + .answerbox__action[name="ab-share"] { + display: none; + } } \ No newline at end of file diff --git a/app/javascript/retrospring/initializers/share.ts b/app/javascript/retrospring/initializers/share.ts new file mode 100644 index 00000000..26b0716f --- /dev/null +++ b/app/javascript/retrospring/initializers/share.ts @@ -0,0 +1,20 @@ +import $ from 'jquery'; + +export default (): void => { + document.addEventListener('turbolinks:load', function () { + if (navigator.share) { + document.body.classList.add('cap-web-share') + $(document).on('click', 'button[name=ab-share]', function () { + const card = $(this).closest('.card') + + navigator.share({ + url: card.find('.answerbox__answer-date a')[0].href + }).then(() => { + // do nothing, prevents exception from being thrown + }).catch(() => { + // do nothing, prevents exception from being thrown + }) + }) + } + }) +} \ No newline at end of file diff --git a/app/views/answerbox/_actions.haml b/app/views/answerbox/_actions.haml index f6f99c12..8287f749 100644 --- a/app/views/answerbox/_actions.haml +++ b/app/views/answerbox/_actions.haml @@ -16,6 +16,8 @@ %button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-comments', data: { a_id: a.id, state: :hidden } } %i.fa.fa-fw.fa-comments %span{ id: "ab-comment-count-#{a.id}" }= a.comment_count +%button.btn.btn-link.answerbox__action{ type: :button, name: 'ab-share'} + %i.fa.fa-fw.fa-share-alt{ title: 'Share' } - if user_signed_in? .btn-group %button.btn.btn-default.btn-sm.dropdown-toggle{ data: { toggle: :dropdown }, aria: { expanded: false } }