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

Commit

Permalink
Merge pull request #58 from ckeditor/t/split-at-end
Browse files Browse the repository at this point in the history
Fix: Enter command will use `writer.split()` also at the beginning and at the end of a block.
  • Loading branch information
Piotr Jasiun authored Oct 23, 2018
2 parents fe49f84 + 9dac421 commit c159958
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 42 deletions.
24 changes: 4 additions & 20 deletions src/entercommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function enterBlock( model, writer, selection, schema ) {
}

if ( isSelectionEmpty ) {
splitBlock( writer, selection, range.start );
splitBlock( writer, range.start );
} else {
const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
const isContainedWithinOneElement = ( startElement == endElement );
Expand All @@ -68,7 +68,7 @@ function enterBlock( model, writer, selection, schema ) {
//
// <h>x[xx]x</h> -> <h>x^x</h> -> <h>x</h><h>^x</h>
if ( isContainedWithinOneElement ) {
splitBlock( writer, selection, selection.focus );
splitBlock( writer, selection.focus );
}
// Selection over multiple elements.
//
Expand All @@ -80,23 +80,7 @@ function enterBlock( model, writer, selection, schema ) {
}
}

function splitBlock( writer, selection, splitPos ) {
const oldElement = splitPos.parent;
const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );

if ( splitPos.isAtEnd ) {
// If the split is at the end of element, instead of splitting, just create a clone of position's parent
// element and insert it after split element. The result is the same but less operations are done
// and it's more semantically correct (when it comes to operational transformation).
writer.insert( newElement, splitPos.parent, 'after' );
} else if ( splitPos.isAtStart ) {
// If the split is at the start of element, instead of splitting, just create a clone of position's parent
// element and insert it before split element. The result is the same but less operations are done
// and it's more semantically correct (when it comes to operational transformation).
writer.insert( newElement, splitPos.parent, 'before' );
} else {
writer.split( splitPos );
}

function splitBlock( writer, splitPos ) {
writer.split( splitPos );
writer.setSelection( splitPos.parent.nextSibling, 0 );
}
23 changes: 1 addition & 22 deletions tests/entercommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
import EnterCommand from '../src/entercommand';
import InsertOperation from '@ckeditor/ckeditor5-engine/src/model/operation/insertoperation';
import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';

describe( 'EnterCommand', () => {
Expand Down Expand Up @@ -46,7 +45,7 @@ describe( 'EnterCommand', () => {
} );

describe( 'EnterCommand', () => {
it( 'enters a block using parent batch', () => {
it( 'splits a block using parent batch', () => {
setData( model, '<p>foo[]</p>' );

model.change( writer => {
Expand All @@ -55,26 +54,6 @@ describe( 'EnterCommand', () => {
expect( writer.batch.operations ).to.length.above( 0 );
} );
} );

it( 'creates InsertOperation if enter is at the beginning of block', () => {
setData( model, '<p>[]foo</p>' );

editor.execute( 'enter' );

const ops = doc.history.getOperations();

expect( ops[ ops.length - 1 ] ).to.be.instanceof( InsertOperation );
} );

it( 'creates InsertOperation if enter is at the end of block', () => {
setData( model, '<p>foo[]</p>' );

editor.execute( 'enter' );

const operations = Array.from( doc.history.getOperations() );

expect( operations[ operations.length - 1 ] ).to.be.instanceof( InsertOperation );
} );
} );

describe( 'execute()', () => {
Expand Down

0 comments on commit c159958

Please sign in to comment.