浏览代码

Merge pull request #2 from ckeditor/t/1

t/1: First implementation of the creator.
Piotrek Koszuliński 9 年之前
父节点
当前提交
d8ed84b47e

+ 130 - 0
packages/ckeditor5-editor-classic/src/classiccreator.js

@@ -0,0 +1,130 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import StandardCreator from '/ckeditor5/creator/standardcreator.js';
+
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+import Editable from '/ckeditor5/editable.js';
+
+import { createEditableUI, createEditorUI } from '/ckeditor5/ui/creator-utils.js';
+
+import BoxedEditorUI from '/ckeditor5/ui/editorui/boxed/boxededitorui.js';
+import BoxedEditorUIView from '/ckeditor5/ui/editorui/boxed/boxededitoruiview.js';
+
+import EditableUI from '/ckeditor5/ui/editableui/editableui.js';
+import InlineEditableUIView from '/ckeditor5/ui/editableui/inline/inlineeditableuiview.js';
+
+import Model from '/ckeditor5/ui/model.js';
+import StickyToolbar from '/ckeditor5/ui/bindings/stickytoolbar.js';
+import StickyToolbarView from '/ckeditor5/ui/stickytoolbar/stickytoolbarview.js';
+
+import { imitateFeatures, imitateDestroyFeatures } from './utils/imitatefeatures.js';
+
+/**
+ * Classic editor creator using inline editable and sticky toolbar, all
+ * enclosed in a boxed UI.
+ *
+ * @memberOf creator-classic
+ * @extends ckeditor5.creator.StandardCreator
+ */
+export default class ClassicCreator extends StandardCreator {
+	/**
+	 * Creates an instance of the classic creator.
+	 *
+	 * @param {ckeditor5.Editor} The editor instance.
+	 */
+	constructor( editor ) {
+		super( editor, new HtmlDataProcessor() );
+
+		const editableName = editor.firstElementName;
+		editor.editables.add( new Editable( editor, editableName ) );
+		editor.document.createRoot( editableName );
+
+		// UI.
+		createEditorUI( editor, BoxedEditorUI, BoxedEditorUIView );
+
+		// Data controller mock.
+		this._mockDataController();
+	}
+
+	/**
+	 * Creates editor UI and loads startup data into the editor.
+	 *
+	 * @returns {Promise}
+	 */
+	create() {
+		const editor = this.editor;
+		const editable = editor.editables.get( 0 );
+
+		// Features mock.
+		imitateFeatures( editor );
+
+		// UI.
+		this._replaceElement( editor.firstElement, editor.ui.view.element );
+		this._createToolbar();
+		editor.ui.add( 'main', createEditableUI( editor, editable, EditableUI, InlineEditableUIView ) );
+
+		// Init.
+		return super.create()
+			.then( () => editor.ui.init() )
+			// We'll be able to do that much earlier once the loading will be done to the document model,
+			// rather than straight to the editable.
+			.then( () => this.loadDataFromEditorElement() );
+	}
+
+	/**
+	 * Updates the original editor element with data and destroys
+	 * the UI.
+	 *
+	 * @returns {Promise}
+	 */
+	destroy() {
+		imitateDestroyFeatures();
+
+		this.updateEditorElement();
+
+		super.destroy();
+
+		return this.editor.ui.destroy();
+	}
+
+	/**
+	 * Creates editor sticky toolbar and fills it with children using the configuration.
+	 *
+	 * @protected
+	 */
+	_createToolbar() {
+		const editor = this.editor;
+
+		// Note: StickyToolbar and StickyToolbarView share the same model. It may change in the future.
+		const toolbarModel = new Model();
+		const toolbarView = new StickyToolbarView( toolbarModel, editor.locale );
+		const toolbar = new StickyToolbar( toolbarModel, toolbarView, editor );
+
+		toolbar.addButtons( editor.config.toolbar );
+
+		this.editor.ui.add( 'top', toolbar );
+	}
+
+	/**
+	 * TEMP: Mocks basic data IO for the purposes of the creator.
+	 * TODO: To be replaced with actual engine bindings.
+	 *
+	 * @protected
+	 */
+	_mockDataController() {
+		const editor = this.editor;
+
+		editor.data.get = ( rootName ) => {
+			return editor.editables.get( rootName ).domElement.innerHTML + `<p>getData( '${ rootName }' )</p>`;
+		};
+
+		this.editor.data.set = ( rootName, data ) => {
+			editor.editables.get( rootName ).domElement.innerHTML = data + `<p>setData( '${ rootName }' )</p>`;
+		};
+	}
+}

