Skip to content
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

fix(core): portable text annotations slow to show #7588

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@
[getComment, onCommentsOpen, scrollToComment, setSelectedPath],
)

const blurred = useRef<boolean>(false)

const handleSelectionChange = useCallback(
(selection: EditorSelection | null) => {
const isRangeSelected = selection?.anchor.offset !== selection?.focus.offset
Expand All @@ -234,7 +236,15 @@
setCanSubmit(false)
return
}

/**
* When the portable text editor loses focus we will save the blurred.current to true.
* Later, when it restores focus the editor will emit a selection change event, but we don't want to immediately show the comment input
* instead, we want to wait until the user selects a new range.
*/
if (blurred.current) {
blurred.current = false
return
}
// If the mouse is not down, we want to set the current selection rect
// when the selection changes. Otherwise, we want to wait until the mouse
// is up to set the current selection rect (see `handleMouseUp`).
Expand All @@ -249,7 +259,7 @@
)

const debounceSelectionChange = useMemo(
() => debounce(handleSelectionChange, 200),

Check warning on line 262 in packages/sanity/src/core/comments/plugin/input/components/CommentsPortableTextInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef)
[handleSelectionChange],
)

Expand Down Expand Up @@ -387,6 +397,9 @@
if (change.type === 'mutation') {
updateCommentRange()
}
if (change.type === 'blur') {
blurred.current = true
}
if (change.type === 'selection') {
debounceSelectionChange(change.selection)
}
Expand Down Expand Up @@ -425,7 +438,7 @@
const currentSelectionIsOverlapping = useMemo(() => {
if (!currentSelection || addedCommentsDecorations.length === 0) return false

return addedCommentsDecorations.some((d) => {

Check warning on line 441 in packages/sanity/src/core/comments/plugin/input/components/CommentsPortableTextInput.tsx

View workflow job for this annotation

GitHub Actions / lint

Ref values (the `current` property) may not be accessed during render. (https://react.dev/reference/react/useRef)
if (!editorRef.current) return false

const testA = PortableTextEditor.isSelectionsOverlapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export const DefaultAnnotationComponent = (props: BlockAnnotationProps) => {
const hasError = validation.some((v) => v.level === 'error')
const hasWarning = validation.some((v) => v.level === 'warning')
const hasMarkers = markers.length > 0
const isReady = Boolean(children)

const {t} = useTranslation()
const toneKey = useMemo(() => {
Expand Down Expand Up @@ -296,18 +297,22 @@ export const DefaultAnnotationComponent = (props: BlockAnnotationProps) => {
onClick={readOnly ? onOpen : undefined}
>
{textElement}
<AnnotationToolbarPopover
annotationOpen={open}
floatingBoundary={floatingBoundary}
onOpen={onOpen}
onRemove={onRemove}
referenceBoundary={referenceBoundary}
referenceElement={referenceElement}
selected={selected}
title={
schemaType.i18nTitleKey ? t(schemaType.i18nTitleKey) : schemaType.title || schemaType.name
}
/>
{isReady && (
<AnnotationToolbarPopover
annotationOpen={open}
floatingBoundary={floatingBoundary}
onOpen={onOpen}
onRemove={onRemove}
referenceBoundary={referenceBoundary}
referenceElement={referenceElement}
selected={selected}
title={
schemaType.i18nTitleKey
? t(schemaType.i18nTitleKey)
: schemaType.title || schemaType.name
}
/>
)}
{open && (
<ObjectEditModal
defaultType="popover"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ function Content(props: PopoverEditDialogProps) {
const [contentElement, setContentElement] = useState<HTMLDivElement | null>(null)
const containerElement = useRef<HTMLDivElement | null>(null)

useEffect(() => {
// When rendered, focus on the first input element in the content
if (contentElement) {
contentElement.querySelector('input')?.focus()
}
}, [contentElement])

return (
<VirtualizerScrollInstanceProvider
scrollElement={contentElement}
Expand Down
Loading