Skip to content

Commit f5aec1a

Browse files
S0ulDrag0njustinjung04
authored andcommitted
[update] auto notes scrolling (#188)
Updated notes to scroll into view when the related annotation is selected. Only triggers when a single annotation is selected.
1 parent babbc6f commit f5aec1a

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/components/Note/Note.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Note extends React.PureComponent {
1818
annotation: PropTypes.object.isRequired,
1919
isNoteEditing: PropTypes.bool.isRequired,
2020
isNoteExpanded: PropTypes.bool.isRequired,
21+
isAnnotationFocused: PropTypes.bool,
2122
setIsNoteEditing: PropTypes.func.isRequired,
2223
searchInput: PropTypes.string,
2324
isReadOnly: PropTypes.bool,
@@ -35,6 +36,7 @@ class Note extends React.PureComponent {
3536
isRootContentEditing: false,
3637
isReplyFocused: false
3738
};
39+
this.containerRef = React.createRef();
3840
}
3941

4042
componentDidMount() {
@@ -47,6 +49,7 @@ class Note extends React.PureComponent {
4749
const commentEditable = core.canModify(annotation) && !annotation.getContents();
4850
const noteBeingEdited = !prevProps.isNoteEditing && this.props.isNoteEditing;
4951
const noteCollapsed = prevProps.isNoteExpanded && !this.props.isNoteExpanded;
52+
const annotationWasFocused = !prevProps.isAnnotationFocused && this.props.isAnnotationFocused;
5053

5154
if (noteBeingEdited) {
5255
if (commentEditable) {
@@ -62,6 +65,10 @@ class Note extends React.PureComponent {
6265
isReplyFocused: false
6366
});
6467
}
68+
69+
if (annotationWasFocused) {
70+
this.scrollIntoView();
71+
}
6572
}
6673

6774
componentWillUnmount() {
@@ -99,6 +106,10 @@ class Note extends React.PureComponent {
99106
}
100107
}
101108

109+
scrollIntoView = () => {
110+
this.containerRef.current.scrollIntoView();
111+
}
112+
102113
openRootEditing = () => {
103114
this.setState({ isRootContentEditing: true });
104115
}
@@ -205,7 +216,7 @@ class Note extends React.PureComponent {
205216
].join(' ').trim();
206217

207218
return (
208-
<div className={className} onClick={this.onClickNote}>
219+
<div ref={this.containerRef} className={className} onClick={this.onClickNote}>
209220
<NoteRoot
210221
annotation={annotation}
211222
contents={rootContents}
@@ -256,6 +267,7 @@ class Note extends React.PureComponent {
256267
const mapStateToProps = (state, ownProps) => ({
257268
isNoteExpanded: selectors.isNoteExpanded(state, ownProps.annotation.Id),
258269
isNoteEditing: selectors.isNoteEditing(state, ownProps.annotation.Id),
270+
isAnnotationFocused: selectors.isAnnotationFocused(state, ownProps.annotation.Id),
259271
isReadOnly: selectors.isDocumentReadOnly(state),
260272
isReplyDisabled: selectors.isElementDisabled(state, 'noteReply')
261273
});

src/redux/selectors/exposedSelectors.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const getActiveToolGroup = state => state.viewer.activeToolGroup;
3636
export const getNotePopupId = state => state.viewer.notePopupId;
3737
export const isNoteExpanded = (state, id) => !!state.viewer.expandedNotes[id];
3838
export const isNoteEditing = (state, id) => state.viewer.isNoteEditing && isNoteExpanded(state, id);
39+
export const isAnnotationFocused = (state, id) => Object.keys(state.viewer.expandedNotes).length === 1 && isNoteExpanded(state, id); // Considered focused when it is the only annotation selected
3940
export const getFitMode = state => state.viewer.fitMode;
4041
export const getZoom = state => state.viewer.zoom;
4142
export const getDisplayMode = state => state.viewer.displayMode;

0 commit comments

Comments
 (0)