@@ -127,11 +127,13 @@ function Block({ id, onKeyDown, index }: BlockProps) {
127127 } ;
128128
129129 ee . on ( `${ SOCKET_MESSAGE . BLOCK . INIT } -${ id } ` , onInitialize ) ;
130+ ee . on ( `${ SOCKET_MESSAGE . BLOCK . UPDATE_TEXT } -${ id } ` , onInitialize ) ;
130131 ee . on ( `${ SOCKET_MESSAGE . BLOCK . INSERT_TEXT } -${ id } ` , onInsert ) ;
131132 ee . on ( `${ SOCKET_MESSAGE . BLOCK . DELETE_TEXT } -${ id } ` , onDelete ) ;
132133
133134 return ( ) => {
134135 ee . off ( `${ SOCKET_MESSAGE . BLOCK . INIT } -${ id } ` , onInitialize ) ;
136+ ee . off ( `${ SOCKET_MESSAGE . BLOCK . UPDATE_TEXT } -${ id } ` , onInitialize ) ;
135137 ee . off ( `${ SOCKET_MESSAGE . BLOCK . INSERT_TEXT } -${ id } ` , onInsert ) ;
136138 ee . off ( `${ SOCKET_MESSAGE . BLOCK . DELETE_TEXT } -${ id } ` , onDelete ) ;
137139 } ;
@@ -158,6 +160,26 @@ function Block({ id, onKeyDown, index }: BlockProps) {
158160
159161 const onPaste : React . ClipboardEventHandler < HTMLParagraphElement > = ( e ) => {
160162 e . preventDefault ( ) ;
163+
164+ setOffset ( ) ;
165+ if ( offsetRef . current === null || ! blockRef . current ) return ;
166+
167+ let previousLetterIndex = offsetRef . current - 1 ;
168+ const previousText = blockRef . current . innerText . slice (
169+ 0 ,
170+ previousLetterIndex + 1 ,
171+ ) ;
172+ const nextText = blockRef . current . innerText . slice ( previousLetterIndex + 1 ) ;
173+
174+ const pastedText = e . clipboardData . getData ( 'text/plain' ) . replace ( '\n' , '' ) ;
175+ const remoteInsertions = pastedText
176+ . split ( '' )
177+ . map ( ( letter ) => localInsertCRDT ( previousLetterIndex ++ , letter ) ) ;
178+
179+ socket . emit ( SOCKET_MESSAGE . BLOCK . UPDATE_TEXT , id , remoteInsertions ) ;
180+
181+ blockRef . current . innerText = previousText + pastedText + nextText ;
182+ updateCaretPosition ( pastedText . length ) ;
161183 } ;
162184
163185 const onKeyDownComposite : React . KeyboardEventHandler < HTMLParagraphElement > = (
0 commit comments