Skip to content

Commit fe554b9

Browse files
authored
Merge pull request #10409 from ckeditor/ck/10012-hit-enter-to-find-next
Feature (find-and-replace): Made it possible to cycle find results by using <kbd>Enter</kbd> and <kbd>Shift</kbd>+<kbd>Enter</kbd> keystrokes. Closes #10012.
2 parents f6f5fa5 + c806b16 commit fe554b9

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,14 +719,35 @@ 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+
return;
740+
}
741+
742+
if ( this._areCommandsEnabled.findPrevious ) {
743+
this._findPrevButtonView.fire( 'execute' );
744+
} else {
745+
this._findButtonView.fire( 'execute' );
746+
}
747+
748+
stopPropagationAndPreventDefault( event );
749+
} );
750+
730751
// Since the form is in the dropdown panel which is a child of the toolbar, the toolbar's
731752
// keystroke handler would take over the key management in the URL input.
732753
// 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)