浏览代码

Added draft of ClassicCreator.

Aleksander Nowodzinski 9 年之前
父节点
当前提交
055a842255

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

@@ -0,0 +1,93 @@
+/**
+ * @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/boxededitorui/boxededitorui.js';
+import BoxedEditorUIView from '/ckeditor5/ui/boxededitorui/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 Toolbar from '/ckeditor5/ui/bindings/stickytoolbar.js';
+import StickyToolbarView from '/ckeditor5/ui/stickytoolbar/stickytoolbarview.js';
+
+import { imitateFeatures, imitateDestroyFeatures } from './utils/imitatefeatures.js';
+
+export default class ClassicCreator extends StandardCreator {
+	constructor( editor ) {
+		super( editor, new HtmlDataProcessor() );
+
+		const editableName = editor.firstElementName;
+		editor.editables.add( new Editable( editor, editableName ) );
+		editor.document.createRoot( editableName, '$root' );
+
+		// UI.
+		createEditorUI( editor, BoxedEditorUI, BoxedEditorUIView );
+
+		// Data controller mock.
+		this._mockDataController();
+	}
+
+	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( 'editable', 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() );
+	}
+
+	destroy() {
+		imitateDestroyFeatures();
+
+		this.updateEditorElement();
+
+		super.destroy();
+
+		return this.editor.ui.destroy();
+	}
+
+	_createToolbar() {
+		const editor = this.editor;
+		const toolbarModel = new Model();
+		const toolbar = new Toolbar( toolbarModel, new StickyToolbarView( toolbarModel, editor.locale ), editor );
+
+		toolbar.addButtons( editor.config.toolbar );
+
+		this.editor.ui.add( 'top', toolbar );
+	}
+
+	_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>`;
+		};
+	}
+}

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

@@ -0,0 +1,59 @@
+/**
+ * @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'
+	} );
+
+	boldModel.on( 'execute', () => {
+		/* global console */
+		console.log( 'bold executed' );
+
+		boldModel.isOn = !boldModel.isOn;
+	} );
+
+	editor.ui.featureComponents.add( 'bold', Button, ButtonView, boldModel );
+
+	const italicModel = new Model( {
+		isEnabled: true,
+		isOn: false,
+		label: t( 'Italic' ),
+		icon: 'italic'
+	} );
+
+	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;
+}