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

Add default converter for 'h1'.

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

+ 27 - 0
packages/ckeditor5-heading/src/headingediting.js

@@ -11,6 +11,8 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import HeadingCommand from './headingcommand';
 
+import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
+
 const defaultModelElement = 'paragraph';
 
 /**
@@ -67,6 +69,8 @@ export default class HeadingEditing extends Plugin {
 			}
 		}
 
+		this._addDefaultH1Conversion( editor, options );
+
 		// Register the heading command for this option.
 		editor.commands.add( 'heading', new HeadingCommand( editor, modelElements ) );
 	}
@@ -92,4 +96,27 @@ export default class HeadingEditing extends Plugin {
 			} );
 		}
 	}
+
+	/**
+	 * Adds default converter for `h1` -> `heading1`. It will be added only if configuration with `h2` -> `heading1` mapping is used.
+	 *
+	 * @private
+	 * @param editor
+	 * @param options
+	 */
+	_addDefaultH1Conversion( editor, options ) {
+		// Add `h1` -> `heading1` conversion with a low priority. This means if no other conversion was provided,
+		// `h1` will be handled here. Proceed only if `heading1` -> `h2` mapping was registered.
+		const heading1 = options.find( option => option.model === 'heading1' && option.view === 'h2' );
+		if ( heading1 ) {
+			const optionH1 = Object.assign( {}, heading1 );
+
+			optionH1.view = 'h1';
+			// With a 'low' priority `paragraph` plugin autoparagraphing mechanism is used.
+			// Make sure this listener is called before it. If not, 'h1' will be transformed into paragraph.
+			optionH1.converterPriority = priorities.get( 'low' ) + 1;
+
+			editor.conversion.elementToElement( optionH1 );
+		}
+	}
 }

+ 36 - 0
packages/ckeditor5-heading/tests/headingediting.js

@@ -73,6 +73,13 @@ describe( 'HeadingEditing', () => {
 		expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
 	} );
 
+	it( 'should convert h1 to heading1', () => {
+		editor.setData( '<h1>foobar</h1>' );
+
+		expect( getData( model, { withoutSelection: true } ) ).to.equal( '<heading1>foobar</heading1>' );
+		expect( editor.getData() ).to.equal( '<h2>foobar</h2>' );
+	} );
+
 	describe( 'user defined', () => {
 		beforeEach( () => {
 			return VirtualTestEditor
@@ -114,6 +121,35 @@ describe( 'HeadingEditing', () => {
 
 			expect( editor.getData() ).to.equal( '<h1>foobar</h1><p>Normal paragraph</p>' );
 		} );
+
+		it( 'should use user defined h1 conversion instead of the default one (high priority)', () => {
+			editor.setData( '<h2>h2</h2><h1>h1</h1>' );
+
+			expect( getData( model, { withoutSelection: true } ) ).to.equal( '<paragraph>h2</paragraph><heading1>h1</heading1>' );
+			expect( editor.getData() ).to.equal( '<p>h2</p><h1>h1</h1>' );
+		} );
+
+		it( 'should use user defined h1 conversion instead of the default one (default priority)', () => {
+			return VirtualTestEditor
+				.create( {
+					plugins: [ HeadingEditing ],
+					heading: {
+						options: [
+							{ model: 'paragraph', title: 'paragraph' },
+							{ model: 'heading1', view: 'h2', title: 'User H1' },
+							{ model: 'heading2', view: 'h1', title: 'User H2' }
+						]
+					}
+				} )
+				.then( editor => {
+					editor.setData( '<h2>h2</h2><h1>h1</h1>' );
+
+					expect( getData( editor.model, { withoutSelection: true } ) )
+						.to.equal( '<heading1>h2</heading1><heading2>h1</heading2>' );
+
+					expect( editor.getData() ).to.equal( '<h2>h2</h2><h1>h1</h1>' );
+				} );
+		} );
 	} );
 
 	it( 'should not blow up if there\'s no enter command in the editor', () => {