Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Copy to Clipboard for Answers #1461

Merged
14 changes: 14 additions & 0 deletions app/javascript/retrospring/controllers/clipboard_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {

static values = {
copy: String
};

declare readonly copyValue: string;

async copy(){
await navigator.clipboard.writeText(this.copyValue);
}
}
2 changes: 2 additions & 0 deletions app/javascript/retrospring/initializers/stimulus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ToastController from "retrospring/controllers/toast_controller";
import PwaBadgeController from "retrospring/controllers/pwa_badge_controller";
import NavigationController from "retrospring/controllers/navigation_controller";
import ShareController from "retrospring/controllers/share_controller";
import ClipboardController from "retrospring/controllers/clipboard_controller";

/**
* This module sets up Stimulus and our controllers
Expand All @@ -39,4 +40,5 @@ export default function (): void {
window['Stimulus'].register('theme', ThemeController);
window['Stimulus'].register('toast', ToastController);
window['Stimulus'].register('share', ShareController);
window['Stimulus'].register('clipboard', ClipboardController);
}
3 changes: 3 additions & 0 deletions app/views/actions/_share.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
%a.dropdown-item{ href: telegram_share_url(answer), target: "_blank" }
%i.fa.fa-fw.fa-telegram
= t(".telegram")
%a.dropdown-item{ href: "#", data: { controller: :clipboard, action: "clipboard#copy", clipboard_copy_value: prepare_tweet(answer) } }
%i.fa.fa-fw.fa-solid.fa-copy
= t(".copy")
%a.dropdown-item{ href: "#", data: { controller: :share, action: "share#share", share_url_value: answer_share_url(answer) } }
= t(".other")
1 change: 1 addition & 0 deletions config/locales/views.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ en:
pin: "Pin to Profile"
unpin: "Unpin from Profile"
share:
copy: "Copy to Clipboard"
twitter: "Share on Twitter"
tumblr: "Share on Tumblr"
telegram: "Share on Telegram"
Expand Down
6 changes: 5 additions & 1 deletion spec/views/actions/_share.html.haml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
end

it "has a dropdown item to share to anywhere else" do
expect(rendered).to have_css(%(a.dropdown-item[data-controller="share"]))
expect(rendered).to have_css(%(a.dropdown-item[data-action="share#share"]))
end

it "has a dropdown item to copy to clipboard" do
expect(rendered).to have_css(%(a.dropdown-item[data-action="clipboard#copy"]))
end
end