Commit b9b4b56c authored by Robert Knight's avatar Robert Knight

Request annotation focus after selection in FrameSyncService

If the guest has requested it, request keyboard focus for the first
annotation in the selection after a "showAnnotations" message is
received in FrameSyncService.
parent 4534acdc
...@@ -366,9 +366,23 @@ export class FrameSyncService { ...@@ -366,9 +366,23 @@ export class FrameSyncService {
guestRPC.on( guestRPC.on(
'showAnnotations', 'showAnnotations',
/** @param {string[]} tags */ tags => { /**
this._store.selectAnnotations(this._store.findIDsForTags(tags)); * @param {string[]} tags
* @param {boolean} focusFirstInSelection - Whether to give keyboard focus
* to the first annotation in the selection.
*/
(tags, focusFirstInSelection = false) => {
const ids = this._store.findIDsForTags(tags);
this._store.selectAnnotations(ids);
this._store.selectTab('annotation'); this._store.selectTab('annotation');
// Attempt to transfer keyboard focus to the first selected annotation.
// This may be blocked in WebKit-based browsers if the user has not
// previously interacted with the frame.
if (ids.length > 0 && focusFirstInSelection) {
this._store.setAnnotationFocusRequest(ids[0]);
window.focus();
}
} }
); );
......
...@@ -138,6 +138,7 @@ describe('FrameSyncService', () => { ...@@ -138,6 +138,7 @@ describe('FrameSyncService', () => {
openSidebarPanel: sinon.stub(), openSidebarPanel: sinon.stub(),
selectAnnotations: sinon.stub(), selectAnnotations: sinon.stub(),
selectTab: sinon.stub(), selectTab: sinon.stub(),
setAnnotationFocusRequest: sinon.stub(),
setSidebarOpened: sinon.stub(), setSidebarOpened: sinon.stub(),
toggleSelectedAnnotations: sinon.stub(), toggleSelectedAnnotations: sinon.stub(),
updateAnchorStatus: sinon.stub(), updateAnchorStatus: sinon.stub(),
...@@ -663,6 +664,26 @@ describe('FrameSyncService', () => { ...@@ -663,6 +664,26 @@ describe('FrameSyncService', () => {
assert.calledWith(fakeStore.selectAnnotations, ['id1', 'id2', 'id3']); assert.calledWith(fakeStore.selectAnnotations, ['id1', 'id2', 'id3']);
assert.calledWith(fakeStore.selectTab, 'annotation'); assert.calledWith(fakeStore.selectTab, 'annotation');
}); });
it('does not request keyboard focus if `focusFirstInSelection` is false', () => {
fakeStore.findIDsForTags.returns(['id1', 'id2', 'id3']);
emitGuestEvent(
'showAnnotations',
['tag1', 'tag2', 'tag3'],
false /* focus */
);
assert.notCalled(fakeStore.setAnnotationFocusRequest);
});
it('requests keyboard focus for first annotation in selection', () => {
fakeStore.findIDsForTags.returns(['id1', 'id2', 'id3']);
emitGuestEvent(
'showAnnotations',
['tag1', 'tag2', 'tag3'],
true /* focus */
);
assert.calledWith(fakeStore.setAnnotationFocusRequest, 'id1');
});
}); });
describe('on "hoverAnnotations" message', () => { describe('on "hoverAnnotations" message', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment