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

Used expandToolbarConfig() and enableToolbarKeyboardFocus() toolbar utils in ClassicEditorUI class.

Aleksander Nowodzinski 8 лет назад
Родитель
Сommit
24ce29e085

+ 14 - 30
packages/ckeditor5-editor-classic/src/classiceditorui.js

@@ -9,6 +9,10 @@
 
 import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
 import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
+import {
+	expandToolbarConfig,
+	enableToolbarKeyboardFocus
+} from '@ckeditor/ckeditor5-ui/src/toolbar/utils';
 
 /**
  * The classic editor UI class.
@@ -79,38 +83,18 @@ export default class ClassicEditorUI {
 
 		return this.view.init()
 			.then( () => {
-				const toolbarConfig = editor.config.get( 'toolbar' );
-				const promises = [];
-
-				if ( toolbarConfig ) {
-					for ( let name of toolbarConfig ) {
-						promises.push( this.view.toolbar.items.add( this.componentFactory.create( name ) ) );
-					}
-				}
-
-				return Promise.all( promises );
+				return expandToolbarConfig(
+					editor.config.get( 'toolbar' ),
+					this.view.toolbar.items,
+					this.componentFactory
+				);
 			} )
 			.then( () => {
-				const toolbarFocusTracker = this.view.toolbar.focusTracker;
-
-				// Because toolbar items can get focus, the overall state of
-				// the toolbar must also be tracked.
-				this.focusTracker.add( this.view.toolbar.element );
-
-				// Focus the toolbar on the keystroke, if not already focused.
-				editor.keystrokes.set( 'Alt+F10', ( data, cancel ) => {
-					if ( this.focusTracker.isFocused && !toolbarFocusTracker.isFocused ) {
-						this.view.toolbar.focus();
-						cancel();
-					}
-				} );
-
-				// Blur the toolbar and bring the focus back to editable on the keystroke.
-				this.view.toolbar.keystrokes.set( 'Esc', ( data, cancel ) => {
-					if ( toolbarFocusTracker.isFocused ) {
-						editor.editing.view.focus();
-						cancel();
-					}
+				enableToolbarKeyboardFocus( {
+					origin: editor.editing.view,
+					originFocusTracker: this.focusTracker,
+					originKeystrokeHandler: editor.keystrokes,
+					toolbar: this.view.toolbar
 				} );
 			} );
 	}

+ 16 - 59
packages/ckeditor5-editor-classic/tests/classiceditorui.js

@@ -13,7 +13,7 @@ import ClassicEditorUIView from '../src/classiceditoruiview';
 import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 
 import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
-import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+import * as toolbarUtils from '@ckeditor/ckeditor5-ui/src/toolbar/utils';
 
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
@@ -139,69 +139,26 @@ describe( 'ClassicEditorUI', () => {
 		} );
 
 		it( 'fills view.toolbar#items with editor config', () => {
+			const spy = testUtils.sinon.spy( toolbarUtils, 'expandToolbarConfig' );
+
 			return ui.init().then( () => {
-				expect( view.toolbar.items ).to.have.length( 2 );
-				expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
-				expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
+				sinon.assert.calledWithExactly( spy,
+					editor.config.get( 'toolbar' ),
+					view.toolbar.items,
+					ui.componentFactory
+				);
 			} );
 		} );
 
-		describe( 'activates keyboard navigation for the toolbar', () => {
-			it( 'Alt+F10: focus the first focusable toolbar item', () => {
-				return ui.init().then( () => {
-					const spy = sinon.spy( view.toolbar, 'focus' );
-					const toolbarFocusTracker = view.toolbar.focusTracker;
-					const keyEvtData = {
-						keyCode: keyCodes.f10,
-						altKey: true,
-						preventDefault: sinon.spy(),
-						stopPropagation: sinon.spy()
-					};
-
-					toolbarFocusTracker.isFocused = false;
-					ui.focusTracker.isFocused = false;
-
-					editor.keystrokes.press( keyEvtData );
-					sinon.assert.notCalled( spy );
-
-					toolbarFocusTracker.isFocused = true;
-					ui.focusTracker.isFocused = true;
+		it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
+			const spy = testUtils.sinon.spy( toolbarUtils, 'enableToolbarKeyboardFocus' );
 
-					editor.keystrokes.press( keyEvtData );
-					sinon.assert.notCalled( spy );
-
-					toolbarFocusTracker.isFocused = false;
-					ui.focusTracker.isFocused = true;
-
-					editor.keystrokes.press( keyEvtData );
-					sinon.assert.calledOnce( spy );
-
-					sinon.assert.calledOnce( keyEvtData.preventDefault );
-					sinon.assert.calledOnce( keyEvtData.stopPropagation );
-				} );
-			} );
-
-			it( 'esc: re–foucus editable when toolbar is focused', () => {
-				return ui.init().then( () => {
-					const spy = sinon.spy( editor.editing.view, 'focus' );
-					const toolbarFocusTracker = view.toolbar.focusTracker;
-					const keyEvtData = { keyCode: keyCodes.esc,
-						preventDefault: sinon.spy(),
-						stopPropagation: sinon.spy()
-					};
-
-					toolbarFocusTracker.isFocused = false;
-
-					ui.view.toolbar.keystrokes.press( keyEvtData );
-					sinon.assert.notCalled( spy );
-
-					toolbarFocusTracker.isFocused = true;
-
-					ui.view.toolbar.keystrokes.press( keyEvtData );
-
-					sinon.assert.calledOnce( spy );
-					sinon.assert.calledOnce( keyEvtData.preventDefault );
-					sinon.assert.calledOnce( keyEvtData.stopPropagation );
+			return ui.init().then( () => {
+				sinon.assert.calledWithExactly( spy, {
+					origin: editor.editing.view,
+					originFocusTracker: ui.focusTracker,
+					originKeystrokeHandler: editor.keystrokes,
+					toolbar: view.toolbar
 				} );
 			} );
 		} );