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 auto-scrolling issue when adding new lines #243

Merged
merged 1 commit into from
Jul 20, 2023
Merged
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
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
Loading