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

Merge pull request #29 from ckeditor/t/28

Changed: Use EnterCommand's afterExecute event to alter it's behavior.
Piotrek Koszuliński 9 лет назад
Родитель
Сommit
bd8176ced2

+ 5 - 1
packages/ckeditor5-heading/src/headings.js

@@ -3,11 +3,14 @@
  * For licensing, see LICENSE.md.
  * For licensing, see LICENSE.md.
  */
  */
 
 
-import Feature from '../core/feature.js';
 import HeadingsEngine from './headingsengine.js';
 import HeadingsEngine from './headingsengine.js';
+
+import Feature from '../core/feature.js';
+
 import Model from '../ui/model.js';
 import Model from '../ui/model.js';
 import ListDropdownController from '../ui/dropdown/list/listdropdown.js';
 import ListDropdownController from '../ui/dropdown/list/listdropdown.js';
 import ListDropdownView from '../ui/dropdown/list/listdropdownview.js';
 import ListDropdownView from '../ui/dropdown/list/listdropdownview.js';
+
 import Collection from '../utils/collection.js';
 import Collection from '../utils/collection.js';
 
 
 /**
 /**
@@ -65,6 +68,7 @@ export default class Headings extends Feature {
 			editor.editing.view.focus();
 			editor.editing.view.focus();
 		} );
 		} );
 
 
+		// Register UI component.
 		editor.ui.featureComponents.add( 'headings', ListDropdownController, ListDropdownView, dropdownModel );
 		editor.ui.featureComponents.add( 'headings', ListDropdownController, ListDropdownView, dropdownModel );
 	}
 	}
 }
 }

+ 3 - 2
packages/ckeditor5-heading/src/headingscommand.js

@@ -64,8 +64,9 @@ export default class HeadingsCommand extends Command {
 	}
 	}
 
 
 	/**
 	/**
-	 * Executes the command if it is enabled.
+	 * Executes command.
 	 *
 	 *
+	 * @protected
 	 * @param {String} [formatId] The identifier of the heading format that should be applied. It should be one of the
 	 * @param {String} [formatId] The identifier of the heading format that should be applied. It should be one of the
 	 * {@link headings.HeadingsFormat heading formats} provided to the command constructor. If this parameter is not provided,
 	 * {@link headings.HeadingsFormat heading formats} provided to the command constructor. If this parameter is not provided,
 	 * the value from {@link headings.HeadingsCommand#defaultFormat defaultFormat} will be used.
 	 * the value from {@link headings.HeadingsCommand#defaultFormat defaultFormat} will be used.
@@ -134,7 +135,7 @@ export default class HeadingsCommand extends Command {
 	 * @returns {headings.HeadingsFormat}
 	 * @returns {headings.HeadingsFormat}
 	 */
 	 */
 	_getFormatById( id ) {
 	_getFormatById( id ) {
-		return this.formats.find( item => item.id === id );
+		return this.formats.find( item => item.id === id ) || this.defaultFormat;
 	}
 	}
 }
 }
 
 

+ 17 - 1
packages/ckeditor5-heading/src/headingsengine.js

@@ -57,8 +57,24 @@ export default class HeadingsEngine extends Feature {
 			}
 			}
 		}
 		}
 
 
-		// Register command.
+		// Register the headings command.
 		const command = new HeadingsCommand( editor, formats );
 		const command = new HeadingsCommand( editor, formats );
 		editor.commands.set( 'headings', command );
 		editor.commands.set( 'headings', command );
+
+		// If the enter command is added to the editor, alter its behavior.
+		// Enter at the end of a heading element should create a paragraph.
+		const enterCommand = editor.commands.get( 'enter' );
+
+		if ( enterCommand ) {
+			this.listenTo( enterCommand, 'afterExecute', ( evt, data ) => {
+				const positionParent = editor.document.selection.getFirstPosition().parent;
+				const batch = data.batch;
+				const isHeading = formats.some( ( format ) => format.id == positionParent.name );
+
+				if ( isHeading && positionParent.name != command.defaultFormat.id && positionParent.childCount === 0 ) {
+					batch.rename( positionParent, command.defaultFormat.id );
+				}
+			} );
+		}
 	}
 	}
 }
 }

+ 11 - 2
packages/ckeditor5-heading/tests/headingscommand.js

