Skip to content

Commit

Permalink
refactor(comments): introduce focus to end of content as explicit fn
Browse files Browse the repository at this point in the history
Make a explicit function to focus the editor at the end of the
edited comment content as focusEditor fn is now used internally
in the context provider.

Also simplify an effect that focuses the component this way.
  • Loading branch information
skogsmaskin committed Sep 27, 2023
1 parent 686d172 commit ec15b9c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CommentInputContextValue {
expandOnFocus?: boolean
focused: boolean
focusEditor: () => void
focusEditorEndOfContent: () => void
focusOnMount?: boolean
hasChanges: boolean
insertAtChar: () => void
Expand Down Expand Up @@ -72,6 +73,32 @@ export function CommentInputProvider(props: CommentInputProviderProps) {

const focusEditor = useCallback(() => PortableTextEditor.focus(editor), [editor])

const focusEditorEndOfContent = useCallback(() => {
const _value = PortableTextEditor.getValue(editor)
const lastBlock = (_value || []).slice(-1)[0]
if (!lastBlock) {
PortableTextEditor.focus(editor)
return
}
const lastChild = isPortableTextTextBlock(lastBlock)
? lastBlock.children.slice(-1)[0]
: undefined
if (!lastChild) {
PortableTextEditor.focus(editor)
return
}
const point = {
path: [{_key: lastBlock._key}, 'children', {_key: lastChild._key}],
offset: isPortableTextSpan(lastChild) ? lastChild.text.length : 0,
}
const newSelection = {
focus: point,
anchor: point,
}
PortableTextEditor.select(editor, newSelection)
PortableTextEditor.focus(editor)
}, [editor])

const closeMentions = useCallback(() => {
setMentionsMenuOpen(false)
setMentionsSearchTerm('')
Expand Down Expand Up @@ -218,6 +245,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {
expandOnFocus,
focused,
focusEditor,
focusEditorEndOfContent,
focusOnMount,
hasChanges,
insertAtChar,
Expand All @@ -236,6 +264,7 @@ export function CommentInputProvider(props: CommentInputProviderProps) {
expandOnFocus,
focused,
focusEditor,
focusEditorEndOfContent,
focusOnMount,
hasChanges,
insertAtChar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {PortableTextEditable, usePortableTextEditorSelection} from '@sanity/port
import styled, {css} from 'styled-components'
import {isEqual} from 'lodash'
import {MentionsMenu, MentionsMenuHandle} from '../../mentions'
import {useDidUpdate} from '../../../../form'
import {renderBlock, renderChild} from '../render'
import {useCommentInput} from './useCommentInput'
import {useCursorElement} from './useCursorElement'
Expand Down Expand Up @@ -58,6 +57,7 @@ export function Editable(props: EditableProps) {
const {
closeMentions,
focusEditor,
focusEditorEndOfContent,
focusOnMount,
insertMention,
mentionOptions,
Expand All @@ -77,11 +77,13 @@ export function Editable(props: EditableProps) {
[popoverElement],
)

useDidUpdate(focusOnMount, () => {
// This effect will focus the editor when the component mounts (if focusOnMount context value is true)
useEffect(() => {
if (focusOnMount) {
focusEditor()
// Focus to the end of the content
focusEditorEndOfContent()
}
})
}, [focusOnMount, focusEditorEndOfContent])

// Update the mentions search term in the mentions menu
useEffect(() => {
Expand Down

0 comments on commit ec15b9c

Please sign in to comment.