Skip to content

Commit 3e39948

Browse files
committed
[#131] refactor: focus 관련 로직 분할
1 parent 3a62b6e commit 3e39948

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

frontend/src/hooks/useCommand.tsx

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useRecoilState } from 'recoil';
2-
import { focusState, blockRefState } from '@/stores';
2+
import { focusState } from '@/stores';
33
import { useManager } from '@/hooks';
4-
import { Block, BlockType } from '@/schemes';
4+
import { BlockType } from '@/schemes';
55

66
const useCommand = () => {
7-
const [focusId, setFocusId] = useRecoilState(focusState);
7+
const [focusId] = useRecoilState(focusState);
88
const [
9-
{ block, blockIndex, siblingsIdList, parent, grandParent, blockMap },
9+
{ block, blockIndex, siblingsIdList, grandParent },
1010
{
1111
getPrevBlock,
1212
getNextBlock,
@@ -19,30 +19,11 @@ const useCommand = () => {
1919
startTransaction,
2020
commitTransaction,
2121
deleteBlock,
22+
setFocus,
23+
setCaretOffset,
2224
},
2325
] = useManager(focusId);
2426

25-
const setFocus = (targetBlock: Block) => {
26-
if (!targetBlock) {
27-
return null;
28-
}
29-
const beforeOffset = window.getSelection().focusOffset;
30-
setFocusId(targetBlock.id);
31-
const targetRef = blockRefState[targetBlock.id];
32-
targetRef
33-
? targetRef.current.focus()
34-
: blockRefState[block.id].current.blur();
35-
return beforeOffset;
36-
};
37-
38-
const setCaretOffset = (offset: number) => {
39-
const sel = window.getSelection();
40-
const { focusNode: node } = sel;
41-
const { length } = node as any;
42-
!(node instanceof HTMLElement) &&
43-
sel.collapse(node, offset > length ? length : offset);
44-
};
45-
4627
const getSlicedValueToCaretOffset = () => {
4728
const { focusNode, anchorOffset, focusOffset } = window.getSelection();
4829
return [

0 commit comments

Comments
 (0)