@@ -16,7 +16,7 @@ const formats = [
 ];
 ];
 
 
 describe( 'HeadingsCommand', () => {
 describe( 'HeadingsCommand', () => {
-	let editor, document, command, root;
+	let editor, document, command, root, schema;
 
 
 	beforeEach( () => {
 	beforeEach( () => {
 		return ModelTestEditor.create()
 		return ModelTestEditor.create()
@@ -24,7 +24,7 @@ describe( 'HeadingsCommand', () => {
 				editor = newEditor;
 				editor = newEditor;
 				document = editor.document;
 				document = editor.document;
 				command = new HeadingsCommand( editor, formats );
 				command = new HeadingsCommand( editor, formats );
-				const schema = document.schema;
+				schema = document.schema;
 
 
 				for ( let format of formats ) {
 				for ( let format of formats ) {
 					schema.registerItem( format.id, '$block' );
 					schema.registerItem( format.id, '$block' );
@@ -53,6 +53,15 @@ describe( 'HeadingsCommand', () => {
 				expect( command.value ).to.equal( format );
 				expect( command.value ).to.equal( format );
 			} );
 			} );
 		}
 		}
+
+		it( 'should be equal to defaultFormat if format has not been found', () => {
+			schema.registerItem( 'div', '$block' );
+			setData( document, '<div>xyz</div>' );
+			const element = root.getChild( 0 );
+			document.selection.addRange( Range.createFromParentsAndOffsets( element, 1, element, 1 ) );
+
+			expect( command.value ).to.equal( command.defaultFormat );
+		} );
 	} );
 	} );
 
 
 	describe( '_doExecute', () => {
 	describe( '_doExecute', () => {

+ 21 - 1
packages/ckeditor5-heading/tests/headingsengine.js

@@ -7,6 +7,7 @@ import HeadingsEngine from '/ckeditor5/headings/headingsengine.js';
 import Paragraph from '/ckeditor5/paragraph/paragraph.js';
 import Paragraph from '/ckeditor5/paragraph/paragraph.js';
 import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
 import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
 import HeadingsCommand from '/ckeditor5/headings/headingscommand.js';
 import HeadingsCommand from '/ckeditor5/headings/headingscommand.js';
+import Enter from '/ckeditor5/enter/enter.js';
 import { getData } from '/tests/engine/_utils/model.js';
 import { getData } from '/tests/engine/_utils/model.js';
 
 
 describe( 'HeadingsEngine', () => {
 describe( 'HeadingsEngine', () => {
@@ -14,7 +15,7 @@ describe( 'HeadingsEngine', () => {
 
 
 	beforeEach( () => {
 	beforeEach( () => {
 		return VirtualTestEditor.create( {
 		return VirtualTestEditor.create( {
-			features: [ HeadingsEngine ]
+			features: [ Enter, HeadingsEngine ]
 		} )
 		} )
 		.then( newEditor => {
 		.then( newEditor => {
 			editor = newEditor;
 			editor = newEditor;
@@ -72,4 +73,23 @@ describe( 'HeadingsEngine', () => {
 		expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
 		expect( getData( document, { withoutSelection: true } ) ).to.equal( '<heading3>foobar</heading3>' );
 		expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
 		expect( editor.getData() ).to.equal( '<h4>foobar</h4>' );
 	} );
 	} );
+
+	it( 'should make enter command insert a defaultFormat block if selection ended at the end of heading block', () => {
+		editor.setData( '<h2>foobar</h2>' );
+		document.selection.collapse( document.getRoot().getChild( 0 ), 'end' );
+
+		editor.execute( 'enter' );
+
+		expect( getData( document ) ).to.equal( '<heading1>foobar</heading1><paragraph>[]</paragraph>' );
+	} );
+
+	it( 'should not alter enter command if selection not ended at the end of heading block', () => {
+		// This test is to fill code coverage.
+		editor.setData( '<h2>foobar</h2>' );
+		document.selection.collapse( document.getRoot().getChild( 0 ), 3 );
+
+		editor.execute( 'enter' );
+
+		expect( getData( document ) ).to.equal( '<heading1>foo</heading1><heading1>[]bar</heading1>' );
+	} );
 } );
 } );

+ 6 - 3
packages/ckeditor5-heading/tests/manual/headings.md

@@ -5,6 +5,9 @@
 1. The data should be loaded with three headings and one paragraph.
 1. The data should be loaded with three headings and one paragraph.
 2. Put selection inside each block and check if dropdown label is changing properly.
 2. Put selection inside each block and check if dropdown label is changing properly.
 3. Play with headings:
 3. Play with headings:
-	- Switch headings using dropdown.
-	- Apply same heading twice to same block to see if its switching with paragraph.
-	- Put selection that spans across multiple blocks and switch headings.
+	* Switch headings using dropdown.
+	* Apply same heading twice to same block to see if its switching with paragraph.
+	* Put selection that spans across multiple blocks and switch headings.
+4. Enter behavior alternations:
+	* At the end of a heading should create a new paragraph.
+	* In the middle of a heading should split it.