@@ -22,6 +22,7 @@ export namespace Tasks {
2222 on_add : ( ) => void
2323 on_add_subtask ?: ( parent_task : Task ) => void
2424 on_delete : ( created_at : number ) => void
25+ on_forward : ( text : string ) => void
2526 translations : {
2627 placeholder : string
2728 add_new : string
@@ -63,8 +64,8 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
6364 null
6465 )
6566 const [ editing_initial_text , set_editing_initial_text ] = useState < string > ( '' )
66- const [ copied_timestamp , set_copied_timestamp ] = useState < number | null > ( null )
6767 const prevent_edit_ref = useRef ( false )
68+ const is_canceling_ref = useRef ( false )
6869
6970 const handle_set_editing = ( timestamp : number | null , text ?: string ) => {
7071 set_editing_timestamp ( timestamp )
@@ -75,7 +76,25 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
7576
7677 use_auto_focus_new_task ( props . tasks , handle_set_editing )
7778
79+ const handle_cancel_edit = ( task : Task ) => {
80+ is_canceling_ref . current = true
81+ set_editing_timestamp ( null )
82+
83+ const has_children = task . children && task . children . length > 0
84+ if ( ! editing_initial_text . trim ( ) && ! has_children ) {
85+ props . on_delete ( task . created_at )
86+ } else {
87+ const { id, ...rest } = task as any
88+ props . on_change ( { ...rest , text : editing_initial_text } )
89+ }
90+
91+ setTimeout ( ( ) => {
92+ is_canceling_ref . current = false
93+ } , 100 )
94+ }
95+
7896 const handle_stop_editing = ( task : Task ) => {
97+ if ( is_canceling_ref . current ) return
7998 if ( editing_timestamp !== task . created_at ) return
8099 set_editing_timestamp ( null )
81100 if ( ! task . text . trim ( ) ) {
@@ -189,46 +208,17 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
189208 >
190209 { params . task . text && (
191210 < IconButton
192- codicon_icon = {
193- copied_timestamp == params . task . created_at
194- ? 'check'
195- : 'copy'
196- }
197- style = {
198- copied_timestamp == params . task . created_at
199- ? { cursor : 'default' }
200- : undefined
201- }
211+ codicon_icon = "forward"
202212 on_mouse_down = {
203213 is_editing ? ( e ) => e . preventDefault ( ) : undefined
204214 }
205215 on_click = { ( e ) => {
206216 e . stopPropagation ( )
207- navigator . clipboard . writeText ( params . task . text )
208- set_copied_timestamp ( params . task . created_at )
209- setTimeout ( ( ) => {
210- set_copied_timestamp ( ( prev ) =>
211- prev === params . task . created_at ? null : prev
212- )
213- } , 2000 )
217+ props . on_forward ( params . task . text )
214218 } }
215- title = "Copy "
219+ title = "Forward to edit instructions "
216220 />
217221 ) }
218- < IconButton
219- codicon_icon = { is_editing ? 'check' : 'edit' }
220- on_mouse_down = {
221- is_editing ? ( e ) => e . preventDefault ( ) : undefined
222- }
223- on_click = { ( ) => {
224- if ( is_editing ) {
225- handle_stop_editing ( params . task )
226- } else {
227- handle_set_editing ( params . task . created_at , params . task . text )
228- }
229- } }
230- title = { is_editing ? 'Done' : 'Edit' }
231- />
232222 < IconButton
233223 codicon_icon = "trash"
234224 on_mouse_down = {
@@ -255,7 +245,10 @@ export const Tasks: React.FC<Tasks.Props> = (props) => {
255245 handle_stop_editing ( params . task )
256246 } }
257247 on_key_down = { ( e ) => {
258- if ( e . key == 'Enter' && ! e . shiftKey ) {
248+ if ( e . key == 'Escape' ) {
249+ e . preventDefault ( )
250+ handle_cancel_edit ( params . task )
251+ } else if ( e . key == 'Enter' && ! e . shiftKey ) {
259252 e . preventDefault ( )
260253 }
261254 } }
0 commit comments