Jelajahi Sumber

Initial implementation.

Piotrek Koszuliński 9 tahun lalu
induk
melakukan
5226bae554

+ 115 - 1
packages/ckeditor5-paragraph/src/paragraph.js

@@ -8,9 +8,14 @@
  */
 
 import Plugin from '../core/plugin.js';
+
+import ModelElement from '../engine/model/element.js';
+
 import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
 import buildViewConverter from '../engine/conversion/buildviewconverter.js';
 
+import isArray from '../utils/lib/lodash/isArray.js';
+
 /**
  * The paragraph feature for the editor.
  * Introduces the `<paragraph>` element in the model which renders as a `<p>` element in the DOM and data.
@@ -23,11 +28,12 @@ export default class Paragraph extends Plugin {
 	 */
 	init() {
 		const editor = this.editor;
+		const doc = editor.document;
 		const data = editor.data;
 		const editing = editor.editing;
 
 		// Schema.
-		editor.document.schema.registerItem( 'paragraph', '$block' );
+		doc.schema.registerItem( 'paragraph', '$block' );
 
 		// Build converter from model to view for data and editing pipelines.
 		buildModelConverter().for( data.modelToView, editing.modelToView )
@@ -38,5 +44,113 @@ export default class Paragraph extends Plugin {
 		buildViewConverter().for( data.viewToModel )
 			.fromElement( 'p' )
 			.toElement( 'paragraph' );
+
+		// Autoparagraph text.
+		data.viewToModel.on( 'text', ( evt, data, consumable, conversionApi ) => {
+			autoparagraphText( doc, evt, data, consumable, conversionApi );
+		}, { priority: 'lowest' } );
+
+		// Post-fix potential subsequent paragraphs created by autoparagraphText().
+		data.viewToModel.on( 'element', mergeSubsequentParagraphs, { priority: 'lowest' } );
+		data.viewToModel.on( 'documentFragment', mergeSubsequentParagraphs, { priority: 'lowest' } );
+	}
+}
+
+/**
+ * List of element names which should be treated by the autoparagraphing algorithms as
+ * paragraph-like. This means that e.g. the following content:
+ *
+ *		<h1>Foo</h1>
+ *		<table>
+ *			<tr><td>X</td><td>Y</td></tr>
+ *			<tr><td>X</td><td>Y</td></tr>
+ *		</table>
+ *
+ * Contains three paragraph-like elements – `<h1>` and two `<tr>`. Hence, if none of the features is going to convert
+ * those elements the above content will automatically be handled by the paragraph feature and converted to:
+ *
+ *		<p>Foo</p>
+ *		<p>XY</p>
+ *		<p>XY</p>
+ *
+ * Note that subsequent cells were merged, but `<tr>` elements were maintaned.
+ *
+ * @member {Set.<String>} module:paragraph/paragraph~Paragraph.paragraphLikeElements
+ */
+Paragraph.paragraphLikeElements = new Set( [ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'tr', 'li' ] );
+
+// A map of paragraphs to merge (model elements) to their block contexts in the view.
+// The context is used in order to check whether two paragraps should really be merged.
+// E.g.
+// <h1>foo</h1><h1>bar</h1> => <p>foo</p><p>bar</p> (contexts: two different h1 elements – no merge)
+// <div>foo<img>bar</div> => <p>foo</p><p><img></p><p>bar</p> (contexts: 3x the same div element – merge)
+const paragraphsToMerge = new WeakMap();
+
+function autoparagraphText( doc, evt, data, consumable, conversionApi ) {
+	// If text wasn't consumed by the default converter...
+	if ( !consumable.test( data.input ) ) {
+		return;
 	}
+
+	// And paragraph is allowed in this context...
+	if ( !doc.schema.check( { name: 'paragraph', inside: data.context } ) ) {
+		return;
+	}
+
+	// Let's do autoparagraphing.
+
+	const paragraph = new ModelElement( 'paragraph' );
+
+	paragraphsToMerge.set( paragraph, getBlockContext( data.input ) );
+
+	const newContext = data.context.concat( paragraph );
+	const text = conversionApi.convertItem( data.input, consumable, { context: newContext } );
+
+	if ( text ) {
+		data.output = paragraph;
+		paragraph.appendChildren( text );
+	}
+}
+
+// Merges subsequent paragraphs if they should be merged (see shouldMerge).
+function mergeSubsequentParagraphs( evt, data ) {
+	if ( !data.output ) {
+		return;
+	}
+
+	let node;
+
+	if ( isArray( data.output ) ) {
+		node = data.output[ 0 ];
+	} else {
+		node = data.output.getChild( 0 );
+	}
+
+	while ( node && node.nextSibling ) {
+		const nextSibling = node.nextSibling;
+
+		if ( shouldMerge( node, nextSibling ) ) {
+			node.appendChildren( nextSibling.getChildren() );
+			nextSibling.remove();
+		} else {
+			node = node.nextSibling;
+		}
+	}
+}
+
+// Checks whether two paragraphs should be merged. This
+// may happen only if they were created by autoparagraphText() and were
+// created from two nodes in the same block context.
+function shouldMerge( paragraphA, paragraphB ) {
+	return paragraphsToMerge.has( paragraphA ) && paragraphsToMerge.has( paragraphB ) &&
+		( paragraphsToMerge.get( paragraphA ) == paragraphsToMerge.get( paragraphB ) );
+}
+
+// Returns first ancestor which name exists in paragraphLikeElements or the root.
+function getBlockContext( node ) {
+	const blockLikeAncestor = node.getAncestors( { parentFirst: true } ).find( ( ancestor ) => {
+		return Paragraph.paragraphLikeElements.has( ancestor.name );
+	} );
+
+	return blockLikeAncestor ? blockLikeAncestor : node.root;
 }

