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

Commit

Permalink
Improved the docs and adjusted to changes in engine (Selection API).
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Piechaczek committed Aug 18, 2017
1 parent 45e9beb commit 54b4ad8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/deletecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ export default class DeleteCommand extends Command {
* we want to replace the entire content with a paragraph if:
*
* * the current limit element is empty,
* * the paragraph is allowed in the common ancestor,
* * other paragraph does not occur in the editor.
* * the paragraph is allowed in the limit element,
* * other empty paragraph does not occur in the limit element.
*
* See https://github.com/ckeditor/ckeditor5-typing/issues/61.
*
Expand All @@ -138,9 +138,10 @@ export default class DeleteCommand extends Command {
const document = this.editor.document;
const selection = document.selection;
const limitElement = document.schema.getLimitElement( selection );

// If a collapsed selection contains the whole content it means that the content is empty
// (from the user perspective).
const limitElementIsEmpty = selection.isCollapsed && selection.isEntireContentSelected( limitElement );
const limitElementIsEmpty = selection.isCollapsed && selection.containsEntireContent( limitElement );

if ( !limitElementIsEmpty ) {
return false;
Expand All @@ -150,8 +151,10 @@ export default class DeleteCommand extends Command {
return false;
}

// Does nothing if editor already contains an empty paragraph.
if ( selection.getFirstRange().getCommonAncestor().name === 'paragraph' ) {
const limitElementFirstChild = limitElement.getChild( 0 );

// Does nothing if limit element already contains an empty paragraph.
if ( limitElementFirstChild && limitElementFirstChild.name === 'paragraph' ) {
return false;
}

Expand Down
42 changes: 29 additions & 13 deletions tests/deletecommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe( 'DeleteCommand', () => {

editor.execute( 'delete' );

expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>[]</paragraph>' );
expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
} );

it( 'leaves an empty paragraph after removing the whole content inside limit element', () => {
Expand All @@ -157,24 +157,40 @@ describe( 'DeleteCommand', () => {

setData( doc,
'<heading1>Foo</heading1>' +
'<section>' +
'<heading1>[Header 1</heading1>' +
'<paragraph>Some text.]</paragraph>' +
'</section>' +
'<section>' +
'<heading1>[Header 1</heading1>' +
'<paragraph>Some text.]</paragraph>' +
'</section>' +
'<paragraph>Bar.</paragraph>'
);

editor.execute( 'delete' );

expect( getData( doc, { selection: true } ) ).to.equal(
expect( getData( doc ) ).to.equal(
'<heading1>Foo</heading1>' +
'<section>' +
'<paragraph>[]</paragraph>' +
'<paragraph>[]</paragraph>' +
'</section>' +
'<paragraph>Bar.</paragraph>'
);
} );

it( 'leaves an empty paragraph after removing another paragraph from block element', () => {
doc.schema.registerItem( 'section', '$block' );
doc.schema.registerItem( 'blockQuote', '$block' );
doc.schema.limits.add( 'section' );
doc.schema.allow( { name: 'section', inside: '$root' } );
doc.schema.allow( { name: 'paragraph', inside: 'section' } );
doc.schema.allow( { name: 'blockQuote', inside: 'section' } );
doc.schema.allow( { name: 'paragraph', inside: 'blockQuote' } );

setData( doc, '<section><blockQuote><paragraph>[]</paragraph></blockQuote></section>' );

editor.execute( 'delete' );

expect( getData( doc ) ).to.equal( '<section><paragraph>[]</paragraph></section>' );
} );

it( 'leaves an empty paragraph after removing the whole content when root element was not added as Schema.limits', () => {
doc.schema.limits.delete( '$root' );

Expand All @@ -190,7 +206,7 @@ describe( 'DeleteCommand', () => {

editor.execute( 'delete' );

expect( getData( doc, { selection: true } ) ).to.equal( '<paragraph>[]</paragraph>' );
expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
} );

it( 'does not replace an element when Backspace or Delete key is held', () => {
Expand All @@ -200,17 +216,17 @@ describe( 'DeleteCommand', () => {
editor.execute( 'delete', { sequence } );
}

expect( getData( doc, { selection: true } ) ).to.equal( '<heading1>[]</heading1>' );
expect( getData( doc ) ).to.equal( '<heading1>[]</heading1>' );
} );

it( 'does not replace an element if a paragraph is a common ancestor', () => {
it( 'does not replace with paragraph in another paragraph already occurs in limit element', () => {
setData( doc, '<paragraph>[]</paragraph>' );

const element = doc.selection.getFirstRange().getCommonAncestor();
const element = doc.getRoot().getNodeByPath( [ 0 ] );

editor.execute( 'delete' );

expect( element ).is.equal( doc.selection.getFirstRange().getCommonAncestor() );
expect( element ).is.equal( doc.getRoot().getNodeByPath( [ 0 ] ) );
} );

it( 'does not replace an element if a paragraph is not allowed in current position', () => {
Expand All @@ -221,7 +237,7 @@ describe( 'DeleteCommand', () => {
editor.execute( 'delete' );

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

0 comments on commit 54b4ad8

Please sign in to comment.