+ 65 - 0
packages/ckeditor5-editor-classic/src/utils/imitatefeatures.js

@@ -0,0 +1,65 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Model from '/ckeditor5/ui/model.js';
+import Button from '/ckeditor5/ui/button/button.js';
+import ButtonView from '/ckeditor5/ui/button/buttonview.js';
+
+/**
+ * Immitates that some features were loaded and did their job.
+ *
+ * @param {ckeditor5.Editor} editor
+ */
+export function imitateFeatures( editor ) {
+	const t = editor.t;
+
+	const boldModel = new Model( {
+		isEnabled: true,
+		isOn: false,
+		label: t( 'Bold' ),
+		icon: 'bold'
+	} );
+
+	// Note – most of the contents of this file is ignored, as it's a temporary file that will
+	// be replaced with real features.
+
+	/* istanbul ignore next */
+	boldModel.on( 'execute', () => {
+		/* global console */
+		console.log( 'bold executed' );
+
+		boldModel.isOn = !boldModel.isOn;
+	} );
+
+	editor.ui.featureComponents.add( 'bold', Button, ButtonView, boldModel );
+
+	/* istanbul ignore next */
+	const italicModel = new Model( {
+		isEnabled: true,
+		isOn: false,
+		label: t( 'Italic' ),
+		icon: 'italic'
+	} );
+
+	/* istanbul ignore next */
+	italicModel.on( 'execute', () => {
+		/* global console */
+		console.log( 'italic executed' );
+
+		italicModel.isOn = !italicModel.isOn;
+	} );
+
+	editor.ui.featureComponents.add( 'italic', Button, ButtonView, italicModel );
+
+	window.boldModel = boldModel;
+	window.italicModel = italicModel;
+}
+
+export function imitateDestroyFeatures() {
+	delete window.boldModel;
+	delete window.italicModel;
+}

+ 3 - 0
packages/ckeditor5-editor-classic/tests/classiccreator.html

@@ -0,0 +1,3 @@
+<head>
+	<link rel="stylesheet" href="%APPS_DIR%ckeditor/build/amd/theme/ckeditor.css">
+</head>

+ 185 - 0
packages/ckeditor5-editor-classic/tests/classiccreator.js

