diff --git a/app/assets/images/social_icons/email.svg b/app/assets/images/social_icons/email.svg new file mode 100644 index 0000000000..d35d51d60f --- /dev/null +++ b/app/assets/images/social_icons/email.svg @@ -0,0 +1,4 @@ + diff --git a/app/assets/images/social_icons/facebook.svg b/app/assets/images/social_icons/facebook.svg new file mode 100644 index 0000000000..80241473eb --- /dev/null +++ b/app/assets/images/social_icons/facebook.svg @@ -0,0 +1,4 @@ + diff --git a/app/assets/images/social_icons/linkedin.svg b/app/assets/images/social_icons/linkedin.svg new file mode 100644 index 0000000000..908c867517 --- /dev/null +++ b/app/assets/images/social_icons/linkedin.svg @@ -0,0 +1,4 @@ + diff --git a/app/assets/images/social_icons/mastodon.svg b/app/assets/images/social_icons/mastodon.svg new file mode 100644 index 0000000000..a8b4bc44e0 --- /dev/null +++ b/app/assets/images/social_icons/mastodon.svg @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/assets/images/social_icons/telegram.svg b/app/assets/images/social_icons/telegram.svg new file mode 100644 index 0000000000..84c087929f --- /dev/null +++ b/app/assets/images/social_icons/telegram.svg @@ -0,0 +1,16 @@ + diff --git a/app/assets/images/social_icons/x.svg b/app/assets/images/social_icons/x.svg new file mode 100644 index 0000000000..937dcc732c --- /dev/null +++ b/app/assets/images/social_icons/x.svg @@ -0,0 +1 @@ + diff --git a/app/assets/javascripts/social_share_button.js b/app/assets/javascripts/social_share_button.js new file mode 100644 index 0000000000..409b2c58a7 --- /dev/null +++ b/app/assets/javascripts/social_share_button.js @@ -0,0 +1,21 @@ +function openShareUrl(url, initialWidth = 640, initialHeight = 480) { + const width = Math.max(100, Math.min(screen.width, initialWidth)); + const height = Math.max(100, Math.min(screen.height, initialHeight)); + + const left = (screen.width / 2) - (width / 2); + const top = (screen.height * 0.3) - (height / 2); + const opts = `width=${width},height=${height},left=${left},top=${top},menubar=no,status=no,location=no`; + + window.open(url, "popup", opts); +} + +$(document).ready(function () { + $(".ssb-icon").on("click", function (e) { + const shareUrl = $(this).attr("href"); + if (!shareUrl.startsWith("mailto:")) { + e.preventDefault(); + openShareUrl(shareUrl); + } + }); +}); + diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fcf253289e..5558e69fbd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,6 @@ module ApplicationHelper require "rexml/document" + include SocialShareButtonHelper def linkify(text) if text.html_safe? @@ -75,4 +76,32 @@ def render_flash(flash) rescue StandardError flash.inspect if Rails.env.development? end + + # Generates a set of social share buttons based on the specified options. + def render_social_share_buttons(opts = {}) + sites = opts.fetch(:allow_sites, []) + valid_sites, invalid_sites = SocialShareButtonHelper.filter_allowed_sites(sites) + + # Log invalid sites + invalid_sites.each do |invalid_site| + Rails.logger.error("Invalid site or icon not configured: #{invalid_site}") + end + + tag.div( + :class => "social-share-button d-flex gap-1 align-items-end flex-wrap mb-3" + ) do + valid_sites.map do |site| + link_options = { + :rel => ["nofollow", opts[:rel]].compact, + :class => "ssb-icon rounded-circle", + :title => I18n.t("application.share.#{site}.title"), + :target => "_blank" + } + + link_to SocialShareButtonHelper.generate_share_url(site, opts), link_options do + image_tag(SocialShareButtonHelper.icon_path(site), :alt => I18n.t("application.share.#{site}.alt"), :size => 28) + end + end.join.html_safe + end + end end diff --git a/app/views/diary_entries/_diary_entry.html.erb b/app/views/diary_entries/_diary_entry.html.erb index 62e701d144..2d8243d1b2 100644 --- a/app/views/diary_entries/_diary_entry.html.erb +++ b/app/views/diary_entries/_diary_entry.html.erb @@ -44,4 +44,5 @@ <% end %> + diff --git a/app/views/diary_entries/show.html.erb b/app/views/diary_entries/show.html.erb index e80a44f096..14b1576f0f 100644 --- a/app/views/diary_entries/show.html.erb +++ b/app/views/diary_entries/show.html.erb @@ -1,3 +1,7 @@ +<% content_for :head do %> + <%= javascript_include_tag "social_share_button" %> +<% end %> + <% content_for :heading do %>