1+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2+ /* TextBlock 마운트 이후 항상 존재하는 blockRef.current 존재 여부 확인하는 불필요한 가드를 제거하기 위함 */
3+
14import { BlockType } from '@wabinar/constants/block' ;
25import { BLOCK_EVENT } from '@wabinar/constants/socket-message' ;
36import {
@@ -12,7 +15,7 @@ import { useOffset } from 'src/hooks/useOffset';
1215
1316import ee from '../Mom/EventEmitter' ;
1417
15- interface BlockProps {
18+ interface TextBlockProps {
1619 id : string ;
1720 index : number ;
1821 onHandleBlocks : React . KeyboardEventHandler ;
@@ -30,7 +33,7 @@ function TextBlock({
3033 setType,
3134 isLocalTypeUpdate,
3235 registerRef,
33- } : BlockProps ) {
36+ } : TextBlockProps ) {
3437 const { momSocket : socket } = useSocketContext ( ) ;
3538
3639 const initBlock = ( ) => {
@@ -56,7 +59,7 @@ function TextBlock({
5659
5760 // 리모트 연산 수행결과로 innerText 변경 시 커서의 위치 조정
5861 const updateCaretPosition = ( updateOffset = 0 ) => {
59- if ( ! blockRef . current || offsetRef . current === null ) return ;
62+ if ( offsetRef . current === null ) return ;
6063
6164 const selection = window . getSelection ( ) ;
6265
@@ -67,14 +70,14 @@ function TextBlock({
6770 const range = new Range ( ) ;
6871
6972 // 우선 블럭의 첫번째 text node로 고정, text node가 없는 경우 clearOffset()
70- if ( ! blockRef . current . firstChild ) {
73+ if ( ! blockRef . current ! . firstChild ) {
7174 clearOffset ( ) ;
7275 return ;
7376 }
7477
7578 // range start와 range end가 같은 경우만 가정
7679 range . setStart (
77- blockRef . current . firstChild ,
80+ blockRef . current ! . firstChild ,
7881 offsetRef . current + updateOffset ,
7982 ) ;
8083 range . collapse ( ) ;
@@ -87,9 +90,7 @@ function TextBlock({
8790 const onInitialize = ( crdt : unknown ) => {
8891 syncCRDT ( crdt ) ;
8992
90- if ( ! blockRef . current ) return ;
91-
92- blockRef . current . innerText = readCRDT ( ) ;
93+ blockRef . current ! . innerText = readCRDT ( ) ;
9394
9495 updateCaretPosition ( ) ;
9596 } ;
@@ -104,9 +105,7 @@ function TextBlock({
104105 return ;
105106 }
106107
107- if ( ! blockRef . current ) return ;
108-
109- blockRef . current . innerText = readCRDT ( ) ;
108+ blockRef . current ! . innerText = readCRDT ( ) ;
110109
111110 if ( prevIndex === null || offsetRef . current === null ) return ;
112111
@@ -123,9 +122,7 @@ function TextBlock({
123122 return ;
124123 }
125124
126- if ( ! blockRef . current ) return ;
127-
128- blockRef . current . innerText = readCRDT ( ) ;
125+ blockRef . current ! . innerText = readCRDT ( ) ;
129126
130127 if ( targetIndex === null || offsetRef . current === null ) return ;
131128
@@ -151,6 +148,7 @@ function TextBlock({
151148
152149 useEffect ( ( ) => {
153150 registerRef ( blockRef ) ;
151+ blockRef . current ! . setAttribute ( 'data-index' , index . toString ( ) ) ;
154152 } , [ index ] ) ;
155153
156154 useEffect ( ( ) => {
@@ -162,20 +160,16 @@ function TextBlock({
162160 const remoteDeletion = localDeleteCRDT ( 0 ) ;
163161 socket . emit ( BLOCK_EVENT . DELETE_TEXT , id , remoteDeletion ) ;
164162
165- if ( ! blockRef . current ) return ;
166-
167- blockRef . current . innerText = readCRDT ( ) ;
168- blockRef . current . focus ( ) ;
163+ blockRef . current ! . innerText = readCRDT ( ) ;
164+ blockRef . current ! . focus ( ) ;
169165 }
170166 } , [ type ] ) ;
171167
172168 // 로컬에서 일어나는 작성 - 삽입과 삭제 연산
173169 const onInput : React . FormEventHandler = ( e ) => {
174170 setOffset ( ) ;
175171
176- if ( ! blockRef . current ) return ;
177-
178- if ( blockRef . current . innerText === '/' ) {
172+ if ( blockRef . current ! . innerText === '/' ) {
179173 setIsOpen ( true ) ;
180174 } else if ( isOpen ) {
181175 setIsOpen ( false ) ;
@@ -238,14 +232,14 @@ function TextBlock({
238232 e . preventDefault ( ) ;
239233
240234 setOffset ( ) ;
241- if ( offsetRef . current === null || ! blockRef . current ) return ;
235+ if ( offsetRef . current === null ) return ;
242236
243237 let previousLetterIndex = offsetRef . current - 1 ;
244- const previousText = blockRef . current . innerText . slice (
238+ const previousText = blockRef . current ! . innerText . slice (
245239 0 ,
246240 previousLetterIndex + 1 ,
247241 ) ;
248- const nextText = blockRef . current . innerText . slice ( previousLetterIndex + 1 ) ;
242+ const nextText = blockRef . current ! . innerText . slice ( previousLetterIndex + 1 ) ;
249243
250244 const pastedText = e . clipboardData . getData ( 'text/plain' ) . replace ( '\n' , '' ) ;
251245 const remoteInsertions = pastedText
@@ -254,7 +248,7 @@ function TextBlock({
254248
255249 socket . emit ( BLOCK_EVENT . UPDATE_TEXT , id , remoteInsertions ) ;
256250
257- blockRef . current . innerText = previousText + pastedText + nextText ;
251+ blockRef . current ! . innerText = previousText + pastedText + nextText ;
258252 updateCaretPosition ( pastedText . length ) ;
259253 } ;
260254
0 commit comments