+ 108 - 3
packages/ckeditor5-paragraph/tests/paragraph.js

@@ -5,9 +5,15 @@
 
 import Paragraph from 'ckeditor5/paragraph/paragraph.js';
 import VirtualTestEditor from 'tests/core/_utils/virtualtesteditor.js';
-import { getData as getModelData } from 'ckeditor5/engine/dev-utils/model.js';
+import {
+	getData as getModelData,
+	setData as setModelData,
+	stringify as stringifyModel
+} from 'ckeditor5/engine/dev-utils/model.js';
 import { getData as getViewData } from 'ckeditor5/engine/dev-utils/view.js';
 
+import buildViewConverter from 'ckeditor5/engine/conversion/buildviewconverter.js';
+
 describe( 'Paragraph feature', () => {
 	let editor, doc;
 
@@ -31,6 +37,10 @@ describe( 'Paragraph feature', () => {
 		expect( doc.schema.check( { name: '$inline', inside: 'paragraph' } ) ).to.be.true;
 	} );
 
+	it( 'should have a static paragraphLikeElements property', () => {
+		expect( Paragraph ).to.have.property( 'paragraphLikeElements' );
+	} );
+
 	describe( 'data pipeline conversions', () => {
 		it( 'should convert paragraph', () => {
 			editor.setData( '<p>foobar</p>' );
@@ -52,14 +62,109 @@ describe( 'Paragraph feature', () => {
 			expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph><paragraph>baz</paragraph>' );
 			expect( editor.getData() ).to.equal( '<p>foo</p><p>baz</p>' );
 		} );
+
+		describe( 'generic block converter (autoparagraphing)', () => {
+			it( 'should autoparagraph text', () => {
+				editor.setData( 'foo' );
+
+				expect( getModelData( doc, { withoutSelection: true } ) ).to.equal( '<paragraph>foo</paragraph>' );
+				expect( editor.getData() ).to.equal( '<p>foo</p>' );
+			} );
+
+			it( 'should autoparagraph text next to allowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<h1>foo</h1>bar<p>bom</p>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph><paragraph>bom</paragraph>' );
+			} );
+
+			it( 'should autoparagraph 3 inline things into one paragraph', () => {
+				const modelFragment = editor.data.parse( 'foo<b>bar</b>bom' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foobarbom</paragraph>' );
+			} );
+
+			it( 'should autoparagraph h1, h2...', () => {
+				const modelFragment = editor.data.parse( '<h1>foo</h1><h2>bar</h2>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should autoparagraph ul > li', () => {
+				const modelFragment = editor.data.parse( '<ul><li>foo</li><li>bar</li></ul>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should autoparagraph tr', () => {
+				const modelFragment = editor.data.parse( '<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<paragraph>ab</paragraph><paragraph>cd</paragraph>' );
+			} );
+
+			it( 'should autoparagraph inside some container', () => {
+				doc.schema.registerItem( 'div' );
+				doc.schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
+				doc.schema.allow( { name: 'div', inside: '$root' } );
+				doc.schema.allow( { name: 'paragraph', inside: 'div' } );
+
+				buildViewConverter().for( editor.data.viewToModel )
+					.fromElement( 'b' )
+					.toAttribute( 'bold', true );
+
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'div' ).toElement( 'div' );
+
+				const modelFragment = editor.data.parse( '<div><ul><li>foo</li><li>bar</li></ul></div><div>bom<p>bim</p></div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal(
+						'<div><paragraph>foo</paragraph><paragraph>bar</paragraph></div>' +
+						'<div><paragraph>bom</paragraph><paragraph>bim</paragraph></div>'
+					);
+			} );
+
+			it( 'should not autopargraph when disallowed element contains allowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<div><h1>foo</h1>bar</div>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foo</heading1><paragraph>bar</paragraph>' );
+			} );
+
+			it( 'should not autopargraph when allowed element contains disallowed element', () => {
+				doc.schema.registerItem( 'heading1', '$block' );
+				buildViewConverter().for( editor.data.viewToModel ).fromElement( 'h1' ).toElement( 'heading1' );
+
+				const modelFragment = editor.data.parse( '<h1><b>foo</b>bar</h1>' );
+
+				expect( stringifyModel( modelFragment ) )
+					.to.equal( '<heading1>foobar</heading1>' );
+			} );
+		} );
 	} );
 
 	describe( 'editing pipeline conversion', () => {
 		it( 'should convert paragraph', () => {
-			// Workaround for setting model data: https://github.com/ckeditor/ckeditor5-engine/issues/455
-			editor.setData( '<p>foo</p><p>bar</p>' );
+			setModelData( doc, '<paragraph>foo</paragraph><paragraph>bar</paragraph>' );
 
 			expect( getViewData( editor.editing.view, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p>bar</p>' );
 		} );
 	} );
+
+	describe( 'autoparagraphing on data load', () => {
+		it( 'wraps text and place selection at the beginning of that paragraph', () => {
+			editor.setData( 'foo' );
+
+			expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo</paragraph>' );
+		} );
+	} );
 } );