8
0
Просмотр исходного кода

Simplified adding default 'h1' -> 'heading1' converter.

Krzysztof Krztoń 7 лет назад
Родитель
Сommit
9130d99e3e

+ 9 - 23
packages/ckeditor5-heading/src/headingediting.js

@@ -69,7 +69,7 @@ export default class HeadingEditing extends Plugin {
 			}
 		}
 
-		this._addDefaultH1Conversion( editor, options );
+		this._addDefaultH1Conversion( editor );
 
 		// Register the heading command for this option.
 		editor.commands.add( 'heading', new HeadingCommand( editor, modelElements ) );
@@ -100,32 +100,18 @@ export default class HeadingEditing extends Plugin {
 	/**
 	 * Adds default conversion for `h1` -> `heading1` with a low priority.
 	 *
-	 * The default conversion will be added only if `heading.options` configuration with `h2` -> `heading1`
-	 * conversion is defined and there are no other conversion definitions for `h1` provided.
-	 *
 	 * @private
 	 * @param {module:core/editor/editor~Editor} editor Editor instance on which to add the `h1` conversion.
-	 * @param {Array.<module:engine/conversion/conversion~ConverterDefinition} definitions List of already used conversion
-	 * definitions. The added default `h1` conversion is based on this list.
 	 */
-	_addDefaultH1Conversion( editor, definitions ) {
-		// Do not add default conversions if conversion for `<h1>` is already defined.
-		if ( definitions.find( option => option.view === 'h1' ) ) {
-			return;
-		}
-
-		// Add `h1` -> `heading1` conversion with a low priority. This means if no other conversions were provided,
-		// `h1` will be handled here. Proceed only if `h2` -> `heading1` conversion was configured.
-		const heading1 = definitions.find( option => option.model === 'heading1' && option.view === 'h2' );
-		if ( heading1 ) {
-			const optionH1 = Object.assign( {}, heading1 );
-
-			optionH1.view = 'h1';
+	_addDefaultH1Conversion( editor ) {
+		editor.conversion.elementToElement( {
+			model: 'heading1',
+			view: 'h1',
+			title: 'Heading 1',
+			class: 'ck-heading_heading1',
 			// With a `low` priority, `paragraph` plugin autoparagraphing mechanism is executed. Make sure
 			// this listener is called before it. If not, `h1` will be transformed into a paragraph.
-			optionH1.converterPriority = priorities.get( 'low' ) + 1;
-
-			editor.conversion.elementToElement( optionH1 );
-		}
+			converterPriority: priorities.get( 'low' ) + 1
+		} );
 	}
 }

+ 16 - 81
packages/ckeditor5-heading/tests/headingediting.js

@@ -135,32 +135,23 @@ describe( 'HeadingEditing', () => {
 			elementToElementSpy = testUtils.sinon.spy( Conversion.prototype, 'elementToElement' );
 		} );
 
-		it( 'should set default h1 conversion for default `options.heading` config', () => {
+		it( 'should set default h1 conversion with default `options.heading` config', () => {
 			return VirtualTestEditor
 				.create( {
 					plugins: [ HeadingEditing ]
 				} )
 				.then( () => {
-					expect( elementToElementSpy.calledWith( getDefaultConversion() ) ).to.true;
+					expect( elementToElementSpy.calledWith( {
+						model: 'heading1',
+						view: 'h1',
+						title: 'Heading 1',
+						class: 'ck-heading_heading1',
+						converterPriority: priorities.get( 'low' ) + 1
+					} ) ).to.true;
 				} );
 		} );
 
-		it( 'should set default h1 conversion based on provided config if `h2` -> `heading1` conversion defined', () => {
-			const options = [
-				{ model: 'heading1', view: 'h2', title: 'User H2' }
-			];
-
-			return VirtualTestEditor
-				.create( {
-					plugins: [ HeadingEditing ],
-					heading: { options }
-				} )
-				.then( () => {
-					expect( elementToElementSpy.calledWith( getDefaultConversion( options[ 0 ] ) ) ).to.true;
-				} );
-		} );
-
-		it( 'should not set default h1 conversion if no `h2` -> `heading1` conversion defined', () => {
+		it( 'should set default h1 conversion independently from `options.heading` config', () => {
 			const options = [
 				{ model: 'heading1', view: 'h3', title: 'User H3' },
 				{ model: 'heading2', view: 'h4', title: 'User H4' }
@@ -172,71 +163,15 @@ describe( 'HeadingEditing', () => {
 					heading: { options }
 				} )
 				.then( () => {
-					expect( elementToElementSpy.neverCalledWith( getDefaultConversion( options[ 0 ] ) ) ).to.true;
-				} );
-		} );
-
-		it( 'should use user defined h1 conversion (low + 1 priority)', () => {
-			const options = [
-				{ model: 'paragraph', title: 'paragraph' },
-				{ model: 'heading3', view: 'h1', title: 'User H1', converterPriority: priorities.get( 'low' ) + 1 },
-				{ model: 'heading4', view: 'h2', title: 'User H2' }
-			];
-
-			return VirtualTestEditor
-				.create( {
-					plugins: [ HeadingEditing ],
-					heading: { options }
-				} )
-				.then( editor => {
-					editor.setData( '<h2>h2</h2><h1>h1</h1>' );
-
-					expect( getData( editor.model, { withoutSelection: true } ) )
-						.to.equal( '<heading4>h2</heading4><heading3>h1</heading3>' );
-
-					expect( editor.getData() ).to.equal( '<h2>h2</h2><h1>h1</h1>' );
-				} );
-		} );
-
-		it( 'should use user defined h1 conversion not adding default one (default priority)', () => {
-			const options = [
-				{ model: 'paragraph', title: 'paragraph' },
-				{ model: 'heading1', view: 'h6', title: 'User H6' },
-				{ model: 'heading2', view: 'h1', title: 'User H1' },
-				{ model: 'heading3', view: 'h2', title: 'User H2' }
-			];
-
-			return VirtualTestEditor
-				.create( {
-					plugins: [ HeadingEditing ],
-					heading: { options }
-				} )
-				.then( editor => {
-					editor.setData( '<h2>h2</h2><h1>h1</h1>' );
-
-					expect( elementToElementSpy.neverCalledWith( getDefaultConversion() ) ).to.true;
-					expect( elementToElementSpy.neverCalledWith( getDefaultConversion( options[ 1 ] ) ) ).to.true;
-
-					expect( getData( editor.model, { withoutSelection: true } ) )
-						.to.equal( '<heading3>h2</heading3><heading2>h1</heading2>' );
-
-					expect( editor.getData() ).to.equal( '<h2>h2</h2><h1>h1</h1>' );
+					expect( elementToElementSpy.calledWith( {
+						model: 'heading1',
+						view: 'h1',
+						title: 'Heading 1',
+						class: 'ck-heading_heading1',
+						converterPriority: priorities.get( 'low' ) + 1
+					} ) ).to.true;
 				} );
 		} );
-
-		function getDefaultConversion( conversionOptionBase = null ) {
-			if ( conversionOptionBase ) {
-				return Object.assign( {}, conversionOptionBase, { view: 'h1', converterPriority: priorities.get( 'low' ) + 1 } );
-			}
-
-			return {
-				model: 'heading1',
-				view: 'h1',
-				title: 'Heading 1',
-				class: 'ck-heading_heading1',
-				converterPriority: priorities.get( 'low' ) + 1
-			};
-		}
 	} );
 
 	it( 'should not blow up if there\'s no enter command in the editor', () => {