Skip to content

Commit

Permalink
fix: 블록 클릭시 클릭한 위치에 캐럿 이동하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludovico7 committed Nov 17, 2024
1 parent 5e7ae04 commit 484140c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
12 changes: 10 additions & 2 deletions client/src/features/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,22 @@ export const Editor = ({ onTitleChange }: EditorProps) => {
onTitleChange(e.target.value);
};

const handleBlockClick = (blockId: BlockId) => {
const handleBlockClick = (blockId: BlockId, e: React.MouseEvent<HTMLDivElement>) => {
const block = editorState.linkedList.getNode(blockId);
if (!block) return;

// 클릭된 요소 내에서의 위치를 가져오기 위해
const range = document.caretRangeFromPoint(e.clientX, e.clientY);
if (!range) return;

const selection = window.getSelection();
if (!selection) return;

// 클릭한 위치의 offset을 currentCaret으로 저장
// 새로운 Range로 Selection 설정
selection.removeAllRanges();
selection.addRange(range);

// 현재 캐럿 위치를 저장
block.crdt.currentCaret = selection.focusOffset;

setEditorState((prev) => ({
Expand Down
9 changes: 2 additions & 7 deletions client/src/features/editor/components/block/Block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface BlockProps {
isActive: boolean;
onInput: (e: React.FormEvent<HTMLDivElement>, blockId: BlockId) => void;
onKeyDown: (e: React.KeyboardEvent<HTMLDivElement>) => void;
onClick: (blockId: BlockId) => void;
onClick: (blockId: BlockId, e: React.MouseEvent<HTMLDivElement>) => void;
}

export const Block: React.FC<BlockProps> = memo(
Expand All @@ -34,11 +34,6 @@ export const Block: React.FC<BlockProps> = memo(
onInput(e, block.id);
};

const handleClick = (e: React.MouseEvent) => {
e.preventDefault();
onClick(block.id);
};

const setFocusAndCursor = () => {
if (blockRef.current && isActive) {
const selection = window.getSelection();
Expand Down Expand Up @@ -79,7 +74,7 @@ export const Block: React.FC<BlockProps> = memo(
ref={blockRef}
onKeyDown={onKeyDown}
onInput={handleInput}
onClick={handleClick}
onClick={(e) => onClick(block.id, e)}
contentEditable
suppressContentEditableWarning
className={textContainerStyle({
Expand Down
2 changes: 1 addition & 1 deletion client/tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit 484140c

Please sign in to comment.