-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/#175 캐럿 관리방식 변경 #176
Merged
The head ref may contain hidden characters: "Feature/#175_\uCE90\uB7FF_\uAD00\uB9AC\uBC29\uC2DD_\uBCC0\uACBD"
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9073fea
feat: localUpdate함수 작성
Ludovico7 2ba1b3d
chore: 불필요한 console.log 제거 및 import 순서 변경
Ludovico7 f9b95b6
feat: 캐럿 관리 유틸리티 함수 구현
Ludovico7 e6a9a31
feat: editorState 및 캐럿 관리 구조 변경
Ludovico7 ec34926
feat: block 캐럿 관리 구조 변경
Ludovico7 e62f1de
feat: 키입력 핸들러 구조 수정
Ludovico7 1af8fcd
Merge branch 'dev' into Feature/#175_캐럿_관리방식_변경
Ludovico7 16aa9c5
feat: editorState 데이터 구조 변경된 내용 반영
Ludovico7 f5c9221
feat: 라벨 이름 변경
Ludovico7 400be0a
feat: 블록 첫 추가 버튼 블록 없을때만 보이도록 수정
Ludovico7 8f493b4
chore: lint 설정
Ludovico7 5434d30
Merge branch 'dev' into Feature/#175_캐럿_관리방식_변경
Ludovico7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { BlockLinkedList, TextLinkedList } from "@noctaCrdt/LinkedList"; | ||
import { BlockId } from "@noctaCrdt/NodeId"; | ||
|
||
interface SetCaretPositionProps { | ||
blockId: BlockId; | ||
linkedList: BlockLinkedList | TextLinkedList; | ||
clientX?: number; | ||
clientY?: number; | ||
position?: number; // 특정 위치로 캐럿을 설정하고 싶을 때 사용 | ||
} | ||
|
||
export const setCaretPosition = ({ | ||
blockId, | ||
linkedList, | ||
clientX, | ||
clientY, | ||
position, | ||
}: SetCaretPositionProps): boolean => { | ||
try { | ||
const selection = window.getSelection(); | ||
if (!selection) return false; | ||
|
||
const blockElements = Array.from( | ||
document.querySelectorAll('.d_flex.pos_relative.w_full[data-group="true"]'), | ||
); | ||
const currentIndex = linkedList.spread().findIndex((b) => b.id === blockId); | ||
const targetElement = blockElements[currentIndex]; | ||
if (!targetElement) return false; | ||
|
||
const editableDiv = targetElement.querySelector('[contenteditable="true"]') as HTMLDivElement; | ||
if (!editableDiv) return false; | ||
|
||
editableDiv.focus(); | ||
|
||
let range: Range; | ||
|
||
if (clientX !== undefined && clientY !== undefined) { | ||
// 클릭 위치에 따른 캐럿 설정 | ||
const clickRange = document.caretRangeFromPoint(clientX, clientY); | ||
if (!clickRange) return false; | ||
range = clickRange; | ||
} else if (position !== undefined) { | ||
// 특정 위치에 캐럿 설정 | ||
range = document.createRange(); | ||
const textNode = | ||
Array.from(editableDiv.childNodes).find((node) => node.nodeType === Node.TEXT_NODE) || null; | ||
if (!textNode) { | ||
// 텍스트 노드가 없으면 새로운 텍스트 노드를 추가 | ||
const newTextNode = document.createTextNode(""); | ||
editableDiv.appendChild(newTextNode); | ||
range.setStart(newTextNode, 0); | ||
} else { | ||
// position이 텍스트 길이를 초과하지 않도록 조정 | ||
const safePosition = Math.min(position, textNode.textContent?.length || 0); | ||
range.setStart(textNode, safePosition); | ||
} | ||
|
||
range.collapse(true); | ||
} else { | ||
return false; | ||
} | ||
|
||
selection.removeAllRanges(); | ||
selection.addRange(range); | ||
|
||
return true; | ||
} catch (error) { | ||
console.error("Error setting caret position:", error); | ||
return false; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이부분이 브라우저의 에디터 요소를 찾는 구문이군요..
기존에 key입력마다 처리하는 방법에서 브라우저를 활용한다면 더욱 편하게 관리할 수 있을 듯 합니다.
다만 몇개 버그를 찾은 것 같습니다!
12321.mp4
위와같은 현상은 아직 page별로 캐럿이 제대로 관리되지 않아서 생기는 현상일까요?
아마 이전에 focus된 캐럿으로 다른 page를 눌러도 새로운 page로 캐럿위치가 업데이트 되지 않는것 같군요..