Răsfoiți Sursa

Removed KeystrokeHandler class and its tests.

Aleksander Nowodzinski 9 ani în urmă
părinte
comite
1730e979cc

+ 0 - 109
packages/ckeditor5-core/src/keystrokehandler.js

@@ -1,109 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/**
- * @module core/keystrokehandler
- */
-
-import EmitterMixin from 'ckeditor5-utils/src/emittermixin';
-import { getCode, parseKeystroke } from 'ckeditor5-utils/src/keyboard';
-
-/**
- * Keystroke handler. Its instance is available in {@link module:core/editor/standardeditor~StandardEditor#keystrokes} so plugins
- * can register their keystrokes.
- *
- * E.g. an undo plugin would do this:
- *
- *		editor.keystrokes.set( 'ctrl + Z', 'undo' );
- *		editor.keystrokes.set( 'ctrl + shift + Z', 'redo' );
- *		editor.keystrokes.set( 'ctrl + Y', 'redo' );
- */
-export default class KeystrokeHandler {
-	/**
-	 * Creates an instance of the keystroke handler.
-	 *
-	 * @param {module:core/editor/editor~Editor} editor
-	 */
-	constructor( editor ) {
-		/**
-		 * The editor instance.
-		 *
-		 * @readonly
-		 * @member {module:core/editor/editor~Editor}
-		 */
-		this.editor = editor;
-
-		/**
-		 * Listener used to listen to events for easier keystroke handler destruction.
-		 *
-		 * @private
-		 * @member {module:utils/emittermixin~Emitter}
-		 */
-		this._listener = Object.create( EmitterMixin );
-
-		/**
-		 * Map of the defined keystrokes. Keystroke codes are the keys.
-		 *
-		 * @private
-		 * @member {Map}
-		 */
-		this._keystrokes = new Map();
-
-		this._listener.listenTo( editor.editing.view, 'keydown', ( evt, data ) => {
-			const handled = this.press( data );
-
-			if ( handled ) {
-				data.preventDefault();
-			}
-		} );
-	}
-
-	/**
-	 * Registers a handler for the specified keystroke.
-	 *
-	 * The handler can be specified as a command name or a callback.
-	 *
-	 * @param {String|Array.<String|Number>} keystroke Keystroke defined in a format accepted by
-	 * the {@link module:utils/keyboard~parseKeystroke} function.
-	 * @param {String|Function} callback If a string is passed, then the keystroke will
-	 * {@link module:core/editor/editor~Editor#execute execute a command}.
-	 * If a function, then it will be called with the
-	 * {@link module:engine/view/observer/keyobserver~KeyEventData key event data} object.
-	 */
-	set( keystroke, callback ) {
-		this._keystrokes.set( parseKeystroke( keystroke ), callback );
-	}
-
-	/**
-	 * Triggers a keystroke handler for a specified key combination, if such a keystroke was {@link #set defined}.
-	 *
-	 * @param {module:engine/view/observer/keyobserver~KeyEventData} keyEventData Key event data.
-	 * @returns {Boolean} Whether the keystroke was handled.
-	 */
-	press( keyEventData ) {
-		const keyCode = getCode( keyEventData );
-		const callback = this._keystrokes.get( keyCode );
-
-		if ( !callback ) {
-			return false;
-		}
-
-		if ( typeof callback == 'string' ) {
-			this.editor.execute( callback );
-		} else {
-			callback( keyEventData );
-		}
-
-		return true;
-	}
-
-	/**
-	 * Destroys the keystroke handler.
-	 */
-	destroy() {
-		this._keystrokes = new Map();
-		this._listener.stopListening();
-	}
-}

+ 0 - 130
packages/ckeditor5-core/tests/keystrokehandler.js

