Skip to content

Commit

Permalink
fix(comments): only calculate current caret element on demand (#4935)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon authored and hermanwikner committed Oct 30, 2023
1 parent be4ebc0 commit cd33361
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import {
PortableTextEditor,
usePortableTextEditor,
} from '@sanity/portable-text-editor'
import React, {FormEventHandler, useCallback, useMemo, useState} from 'react'
import React, {FormEventHandler, startTransition, useCallback, useMemo, useState} from 'react'
import {Path, isPortableTextSpan, isPortableTextTextBlock} from '@sanity/types'
import {CommentMessage} from '../../../types'
import {useDidUpdate} from '../../../../form'
import {useCommentHasChanged} from '../../../helpers'
import {MentionOptionsHookValue} from '../../../hooks'
import {getCaretElement} from './getCaretElement'

type FIXME = any

export interface CommentInputContextValue {
activeCaretElement?: HTMLElement | null
canSubmit?: boolean
closeMentions: () => void
editor: PortableTextEditor
Expand Down Expand Up @@ -58,6 +60,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {

const editor = usePortableTextEditor()

const [activeCaretElement, setActiveCaretElement] = useState<HTMLElement | null>(null)
const [mentionsMenuOpen, setMentionsMenuOpen] = useState<boolean>(false)
const [selectionAtMentionInsert, setSelectionAtMentionInsert] = useState<EditorSelection>(null)

Expand Down Expand Up @@ -100,7 +103,10 @@ export function CommentInputProvider(props: CommentInputProviderProps) {
const onBeforeInput = useCallback(
(event: FIXME): void => {
if (event.inputType === 'insertText' && event.data === '@') {
setMentionsMenuOpen(true)
const element = getCaretElement(event.target)
setActiveCaretElement(element)
startTransition(() => setMentionsMenuOpen(true))

setSelectionAtMentionInsert(PortableTextEditor.getSelection(editor))
}
},
Expand All @@ -109,6 +115,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {

const closeMentions = useCallback(() => {
if (!mentionsMenuOpen) return
setActiveCaretElement(null)
setMentionsMenuOpen(false)
focusEditor()
setSelectionAtMentionInsert(null)
Expand Down Expand Up @@ -181,6 +188,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {
const ctxValue = useMemo(
() =>
({
activeCaretElement,
canSubmit,
closeMentions,
editor,
Expand All @@ -198,6 +206,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {
mentionOptions,
}) satisfies CommentInputContextValue,
[
activeCaretElement,
canSubmit,
closeMentions,
editor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {MentionsMenu} from '../../mentions'
import {useDidUpdate} from '../../../../form'
import {renderBlock, renderChild} from '../render'
import {useCommentInput} from './useCommentInput'
import {useCursorElement} from './useCursorElement'

const INLINE_STYLE: React.CSSProperties = {outline: 'none'}

Expand Down Expand Up @@ -53,6 +52,7 @@ export function Editable(props: EditableProps) {
const editableRef = useRef<HTMLDivElement | null>(null)

const {
activeCaretElement,
closeMentions,
expanded,
focusEditor,
Expand All @@ -63,11 +63,6 @@ export function Editable(props: EditableProps) {
onBeforeInput,
} = useCommentInput()

const cursorElement = useCursorElement({
disabled: mentionsMenuOpen,
rootElement: editableRef.current,
})

const renderPlaceholder = useCallback(() => <span>{placeholder}</span>, [placeholder])

useGlobalKeyDown(
Expand Down Expand Up @@ -100,7 +95,7 @@ export function Editable(props: EditableProps) {
<PortalProvider element={rootElementRef.current}>
<StyledPopover
constrainSize
hidden={!mentionsMenuOpen || !cursorElement}
disabled={!mentionsMenuOpen}
content={
<MentionsMenu
loading={mentionOptions.loading}
Expand All @@ -113,7 +108,7 @@ export function Editable(props: EditableProps) {
placement="bottom-start"
portal
ref={setPopoverElement}
referenceElement={cursorElement}
referenceElement={activeCaretElement}
/>
</PortalProvider>

Expand Down
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
}

This file was deleted.

0 comments on commit cd33361

Please sign in to comment.