Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Simplified the code in order to avoid repeating the code with the eng…
Browse files Browse the repository at this point in the history
…ine.
  • Loading branch information
Kamil Piechaczek committed Jul 24, 2017
1 parent 6558d59 commit ef6a30e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
40 changes: 13 additions & 27 deletions src/deletecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

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';
Expand Down Expand Up @@ -107,9 +105,12 @@ export default class DeleteCommand extends Command {
* the whole element without removing them.
*
* 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.
*
* * 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.
*
* @private
* @param {Number} sequence A number describing which subsequent delete event it is without the key being released.
Expand All @@ -123,23 +124,10 @@ export default class DeleteCommand extends Command {

const document = this.editor.document;
const selection = document.selection;
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;
}
const commonAncestor = selection.getFirstRange().getCommonAncestor();

if ( !document.schema.check( { name: 'paragraph', inside: limitElement.name } ) ) {
return false;
}

// Does nothing if editor contains an empty paragraph.
if ( selection.getFirstRange().getCommonAncestor().name === 'paragraph' ) {
// Does nothing if editor already contains an empty paragraph.
if ( commonAncestor.name === 'paragraph' ) {
return false;
}

Expand All @@ -152,15 +140,13 @@ export default class DeleteCommand extends Command {
* @private
*/
_replaceEntireContentWithParagraph() {
const document = this.editor.document;
const editor = this.editor;
const document = 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.collapse( paragraph );
selection.setRanges( [ Range.createIn( limitElement ) ], selection.isBackward );
editor.data.deleteContent( selection, this._buffer.batch, { skipParentsCheck: true } );
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/deletecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,14 @@ describe( 'DeleteCommand', () => {
expect( element ).is.equal( doc.selection.getFirstRange().getCommonAncestor() );
} );

it( 'does not replace an element if a paragraph is not allowed in current position', () => {
xit( '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>' );

editor.execute( 'delete' );

// Returned data: '[]' instead of the heading element.
expect( getData( doc, { selection: true } ) ).to.equal( '<heading1>[]</heading1>' );
} );
} );
Expand Down

0 comments on commit ef6a30e

Please sign in to comment.