浏览代码

Handle more complex nesting scenarios.

Krzysztof Krztoń 6 年之前
父节点
当前提交
69e1dff8ce
共有 1 个文件被更改,包括 26 次插入10 次删除
  1. 26 10
      packages/ckeditor5-paste-from-office/src/filters/list.js

+ 26 - 10
packages/ckeditor5-paste-from-office/src/filters/list.js

@@ -36,16 +36,32 @@ export function transformListItemLikeElementsIntoLists( documentFragment, styles
 	let currentList = null;
 
 	itemLikeElements.forEach( ( itemLikeElement, i ) => {
-		if ( !currentList || isNewListNeeded( itemLikeElements[ i - 1 ], itemLikeElement ) ) {
+		const isDifferentList = isNewListNeeded( itemLikeElements[ i - 1 ], itemLikeElement );
+		const previousListItem = isDifferentList ? null : itemLikeElements[ i - 1 ];
+		const indentationDifference = getIndentationDifference( previousListItem, itemLikeElement );
+
+		if ( isDifferentList ) {
+			currentList = null;
+		}
+
+		if ( !currentList || isDifferentList || indentationDifference !== 0 ) {
 			const listStyle = detectListStyle( itemLikeElement, stylesString );
-			const indentationDifference = getIndentationDifference( itemLikeElements[ i - 1 ], itemLikeElement );
 
 			if ( indentationDifference < 0 ) {
 				currentList = findParentListAtLevel( currentList, indentationDifference );
+
+				if ( !currentList.is( listStyle.type ) ) {
+					currentList = writer.rename( listStyle.type, currentList );
+				}
 			} else if ( indentationDifference > 0 ) {
-				const lastListItem = currentList.getChild( currentList.childCount - 1 );
-				const lastListItemChild = lastListItem.getChild( lastListItem.childCount - 1 );
-				currentList = insertNewEmptyList( listStyle, lastListItemChild, writer, indentationDifference );
+				if ( currentList ) {
+					const lastListItem = currentList.getChild( currentList.childCount - 1 );
+					const lastListItemChild = lastListItem.getChild( lastListItem.childCount - 1 );
+					currentList = insertNewEmptyList( listStyle, lastListItemChild, writer, indentationDifference );
+				} else {
+					// First item in the list has indentation.
+					currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer, indentationDifference + 1 );
+				}
 			} else {
 				currentList = insertNewEmptyList( listStyle, itemLikeElement.element, writer );
 			}
@@ -266,6 +282,10 @@ function removeBulletElement( element, writer ) {
 // @param {Object} currentItem
 // @returns {Boolean}
 function isNewListNeeded( previousItem, currentItem ) {
+	if ( !previousItem ) {
+		return true;
+	}
+
 	if ( previousItem.id !== currentItem.id ) {
 		return true;
 	}
@@ -276,10 +296,6 @@ function isNewListNeeded( previousItem, currentItem ) {
 		return true;
 	}
 
-	if ( getIndentationDifference( previousItem, currentItem ) !== 0 ) {
-		return true;
-	}
-
 	// Even with the same id the list does not have to be continuous (#43).
 	return !isList( previousSibling );
 }
@@ -294,7 +310,7 @@ function isList( element ) {
 // @param {Object} currentItem
 // @returns {Number}
 function getIndentationDifference( previousItem, currentItem ) {
-	return previousItem ? currentItem.indent - previousItem.indent : 0;
+	return previousItem ? currentItem.indent - previousItem.indent : currentItem.indent - 1;
 }
 
 // Finds parent list of a given list with indentation level lower by a given value.