Kaynağa Gözat

Moved enableToolbarKeyboardFocus ToolbarView utils to a separate module.

Aleksander Nowodzinski 9 yıl önce
ebeveyn
işleme
47ed0c50cd

+ 2 - 22
packages/ckeditor5-ui/src/toolbar/utils.js → packages/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus.js

@@ -4,29 +4,9 @@
  */
  */
 
 
 /**
 /**
- * @module ui/toolbar/utils
+ * @module ui/toolbar/enabletoolbarkeyboardfocus
  */
  */
 
 
-/**
- * An utility which expands a plain toolbar configuration into a collection
- * of {@link module:ui/view~View views} using a given factory.
- *
- * @param {Object} config The toolbar config.
- * @param {module:utils/collection~Collection} collection A collection into which the config
- * is expanded.
- * @param {module:ui/componentfactory~ComponentFactory} factory A factory producing toolbar items.
- * @returns {Promise} A promise resolved when all toolbar items are initialized.
- */
-export function getItemsFromConfig( config, collection, factory ) {
-	let promises = [];
-
-	if ( config ) {
-		promises = config.map( name => collection.add( factory.create( name ) ) );
-	}
-
-	return Promise.all( promises );
-}
-
 /**
 /**
  * Enables focus/blur toolbar navigation using `Alt+F10` and `Esc` keystrokes.
  * Enables focus/blur toolbar navigation using `Alt+F10` and `Esc` keystrokes.
  *
  *
@@ -40,7 +20,7 @@ export function getItemsFromConfig( config, collection, factory ) {
  * @param {module:ui/toolbar/toolbarview~ToolbarView} options.toolbar A toolbar which is to gain
  * @param {module:ui/toolbar/toolbarview~ToolbarView} options.toolbar A toolbar which is to gain
  * focus when `Alt+F10` is pressed.
  * focus when `Alt+F10` is pressed.
  */
  */
