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

Revert: ef6a30e. It was not good idea to follow this way.

Kamil Piechaczek 8 лет назад
Родитель
Сommit
44c3d829f7

+ 26 - 11
packages/ckeditor5-typing/src/deletecommand.js

@@ -9,6 +9,8 @@
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
 import Selection from '@ckeditor/ckeditor5-engine/src/model/selection';
+import Element from '@ckeditor/ckeditor5-engine/src/model/element';
+import Position from '@ckeditor/ckeditor5-engine/src/model/position';
 import Range from '@ckeditor/ckeditor5-engine/src/model/range';
 import ChangeBuffer from './changebuffer';
 import count from '@ckeditor/ckeditor5-utils/src/count';
@@ -106,11 +108,9 @@ export default class DeleteCommand extends Command {
 	 *
 	 * But, if the user pressed and released the key, we want to replace the entire content with a paragraph if:
 	 *
-	 *  * the entire content is selected,
-	 *  * the paragraph is allowed in the common ancestor,
-	 *  * other paragraph does not occur in the editor.
-	 *
-	 *  Rest of the checks are done in {@link module:engine/controller/datacontroller~DataController#deleteContent} method.
+	 * * the entire content is selected,
+	 * * the paragraph is allowed in the common ancestor,
+	 * * other paragraph does not occur in the editor.
 	 *
 	 * @private
 	 * @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
@@ -124,10 +124,23 @@ export default class DeleteCommand extends Command {
 
 		const document = this.editor.document;
 		const selection = document.selection;
-		const commonAncestor = selection.getFirstRange().getCommonAncestor();
+		const limitElement = getLimitElement( document.schema, selection );
+		const limitStartPosition = Position.createAt( limitElement );
+		const limitEndPosition = Position.createAt( limitElement, 'end' );
+
+		if (
+			!limitStartPosition.isTouching( selection.getFirstPosition() ) ||
+			!limitEndPosition.isTouching( selection.getLastPosition() )
+		) {
+			return false;
+		}
+
+		if ( !document.schema.check( { name: 'paragraph', inside: limitElement.name } ) ) {
+			return false;
+		}
 
 		// Does nothing if editor already contains an empty paragraph.
-		if ( commonAncestor.name === 'paragraph' ) {
+		if ( selection.getFirstRange().getCommonAncestor().name === 'paragraph' ) {
 			return false;
 		}
 
@@ -140,13 +153,15 @@ export default class DeleteCommand extends Command {
 	 * @private
 	 */
 	_replaceEntireContentWithParagraph() {
-		const editor = this.editor;
-		const document = editor.document;
+		const document = this.editor.document;
 		const selection = document.selection;
 		const limitElement = getLimitElement( document.schema, selection );
+		const paragraph = new Element( 'paragraph' );
+
+		this._buffer.batch.remove( Range.createIn( limitElement ) );
+		this._buffer.batch.insert( Position.createAt( limitElement ), paragraph );
 
-		selection.setRanges( [ Range.createIn( limitElement ) ], selection.isBackward );
-		editor.data.deleteContent( selection, this._buffer.batch, { skipParentsCheck: true } );
+		selection.collapse( paragraph );
 	}
 }
 

+ 1 - 1
packages/ckeditor5-typing/tests/deletecommand.js

@@ -185,7 +185,7 @@ describe( 'DeleteCommand', () => {
 			expect( element ).is.equal( doc.selection.getFirstRange().getCommonAncestor() );
 		} );
 
-		xit( 'does not replace an element if a paragraph is not allowed in current position', () => {
+		it( 'does not replace an element if a paragraph is not allowed in current position', () => {
 			doc.schema.disallow( { name: 'paragraph', inside: '$root' } );
 
 			setData( doc, '<heading1>[]</heading1>' );