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

Commit 734166a

Browse files
authored
Merge pull request #131 from ckeditor/t/130
Feature: Introduced the `EditorUI#update` event. Closes #130. BREAKING CHANGE: The `EditorUI` is now a class (no longer an interface).
2 parents ec89d8d + 051d76b commit 734166a

File tree

7 files changed

+195
-158
lines changed

7 files changed

+195
-158
lines changed

src/editor/editorui.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
/**
7+
* @module core/editor/editorui
8+
*/
9+
10+
import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
11+
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
12+
13+
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
14+
import mix from '@ckeditor/ckeditor5-utils/src/mix';
15+
16+
/**
17+
* A class providing the minimal interface that is required to successfully bootstrap any editor UI.
18+
*
19+
* @mixes module:utils/emittermixin~EmitterMixin
20+
*/
21+
export default class EditorUI {
22+
/**
23+
* Creates an instance of the editor UI class.
24+
*
25+
* @param {module:core/editor/editor~Editor} editor The editor instance.
26+
* @param {module:ui/editorui/editoruiview~EditorUIView} view The view of the UI.
27+
*/
28+
constructor( editor, view ) {
29+
/**
30+
* The editor that the UI belongs to.
31+
*
32+
* @readonly
33+
* @member {module:core/editor/editor~Editor} #editor
34+
*/
35+
this.editor = editor;
36+
37+
/**
38+
* The main (top–most) view of the editor UI.
39+
*
40+
* @readonly
41+
* @member {module:ui/editorui/editoruiview~EditorUIView} #view
42+
*/
43+
this.view = view;
44+
45+
/**
46+
* An instance of the {@link module:ui/componentfactory~ComponentFactory}, a registry used by plugins
47+
* to register factories of specific UI components.
48+
*
49+
* @readonly
50+
* @member {module:ui/componentfactory~ComponentFactory} #componentFactory
51+
*/
52+
this.componentFactory = new ComponentFactory( editor );
53+
54+
/**
55+
* Stores the information about the editor UI focus and propagates it so various plugins and components
56+
* are unified as a focus group.
57+
*
58+
* @readonly
59+
* @member {module:utils/focustracker~FocusTracker} #focusTracker
60+
*/
61+
this.focusTracker = new FocusTracker();
62+
63+
// Informs UI components that should be refreshed after layout change.
64+
this.listenTo( editor.editing.view.document, 'layoutChanged', () => this.update() );
65+
}
66+
67+
/**
68+
* Fires the {@link module:core/editor/editorui~EditorUI#event:update} event.
69+
*/
70+
update() {
71+
this.fire( 'update' );
72+
}
73+
74+
/**
75+
* Destroys the UI.
76+
*/
77+
destroy() {
78+
this.stopListening();
79+
this.view.destroy();
80+
}
81+
82+
/**
83+
* Fired whenever the UI (all related components) should be refreshed.
84+
*
85+
* **Note:**: The event is fired after each {@link module:engine/view/document~Document#event:layoutChanged}.
86+
* It can also be fired manually via the {@link module:core/editor/editorui~EditorUI#update} method.
87+
*
88+
* @event update
89+
*/
90+
}
91+
92+
mix( EditorUI, EmitterMixin );

src/editor/editorui.jsdoc

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/_utils-tests/classictesteditor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import ClassicTestEditor from '../../tests/_utils/classictesteditor';
1111
import Plugin from '../../src/plugin';
1212
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
1313

14-
import ClassicTestEditorUI from '../../tests/_utils/classictesteditorui';
14+
import EditorUI from '../../src/editor/editorui';
1515
import BoxedEditorUIView from '@ckeditor/ckeditor5-ui/src/editorui/boxed/boxededitoruiview';
1616
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
1717

