Commit e938f0f3 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner

Add save-with-key-combo support to `AnnotationOmega`

Resolves #1838
parent 5407f085
...@@ -99,10 +99,23 @@ function AnnotationOmega({ ...@@ -99,10 +99,23 @@ function AnnotationOmega({
} }
}; };
// Allow saving of annotation by pressing CMD/CTRL-Enter
const onKeyDown = event => {
if (isEmpty || !isEditing) {
return;
}
if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
event.stopPropagation();
event.preventDefault();
onSave();
}
};
const onToggleReplies = () => setCollapsed(annotation.id, !threadIsCollapsed); const onToggleReplies = () => setCollapsed(annotation.id, !threadIsCollapsed);
return ( return (
<div className="annotation-omega"> /* eslint-disable-next-line jsx-a11y/no-static-element-interactions */
<div className="annotation-omega" onKeyDown={onKeyDown}>
<AnnotationHeader <AnnotationHeader
annotation={annotation} annotation={annotation}
isEditing={isEditing} isEditing={isEditing}
......
...@@ -4,6 +4,7 @@ import { act } from 'preact/test-utils'; ...@@ -4,6 +4,7 @@ import { act } from 'preact/test-utils';
import * as fixtures from '../../test/annotation-fixtures'; import * as fixtures from '../../test/annotation-fixtures';
import { checkAccessibility } from '../../../test-util/accessibility';
import mockImportedComponents from '../../../test-util/mock-imported-components'; import mockImportedComponents from '../../../test-util/mock-imported-components';
import { waitFor } from '../../../test-util/wait'; import { waitFor } from '../../../test-util/wait';
...@@ -230,6 +231,79 @@ describe('AnnotationOmega', () => { ...@@ -230,6 +231,79 @@ describe('AnnotationOmega', () => {
await waitFor(() => fakeFlash.error.called); await waitFor(() => fakeFlash.error.called);
}); });
describe('saving using shortcut-key combo', () => {
context('in editing mode with text or tag content populated', () => {
beforeEach(() => {
// Put into editing mode by presence of draft, and add some `text`
// so that the annotation is not seen as "empty"
const draft = fixtures.defaultDraft();
draft.text = 'bananas';
fakeStore.getDraft.returns(draft);
});
it('should save annotation if `CTRL+Enter` is typed', () => {
const wrapper = createComponent();
wrapper
.find('.annotation-omega')
.simulate('keydown', { key: 'Enter', ctrlKey: true });
assert.calledWith(
fakeAnnotationsService.save,
wrapper.props().annotation
);
});
it('should save annotation if `META+Enter` is typed', () => {
const wrapper = createComponent();
wrapper
.find('.annotation-omega')
.simulate('keydown', { key: 'Enter', metaKey: true });
assert.calledWith(
fakeAnnotationsService.save,
wrapper.props().annotation
);
});
it('should not save annotation if `META+g` is typed', () => {
// i.e. don't save on non-"Enter" keys
const wrapper = createComponent();
wrapper
.find('.annotation-omega')
.simulate('keydown', { key: 'g', metaKey: true });
assert.notCalled(fakeAnnotationsService.save);
});
});
context('empty or not in editing mode', () => {
it('should not save annotation if not in editing mode', () => {
fakeStore.getDraft.returns(null);
const wrapper = createComponent();
wrapper
.find('.annotation-omega')
.simulate('keydown', { key: 'Enter', metaKey: true });
assert.notCalled(fakeAnnotationsService.save);
});
it('should not save annotation if content is empty', () => {
fakeStore.getDraft.returns(fixtures.defaultDraft());
const wrapper = createComponent();
wrapper
.find('.annotation-omega')
.simulate('keydown', { key: 'Enter', ctrlKey: true });
assert.notCalled(fakeAnnotationsService.save);
});
});
});
}); });
}); });
...@@ -391,5 +465,21 @@ describe('AnnotationOmega', () => { ...@@ -391,5 +465,21 @@ describe('AnnotationOmega', () => {
assert.isFalse(wrapper.find('AnnotationActionBar').exists()); assert.isFalse(wrapper.find('AnnotationActionBar').exists());
}); });
it(
'should pass a11y checks',
checkAccessibility([
{
content: () => createComponent(),
},
{
name: 'When editing',
content: () => {
setEditingMode(true);
return createComponent();
},
},
])
);
}); });
}); });
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