Skip to content

Commit

Permalink
bug: Fix auto-scrolling issue when adding new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Jul 20, 2023
1 parent f05fbab commit 9026114
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/app/src/components/commons/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const Editor: React.FC = () => {
setContentImageFiles,
setLinkComponentUrl,
} = useArticleContext()
const editorContainer = useRef<HTMLDivElement>(null)
const navigate = useNavigate()
const linkDecorator = useLinkDecorator()
const decorators = new CompositeDecorator(linkDecorator)
Expand Down Expand Up @@ -217,6 +218,23 @@ const Editor: React.FC = () => {
}
}, [editorState, setShowInlinePopup])

useEffect(() => {
scrollToBottom()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [editorState])

// go to the end of the text editor
const scrollToBottom = () => {
const currentSelection = editorState.getSelection()
const anchorKey = currentSelection.getAnchorKey()
const currentContent = editorState.getCurrentContent()
const currentBlock = currentContent.getBlockForKey(anchorKey)

//check if the current blocks is the last
if (currentBlock === currentContent.getLastBlock()) {
editorContainer.current?.scrollIntoView({ behavior: "smooth", block: "end" })
}
}
const handleState = (editorState: EditorState) => {
const finalEditorState = handleSlashCommand(editorState)
setEditorState(finalEditorState)
Expand Down Expand Up @@ -322,7 +340,7 @@ const Editor: React.FC = () => {
}

return (
<Box>
<Box ref={editorContainer}>
<DraftEditor
ref={editor}
editorState={editorState}
Expand Down

0 comments on commit 9026114

Please sign in to comment.