@@ -39,7 +39,7 @@ describe( 'ClassicTestEditor', () => {
3939
expect( editor ).to.be.instanceof( Editor );
4040
expect( editor.config.get( 'foo' ) ).to.equal( 1 );
4141
expect( editor.element ).to.equal( editorElement );
42-
expect( editor.ui ).to.be.instanceOf( ClassicTestEditorUI );
42+
expect( editor.ui ).to.be.instanceOf( EditorUI );
4343
expect( editor.ui.view ).to.be.instanceOf( BoxedEditorUIView );
4444
expect( editor.data.processor ).to.be.instanceof( HtmlDataProcessor );
4545
} );
@@ -82,7 +82,7 @@ describe( 'ClassicTestEditor', () => {
8282
it( 'creates and initializes the UI', () => {
8383
return ClassicTestEditor.create( editorElement, { foo: 1 } )
8484
.then( editor => {
85-
expect( editor.ui ).to.be.instanceOf( ClassicTestEditorUI );
85+
expect( editor.ui ).to.be.instanceOf( EditorUI );
8686
expect( editor.ui.view ).to.be.instanceOf( BoxedEditorUIView );
8787
} );
8888
} );

tests/_utils-tests/classictesteditorui.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/_utils/classictesteditor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Editor from '../../src/editor/editor';
77
import ElementApiMixin from '../../src/editor/utils/elementapimixin';
88
import DataApiMixin from '../../src/editor/utils/dataapimixin';
99
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
10-
import ClassicTestEditorUI from './classictesteditorui';
10+
import EditorUI from '../../src/editor/editorui';
1111
import BoxedEditorUIView from '@ckeditor/ckeditor5-ui/src/editorui/boxed/boxededitoruiview';
1212
import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
1313
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
@@ -33,7 +33,7 @@ export default class ClassicTestEditor extends Editor {
3333
// Use the HTML data processor in this editor.
3434
this.data.processor = new HtmlDataProcessor();
3535

36-
this.ui = new ClassicTestEditorUI( this, new BoxedEditorUIView( this.locale ) );
36+
this.ui = new EditorUI( this, new BoxedEditorUIView( this.locale ) );
3737

3838
// Expose properties normally exposed by the ClassicEditorUI.
3939
this.ui.view.editable = new InlineEditableUIView( this.ui.view.locale );

tests/_utils/classictesteditorui.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/editor/editorui.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
3+
* For licensing, see LICENSE.md.
4+
*/
5+
6+
import EditorUI from '../../src/editor/editorui';
7+
import Editor from '../../src/editor/editor';
8+
9+
import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
10+
import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
11+
import View from '@ckeditor/ckeditor5-ui/src/view';
12+
13+
import testUtils from '../_utils/utils';
14+
15+
testUtils.createSinonSandbox();
16+
17+
describe( 'EditorUI', () => {
18+
let editor, view, ui;
19+
20+
beforeEach( () => {
21+
editor = new Editor();
22+
view = new View();
23+
ui = new EditorUI( editor, view );
24+
} );
25+
26+
afterEach( () => {
27+
return Promise.all( [
28+
editor.destroy(),
29+
ui.destroy()
30+
] );
31+
} );
32+
33+
describe( 'constructor()', () => {
34+
it( 'should set #editor', () => {
35+
expect( ui.editor ).to.equal( editor );
36+
} );
37+
38+
it( 'should set #view', () => {
39+
expect( ui.view ).to.equal( view );
40+
} );
41+
42+
it( 'should create #componentFactory factory', () => {
43+
expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
44+
} );
45+
46+
it( 'should create #focusTracker', () => {
47+
expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
48+
} );
49+
50+
it( 'should fire update event after viewDocument#layoutChanged', () => {
51+
const spy = sinon.spy();
52+
53+
ui.on( 'update', spy );
54+
55+
editor.editing.view.document.fire( 'layoutChanged' );
56+
57+
sinon.assert.calledOnce( spy );
58+
59+
editor.editing.view.document.fire( 'layoutChanged' );
60+
61+
sinon.assert.calledTwice( spy );
62+
} );
63+
} );
64+
65+
describe( 'update()', () => {
66+
it( 'should fire update event', () => {
67+
const spy = sinon.spy();
68+
69+
ui.on( 'update', spy );
70+
71+
ui.update();
72+
73+
sinon.assert.calledOnce( spy );
74+
75+
ui.update();
76+
77+
sinon.assert.calledTwice( spy );
78+
} );
79+
} );
80+
81+
describe( 'destroy()', () => {
82+
it( 'should stop listening', () => {
83+
const spy = sinon.spy( ui, 'stopListening' );
84+
85+
ui.destroy();
86+
87+
sinon.assert.called( spy );
88+
} );
89+
90+
it( 'should destroy the #view', () => {
91+
const spy = sinon.spy( view, 'destroy' );
92+
93+
ui.destroy();
94+
95+
sinon.assert.called( spy );
96+
} );
97+
} );
98+
} );

0 commit comments

Comments
 (0)