From be88b84cd25635d9b41e90c0987df761d8b99c99 Mon Sep 17 00:00:00 2001 From: Ludovico7 Date: Thu, 5 Dec 2024 23:38:02 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=B8=8C=EB=9D=BC=EC=9A=B0=EC=A0=80=20u?= =?UTF-8?q?ncaught=20error=20=EC=95=88=EB=82=98=EC=98=A4=EB=8F=84=EB=A1=9D?= =?UTF-8?q?=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jang seo yun Co-authored-by: hyonun --- client/src/utils/caretUtils.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/client/src/utils/caretUtils.ts b/client/src/utils/caretUtils.ts index 42d47f4e..ece233e5 100644 --- a/client/src/utils/caretUtils.ts +++ b/client/src/utils/caretUtils.ts @@ -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();