Skip to content

Commit cf902bf

Browse files
authored
Fix/#303-B: 블록 추가시 포커스 잡히지 않는 버그 해결 (#308)
* fix: 오타 수정 Resolve: #303 * fix: 블록 추가 시 focus 안되는 버그 해결 Resolve: #303 * fix: 블럭 사이에 블럭 추가 시 버그 해결 블럭 사이에 추가할 때 index에 바로 ref를 넣기 때문에 뒤에 있던 ref위에 덮어씌워짐 index가 바뀌면 registerRef를 실행하게하여 blockRefs 갱신 Resolve: #303 * feat: focus잡으려는 블럭일때 포커스 변경 Resolve: #303 * refactor: 무조건 index를 넘기게 리팩토링 Resolve: #303 * fix: 타입 에러 수정 * fix: focusIndex가 0일 때 포커스 잡히게 조건문 수정 Resolve: #303
1 parent 14cdc7d commit cf902bf

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

client/src/components/Block/TextBlock.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ function TextBlock({
110110

111111
// crdt의 초기화와 소켓을 통해 전달받는 리모트 연산 처리
112112
useEffect(() => {
113-
registerRef(blockRef);
114-
115113
socket.emit(BLOCK_EVENT.INIT, id);
116114

117115
ee.on(`${BLOCK_EVENT.INIT}-${id}`, onInitialize);
@@ -127,6 +125,10 @@ function TextBlock({
127125
};
128126
}, []);
129127

128+
useEffect(() => {
129+
registerRef(blockRef);
130+
}, [index]);
131+
130132
useEffect(() => {
131133
updateCaretPosition();
132134
}, [isOpen]);
@@ -240,7 +242,7 @@ function TextBlock({
240242
{
241243
ref: blockRef,
242244
'data-id': id,
243-
'date-index': index,
245+
'data-index': index,
244246
...commonHandlers,
245247
contentEditable: true,
246248
suppressContentEditableWarning: true,

client/src/components/Mom/index.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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();
@@ -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']}>
@@ -206,9 +211,7 @@ function Mom() {
206211
id={id}
207212
index={index}
208213
onHandleBlock={onHandleBlock}
209-
registerRef={(ref: React.RefObject<HTMLElement>) => {
210-
blockRefs.current[index] = ref;
211-
}}
214+
registerRef={registerRef(index)}
212215
/>
213216
))}
214217
</div>

client/src/hooks/useOffset.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ export function useOffset(blockRef: React.RefObject<HTMLParagraphElement>) {
5454
};
5555

5656
const offsetHandlers = {
57-
onFocus:
58-
setOffset as unknown as React.FocusEventHandler<HTMLParagraphElement>,
5957
onClick:
6058
setOffset as unknown as React.MouseEventHandler<HTMLParagraphElement>,
6159
onKeyUp: onArrowKeyup,

0 commit comments

Comments
 (0)