浏览代码

Introduced editor.config#formats and editor.config#defaultFormatId.

Aleksander Nowodzinski 8 年之前
父节点
当前提交
0dcce1e1cd

+ 1 - 1
packages/ckeditor5-heading/src/headingcommand.js

@@ -53,7 +53,7 @@ export default class HeadingCommand extends Command {
 	 */
 	get defaultFormat() {
 		// See https://github.com/ckeditor/ckeditor5/issues/98.
-		return this._getFormatById( 'paragraph' );
+		return this._getFormatById( this.editor.config.get( 'heading.defaultFormatId' ) );
 	}
 
 	/**

+ 25 - 8
packages/ckeditor5-heading/src/headingengine.js

@@ -13,13 +13,15 @@ import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildv
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import HeadingCommand from './headingcommand';
 
-const formats = [
-	{ id: 'paragraph', viewElement: 'p', label: 'Paragraph' },
-	{ id: 'heading1', viewElement: 'h2', label: 'Heading 1' },
-	{ id: 'heading2', viewElement: 'h3', label: 'Heading 2' },
-	{ id: 'heading3', viewElement: 'h4', label: 'Heading 3' }
+const defaultFormats = [
+	{ id: 'paragraph', element: 'p', label: 'Paragraph' },
+	{ id: 'heading1', element: 'h2', label: 'Heading 1' },
+	{ id: 'heading2', element: 'h3', label: 'Heading 2' },
+	{ id: 'heading3', element: 'h4', label: 'Heading 3' }
 ];
 
+const defaultFormatId = 'paragraph';
+
 /**
  * The headings engine feature. It handles switching between block formats – headings and paragraph.
  * This class represents the engine part of the heading feature. See also {@link module:heading/heading~Heading}.
@@ -30,6 +32,18 @@ export default class HeadingEngine extends Plugin {
 	/**
 	 * @inheritDoc
 	 */
+	constructor( editor ) {
+		super( editor );
+
+		editor.config.define( 'heading', {
+			formats: defaultFormats,
+			defaultFormatId: defaultFormatId
+		} );
+	}
+
+	/**
+	 * @inheritDoc
+	 */
 	static get requires() {
 		return [ Paragraph ];
 	}
@@ -41,21 +55,23 @@ export default class HeadingEngine extends Plugin {
 		const editor = this.editor;
 		const data = editor.data;
 		const editing = editor.editing;
+		const formats = editor.config.get( 'heading.formats' );
+		const defaultFormatId = editor.config.get( 'heading.defaultFormatId' );
 
 		for ( let format of formats ) {
 			// Skip paragraph - it is defined in required Paragraph feature.
-			if ( format.id !== 'paragraph' ) {
+			if ( format.id !== defaultFormatId ) {
 				// Schema.
 				editor.document.schema.registerItem( format.id, '$block' );
 
 				// Build converter from model to view for data and editing pipelines.
 				buildModelConverter().for( data.modelToView, editing.modelToView )
 					.fromElement( format.id )
-					.toElement( format.viewElement );
+					.toElement( format.element );
 
 				// Build converter from view to model for data pipeline.
 				buildViewConverter().for( data.viewToModel )
-					.fromElement( format.viewElement )
+					.fromElement( format.element )
 					.toElement( format.id );
 			}
 		}
@@ -75,6 +91,7 @@ export default class HeadingEngine extends Plugin {
 		const editor = this.editor;
 		const command = editor.commands.get( 'heading' );
 		const enterCommand = editor.commands.get( 'enter' );
+		const formats = editor.config.get( 'heading.formats' );
 
 		if ( enterCommand ) {
 			this.listenTo( enterCommand, 'afterExecute', ( evt, data ) => {

+ 22 - 18
packages/ckeditor5-heading/tests/headingcommand.js

@@ -9,29 +9,33 @@ import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 const formats = [
-	{ id: 'paragraph', viewElement: 'p', default: true },
-	{ id: 'heading1', viewElement: 'h2' },
-	{ id: 'heading2', viewElement: 'h3' },
-	{ id: 'heading3', viewElement: 'h4' }
+	{ id: 'paragraph', element: 'p', default: true },
+	{ id: 'heading1', element: 'h2' },
+	{ id: 'heading2', element: 'h3' },
+	{ id: 'heading3', element: 'h4' }
 ];
 
 describe( 'HeadingCommand', () => {
 	let editor, document, command, root, schema;
 
 	beforeEach( () => {
-		return ModelTestEditor.create()
-			.then( newEditor => {
-				editor = newEditor;
-				document = editor.document;
-				command = new HeadingCommand( editor, formats );
-				schema = document.schema;
-
-				for ( let format of formats ) {
-					schema.registerItem( format.id, '$block' );
-				}
-
-				root = document.getRoot();
-			} );
+		return ModelTestEditor.create( {
+			heading: {
+				defaultFormatId: 'paragraph'
+			}
+		} )
+		.then( newEditor => {
+			editor = newEditor;
+			document = editor.document;
+			command = new HeadingCommand( editor, formats );
+			schema = document.schema;
+
+			for ( let format of formats ) {
+				schema.registerItem( format.id, '$block' );
+			}
+
+			root = document.getRoot();
+		} );
 	} );
 
 	afterEach( () => {
@@ -71,7 +75,7 @@ describe( 'HeadingCommand', () => {
 			expect( getData( document ) ).to.equal( '<heading1>[]</heading1>' );
 			expect( command.value ).to.be.object;
 			expect( command.value.id ).to.equal( 'heading1' );
-			expect( command.value.viewElement ).to.equal( 'h2' );
+			expect( command.value.element ).to.equal( 'h2' );
 		} );
 
 		describe( 'custom options', () => {

+ 70 - 0
packages/ckeditor5-heading/tests/headingengine.js

@@ -92,4 +92,74 @@ describe( 'HeadingEngine', () => {
 
 		expect( getData( document ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
 	} );
+
+	describe( 'config', () => {
+		describe( 'formats', () => {
+			it( 'should have default value', () => {
+				expect( editor.config.get( 'heading.formats' ) ).to.deep.equal( [
+					{ id: 'paragraph', element: 'p', label: 'Paragraph' },
+					{ id: 'heading1', element: 'h2', label: 'Heading 1' },
+					{ id: 'heading2', element: 'h3', label: 'Heading 2' },
+					{ id: 'heading3', element: 'h4', label: 'Heading 3' }
+				] );
+			} );
+
+			it( 'should customize formats', () => {
+				const formats = [
+					{ id: 'paragraph', element: 'p', label: 'Paragraph' },
+					{ id: 'h4', element: 'h4', label: 'H4' }
+				];
+
+				return VirtualTestEditor.create( {
+					plugins: [ Enter, HeadingEngine ],
+					heading: {
+						formats: formats
+					}
+				} )
+				.then( editor => {
+					document = editor.document;
+
+					expect( editor.commands.get( 'heading' ).formats ).to.deep.equal( formats );
+
+					expect( document.schema.hasItem( 'paragraph' ) ).to.be.true;
+					expect( document.schema.hasItem( 'h4' ) ).to.be.true;
+
+					expect( document.schema.hasItem( 'heading1' ) ).to.be.false;
+					expect( document.schema.hasItem( 'heading2' ) ).to.be.false;
+					expect( document.schema.hasItem( 'heading3' ) ).to.be.false;
+				} );
+			} );
+		} );
+
+		describe( 'defaultFormatId', () => {
+			it( 'should have default value', () => {
+				expect( editor.config.get( 'heading.defaultFormatId' ) ).to.equal( 'paragraph' );
+			} );
+
+			it( 'should customize formats', () => {
+				return VirtualTestEditor.create( {
+					plugins: [ Enter, HeadingEngine ],
+					heading: {
+						formats: [
+							{ id: 'foo', element: 'f', label: 'Foo' },
+							{ id: 'bar', element: 'b', label: 'Bar' }
+						],
+						defaultFormatId: 'bar'
+					}
+				} )
+				.then( editor => {
+					document = editor.document;
+
+					expect( editor.commands.get( 'heading' ).value ).to.deep.equal( {
+						id: 'bar',
+						element: 'b',
+						label: 'Bar'
+					} );
+
+					expect( document.schema.hasItem( 'foo' ) ).to.be.true;
+					expect( document.schema.hasItem( 'bar' ) ).to.be.false;
+				} );
+			} );
+		} );
+	} );
 } );