@@ -27,7 +27,7 @@ function Mom() {
2727 const titleRef = useRef < HTMLHeadingElement > ( null ) ;
2828
2929 const onTitleUpdate : React . FormEventHandler < HTMLHeadingElement > = useDebounce (
30- ( e ) => {
30+ ( ) => {
3131 if ( ! titleRef . current ) return ;
3232
3333 const title = titleRef . current . innerText ;
@@ -46,13 +46,13 @@ function Mom() {
4646 focusIndex . current = idx ;
4747 } ;
4848
49- const setBlockFocus = ( ) => {
49+ const setBlockFocus = ( index : number ) => {
5050 if ( ! blockRefs . current || focusIndex . current === undefined ) return ;
5151
5252 const idx = focusIndex . current ;
53+ if ( index === undefined || index !== idx ) return ;
5354
5455 const targetBlock = blockRefs . current [ idx ] ;
55-
5656 if ( ! targetBlock || ! targetBlock . current ) return ;
5757
5858 targetBlock . current . focus ( ) ;
@@ -71,7 +71,7 @@ function Mom() {
7171 range . collapse ( ) ;
7272 } ;
7373
74- const onKeyDown : React . KeyboardEventHandler = ( e ) => {
74+ const onHandleBlock : React . KeyboardEventHandler = ( e ) => {
7575 const target = e . target as HTMLParagraphElement ;
7676
7777 const { index : indexString } = target . dataset ;
@@ -104,17 +104,16 @@ function Mom() {
104104 updateBlockFocus ( index - 1 ) ;
105105
106106 setBlocks ( spreadCRDT ( ) ) ;
107- setBlockFocus ( ) ;
107+
108+ if ( focusIndex . current === undefined ) return ;
109+ setBlockFocus ( focusIndex . current ) ;
110+
108111 setCaretToEnd ( ) ;
109112
110113 socket . emit ( MOM_EVENT . DELETE_BLOCK , id , remoteDeletion ) ;
111114 }
112115 } ;
113116
114- useEffect ( ( ) => {
115- setBlockFocus ( ) ;
116- } , [ blocks ] ) ;
117-
118117 useEffect ( ( ) => {
119118 if ( ! selectedMom ) return ;
120119
@@ -184,6 +183,12 @@ function Mom() {
184183 } ;
185184 } , [ selectedMom ] ) ;
186185
186+ const registerRef =
187+ ( index : number ) => ( ref : React . RefObject < HTMLElement > ) => {
188+ blockRefs . current [ index ] = ref ;
189+ setBlockFocus ( index ) ;
190+ } ;
191+
187192 return selectedMom ? (
188193 < div className = { style [ 'mom-container' ] } >
189194 < div className = { style [ 'mom' ] } >
@@ -205,10 +210,8 @@ function Mom() {
205210 key = { id }
206211 id = { id }
207212 index = { index }
208- onKeyDown = { onKeyDown }
209- registerRef = { ( ref : React . RefObject < HTMLElement > ) => {
210- blockRefs . current [ index ] = ref ;
211- } }
213+ onHandleBlock = { onHandleBlock }
214+ registerRef = { registerRef ( index ) }
212215 />
213216 ) ) }
214217 </ div >
0 commit comments