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

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
f1ames committed Oct 16, 2018
1 parent 3939f45 commit 76f50d0
Showing 1 changed file with 7 additions and 26 deletions.
33 changes: 7 additions & 26 deletions src/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export function viewModelConverter( evt, data, conversionApi ) {

writer.insert( listItem, splitResult.position );

const nextPosition = viewToModelListItemChildrenConverter( listItem, data.viewItem.getChildren(), conversionApi, writer );
const nextPosition = viewToModelListItemChildrenConverter( listItem, data.viewItem.getChildren(), conversionApi );

conversionStore.indent--;

Expand Down Expand Up @@ -806,9 +806,8 @@ function generateLiInUl( modelItem, conversionApi ) {
// @param {module:engine/model/element~Element} listItemModel List item model element to which converted children will be inserted.
// @param {Iterable.<module:engine/view/node~Node>} viewChildren View elements which will be converted.
// @param {Object} conversionApi Conversion interface to be used by the callback.
// @param {module:engine/model/writer~Writer} writer Writer used to manipulate model elements.
// @returns {module:engine/model/position~Position} Position on which next elements should be inserted after children conversion.
function viewToModelListItemChildrenConverter( listItemModel, viewChildren, conversionApi, writer ) {
function viewToModelListItemChildrenConverter( listItemModel, viewChildren, conversionApi ) {
let lastListItem = listItemModel;
let nextPosition = ModelPosition.createAfter( lastListItem );

Expand All @@ -824,6 +823,8 @@ function viewToModelListItemChildrenConverter( listItemModel, viewChildren, conv
const result = conversionApi.convertItem( child, ModelPosition.createAt( lastListItem, 'end' ) );
const convertedChild = Array.from( result.modelRange )[ 0 ];

nextPosition = ModelPosition.createAt( lastListItem, 'end' );

// If there is a block element child being converted it will split the current list item, for example:
//
// <li><p>Foo</p></li>
Expand All @@ -837,40 +838,20 @@ function viewToModelListItemChildrenConverter( listItemModel, viewChildren, conv
nextPosition = result.modelCursor;
lastListItem = nextPosition.getAncestors().pop();

// Depending on the used converter for block elements, usually the position (marked as #)
// points to the second list item after conversion:
// Depending on the used converter for block elements, usually the position (`result.modelCursor`
// marked as # below) points to the second list item after conversion:
//
// `<li><p>Foo</p></li>` -> `<listItem></listItem><paragraph>Foo</paragraph><listItem>#</listItem>`
//
// However, in some cases like autoparagraphing the position is placed on the end of the block element:
//
// `<li><h2>Foo</h2></li>` -> `<listItem></listItem><paragraph>Foo#</paragraph><listItem></listItem>`
//
// We need to check fo such cases and use proper list item and position based on it.
// We need to check for such cases and use proper list item and position based on it.
if ( !lastListItem.is( 'listItem' ) && lastListItem.nextSibling && lastListItem.nextSibling.is( 'listItem' ) ) {
lastListItem = lastListItem.nextSibling;
nextPosition = ModelPosition.createAt( lastListItem, 'end' );
}
// If list item was split by the block element, the text from splitted list item needs to be moved.
//
// `<li>Foo<p>123</p>Bar</li>`
//
// Is converted to:
//
// <listItem>FooBar</listItem><paragraph>123</paragraph><listItem></listItem>
//
// So the text which was after splitting element (`<p>` in this case) should be move to second list item:
//
// <listItem>Foo</listItem><paragraph>123</paragraph><listItem>Bar</listItem>
} else if ( convertedChild.type === 'text' && listItemModel !== lastListItem ) {
// 1. Remove text part from the list item positioned before splitting element.
writer.remove( ModelRange.createOn( convertedChild.item ) );

// 2. Insert text part into list item positioned after splitting element.
writer.insert( convertedChild.item, nextPosition );

// 3. Update 'nextPosition' so it points after last list item.
nextPosition = ModelPosition.createAfter( lastListItem );
}
}
}
Expand Down

0 comments on commit 76f50d0

Please sign in to comment.