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

Commit

Permalink
Merge 3babb31 into 180e6d4
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Jan 21, 2019
2 parents 180e6d4 + 3babb31 commit a510e2c
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 51 deletions.
25 changes: 5 additions & 20 deletions src/classiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import attachToForm from '@ckeditor/ckeditor5-core/src/editor/utils/attachtoform
import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
import ClassicEditorUI from './classiceditorui';
import ClassicEditorUIView from './classiceditoruiview';
import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';
import getDataFromElement from '@ckeditor/ckeditor5-utils/src/dom/getdatafromelement';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import log from '@ckeditor/ckeditor5-utils/src/log';
import { isElement } from 'lodash-es';

/**
Expand Down Expand Up @@ -66,14 +66,6 @@ export default class ClassicEditor extends Editor {
this.sourceElement = sourceElementOrData;
}

/**
* The element replacer instance used to hide the editor's source element.
*
* @protected
* @member {module:utils/elementreplacer~ElementReplacer}
*/
this._elementReplacer = new ElementReplacer();

this.data.processor = new HtmlDataProcessor();

this.model.document.createRoot();
Expand All @@ -87,7 +79,8 @@ export default class ClassicEditor extends Editor {
* @inheritDoc
*/
get element() {
return this.ui.view.element;
log.warn( 'deprecated-editor-element: The editor#element is deprecated.' );
return this.ui.element;
}

/**
Expand All @@ -102,7 +95,6 @@ export default class ClassicEditor extends Editor {
this.updateSourceElement();
}

this._elementReplacer.restore();
this.ui.destroy();

return super.destroy();
Expand Down Expand Up @@ -189,15 +181,8 @@ export default class ClassicEditor extends Editor {

resolve(
editor.initPlugins()
.then( () => editor.ui.init() )
.then( () => {
if ( isElement( sourceElementOrData ) ) {
editor._elementReplacer.replace( sourceElementOrData, editor.element );
}

editor.fire( 'uiReady' );
} )
.then( () => editor.editing.view.attachDomRoot( editor.ui.view.editableElement ) )
.then( () => editor.ui.init( isElement( sourceElementOrData ) ? sourceElementOrData : null ) )
.then( () => editor.editing.view.attachDomRoot( editor.ui.getEditableElement() ) )
.then( () => {
const initialData = isElement( sourceElementOrData ) ?
getDataFromElement( sourceElementOrData ) :
Expand Down
70 changes: 63 additions & 7 deletions src/classiceditorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
import enableToolbarKeyboardFocus from '@ckeditor/ckeditor5-ui/src/toolbar/enabletoolbarkeyboardfocus';
import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalizetoolbarconfig';
import ElementReplacer from '@ckeditor/ckeditor5-utils/src/elementreplacer';

/**
* The classic editor UI class.
Expand All @@ -18,24 +19,62 @@ import normalizeToolbarConfig from '@ckeditor/ckeditor5-ui/src/toolbar/normalize
*/
export default class ClassicEditorUI extends EditorUI {
/**
* @inheritDoc
* Creates an instance of the classic 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.
*
* @type {Object}
* @private
* @member {Object}
*/
this._toolbarConfig = normalizeToolbarConfig( editor.config.get( 'toolbar' ) );

/**
* The element replacer instance used to hide the editor's source element.
*
* @protected
* @member {module:utils/elementreplacer~ElementReplacer}
*/
this._elementReplacer = new ElementReplacer();
}

/**
* 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.element;
}

/**
* Initializes the UI.
*
* @param {HTMLElement|null} replacementElement The DOM element that will be the source for the created editor.
*/
init() {
init( replacementElement ) {
const editor = this.editor;
const view = this.view;

Expand All @@ -55,15 +94,32 @@ export default class ClassicEditorUI extends EditorUI {
view.editable.bind( 'isFocused' ).to( editor.editing.view.document );
view.editable.name = editingRoot.rootName;

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

this.view.toolbar.fillFromConfig( this._toolbarConfig.items, this.componentFactory );
this.focusTracker.add( view.editable.element );

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

enableToolbarKeyboardFocus( {
origin: editor.editing.view,
originFocusTracker: this.focusTracker,
originKeystrokeHandler: editor.keystrokes,
toolbar: this.view.toolbar
toolbar: view.toolbar
} );

if ( replacementElement ) {
this._elementReplacer.replace( replacementElement, this.element );
}

this.fire( 'ready' );
}

/**
* @inheritDoc
*/
destroy() {
this._elementReplacer.restore();

super.destroy();
}
}
7 changes: 0 additions & 7 deletions src/classiceditoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,4 @@ export default class ClassicEditorUIView extends BoxedEditorUIView {
this.top.add( this.stickyPanel );
this.main.add( this.editable );
}

/**
* @inheritDoc
*/
get editableElement() {
return this.editable.element;
}
}
28 changes: 27 additions & 1 deletion tests/classiceditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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';

describe( 'ClassicEditor', () => {
let editor, editorElement;
Expand All @@ -30,6 +31,8 @@ describe( 'ClassicEditor', () => {
editorElement.innerHTML = '<p><strong>foo</strong> bar</p>';

document.body.appendChild( editorElement );

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

afterEach( () => {
Expand Down Expand Up @@ -201,6 +204,7 @@ describe( 'ClassicEditor', () => {
class EventWatcher extends Plugin {
init() {
this.editor.on( 'pluginsReady', spy );
this.editor.ui.on( 'ready', spy );
this.editor.on( 'uiReady', spy );
this.editor.on( 'dataReady', spy );
this.editor.on( 'ready', spy );
Expand All @@ -212,7 +216,7 @@ describe( 'ClassicEditor', () => {
plugins: [ EventWatcher ]
} )
.then( newEditor => {
expect( fired ).to.deep.equal( [ 'pluginsReady', 'uiReady', 'dataReady', 'ready' ] );
expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'uiReady', 'dataReady', 'ready' ] );

editor = newEditor;
} );
Expand Down Expand Up @@ -261,6 +265,28 @@ describe( 'ClassicEditor', () => {
editor = newEditor;
} );
} );

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

class EventWatcher extends Plugin {
init() {
this.editor.ui.on( 'ready', () => {
isReady = this.editor.ui.view.isRendered;
} );
}
}

return ClassicEditor
.create( editorElement, {
plugins: [ EventWatcher ]
} )
.then( newEditor => {
expect( isReady ).to.be.true;

editor = newEditor;
} );
} );
} );

describe( 'destroy', () => {
Expand Down
21 changes: 20 additions & 1 deletion tests/classiceditorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ describe( 'ClassicEditorUI', () => {
} );
} );
} );

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

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 null if editable with the given name is absent', () => {
expect( ui.getEditableElement( 'absent' ) ).to.null;
} );
} );
} );