-export function enableToolbarKeyboardFocus( {
+export default function enableToolbarKeyboardFocus( {
 	origin,
 	origin,
 	originKeystrokeHandler,
 	originKeystrokeHandler,
 	originFocusTracker,
 	originFocusTracker,

+ 98 - 0
packages/ckeditor5-ui/tests/toolbar/enabletoolbarkeyboardfocus.js

@@ -0,0 +1,98 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global document */
+
+import View from '../../src/view';
+import ToolbarView from '../../src/toolbar/toolbarview';
+import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
+import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+import enableToolbarKeyboardFocus from '../../src/toolbar/enabletoolbarkeyboardfocus';
+
+describe( 'enableToolbarKeyboardFocus()', () => {
+	let origin, originFocusTracker, originKeystrokeHandler, toolbar;
+
+	beforeEach( () => {
+		origin = viewCreator();
+		originFocusTracker = new FocusTracker();
+		originKeystrokeHandler = new KeystrokeHandler();
+		toolbar = new ToolbarView();
+
+		enableToolbarKeyboardFocus( {
+			origin: origin,
+			originFocusTracker: originFocusTracker,
+			originKeystrokeHandler: originKeystrokeHandler,
+			toolbar: toolbar
+		} );
+
+		return toolbar.init();
+	} );
+
+	it( 'focuses the toolbar on Alt+F10', () => {
+		const spy = sinon.spy( toolbar, 'focus' );
+		const toolbarFocusTracker = toolbar.focusTracker;
+		const keyEvtData = {
+			keyCode: keyCodes.f10,
+			altKey: true,
+			preventDefault: sinon.spy(),
+			stopPropagation: sinon.spy()
+		};
+
+		toolbarFocusTracker.isFocused = false;
+		originFocusTracker.isFocused = false;
+
+		originKeystrokeHandler.press( keyEvtData );
+		sinon.assert.notCalled( spy );
+
+		toolbarFocusTracker.isFocused = true;
+		originFocusTracker.isFocused = true;
+
+		originKeystrokeHandler.press( keyEvtData );
+		sinon.assert.notCalled( spy );
+
+		toolbarFocusTracker.isFocused = false;
+		originFocusTracker.isFocused = true;
+
+		originKeystrokeHandler.press( keyEvtData );
+		sinon.assert.calledOnce( spy );
+
+		sinon.assert.calledOnce( keyEvtData.preventDefault );
+		sinon.assert.calledOnce( keyEvtData.stopPropagation );
+	} );
+
+	it( 're–foucuses origin on Esc', () => {
+		const spy = origin.focus = sinon.spy();
+		const toolbarFocusTracker = toolbar.focusTracker;
+		const keyEvtData = { keyCode: keyCodes.esc,
+			preventDefault: sinon.spy(),
+			stopPropagation: sinon.spy()
+		};
+
+		toolbarFocusTracker.isFocused = false;
+
+		toolbar.keystrokes.press( keyEvtData );
+		sinon.assert.notCalled( spy );
+
+		toolbarFocusTracker.isFocused = true;
+
+		toolbar.keystrokes.press( keyEvtData );
+
+		sinon.assert.calledOnce( spy );
+		sinon.assert.calledOnce( keyEvtData.preventDefault );
+		sinon.assert.calledOnce( keyEvtData.stopPropagation );
+	} );
+} );
+
+function viewCreator( name ) {
+	return ( locale ) => {
+		const view = new View( locale );
+
+		view.name = name;
+		view.element = document.createElement( 'a' );
+
+		return view;
+	};
+}

+ 0 - 133
packages/ckeditor5-ui/tests/toolbar/utils.js

@@ -1,133 +0,0 @@
-/**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
-
-/* global document */
-
-import View from '../../src/view';
-import ComponentFactory from '../../src/componentfactory';
-import ToolbarView from '../../src/toolbar/toolbarview';
-import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
-import Collection from '@ckeditor/ckeditor5-utils/src/collection';
-import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
-import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
-import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
-import {
-	getItemsFromConfig,
-	enableToolbarKeyboardFocus
-} from '../../src/toolbar/utils';
-
-describe( 'utils', () => {
-	describe( 'getItemsFromConfig()', () => {
-		let factory;
-
-		beforeEach( () => {
-			factory = new ComponentFactory( new Editor() );
-
-			factory.add( 'foo', viewCreator( 'foo' ) );
-			factory.add( 'bar', viewCreator( 'bar' ) );
-		} );
-
-		it( 'returns a promise', () => {
-			expect( getItemsFromConfig() ).to.be.instanceOf( Promise );
-		} );
-
-		it( 'expands the config into collection', () => {
-			const collection = new Collection();
-
-			return getItemsFromConfig( [ 'foo', 'bar', 'foo' ], collection, factory )
-				.then( () => {
-					expect( collection ).to.have.length( 3 );
-					expect( collection.get( 0 ).name ).to.equal( 'foo' );
-					expect( collection.get( 1 ).name ).to.equal( 'bar' );
-					expect( collection.get( 2 ).name ).to.equal( 'foo' );
-				} );
-		} );
-	} );
-
-	describe( 'enableToolbarKeyboardFocus()', () => {
-		let origin, originFocusTracker, originKeystrokeHandler, toolbar;
-
-		beforeEach( () => {
-			origin = viewCreator();
-			originFocusTracker = new FocusTracker();
-			originKeystrokeHandler = new KeystrokeHandler();
-			toolbar = new ToolbarView();
-
-			enableToolbarKeyboardFocus( {
-				origin: origin,
-				originFocusTracker: originFocusTracker,
-				originKeystrokeHandler: originKeystrokeHandler,
-				toolbar: toolbar
-			} );
-
-			return toolbar.init();
-		} );
-
-		it( 'focuses the toolbar on Alt+F10', () => {
-			const spy = sinon.spy( toolbar, 'focus' );
-			const toolbarFocusTracker = toolbar.focusTracker;
-			const keyEvtData = {
-				keyCode: keyCodes.f10,
-				altKey: true,
-				preventDefault: sinon.spy(),
-				stopPropagation: sinon.spy()
-			};
-
-			toolbarFocusTracker.isFocused = false;
-			originFocusTracker.isFocused = false;
-
-			originKeystrokeHandler.press( keyEvtData );
-			sinon.assert.notCalled( spy );
-
-			toolbarFocusTracker.isFocused = true;
-			originFocusTracker.isFocused = true;
-
-			originKeystrokeHandler.press( keyEvtData );
-			sinon.assert.notCalled( spy );
-
-			toolbarFocusTracker.isFocused = false;
-			originFocusTracker.isFocused = true;
-
-			originKeystrokeHandler.press( keyEvtData );
-			sinon.assert.calledOnce( spy );
-
-			sinon.assert.calledOnce( keyEvtData.preventDefault );
-			sinon.assert.calledOnce( keyEvtData.stopPropagation );
-		} );
-
-		it( 're–foucuses origin on Esc', () => {
-			const spy = origin.focus = sinon.spy();
-			const toolbarFocusTracker = toolbar.focusTracker;
-			const keyEvtData = { keyCode: keyCodes.esc,
-				preventDefault: sinon.spy(),
-				stopPropagation: sinon.spy()
-			};
-
-			toolbarFocusTracker.isFocused = false;
-
-			toolbar.keystrokes.press( keyEvtData );
-			sinon.assert.notCalled( spy );
-
-			toolbarFocusTracker.isFocused = true;
-
-			toolbar.keystrokes.press( keyEvtData );
-
-			sinon.assert.calledOnce( spy );
-			sinon.assert.calledOnce( keyEvtData.preventDefault );
-			sinon.assert.calledOnce( keyEvtData.stopPropagation );
-		} );
-	} );
-} );
-
-function viewCreator( name ) {
-	return ( locale ) => {
-		const view = new View( locale );
-
-		view.name = name;
-		view.element = document.createElement( 'a' );
-
-		return view;
-	};
-}