-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,4 +44,5 @@ | |
<% end %> | ||
</ul> | ||
</nav> | ||
|
||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module SocialShareButtonHelper | ||
require "uri" | ||
|
||
SOCIAL_SHARE_CONFIG = { | ||
:email => "social_icons/email.svg", | ||
:facebook => "social_icons/facebook.svg", | ||
:linkedin => "social_icons/linkedin.svg", | ||
:mastodon => "social_icons/mastodon.svg", | ||
:telegram => "social_icons/telegram.svg", | ||
:x => "social_icons/x.svg" | ||
}.freeze | ||
|
||
def self.filter_allowed_sites(sites) | ||
valid_sites = sites.empty? ? SOCIAL_SHARE_CONFIG.keys : sites.select { |site| valid_site?(site) } | ||
invalid_sites = sites - valid_sites | ||
[valid_sites, invalid_sites] | ||
end | ||
|
||
def self.icon_path(site) | ||
SOCIAL_SHARE_CONFIG[site.to_sym] || "" | ||
end | ||
|
||
def self.valid_site?(site) | ||
SOCIAL_SHARE_CONFIG.key?(site.to_sym) | ||
end | ||
|
||
def self.generate_share_url(site, params) | ||
site = site.to_sym | ||
case site | ||
when :email | ||
to = params[:to] || "" | ||
subject = CGI.escape(params[:title]) | ||
body = CGI.escape(params[:url]) | ||
"mailto:#{to}?subject=#{subject}&body=#{body}" | ||
when :x | ||
via_str = params[:via] ? "&via=#{URI.encode_www_form_component(params[:via])}" : "" | ||
hashtags_str = params[:hashtags] ? "&hashtags=#{URI.encode_www_form_component(params[:hashtags].join(','))}" : "" | ||
"https://x.com/intent/tweet?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}#{hashtags_str}#{via_str}" | ||
when :linkedin | ||
"https://www.linkedin.com/sharing/share-offsite/?url=#{URI.encode_www_form_component(params[:url])}" | ||
when :facebook | ||
"https://www.facebook.com/sharer/sharer.php?u=#{URI.encode_www_form_component('params[:url]')}&t=#{URI.encode_www_form_component(params[:title])}" | ||
when :mastodon | ||
"https://mastodonshare.com/?text=#{URI.encode_www_form_component(params[:title])}&url=#{URI.encode_www_form_component(params[:url])}" | ||
when :telegram | ||
"https://t.me/share/url?url=#{URI.encode_www_form_component(params[:url])}&text=#{URI.encode_www_form_component(params[:title])}" | ||
else | ||
raise ArgumentError, "Unsupported platform: #{platform}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "test_helper" | ||
|
||
class SocialShareButtonHelperTest < ActionView::TestCase | ||
include SocialShareButtonHelper | ||
include ApplicationHelper | ||
|
||
def setup | ||
@options = { | ||
:allow_sites => %w[x facebook linkedin], | ||
:title => "Test Title", | ||
:url => "https://example.com", | ||
:desc => "Test Description", | ||
:via => "testuser" | ||
} | ||
end | ||
|
||
def test_render_social_share_buttons_with_valid_sites | ||
result = render_social_share_buttons(@options) | ||
assert_includes result, "x" | ||
assert_includes result, "facebook" | ||
assert_includes result, "linkedin" | ||
end | ||
|
||
def test_render_social_share_buttons_with_invalid_site | ||
@options[:allow_sites] << "invalid_site" | ||
result = render_social_share_buttons(@options) | ||
assert_not_includes result, "invalid_site" | ||
end | ||
|
||
def test_render_social_share_buttons_with_no_sites | ||
@options[:allow_sites] = [] | ||
result = render_social_share_buttons(@options) | ||
SocialShareButtonHelper::SOCIAL_SHARE_CONFIG.each_key do |site| | ||
assert_includes result, site.to_s # Convert symbol to string | ||
end | ||
end | ||
|
||
def test_filter_allowed_sites | ||
valid_sites, invalid_sites = SocialShareButtonHelper.filter_allowed_sites(%w[x facebook invalid_site]) | ||
assert_equal %w[x facebook], valid_sites | ||
assert_equal %w[invalid_site], invalid_sites | ||
end | ||
|
||
def test_icon_path | ||
assert_equal "social_icons/x.svg", SocialShareButtonHelper.icon_path("x") | ||
assert_equal "", SocialShareButtonHelper.icon_path("invalid_site") | ||
end | ||
end |