Kaynağa Gözat

Added FormatEngine class.

Szymon Kupś 9 yıl önce
ebeveyn
işleme
ad19504e02

+ 55 - 0
packages/ckeditor5-heading/src/formatengine.js

@@ -0,0 +1,55 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import Feature from '../feature.js';
+import BuildModelConverterFor from '../engine/conversion/model-converter-builder.js';
+import BuildViewConverterFor from '../engine/conversion/view-converter-builder.js';
+
+export default class FormatEngine extends Feature {
+	// TODO - include Paragraph feature
+	init() {
+		const editor = this.editor;
+		const document = editor.document;
+		const schema = document.schema;
+		const data = editor.data;
+
+		// Schema.
+		schema.registerItem( 'heading1', '$block' );
+		schema.registerItem( 'heading2', '$block' );
+		schema.registerItem( 'heading3', '$block' );
+
+		// Build converter from model to view for data pipeline.
+		// TODO: Converter for editing pipeline.
+		BuildModelConverterFor( data.modelToView )
+			.fromElement( 'heading1' )
+			.toElement( 'h2' );
+
+		BuildModelConverterFor( data.modelToView )
+			.fromElement( 'heading2' )
+			.toElement( 'h3' );
+
+		BuildModelConverterFor( data.modelToView )
+			.fromElement( 'heading3' )
+			.toElement( 'h4' );
+
+		// Build converter from view to model for data pipeline.
+		// TODO: Converter for editing pipeline.
+		BuildViewConverterFor( data.viewToModel )
+			.fromElement( 'h2' )
+			.toElement( 'heading1' );
+
+		BuildViewConverterFor( data.viewToModel )
+			.fromElement( 'h3' )
+			.toElement( 'heading2' );
+
+		BuildViewConverterFor( data.viewToModel )
+			.fromElement( 'h4' )
+			.toElement( 'heading3' );
+
+		// TODO: register command.
+	}
+}

+ 67 - 0
packages/ckeditor5-heading/tests/formatengine.js

@@ -0,0 +1,67 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import FormatEngine from '/ckeditor5/format/formatengine.js';
+import Editor from '/ckeditor5/editor.js';
+import StandardCreator from '/ckeditor5/creator/standardcreator.js';
+import { getData } from '/tests/engine/_utils/model.js';
+
+describe( 'FormatEngine', () => {
+	let editor, document;
+
+	beforeEach( () => {
+		editor = new Editor( null, {
+			creator: StandardCreator,
+			features: [ FormatEngine ]
+		} );
+
+		return editor.init().then( () => {
+			document = editor.document;
+			document.createRoot( 'main' );
+		} );
+	} );
+
+	it( 'should be loaded', () => {
+		expect( editor.plugins.get( FormatEngine ) ).to.be.instanceOf( FormatEngine );
+	} );
+
+	it( 'should set proper schema rules', () => {
+		expect( document.schema.hasItem( 'heading1' ) ).to.be.true;
+		expect( document.schema.hasItem( 'heading2' ) ).to.be.true;
+		expect( document.schema.hasItem( 'heading3' ) ).to.be.true;
+
+		expect( document.schema.check( { name: 'heading1', inside: '$root' } ) ).to.be.true;
+		expect( document.schema.check( { name: '$inline', inside: 'heading1' } ) ).to.be.true;
+
+		expect( document.schema.check( { name: 'heading2', inside: '$root' } ) ).to.be.true;
+		expect( document.schema.check( { name: '$inline', inside: 'heading2' } ) ).to.be.true;
+
+		expect( document.schema.check( { name: 'heading3', inside: '$root' } ) ).to.be.true;
+		expect( document.schema.check( { name: '$inline', inside: 'heading3' } ) ).to.be.true;
+	} );
+
+	it( 'should convert heading1', () => {
+		editor.setData( '<h2>foobar</h2>' );
+
+		expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
+		expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
+	} );
+
+	it( 'should convert heading2', () => {
+		editor.setData( '<h3>foobar</h3>' );
+
+		expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading2>foobar</heading2>' );
+		expect( editor.getData() ).to.equal( '<h3>foobar</h3>' );
+	} );
+
+	it( 'should convert heading3', () => {
+		editor.setData( '<h4>foobar</h4>' );
+
+		expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
+		expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
+	} );
+} );