Skip to content

Commit

Permalink
fix: 브라우저 uncaught error 안나오도록 해결
Browse files Browse the repository at this point in the history
Co-authored-by: Jang seo yun <[email protected]>
Co-authored-by: hyonun <[email protected]>
  • Loading branch information
3 people committed Dec 5, 2024
1 parent 4d11247 commit be88b84
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion client/src/utils/caretUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,19 @@ export const setCaretPosition = ({
}

const range = document.createRange();
range.setStart(targetNode, targetOffset);
try {
range.setStart(targetNode, targetOffset);
} catch (rangeError) {
// setStart에 실패한 경우, 첫 번째 텍스트 노드를 찾아서 position 위치에 캐럿 설정
const firstTextNode = walker.firstChild();
if (firstTextNode) {
const textLength = firstTextNode.textContent?.length || 0;
range.setStart(firstTextNode, Math.min(position, textLength));
} else {
// 텍스트 노드가 없는 경우 편집 가능한 div의 시작점에 설정
range.setStart(editableDiv, 0);
}
}
range.collapse(true);

selection.removeAllRanges();
Expand Down

0 comments on commit be88b84

Please sign in to comment.