@@ -0,0 +1,185 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: creator */
+
+'use strict';
+
+import Editor from '/ckeditor5/editor.js';
+
+import BoxedEditorUI from '/ckeditor5/ui/editorui/boxed/boxededitorui.js';
+import BoxedEditorUIView from '/ckeditor5/ui/editorui/boxed/boxededitoruiview.js';
+
+import EditableUI from '/ckeditor5/ui/editableui/editableui.js';
+import InlineEditableUIView from '/ckeditor5/ui/editableui/inline/inlineeditableuiview.js';
+
+import StickyToolbar from '/ckeditor5/ui/bindings/stickytoolbar.js';
+import StickyToolbarView from '/ckeditor5/ui/stickytoolbar/stickytoolbarview.js';
+
+import StandardCreator from '/ckeditor5/creator/standardcreator.js';
+import ClassicCreator from '/ckeditor5/creator-classic/classiccreator.js';
+import HtmlDataProcessor from '/ckeditor5/engine/dataprocessor/htmldataprocessor.js';
+
+import testUtils from '/tests/ckeditor5/_utils/utils.js';
+
+testUtils.createSinonSandbox();
+
+describe( 'ClassicCreator', () => {
+	let creator, editor, fooElement, barElement;
+
+	beforeEach( function() {
+		fooElement = document.createElement( 'div' );
+		fooElement.setAttribute( 'class', 'first' );
+		fooElement.setAttribute( 'data-test', this.currentTest.title );
+
+		document.body.appendChild( fooElement );
+
+		barElement = document.createElement( 'div' );
+		barElement.setAttribute( 'class', 'second' );
+		barElement.setAttribute( 'data-test', this.currentTest.title );
+
+		document.body.appendChild( barElement );
+
+		editor = new Editor(
+			{
+				foo: fooElement,
+				bar: barElement
+			},
+			{
+				toolbar: [ 'bold', 'italic' ]
+			}
+		);
+
+		creator = new ClassicCreator( editor, new HtmlDataProcessor() );
+	} );
+
+	describe( 'constructor', () => {
+		it( 'inherits from the StandardCreator', () => {
+			expect( creator ).to.be.instanceof( StandardCreator );
+		} );
+
+		it( 'creates a single instance of Editable ', () => {
+			expect( editor.editables ).to.have.length( 1 );
+			expect( editor.editables.get( 0 ).name ).to.equal( 'foo' );
+		} );
+
+		it( 'creates the UI using BoxedEditorUI classes', () => {
+			expect( editor.ui ).to.be.instanceof( BoxedEditorUI );
+			expect( editor.ui.view ).to.be.instanceof( BoxedEditorUIView );
+		} );
+
+		it( 'creates a single document root', () => {
+			expect( editor.document.rootNames ).to.have.members( [ 'foo' ] );
+		} );
+	} );
+
+	describe( 'create', () => {
+		it( 'returns a promise', () => {
+			expect( creator.create() ).to.be.instanceof( Promise );
+		} );
+
+		it( 'calls _replaceElement', () => {
+			const spy = testUtils.sinon.spy( creator, '_replaceElement' );
+
+			return creator.create().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+
+		it( 'calls _createToolbar', () => {
+			const spy = testUtils.sinon.spy( creator, '_createToolbar' );
+
+			return creator.create().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+
+		it( 'creates the editable using EditableUI Controller in main region', () => {
+			return creator.create().then( () => {
+				expect( editor.ui.collections.get( 'main' ) ).to.have.length( 1 );
+				expect( editor.ui.collections.get( 'main' ).get( 0 ) ).to.be.instanceof( EditableUI );
+			} );
+		} );
+
+		it( 'creates the editable using InlineEditableUIView in main region', () => {
+			return creator.create().then( () => {
+				expect( editor.ui.collections.get( 'main' ).get( 0 ).view ).to.be.instanceof( InlineEditableUIView );
+			} );
+		} );
+
+		it( 'binds the editable to InlineEditableUIView', () => {
+			return creator.create().then( () => {
+				const editableUIView = editor.ui.collections.get( 'main' ).get( 0 ).view;
+
+				expect( editor.editables.get( 0 ).domElement ).to.equal( editableUIView.element );
+			} );
+		} );
+
+		it( 'initializes the editor.ui', () => {
+			const spy = testUtils.sinon.spy( editor.ui, 'init' );
+
+			return creator.create().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+
+		it( 'calls loadDataFromEditorElement', () => {
+			const spy = testUtils.sinon.spy( creator, 'loadDataFromEditorElement' );
+
+			return creator.create().then( () => {
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
+	} );
+
+	describe( 'destroy', () => {
+		it( 'returns a promise', () => {
+			return creator.create().then( () => {
+				expect( creator.destroy() ).to.be.instanceof( Promise );
+			} );
+		} );
+
+		it( 'destroys parent class', () => {
+			const spy = testUtils.sinon.spy( StandardCreator.prototype, 'destroy' );
+
+			return creator.create().then( () => {
+				return creator.destroy().then( () => {
+					expect( spy.calledOnce ).to.be.true;
+				} );
+			} );
+		} );
+
+		it( 'calls updateEditorElement', () => {
+			const spy = testUtils.sinon.spy( creator, 'updateEditorElement' );
+
+			return creator.create().then( () => {
+				return creator.destroy().then( () => {
+					expect( spy.calledOnce ).to.be.true;
+				} );
+			} );
+		} );
+
+		it( 'destroys the UI', () => {
+			return creator.create().then( () => {
+				return creator.destroy().then( () => {
+					expect( editor.ui.collections ).to.be.null;
+				} );
+			} );
+		} );
+	} );
+
+	describe( '_createToolbar', () => {
+		it( 'creates toolbar using StickyToolbar and StickyToolbarView', () => {
+			return creator.create().then( () => {
+				expect( editor.ui.collections.get( 'top' ) ).to.have.length( 1 );
+
+				const toolbar = editor.ui.collections.get( 'top' ).get( 0 );
+
+				expect( toolbar ).to.be.instanceof( StickyToolbar );
+				expect( toolbar.view ).to.be.instanceof( StickyToolbarView );
+			} );
+		} );
+	} );
+} );

+ 17 - 0
packages/ckeditor5-editor-classic/tests/manual/classiccreator.html

@@ -0,0 +1,17 @@
+<head>
+	<link rel="stylesheet" href="%APPS_DIR%ckeditor/build/amd/theme/ckeditor.css">
+</head>
+
+<p>
+	<button id="destroyEditor">Destroy editor</button>
+	<button id="initEditor">Init editor</button>
+</p>
+
+<div id="editor">
+	<h1>Hello world!</h1>
+	<p>This is an editor instance.</p>
+</div>
+
+<div style="height: 1500px; width: 1500px; background: #eee; outline: 2px dashed #ddd; margin: 1em 0; padding: 1em;">
+	* I'm a dummy div to enable scrolling in this test. *
+</div>

+ 47 - 0
packages/ckeditor5-editor-classic/tests/manual/classiccreator.js

@@ -0,0 +1,47 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* global console:false */
+
+'use strict';
+
+import CKEDITOR from '/ckeditor.js';
+import ClassicCreator from '/ckeditor5/creator-classic/classiccreator.js';
+import testUtils from '/tests/utils/_utils/utils.js';
+
+let editor, editable, observer;
+
+function initEditor() {
+	CKEDITOR.create( '#editor', {
+		creator: ClassicCreator,
+		toolbar: [ 'bold', 'italic' ]
+	} )
+	.then( ( newEditor ) => {
+		console.log( 'Editor was initialized', newEditor );
+		console.log( 'You can now play with it using global `editor` and `editable` variables.' );
+
+		window.editor = editor = newEditor;
+		window.editable = editable = editor.editables.get( 0 );
+
+		observer = testUtils.createObserver();
+		observer.observe( 'Editable', editable );
+	} );
+}
+
+function destroyEditor() {
+	editor.destroy()
+		.then( () => {
+			window.editor = editor = null;
+			window.editable = editable = null;
+
+			observer.stopListening();
+			observer = null;
+
+			console.log( 'Editor was destroyed' );
+		} );
+}
+
+document.getElementById( 'initEditor' ).addEventListener( 'click', initEditor );
+document.getElementById( 'destroyEditor' ).addEventListener( 'click', destroyEditor );

+ 22 - 0
packages/ckeditor5-editor-classic/tests/manual/classiccreator.md

@@ -0,0 +1,22 @@
+@bender-ui: collapsed
+
+1. Click "Init editor".
+2. Expected:
+  * Framed editor should be created.
+  * Original element should disappear.
+  * There should be a toolbar with "Bold" and "Italic" buttons.
+3. Click "Destroy editor".
+4. Expected:
+  * Editor should be destroyed.
+  * Original element should be visible.
+  * The element should contain its data (updated).
+  * The 'ck-body region' should be removed.
+
+## Notes:
+
+* You can play with:
+  * `editable.isEditable`,
+  * `boldModel.isEnabled` and `italicModel.isEnabled`.
+* Changes to `editable.isFocused/isEditable` should be logged to the console.
+* Clicks on the buttons should be logged to the console.
+