File tree Expand file tree Collapse file tree 4 files changed +54
-31
lines changed
javascript/controllers/snippets Expand file tree Collapse file tree 4 files changed +54
-31
lines changed Original file line number Diff line number Diff line change @@ -21,33 +21,31 @@ const WINDOW_OPTIONS = {
21
21
const WINDOW_OPTIONS_ARGUMENT = Object . entries ( WINDOW_OPTIONS )
22
22
. map ( ( [ key , value ] ) => `${ key } =${ value } ` )
23
23
. join ( ',' ) ;
24
- export default class extends Controller {
24
+ export default class extends Controller < HTMLAnchorElement > {
25
25
static values = {
26
- url : String ,
27
26
auto : Boolean ,
27
+ url : String ,
28
28
} ;
29
29
30
- declare urlValue : string ;
31
- declare autoValue : boolean ;
30
+ declare readonly autoValue : boolean ;
31
+ declare readonly urlValue : string ;
32
32
33
33
connect ( ) {
34
34
console . log ( 'Connect!' ) ;
35
35
36
36
if ( this . autoValue ) {
37
- this . tweet ( ) ;
37
+ this . element . click ( ) ;
38
38
Turbo . visit ( this . urlValue ) ;
39
39
}
40
40
}
41
41
42
- tweet ( ) {
43
- console . log ( 'Tweet!' ) ;
44
-
45
- const url = this . urlValue ;
46
-
47
- const tweetText = encodeURIComponent ( `Created with @joyofrails ${ url } ` ) ;
42
+ tweet = ( event : Event ) => {
43
+ if ( event ) {
44
+ event . preventDefault ( ) ;
45
+ }
48
46
49
- const tweetUrl = `https://x.com/intent/post?text= ${ tweetText } ` ;
47
+ const tweetUrl = ( this . element as HTMLAnchorElement ) . href ;
50
48
51
49
window . open ( tweetUrl , '_blank' , WINDOW_OPTIONS_ARGUMENT ) ;
52
- }
50
+ } ;
53
51
}
Original file line number Diff line number Diff line change 1
1
class Share ::SnippetTweets ::Tweet < ApplicationComponent
2
- include Phlex ::Rails ::Helpers ::ButtonTag
3
- include Phlex ::Rails ::Helpers ::ClassNames
4
2
include PhlexConcerns ::FlexBlock
5
3
6
4
def initialize ( snippet , auto : false )
@@ -9,30 +7,17 @@ def initialize(snippet, auto: false)
9
7
end
10
8
11
9
def view_template
12
- div (
13
- class : "snippet-tweet grid-content" ,
14
- data : {
15
- controller : "snippet-tweet" ,
16
- snippet_tweet_url_value : tweet_url ,
17
- snippet_tweet_auto_value : auto? . to_s
18
- }
19
- ) do
10
+ div ( class : "snippet-tweet grid-content" ) do
20
11
render CodeBlock ::Snippet . new (
21
12
@snippet ,
22
13
screenshot : true
23
14
)
24
15
25
16
flex_block do
26
- button_tag "Share" ,
27
- class : class_names ( "button" , "primary" ) ,
28
- data : { action : "click->snippet-tweet#tweet" }
17
+ render Share ::SnippetTweets ::TweetButton . new ( @snippet , auto : auto? )
29
18
end
30
19
end
31
20
end
32
21
33
- def tweet_url
34
- share_snippet_url ( @snippet )
35
- end
36
-
37
22
def auto? = !!@auto
38
23
end
Original file line number Diff line number Diff line change
1
+ class Share ::SnippetTweets ::TweetButton < ApplicationComponent
2
+ def initialize ( snippet , auto : false )
3
+ @snippet = snippet
4
+ @auto = auto
5
+ end
6
+
7
+ def view_template
8
+ a (
9
+ href : tweet_url ,
10
+ class : "button primary" ,
11
+ data : {
12
+ controller : "snippet-tweet" ,
13
+ action : "click->snippet-tweet#tweet" ,
14
+ snippet_tweet_auto_value : auto? . to_s ,
15
+ snippet_tweet_url_value : share_url
16
+ }
17
+ ) do
18
+ "Share"
19
+ end
20
+ end
21
+
22
+ def tweet_url
23
+ "https://x.com/intent/post?text=#{ encode ( tweet_text ) } "
24
+ end
25
+
26
+ def share_url
27
+ share_snippet_url ( @snippet )
28
+ end
29
+
30
+ def tweet_text
31
+ "Created with @joyofrails #{ share_url } "
32
+ end
33
+
34
+ def encode ( text )
35
+ URI . encode_www_form_component ( text )
36
+ end
37
+
38
+ def auto? = !!@auto
39
+ end
Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ def initialize(snippet, current_user: nil)
10
10
11
11
def view_template
12
12
flex_block do
13
- link_to "Share" , share_url , class : "button primary"
13
+ render Share ::SnippetTweets ::TweetButton . new ( @snippet )
14
+
14
15
if @current_user &.can_edit? ( @snippet )
15
16
link_to "Edit this snippet" , edit_share_snippet_path ( @snippet ) ,
16
17
class : "button secondary" ,
You can’t perform that action at this time.
0 commit comments