@@ -15,22 +15,23 @@ import ee from '../Mom/EventEmitter';
1515interface BlockProps {
1616 id : string ;
1717 index : number ;
18- onKeyDown : React . KeyboardEventHandler ;
18+ onHandleBlock : React . KeyboardEventHandler ;
1919 type : BlockType ;
2020 setType : ( arg : BlockType ) => void ;
21+ isLocalTypeUpdate : boolean ;
2122 registerRef : ( arg : React . RefObject < HTMLElement > ) => void ;
2223}
2324
2425function TextBlock ( {
2526 id,
2627 index,
27- onKeyDown ,
28+ onHandleBlock ,
2829 type,
2930 setType,
31+ isLocalTypeUpdate,
3032 registerRef,
3133} : BlockProps ) {
3234 const { momSocket : socket } = useSocketContext ( ) ;
33- const [ isOpen , setIsOpen ] = useState < boolean > ( false ) ;
3435
3536 const {
3637 syncCRDT,
@@ -43,9 +44,12 @@ function TextBlock({
4344
4445 const blockRef = useRef < HTMLParagraphElement > ( null ) ;
4546
46- const { offsetRef, setOffset, clearOffset, offsetHandlers } =
47+ const { offsetRef, setOffset, clearOffset, onArrowKeyDown , offsetHandlers } =
4748 useOffset ( blockRef ) ;
4849
50+ const [ isOpen , setIsOpen ] = useState < boolean > ( false ) ;
51+ const onClose = ( ) => setIsOpen ( false ) ;
52+
4953 // 리모트 연산 수행결과로 innerText 변경 시 커서의 위치 조정
5054 const updateCaretPosition = ( updateOffset = 0 ) => {
5155 if ( ! blockRef . current || offsetRef . current === null ) return ;
@@ -110,27 +114,41 @@ function TextBlock({
110114
111115 // crdt의 초기화와 소켓을 통해 전달받는 리모트 연산 처리
112116 useEffect ( ( ) => {
113- registerRef ( blockRef ) ;
117+ socket . emit ( BLOCK_EVENT . INIT_TEXT , id ) ;
114118
115- socket . emit ( BLOCK_EVENT . INIT , id ) ;
116-
117- ee . on ( `${ BLOCK_EVENT . INIT } -${ id } ` , onInitialize ) ;
119+ ee . on ( `${ BLOCK_EVENT . INIT_TEXT } -${ id } ` , onInitialize ) ;
118120 ee . on ( `${ BLOCK_EVENT . UPDATE_TEXT } -${ id } ` , onInitialize ) ;
119121 ee . on ( `${ BLOCK_EVENT . INSERT_TEXT } -${ id } ` , onInsert ) ;
120122 ee . on ( `${ BLOCK_EVENT . DELETE_TEXT } -${ id } ` , onDelete ) ;
121123
122124 return ( ) => {
123- ee . off ( `${ BLOCK_EVENT . INIT } -${ id } ` , onInitialize ) ;
125+ ee . off ( `${ BLOCK_EVENT . INIT_TEXT } -${ id } ` , onInitialize ) ;
124126 ee . off ( `${ BLOCK_EVENT . UPDATE_TEXT } -${ id } ` , onInitialize ) ;
125127 ee . off ( `${ BLOCK_EVENT . INSERT_TEXT } -${ id } ` , onInsert ) ;
126128 ee . off ( `${ BLOCK_EVENT . DELETE_TEXT } -${ id } ` , onDelete ) ;
127129 } ;
128130 } , [ ] ) ;
129131
132+ useEffect ( ( ) => {
133+ registerRef ( blockRef ) ;
134+ } , [ index ] ) ;
135+
130136 useEffect ( ( ) => {
131137 updateCaretPosition ( ) ;
132138 } , [ isOpen ] ) ;
133139
140+ useEffect ( ( ) => {
141+ if ( isLocalTypeUpdate && readCRDT ( ) . length ) {
142+ const remoteDeletion = localDeleteCRDT ( 0 ) ;
143+ socket . emit ( BLOCK_EVENT . DELETE_TEXT , id , remoteDeletion ) ;
144+
145+ if ( ! blockRef . current ) return ;
146+
147+ blockRef . current . innerText = readCRDT ( ) ;
148+ blockRef . current . focus ( ) ;
149+ }
150+ } , [ type ] ) ;
151+
134152 // 로컬에서 일어나는 작성 - 삽입과 삭제 연산
135153 const onInput : React . FormEventHandler = ( e ) => {
136154 setOffset ( ) ;
@@ -205,18 +223,16 @@ function TextBlock({
205223 updateCaretPosition ( pastedText . length ) ;
206224 } ;
207225
208- const onKeyDownComposite : React . KeyboardEventHandler < HTMLParagraphElement > = (
209- e ,
210- ) => {
211- offsetHandlers . onKeyDown ( e ) ;
212- onKeyDown ( e ) ;
226+ const onKeyDown : React . KeyboardEventHandler < HTMLParagraphElement > = ( e ) => {
227+ onArrowKeyDown ( e ) ;
228+ onHandleBlock ( e ) ;
213229 } ;
214230
215231 const commonHandlers = {
216232 onInput,
217233 onCompositionEnd,
218234 ...offsetHandlers ,
219- onKeyDown : onKeyDownComposite ,
235+ onKeyDown,
220236 onPaste,
221237 } ;
222238
@@ -236,14 +252,14 @@ function TextBlock({
236252 {
237253 ref : blockRef ,
238254 'data-id' : id ,
239- 'date -index' : index ,
255+ 'data -index' : index ,
240256 ...commonHandlers ,
241257 contentEditable : true ,
242258 suppressContentEditableWarning : true ,
243259 } ,
244260 readCRDT ( ) ,
245261 ) }
246- { isOpen && < BlockSelector onSelect = { onSelect } /> }
262+ { isOpen && < BlockSelector onClose = { onClose } onSelect = { onSelect } /> }
247263 </ >
248264 ) ;
249265}
0 commit comments