function viewCreator( name ) {
Expand Down Expand Up @@ -216,7 +236,6 @@ class VirtualClassicTestEditor extends VirtualTestEditor {
editor.initPlugins()
.then( () => {
editor.ui.init();
editor.fire( 'uiReady' );
editor.fire( 'dataReady' );
editor.fire( 'ready' );
} )
Expand Down
19 changes: 4 additions & 15 deletions tests/classiceditoruiview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
* For licensing, see LICENSE.md.
*/

/* globals document */

import ClassicEditorUIView from '../src/classiceditoruiview';
import StickyPanelView from '@ckeditor/ckeditor5-ui/src/panel/sticky/stickypanelview';
import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
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( 'ClassicEditorUIView', () => {
let locale, view;

testUtils.createSinonSandbox();

beforeEach( () => {
locale = new Locale( 'en' );
view = new ClassicEditorUIView( locale );
Expand Down Expand Up @@ -67,17 +69,4 @@ describe( 'ClassicEditorUIView', () => {
} );
} );
} );

describe( 'editableElement', () => {
it( 'returns editable\'s view element', () => {
document.body.appendChild( view.element );

view.stickyPanel.limiterElement = view.element;

expect( view.editableElement.getAttribute( 'contentEditable' ) ).to.equal( 'true' );

view.element.remove();
view.destroy();
} );
} );
} );

0 comments on commit a510e2c

Please sign in to comment.