@@ -21,10 +21,6 @@ defmodule TimelineLive do
21
21
{ :ok , socket }
22
22
end
23
23
24
- def handle_event ( "validate" , % { "content" => content } , socket ) do
25
- { :noreply , assign ( socket , :form , to_form ( % { "content" => content } ) ) }
26
- end
27
-
28
24
def handle_event ( "create_post" , % { "content" => content } , socket ) do
29
25
post = % {
30
26
id: System . unique_integer ( [ :positive ] ) ,
@@ -58,22 +54,35 @@ defmodule TimelineLive do
58
54
< div class = "timeline " >
59
55
< h1 > Timeline</ h1 >
60
56
61
- < . form for = { @ form } phx-submit = "create_post " phx-change = " validate " id = "post-form " >
62
- < textarea name = "content " placeholder = "What's on your mind? " > <%= @ form . params [ "content" ] %> </ textarea >
57
+ < . form for = { @ form } phx-submit = "create_post " id = "post-form " >
58
+ < textarea name = "content " placeholder = "What's on your mind? " id = " content " phx-hook = " CmdEnterSubmit " > <%= @ form . params [ "content" ] %> </ textarea >
63
59
< button type = "submit " > Post</ button >
64
60
</ . form >
65
61
66
62
< div class = "posts " phx-update = "stream " id = "posts " >
67
63
< div :for = { { dom_id , post } <- @ streams . posts } id = { dom_id } class = "post " >
68
64
< div class = "post-content " >
69
65
< p > <%= post . content %> </ p >
70
- < small > <%= post . inserted_at %> </ small >
66
+ < small > { DateTime . truncate ( post . inserted_at , :second ) } </ small >
71
67
</ div >
72
68
< button phx-click = "delete_post " phx-value-dom_id = { dom_id } class = "delete-btn " > Delete</ button >
73
69
</ div >
74
70
</ div >
75
71
</ div >
76
72
73
+ < script >
74
+ window.hooks.CmdEnterSubmit = {
75
+ mounted ( ) {
76
+ this . el . addEventListener ( "keydown" , ( e ) => {
77
+ if ( e . metaKey && e . key === 'Enter' ) {
78
+ this . el . form . dispatchEvent (
79
+ new Event ( 'submit' , { bubbles: true , cancelable: true } ) ) ;
80
+ }
81
+ } )
82
+ }
83
+ }
84
+ </ script >
85
+
77
86
< style >
78
87
* {
79
88
box - sizing : border - box ;
0 commit comments