Skip to content

Commit

Permalink
edit note screen manual scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
yashb196 committed Dec 5, 2023
1 parent 46d2e2f commit 32ebd94
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions lib/screens/EditNoteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,18 @@ const EditNoteScreen: React.FC<EditNoteScreenProps> = ({
checkOwner();
}, [creator]);

const handleScroll = (positionY: number) => {
if (scrollViewRef.current) {
const handleScroll = (position) => {
if (keyboardOpen && scrollViewRef.current) {
const viewportHeight = Dimensions.get('window').height - keyboardHeight;

const scrollToY = positionY - (viewportHeight / 2);

scrollViewRef.current.scrollTo({
y: Math.max(scrollToY, 0),
animated: true,
});
const cursorRelativePosition = position.relativeY;
const spaceBelowCursor = viewportHeight - cursorRelativePosition;

if (spaceBelowCursor < keyboardHeight) {
scrollViewRef.current.scrollTo({
y: position.absoluteY - spaceBelowCursor + keyboardHeight,
animated: true,
});
}
}
};

Expand Down Expand Up @@ -143,16 +145,10 @@ const EditNoteScreen: React.FC<EditNoteScreenProps> = ({

// Add a delay before updating the text state
setTimeout(() => {
if (richTextRef.current) {
richTextRef.current.getContentHtml()
.then(html => {
setText(html); // Update the state with the latest content
})
.catch(error => {
console.error('Error getting content from RichEditor:', error);
});
if (scrollViewRef.current) {
scrollViewRef.current.scrollToEnd({ animated: true });
}
}, 100); // Adjust the delay as needed
}, 500); // Adjust the delay as needed
};

const handleSaveNote = async () => {
Expand Down Expand Up @@ -356,9 +352,7 @@ const EditNoteScreen: React.FC<EditNoteScreenProps> = ({
placeholder="Write your note here"
onChange={(text) => setText(text)}
initialContentHTML={text}
onCursorPosition={(position) => {
handleScroll(position);
}}
onCursorPosition={handleScroll}
disabled={!owner}
/>
</ScrollView>
Expand Down

0 comments on commit 32ebd94

Please sign in to comment.