-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(comments): only calculate current caret element on demand (#4935)
- Loading branch information
1 parent
36c982c
commit f544ede
Showing
4 changed files
with
43 additions
and
96 deletions.
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
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
29 changes: 29 additions & 0 deletions
29
packages/sanity/src/core/comments/components/pte/comment-input/getCaretElement.ts
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,29 @@ | ||
export function getCaretElement(rootElement: HTMLElement | null): HTMLElement | null { | ||
const selection = window.getSelection() | ||
|
||
if (!selection || selection.type !== 'Caret') return null | ||
|
||
const selectionRange = selection.getRangeAt(0) | ||
const isWithinRoot = rootElement?.contains(selectionRange.commonAncestorContainer) | ||
|
||
if (!isWithinRoot) return null | ||
|
||
const {anchorNode, focusNode} = selection | ||
|
||
if (rootElement?.contains(anchorNode) && anchorNode === focusNode) { | ||
try { | ||
const range = window.getSelection()?.getRangeAt(0) | ||
const rect = range?.getBoundingClientRect() | ||
if (rect) { | ||
const element = { | ||
getBoundingClientRect: () => rect, | ||
} as HTMLElement | ||
return element | ||
} | ||
} catch (_) { | ||
return null | ||
} | ||
} | ||
|
||
return null | ||
} |
86 changes: 0 additions & 86 deletions
86
packages/sanity/src/core/comments/components/pte/comment-input/useCursorElement.ts
This file was deleted.
Oops, something went wrong.