Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 2bb1e4e

Browse files
authored
Merge pull request #50 from ckeditor/t/48
Other: Code refactoring in editor-classic to share API with editor-inline. Closes #48.
2 parents 167ecba + 7377a80 commit 2bb1e4e

File tree

3 files changed

+38
-106
lines changed

3 files changed

+38
-106
lines changed

src/classiceditorui.js

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99

1010
import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
1111
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
12+
import enableToolbarKeyboardFocus from '@ckeditor/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus';
1213

1314
/**
1415
* The classic editor UI class.
16+
*
17+
* @implements module:core/editor/editorui~EditorUI
1518
*/
1619
export default class ClassicEditorUI {
1720
/**
@@ -22,34 +25,22 @@ export default class ClassicEditorUI {
2225
*/
2326
constructor( editor, view ) {
2427
/**
25-
* Editor that the UI belongs to.
26-
*
27-
* @readonly
28-
* @member {module:core/editor/editor~Editor}
28+
* @inheritDoc
2929
*/
3030
this.editor = editor;
3131

3232
/**
33-
* View of the ui.
34-
*
35-
* @readonly
36-
* @member {module:ui/editorui/editoruiview~EditorUIView}
33+
* @inheritDoc
3734
*/
3835
this.view = view;
3936

4037
/**
41-
* Instance of the {@link module:ui/componentfactory~ComponentFactory}.
42-
*
43-
* @readonly
44-
* @member {module:ui/componentfactory~ComponentFactory}
38+
* @inheritDoc
4539
*/
4640
this.componentFactory = new ComponentFactory( editor );
4741

4842
/**
49-
* Keeps information about editor focus.
50-
*
51-
* @readonly
52-
* @member {module:utils/focustracker~FocusTracker}
43+
* @inheritDoc
5344
*/
5445
this.focusTracker = new FocusTracker();
5546

@@ -79,36 +70,14 @@ export default class ClassicEditorUI {
7970

8071
return this.view.init()
8172
.then( () => {
82-
const toolbarConfig = editor.config.get( 'toolbar' );
83-
const promises = [];
84-
85-
if ( toolbarConfig ) {
86-
promises.push( this.view.toolbar.fillFromConfig( toolbarConfig, this.componentFactory ) );
87-
}
88-
89-
return Promise.all( promises );
73+
return this.view.toolbar.fillFromConfig( editor.config.get( 'toolbar' ), this.componentFactory );
9074
} )
9175
.then( () => {
92-
const toolbarFocusTracker = this.view.toolbar.focusTracker;
93-
94-
// Because toolbar items can get focus, the overall state of
95-
// the toolbar must also be tracked.
96-
this.focusTracker.add( this.view.toolbar.element );
97-
98-
// Focus the toolbar on the keystroke, if not already focused.
99-
editor.keystrokes.set( 'Alt+F10', ( data, cancel ) => {
100-
if ( this.focusTracker.isFocused && !toolbarFocusTracker.isFocused ) {
101-
this.view.toolbar.focus();
102-
cancel();
103-
}
104-
} );
105-
106-
// Blur the toolbar and bring the focus back to editable on the keystroke.
107-
this.view.toolbar.keystrokes.set( 'Esc', ( data, cancel ) => {
108-
if ( toolbarFocusTracker.isFocused ) {
109-
editor.editing.view.focus();
110-
cancel();
111-
}
76+
enableToolbarKeyboardFocus( {
77+
origin: editor.editing.view,
78+
originFocusTracker: this.focusTracker,
79+
originKeystrokeHandler: editor.keystrokes,
80+
toolbar: this.view.toolbar
11281
} );
11382
} );
11483
}

tests/classiceditorui.js

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import ClassicEditorUIView from '../src/classiceditoruiview';
1313
import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
1414

1515
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
16-
import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
16+
import * as enableToolbarKeyboardFocus from '@ckeditor/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus';
1717

1818
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
1919
import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
@@ -139,69 +139,22 @@ describe( 'ClassicEditorUI', () => {
139139
} );
140140

141141
it( 'fills view.toolbar#items with editor config', () => {
142+
const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
143+
142144
return ui.init().then( () => {
143-
expect( view.toolbar.items ).to.have.length( 2 );
144-
expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
145-
expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
145+
sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
146146
} );
147147
} );
148148

149-
describe( 'activates keyboard navigation for the toolbar', () => {
150-
it( 'Alt+F10: focus the first focusable toolbar item', () => {
151-
return ui.init().then( () => {
152-
const spy = sinon.spy( view.toolbar, 'focus' );
153-
const toolbarFocusTracker = view.toolbar.focusTracker;
154-
const keyEvtData = {
155-
keyCode: keyCodes.f10,
156-
altKey: true,
157-
preventDefault: sinon.spy(),
158-
stopPropagation: sinon.spy()
159-
};
160-
161-
toolbarFocusTracker.isFocused = false;
162-
ui.focusTracker.isFocused = false;
163-
164-
editor.keystrokes.press( keyEvtData );
165-
sinon.assert.notCalled( spy );
166-
167-
toolbarFocusTracker.isFocused = true;
168-
ui.focusTracker.isFocused = true;
169-
170-
editor.keystrokes.press( keyEvtData );
171-
sinon.assert.notCalled( spy );
172-
173-
toolbarFocusTracker.isFocused = false;
174-
ui.focusTracker.isFocused = true;
175-
176-
editor.keystrokes.press( keyEvtData );
177-
sinon.assert.calledOnce( spy );
178-
179-
sinon.assert.calledOnce( keyEvtData.preventDefault );
180-
sinon.assert.calledOnce( keyEvtData.stopPropagation );
181-
} );
182-
} );
183-
184-
it( 'esc: re–foucus editable when toolbar is focused', () => {
185-
return ui.init().then( () => {
186-
const spy = sinon.spy( editor.editing.view, 'focus' );
187-
const toolbarFocusTracker = view.toolbar.focusTracker;
188-
const keyEvtData = { keyCode: keyCodes.esc,
189-
preventDefault: sinon.spy(),
190-
stopPropagation: sinon.spy()
191-
};
192-
193-
toolbarFocusTracker.isFocused = false;
194-
195-
ui.view.toolbar.keystrokes.press( keyEvtData );
196-
sinon.assert.notCalled( spy );
149+
it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
150+
const spy = testUtils.sinon.spy( enableToolbarKeyboardFocus, 'default' );
197151

198-
toolbarFocusTracker.isFocused = true;
199-
200-
ui.view.toolbar.keystrokes.press( keyEvtData );
201-
202-
sinon.assert.calledOnce( spy );
203-
sinon.assert.calledOnce( keyEvtData.preventDefault );
204-
sinon.assert.calledOnce( keyEvtData.stopPropagation );
152+
return ui.init().then( () => {
153+
sinon.assert.calledWithExactly( spy, {
154+
origin: editor.editing.view,
155+
originFocusTracker: ui.focusTracker,
156+
originKeystrokeHandler: editor.keystrokes,
157+
toolbar: view.toolbar
205158
} );
206159
} );
207160
} );

tests/manual/classic.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ <h2>Hello world!</h2>
88
<p>This is an editor instance.</p>
99
</div>
1010

11-
<div style="height: 1500px; width: 1500px; background: #eee; outline: 2px dashed #ddd; margin: 1em 0; padding: 1em;">
12-
* I'm a dummy div to enable scrolling in this test. *
13-
</div>
11+
<style>
12+
body {
13+
width: 10000px;
14+
height: 10000px;
15+
}
16+
17+
.ck-editor {
18+
margin-top: 200px;
19+
margin-left: 100px;
20+
margin-bottom: 200px;
21+
width: 450px;
22+
}
23+
</style>

0 commit comments

Comments
 (0)