@@ -1,130 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
-import KeystrokeHandler from 'ckeditor5-core/src/keystrokehandler';
-import { keyCodes } from 'ckeditor5-utils/src/keyboard';
-
-describe( 'KeystrokeHandler', () => {
-	let editor;
-
-	beforeEach( () => {
-		return VirtualTestEditor.create()
-			.then( newEditor => {
-				editor = newEditor;
-				editor.keystrokes = new KeystrokeHandler( editor );
-			} );
-	} );
-
-	describe( 'constructor()', () => {
-		it( 'triggers #press on #keydown', () => {
-			const spy = sinon.spy( editor.keystrokes, 'press' );
-			const keyEvtData = { keyCode: 1 };
-
-			editor.editing.view.fire( 'keydown', keyEvtData );
-
-			expect( spy.calledOnce ).to.be.true;
-			expect( spy.calledWithExactly( keyEvtData ) );
-		} );
-
-		it( 'prevents default when keystroke was handled', () => {
-			editor.keystrokes.press = () => true;
-
-			const keyEvtData = { keyCode: 1, preventDefault: sinon.spy() };
-
-			editor.editing.view.fire( 'keydown', keyEvtData );
-
-			expect( keyEvtData.preventDefault.calledOnce ).to.be.true;
-		} );
-	} );
-
-	describe( 'press', () => {
-		it( 'executes a command', () => {
-			const spy = sinon.stub( editor, 'execute' );
-
-			editor.keystrokes.set( 'ctrl + A', 'foo' );
-
-			const wasHandled = editor.keystrokes.press( getCtrlA() );
-
-			expect( spy.calledOnce ).to.be.true;
-			expect( spy.calledWithExactly( 'foo' ) ).to.be.true;
-			expect( wasHandled ).to.be.true;
-		} );
-
-		it( 'executes a callback', () => {
-			const spy = sinon.spy();
-			const keyEvtData = getCtrlA();
-
-			editor.keystrokes.set( 'ctrl + A', spy );
-
-			const wasHandled = editor.keystrokes.press( keyEvtData );
-
-			expect( spy.calledOnce ).to.be.true;
-			expect( spy.calledWithExactly( keyEvtData ) ).to.be.true;
-			expect( wasHandled ).to.be.true;
-		} );
-
-		it( 'returns false when no handler', () => {
-			const keyEvtData = getCtrlA();
-
-			const wasHandled = editor.keystrokes.press( keyEvtData );
-
-			expect( wasHandled ).to.be.false;
-		} );
-	} );
-
-	describe( 'set', () => {
-		it( 'handles array format', () => {
-			const spy = sinon.spy();
-
-			editor.keystrokes.set( [ 'ctrl', 'A' ], spy );
-
-			expect( editor.keystrokes.press( getCtrlA() ) ).to.be.true;
-		} );
-
-		it( 'overrides existing keystroke', () => {
-			const spy1 = sinon.spy();
-			const spy2 = sinon.spy();
-
-			editor.keystrokes.set( [ 'ctrl', 'A' ], spy1 );
-			editor.keystrokes.set( [ 'ctrl', 'A' ], spy2 );
-
-			editor.keystrokes.press( getCtrlA() );
-
-			expect( spy1.calledOnce ).to.be.false;
-			expect( spy2.calledOnce ).to.be.true;
-		} );
-	} );
-
-	describe( 'destroy', () => {
-		it( 'detaches #keydown listener', () => {
-			const spy = sinon.spy( editor.keystrokes, 'press' );
-
-			editor.keystrokes.destroy();
-
-			editor.editing.view.fire( 'keydown', { keyCode: 1 } );
-
-			expect( spy.called ).to.be.false;
-		} );
-
-		it( 'removes all keystrokes', () => {
-			const spy = sinon.spy();
-			const keystrokeHandler = editor.keystrokes;
-
-			keystrokeHandler.set( 'ctrl + A', spy );
-
-			keystrokeHandler.destroy();
-
-			const wasHandled = keystrokeHandler.press( getCtrlA() );
-
-			expect( wasHandled ).to.be.false;
-			expect( spy.called ).to.be.false;
-		} );
-	} );
-} );
-
-function getCtrlA() {
-	return { keyCode: keyCodes.a, ctrlKey: true };
-}