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

Commit

Permalink
Merge pull request #43 from ckeditor/t/ckeditor5/1449
Browse files Browse the repository at this point in the history
Other: Editor UI classes API refactoring.

BREAKING CHANGE: Removed `InlineEditor#element` property. The `InlineEditorUI#element` property should be used instead.
BREAKING CHANGE: Removed `InlineEditorUIView#editableElement`. Instead `InlineEditorUI#getEditableElement()` method should be used.
  • Loading branch information
Piotr Jasiun authored Jan 22, 2019
2 parents dd2bb90 + 00089d3 commit f8195da
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 39 deletions.
8 changes: 0 additions & 8 deletions src/inlineeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ export default class InlineEditor extends Editor {
attachToForm( this );
}

/**
* @inheritDoc
*/
get element() {
return this.ui.view.editable.element;
}

/**
* Destroys the editor instance, releasing all resources used by it.
*
Expand Down Expand Up @@ -179,7 +172,6 @@ export default class InlineEditor extends Editor {
editor.initPlugins()
.then( () => {
editor.ui.init();
editor.fire( 'uiReady' );
} )
.then( () => {
const initialData = isElement( sourceElementOrData ) ?
Expand Down
51 changes: 46 additions & 5 deletions src/inlineeditorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalize
*/
export default class InlineEditorUI extends EditorUI {
/**
* @inheritDoc
* Creates an instance of the inline editor UI class.
*
* @param {module:core/editor/editor~Editor} editor The editor instance.
* @param {module:ui/editorui/editoruiview~EditorUIView} view The view of the UI.
*/
constructor( editor, view ) {
super( editor, view );
super( editor );

/**
* The main (top–most) view of the editor UI.
*
* @private
* @member {module:ui/editorui/editoruiview~EditorUIView} #_view
*/
this._view = view;

/**
* A normalized `config.toolbar` object.
Expand All @@ -32,6 +43,23 @@ export default class InlineEditorUI extends EditorUI {
this._toolbarConfig = normalizeToolbarConfig( editor.config.get( 'toolbar' ) );
}

/**
* The main (top–most) view of the editor UI.
*
* @readonly
* @member {module:ui/editorui/editoruiview~EditorUIView} #view
*/
get view() {
return this._view;
}

/**
* @inheritDoc
*/
get element() {
return this.view.editable.element;
}

/**
* Initializes the UI.
*/
Expand All @@ -54,7 +82,7 @@ export default class InlineEditorUI extends EditorUI {
// showing up when there's no focus in the UI.
if ( view.panel.isVisible ) {
view.panel.pin( {
target: view.editableElement,
target: view.editable.element,
positions: view.panelPositions
} );
}
Expand All @@ -67,10 +95,12 @@ export default class InlineEditorUI extends EditorUI {
// Bind to focusTracker instead of editor.editing.view because otherwise
// focused editable styles disappear when view#toolbar is focused.
view.editable.bind( 'isFocused' ).to( this.focusTracker );
editor.editing.view.attachDomRoot( view.editableElement );
editor.editing.view.attachDomRoot( view.editable.element );
view.editable.name = editingRoot.rootName;

this.focusTracker.add( view.editableElement );
this._editableElements.set( view.editable.name, view.editable.element );

this.focusTracker.add( view.editable.element );

view.toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );

Expand All @@ -80,5 +110,16 @@ export default class InlineEditorUI extends EditorUI {
originKeystrokeHandler: editor.keystrokes,
toolbar: view.toolbar
} );

this.fire( 'ready' );
}

/**
* @inheritDoc
*/
destroy() {
this._view.destroy();

super.destroy();
}
}
11 changes: 2 additions & 9 deletions src/inlineeditoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default class InlineEditorUIView extends EditorUIView {

/**
* A set of positioning functions used by the {@link #panel} to float around
* {@link #editableElement}.
* {@link #element editableElement}.
*
* The positioning functions are as follows:
*
Expand Down Expand Up @@ -143,18 +143,11 @@ export default class InlineEditorUIView extends EditorUIView {
this.panel.content.add( this.toolbar );
}

/**
* @inheritDoc
*/
get editableElement() {
return this.editable.element;
}

/**
* Determines the panel top position of the {@link #panel} in {@link #panelPositions}.
*
* @private
* @param {module:utils/dom/rect~Rect} editableRect Rect of the {@link #editableElement}.
* @param {module:utils/dom/rect~Rect} editableRect Rect of the {@link #element}.
* @param {module:utils/dom/rect~Rect} panelRect Rect of the {@link #panel}.
*/
_getPanelPositionTop( editableRect, panelRect ) {
Expand Down
16 changes: 10 additions & 6 deletions tests/inlineeditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import ElementApiMixin from '@ckeditor/ckeditor5-core/src/editor/utils/elementap
import RootElement from '@ckeditor/ckeditor5-engine/src/model/rootelement';

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import log from '@ckeditor/ckeditor5-utils/src/log';

import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
import { describeMemoryUsage, testMemoryUsage } from '@ckeditor/ckeditor5-core/tests/_utils/memory';

Expand All @@ -32,6 +34,8 @@ describe( 'InlineEditor', () => {
editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';

document.body.appendChild( editorElement );

testUtils.sinon.stub( log, 'warn' ).callsFake( () => {} );
} );

afterEach( () => {
Expand Down Expand Up @@ -111,11 +115,11 @@ describe( 'InlineEditor', () => {
} );
} );

it( 'editor.element should contain the whole editor (with UI) element', () => {
it( 'editor.ui.element should contain the whole editor (with UI) element', () => {
return InlineEditor.create( '<p>Hello world!</p>', {
plugins: [ Paragraph ]
} ).then( editor => {
expect( editor.editing.view.getDomRoot() ).to.equal( editor.element );
expect( editor.editing.view.getDomRoot() ).to.equal( editor.ui.element );
} );
} );
} );
Expand Down Expand Up @@ -194,7 +198,7 @@ describe( 'InlineEditor', () => {
class EventWatcher extends Plugin {
init() {
this.editor.on( 'pluginsReady', spy );
this.editor.on( 'uiReady', spy );
this.editor.ui.on( 'ready', spy );
this.editor.on( 'dataReady', spy );
this.editor.on( 'ready', spy );
}
Expand All @@ -205,7 +209,7 @@ describe( 'InlineEditor', () => {
plugins: [ EventWatcher ]
} )
.then( newEditor => {
expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'dataReady', 'ready' ] );

editor = newEditor;
} );
Expand Down Expand Up @@ -233,12 +237,12 @@ describe( 'InlineEditor', () => {
} );
} );

it( 'fires uiReady once UI is ready', () => {
it( 'fires ready once UI is ready', () => {
let isReady;

class EventWatcher extends Plugin {
init() {
this.editor.on( 'uiReady', () => {
this.editor.ui.on( 'ready', () => {
isReady = this.editor.ui.view.isRendered;
} );
}
Expand Down
26 changes: 23 additions & 3 deletions tests/inlineeditorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';

describe( 'InlineEditorUI', () => {
let editor, view, ui;
let editor, view, ui, viewElement;

testUtils.createSinonSandbox();

Expand All @@ -31,6 +31,7 @@ describe( 'InlineEditorUI', () => {

ui = editor.ui;
view = ui.view;
viewElement = view.editable.element;
} );
} );

Expand Down Expand Up @@ -93,7 +94,7 @@ describe( 'InlineEditorUI', () => {
editor.ui.fire( 'update' );
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, {
target: view.editableElement,
target: view.editable.element,
positions: sinon.match.array
} );
} );
Expand Down Expand Up @@ -198,6 +199,26 @@ describe( 'InlineEditorUI', () => {
} );
} );
} );

describe( 'element()', () => {
it( 'returns correct element instance', () => {
expect( ui.element ).to.equal( viewElement );
} );
} );

describe( 'getEditableElement()', () => {
it( 'returns editable element (default)', () => {
expect( ui.getEditableElement() ).to.equal( view.editable.element );
} );

it( 'returns editable element (root name passed)', () => {
expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
} );

it( 'returns undefined if editable with the given name is absent', () => {
expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
} );
} );
} );

function viewCreator( name ) {
Expand Down Expand Up @@ -236,7 +257,6 @@ class VirtualInlineTestEditor extends VirtualTestEditor {
editor.initPlugins()
.then( () => {
editor.ui.init();
editor.fire( 'uiReady' );
editor.fire( 'dataReady' );
editor.fire( 'ready' );
} )
Expand Down
12 changes: 4 additions & 8 deletions tests/inlineeditoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpa
import InlineEditableUIView from '@ckeditor/ckeditor5-ui/src/editableui/inline/inlineeditableuiview';
import Locale from '@ckeditor/ckeditor5-utils/src/locale';

import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'InlineEditorUIView', () => {
let locale, view;

testUtils.createSinonSandbox();

beforeEach( () => {
locale = new Locale( 'en' );
view = new InlineEditorUIView( locale );
Expand Down Expand Up @@ -117,14 +121,6 @@ describe( 'InlineEditorUIView', () => {
} );
} );

describe( 'editableElement', () => {
it( 'returns editable\'s view element', () => {
view.render();
expect( view.editableElement.getAttribute( 'contentEditable' ) ).to.equal( 'true' );
view.destroy();
} );
} );

describe( 'panelPositions', () => {
it( 'returns the positions in the right order', () => {
const positions = view.panelPositions;
Expand Down

0 comments on commit f8195da

Please sign in to comment.