Skip to content

Commit

Permalink
Scroll to first highlighted annotation when loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Apr 15, 2024
1 parent 05aab05 commit 4466716
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/sidebar/components/ThreadList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ export default function ThreadList({ threads }: ThreadListProps) {
}
}, [store, newAnnotationTag]);

const editing = store.countDrafts() > 0;
const highlightedAnnotations = store.highlightedAnnotations();

// Scroll to the first highlighted annotation, unless creating/editing another
// annotation
useEffect(() => {
const [firstHighlightedAnnotation] = highlightedAnnotations;
if (!editing && firstHighlightedAnnotation) {
setScrollToId(firstHighlightedAnnotation);
}
}, [editing, highlightedAnnotations]);

// Effect to scroll a particular thread into view. This is mainly used to
// scroll a newly created annotation into view.
useEffect(() => {
Expand Down
24 changes: 22 additions & 2 deletions src/sidebar/components/test/ThreadList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '@hypothesis/frontend-testing';
import { mount } from 'enzyme';
import { act } from 'preact/test-utils';
import sinon from 'sinon';

import ThreadList from '../ThreadList';
import { $imports } from '../ThreadList';
Expand Down Expand Up @@ -54,6 +55,8 @@ describe('ThreadList', () => {
fakeStore = {
setForcedVisible: sinon.stub(),
unsavedAnnotations: sinon.stub().returns([]),
countDrafts: sinon.stub().returns(0),
highlightedAnnotations: sinon.stub().returns([]),
};

fakeTopThread = {
Expand Down Expand Up @@ -177,10 +180,27 @@ describe('ThreadList', () => {
addNewAnnotation(wrapper, fakeTopThread.children[3].annotation);

// The third thread in a collection of threads at default height (200)
// should be at 600px. This setting of `scrollTop` is the only externally-
// observable thing that happens here...
// should be at 600px. This setting of `scrollTop` is the only
// externally-observable thing that happens here...
assert.calledWith(fakeScrollTop, 600);
});

it('should do nothing for highlighted annotations while creating/editing', () => {
fakeStore.highlightedAnnotations.returns(['t2', 't3']);
fakeStore.countDrafts.returns(10);
createComponent();

assert.notCalled(fakeScrollTop);
});

it('should set the scroll container `scrollTop` to first highlighted annotation', () => {
fakeStore.highlightedAnnotations.returns(['t2', 't3']);
createComponent();

// The first thread in a collection of threads at default height (200)
// should be at 200px.
assert.calledWith(fakeScrollTop, 200);
});
});

/**
Expand Down

0 comments on commit 4466716

Please sign in to comment.