Browse Source

Merge pull request #1048 from ckeditor/t/1045

Internal: Added a flag `doNotResetEntireContent` option to `DataController#deleteContent()` method. Closes #1045.
Piotrek Koszuliński 8 years ago
parent
commit
f29b5572e0

+ 13 - 4
packages/ckeditor5-engine/src/controller/deletecontent.js

@@ -20,12 +20,21 @@ import Element from '../model/element';
  * @param {Object} [options]
  * @param {Boolean} [options.leaveUnmerged=false] Whether to merge elements after removing the content of the selection.
  *
- * For example `<h>x[x</h><p>y]y</p>` will become:
- * * `<h>x^y</h>` with the option disabled (`leaveUnmerged == false`)
- * * `<h>x^</h><p>y</p>` with enabled (`leaveUnmerged == true`).
+ * For example `<heading>x[x</heading><paragraph>y]y</paragraph>` will become:
+ *
+ * * `<heading>x^y</heading>` with the option disabled (`leaveUnmerged == false`)
+ * * `<heading>x^</heading><paragraph>y</paragraph>` with enabled (`leaveUnmerged == true`).
  *
  * Note: {@link module:engine/model/schema~Schema#objects object} and {@link module:engine/model/schema~Schema#limits limit}
  * elements will not be merged.
+ *
+ * @param {Boolean} [options.doNotResetEntireContent=false] Whether to skip replacing the entire content with a
+ * paragraph when the entire content was selected.
+ *
+ * For example `<heading>[x</heading><paragraph>y]</paragraph> will become:
+ *
+ * * `<paragraph>^</paragraph>` with the option disabled (`doNotResetEntireContent == false`)
+ * * `<heading>^</heading>` with enabled (`doNotResetEntireContent == true`).
  */
 export default function deleteContent( selection, batch, options = {} ) {
 	if ( selection.isCollapsed ) {
@@ -34,7 +43,7 @@ export default function deleteContent( selection, batch, options = {} ) {
 
 	// 1. Replace the entire content with paragraph.
 	// See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
-	if ( shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) {
+	if ( !options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph( batch.document.schema, selection ) ) {
 		replaceEntireContentWithParagraph( batch, selection );
 
 		return;

+ 9 - 0
packages/ckeditor5-engine/tests/controller/deletecontent.js

@@ -749,6 +749,15 @@ describe( 'DataController', () => {
 				expect( getData( doc, { rootName: 'paragraphRoot' } ) )
 					.to.equal( 'x[]z' );
 			} );
+
+			test(
+				'but not if the flag "doNotResetEntireContent" is set to true',
+				'<heading1>[</heading1><paragraph>]</paragraph>',
+				'<heading1>[]</heading1>',
+				{
+					doNotResetEntireContent: true
+				}
+			);
 		} );
 
 		function test( title, input, output, options ) {