Skip to content

Commit 793c5cb

Browse files
committed
Add Tweet button for Snippet
Also refactors toolbar
1 parent 3d0cb60 commit 793c5cb

File tree

16 files changed

+156
-62
lines changed

16 files changed

+156
-62
lines changed

app/controllers/share/snippet_screenshots_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ def new
1414
def create
1515
@snippet = Snippet.find(params[:snippet_id])
1616
@snippet.attach_screenshot_from_base64(params[:screenshot])
17-
redirect_to share_snippet_url(@snippet), notice: "Screenshot attached".emojoy
17+
redirect_to new_share_snippet_tweet_path(@snippet), notice: "Screenshot attached".emojoy
1818
end
1919
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Share::SnippetTweetsController < ApplicationController
2+
using Refinements::Emojoy
3+
4+
def new
5+
@snippet = Snippet.find(params[:snippet_id])
6+
end
7+
end

app/javascript/controllers/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import TableOfContents from './table-of-contents';
1818

1919
import FrameForm from './forms/frame';
2020
import SyntaxHighlightPreview from './syntax-highlight/preview';
21+
2122
import SnippetPreview from './snippets/preview';
2223
import SnippetEditor from './snippets/editor';
2324
import SnippetScreenshot from './snippets/screenshot';
25+
import SnippetTweet from './snippets/tweet';
2426

2527
application.register('analytics', AnalyticsCustomEvent);
2628
application.register('code-example', CodeExample);
@@ -39,3 +41,4 @@ application.register('syntax-highlight-preview', SyntaxHighlightPreview);
3941
application.register('snippet-preview', SnippetPreview);
4042
application.register('snippet-editor', SnippetEditor);
4143
application.register('snippet-screenshot', SnippetScreenshot);
44+
application.register('snippet-tweet', SnippetTweet);

app/javascript/controllers/snippets/screenshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ export default class extends Controller<HTMLFormElement> {
2020
);
2121

2222
// submit immediately
23-
// this.submitButtonTarget.click();
24-
// this.submitButtonTarget.disabled = true;
23+
this.submitButtonTarget.click();
24+
this.submitButtonTarget.disabled = true;
2525
}
2626

2727
prepareScreenshot = async (event) => {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
3+
import debug from '../../utils/debug';
4+
5+
const console = debug('app:javascript:controllers:snippets:tweet');
6+
7+
export default class extends Controller {
8+
static values = {
9+
url: String,
10+
};
11+
12+
declare urlValue: string;
13+
14+
connect() {
15+
console.log('Connect!');
16+
17+
this.tweet();
18+
}
19+
20+
tweet() {
21+
console.log('Tweet!');
22+
23+
const tweetText = encodeURIComponent(this.urlValue);
24+
const tweetUrl = `https://twitter.com/intent/tweet?text=${tweetText}`;
25+
26+
window.open(
27+
tweetUrl,
28+
'_blank',
29+
'width=550,height=420,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes',
30+
);
31+
}
32+
}

app/javascript/css/utilities/tailwind.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -957,11 +957,6 @@ focus\:ring-0:focus {
957957
text-align: right;
958958
}
959959

960-
.mx-auto {
961-
margin-left: auto;
962-
margin-right: auto;
963-
}
964-
965960
.max-w-sm {
966961
max-width: 24rem;
967962
}

app/views/share/snippet_screenshots/form.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def view_template
1717
method: :post,
1818
class: "grid-content",
1919
data: {
20-
controller: "snippet-screenshot",
21-
turbo_frame: "_top"
20+
controller: "snippet-screenshot"
2221
}
2322
) do |form|
2423
errors

app/views/share/snippet_screenshots/new.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= render Pages::Header.new(title: "Snippet Screenshot") %>
1+
<%= render Pages::Header.new(title: "Share Snippet: Screenshot") %>
22
<div class="section-content container py-gap">
3-
<%= turbo_frame_tag :snippet_form do %>
3+
<%= turbo_frame_tag :snippet_form, data: {turbo_action: "advance"} do %>
44
<%= render Share::SnippetScreenshots::Form.new(@snippet) %>
55
<% end %>
66

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<%= render Pages::Header.new(title: "Share Snippet: Tweet") %>
2+
<div class="section-content container py-gap">
3+
<%= turbo_frame_tag :snippet_form, data: {turbo_action: "advance"} do %>
4+
<%= render Share::SnippetTweets::Tweet.new(@snippet) %>
5+
<% end %>
6+
7+
<div>
8+
<%= link_to "Back to snippets", share_snippets_path %>
9+
</div>
10+
</div>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Share::SnippetTweets::Tweet < ApplicationComponent
2+
def initialize(snippet)
3+
@snippet = snippet
4+
end
5+
6+
def view_template
7+
render CodeBlock::Snippet.new(
8+
@snippet,
9+
screenshot: true,
10+
data: {
11+
controller: "snippet-tweet",
12+
snippet_tweet_url_value: tweet_url
13+
}
14+
)
15+
end
16+
17+
def tweet_url
18+
@snippet.screenshot.attached? ? rails_storage_proxy_url(@snippet.screenshot) : share_snippet_url(@snippet)
19+
end
20+
end

0 commit comments

Comments
 (0)