8
0
Просмотр исходного кода

Merge pull request #77 from ckeditor/t/ckeditor5/447

Feature: List feature should use `EditingKeystrokeHandler` instead of direct event listeners. Closes #76.
Piotrek Koszuliński 8 лет назад
Родитель
Сommit
2073b60c04
2 измененных файлов с 21 добавлено и 23 удалено
  1. 8 19
      packages/ckeditor5-list/src/list.js
  2. 13 4
      packages/ckeditor5-list/tests/list.js

+ 8 - 19
packages/ckeditor5-list/src/list.js

@@ -13,7 +13,6 @@ import numberedListIcon from '../theme/icons/numberedlist.svg';
 import bulletedListIcon from '../theme/icons/bulletedlist.svg';
 import bulletedListIcon from '../theme/icons/bulletedlist.svg';
 
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
-import { parseKeystroke } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
 
 
 /**
 /**
@@ -62,29 +61,19 @@ export default class List extends Plugin {
 			}
 			}
 		} );
 		} );
 
 
-		// Add Tab key support.
-		// When in list item, pressing Tab should indent list item, if possible.
-		// Pressing Shift+Tab should outdent list item.
-		this.listenTo( this.editor.editing.view, 'keydown', ( evt, data ) => {
-			let commandName;
-
-			if ( data.keystroke == parseKeystroke( 'Tab' ) ) {
-				commandName = 'indentList';
-			} else if ( data.keystroke == parseKeystroke( 'Shift+Tab' ) ) {
-				commandName = 'outdentList';
-			}
-
-			if ( commandName ) {
+		const getCommandExecuter = commandName => {
+			return ( data, cancel ) => {
 				const command = this.editor.commands.get( commandName );
 				const command = this.editor.commands.get( commandName );
 
 
 				if ( command.isEnabled ) {
 				if ( command.isEnabled ) {
 					this.editor.execute( commandName );
 					this.editor.execute( commandName );
-
-					data.preventDefault();
-					evt.stop();
+					cancel();
 				}
 				}
-			}
-		} );
+			};
+		};
+
+		this.editor.keystrokes.set( 'Tab', getCommandExecuter( 'indentList' ) );
+		this.editor.keystrokes.set( 'Shift+Tab', getCommandExecuter( 'outdentList' ) );
 	}
 	}
 
 
 	/**
 	/**

+ 13 - 4
packages/ckeditor5-list/tests/list.js

@@ -121,8 +121,9 @@ describe( 'List', () => {
 
 
 		beforeEach( () => {
 		beforeEach( () => {
 			domEvtDataStub = {
 			domEvtDataStub = {
-				keystroke: getCode( 'Tab' ),
-				preventDefault() {}
+				keyCode: getCode( 'Tab' ),
+				preventDefault: sinon.spy(),
+				stopPropagation: sinon.spy()
 			};
 			};
 
 
 			sinon.spy( editor, 'execute' );
 			sinon.spy( editor, 'execute' );
@@ -143,10 +144,12 @@ describe( 'List', () => {
 
 
 			expect( editor.execute.calledOnce ).to.be.true;
 			expect( editor.execute.calledOnce ).to.be.true;
 			expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
 			expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
+			sinon.assert.calledOnce( domEvtDataStub.preventDefault );
+			sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
 		} );
 		} );
 
 
 		it( 'should execute outdentList command on Shift+Tab keystroke', () => {
 		it( 'should execute outdentList command on Shift+Tab keystroke', () => {
-			domEvtDataStub.keystroke += getCode( 'Shift' );
+			domEvtDataStub.keyCode += getCode( 'Shift' );
 
 
 			setData(
 			setData(
 				doc,
 				doc,
@@ -158,6 +161,8 @@ describe( 'List', () => {
 
 
 			expect( editor.execute.calledOnce ).to.be.true;
 			expect( editor.execute.calledOnce ).to.be.true;
 			expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
 			expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
+			sinon.assert.calledOnce( domEvtDataStub.preventDefault );
+			sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
 		} );
 		} );
 
 
 		it( 'should not indent if command is disabled', () => {
 		it( 'should not indent if command is disabled', () => {
@@ -166,10 +171,12 @@ describe( 'List', () => {
 			editor.editing.view.fire( 'keydown', domEvtDataStub );
 			editor.editing.view.fire( 'keydown', domEvtDataStub );
 
 
 			expect( editor.execute.called ).to.be.false;
 			expect( editor.execute.called ).to.be.false;
+			sinon.assert.notCalled( domEvtDataStub.preventDefault );
+			sinon.assert.notCalled( domEvtDataStub.stopPropagation );
 		} );
 		} );
 
 
 		it( 'should not indent or outdent if alt+tab is pressed', () => {
 		it( 'should not indent or outdent if alt+tab is pressed', () => {
-			domEvtDataStub.keystroke += getCode( 'alt' );
+			domEvtDataStub.keyCode += getCode( 'alt' );
 
 
 			setData(
 			setData(
 				doc,
 				doc,
@@ -180,6 +187,8 @@ describe( 'List', () => {
 			editor.editing.view.fire( 'keydown', domEvtDataStub );
 			editor.editing.view.fire( 'keydown', domEvtDataStub );
 
 
 			expect( editor.execute.called ).to.be.false;
 			expect( editor.execute.called ).to.be.false;
+			sinon.assert.notCalled( domEvtDataStub.preventDefault );
+			sinon.assert.notCalled( domEvtDataStub.stopPropagation );
 		} );
 		} );
 	} );
 	} );
 } );
 } );