Skip to content

Commit e93b51e

Browse files
committed
Hit Enter to find next and Shift+Enter to find previous
1 parent f6f5fa5 commit e93b51e

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

packages/ckeditor5-find-and-replace/src/ui/findandreplaceformview.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,14 +719,32 @@ export default class FindAndReplaceFormView extends View {
719719
const target = event.target;
720720

721721
if ( target === this._findInputView.fieldView.element ) {
722-
this._findButtonView.fire( 'execute' );
722+
if ( this._areCommandsEnabled.findNext ) {
723+
this._findNextButtonView.fire( 'execute' );
724+
} else {
725+
this._findButtonView.fire( 'execute' );
726+
}
723727
stopPropagationAndPreventDefault( event );
724728
} else if ( target === this._replaceInputView.fieldView.element ) {
725729
this._replaceButtonView.fire( 'execute' );
726730
stopPropagationAndPreventDefault( event );
727731
}
728732
} );
729733

734+
// Find previous upon pressing Shift+Enter in the find field.
735+
this._keystrokes.set( 'shift+enter', event => {
736+
const target = event.target;
737+
738+
if ( target === this._findInputView.fieldView.element ) {
739+
if ( this._areCommandsEnabled.findPrevious ) {
740+
this._findPrevButtonView.fire( 'execute' );
741+
} else {
742+
this._findButtonView.fire( 'execute' );
743+
}
744+
stopPropagationAndPreventDefault( event );
745+
}
746+
} );
747+
730748
// Since the form is in the dropdown panel which is a child of the toolbar, the toolbar's
731749
// keystroke handler would take over the key management in the URL input.
732750
// We need to prevent this ASAP. Otherwise, the basic caret movement using the arrow keys will be impossible.

packages/ckeditor5-find-and-replace/tests/ui/findandreplaceformview.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,69 @@ describe( 'FindAndReplaceFormView', () => {
556556
sinon.assert.calledOnceWithExactly( spy, 'execute' );
557557
} );
558558

559+
it( 'handles "enter" when pressed in the find input and goes to the next result', () => {
560+
const keyEvtData = {
561+
keyCode: keyCodes.enter,
562+
preventDefault: sinon.spy(),
563+
stopPropagation: sinon.spy(),
564+
target: view._findInputView.fieldView.element
565+
};
566+
567+
view._areCommandsEnabled = { findNext: true };
568+
569+
const spy = sinon.spy( view._findNextButtonView, 'fire' );
570+
571+
view._keystrokes.press( keyEvtData );
572+
573+
sinon.assert.calledOnce( keyEvtData.preventDefault );
574+
sinon.assert.calledOnce( keyEvtData.stopPropagation );
575+
576+
sinon.assert.calledOnce( spy );
577+
sinon.assert.calledOnceWithExactly( spy, 'execute' );
578+
} );
579+
580+
it( 'handles "shift+enter" when pressed in the find input and performs a search', () => {
581+
const keyEvtData = {
582+
keyCode: keyCodes.enter,
583+
shiftKey: true,
584+
preventDefault: sinon.spy(),
585+
stopPropagation: sinon.spy(),
586+
target: view._findInputView.fieldView.element
587+
};
588+
589+
const spy = sinon.spy( view._findButtonView, 'fire' );
590+
591+
view._keystrokes.press( keyEvtData );
592+
593+
sinon.assert.calledOnce( keyEvtData.preventDefault );
594+
sinon.assert.calledOnce( keyEvtData.stopPropagation );
595+
596+
sinon.assert.calledOnce( spy );
597+
sinon.assert.calledOnceWithExactly( spy, 'execute' );
598+
} );
599+
600+
it( 'handles "shift+enter" when pressed in the find input and goes to the previous result', () => {
601+
const keyEvtData = {
602+
keyCode: keyCodes.enter,
603+
shiftKey: true,
604+
preventDefault: sinon.spy(),
605+
stopPropagation: sinon.spy(),
606+
target: view._findInputView.fieldView.element
607+
};
608+
609+
view._areCommandsEnabled = { findPrevious: true };
610+
611+
const spy = sinon.spy( view._findPrevButtonView, 'fire' );
612+
613+
view._keystrokes.press( keyEvtData );
614+
615+
sinon.assert.calledOnce( keyEvtData.preventDefault );
616+
sinon.assert.calledOnce( keyEvtData.stopPropagation );
617+
618+
sinon.assert.calledOnce( spy );
619+
sinon.assert.calledOnceWithExactly( spy, 'execute' );
620+
} );
621+
559622
it( 'handles "enter" when pressed in the replace input and performs a replacement', () => {
560623
const keyEvtData = {
561624
keyCode: keyCodes.enter,
@@ -590,6 +653,23 @@ describe( 'FindAndReplaceFormView', () => {
590653
sinon.assert.notCalled( keyEvtData.stopPropagation );
591654
sinon.assert.notCalled( spy );
592655
} );
656+
657+
it( 'ignores "shift+enter" when pressed somewhere else', () => {
658+
const keyEvtData = {
659+
keyCode: keyCodes.enter,
660+
shiftKey: true,
661+
preventDefault: sinon.spy(),
662+
stopPropagation: sinon.spy()
663+
};
664+
665+
const spy = sinon.spy( view._replaceButtonView, 'fire' );
666+
667+
view._keystrokes.press( keyEvtData );
668+
669+
sinon.assert.notCalled( keyEvtData.preventDefault );
670+
sinon.assert.notCalled( keyEvtData.stopPropagation );
671+
sinon.assert.notCalled( spy );
672+
} );
593673
} );
594674
} );
595675

0 commit comments

Comments
 (0)