8
0
Просмотр исходного кода

Move paragraph converter from table editing to conversion/downcast.

Maciej Gołaszewski 5 лет назад
Родитель
Сommit
93dbbf9974

+ 35 - 2
packages/ckeditor5-table/src/converters/downcast.js

@@ -8,7 +8,7 @@
  */
 
 import TableWalker from './../tablewalker';
-import { toWidget, toWidgetEditable, setHighlightHandling } from '@ckeditor/ckeditor5-widget/src/utils';
+import { setHighlightHandling, toWidget, toWidgetEditable } from '@ckeditor/ckeditor5-widget/src/utils';
 
 /**
  * Model table element to view table element conversion helper.
@@ -235,6 +235,38 @@ export function downcastRemoveRow() {
 	}, { priority: 'higher' } );
 }
 
+/**
+ * Overrides paragraph inside table cell conversion.
+ *
+ * This converter:
+ * * should be used to override default paragraph conversion in the editing view.
+ * * It will only convert <paragraph> placed directly inside <tableCell>.
+ * * For a single paragraph without attributes it returns `<span>` to simulate data table.
+ * * For all other cases it returns `<p>` element.
+ *
+ * @param {module:engine/model/element~Element} modelElement
+ * @param {module:engine/conversion/downcastdispatcher~DowncastConversionApi} conversionApi
+ * @returns {module:engine/view/containerelement~ContainerElement|undefined}
+ */
+export function convertParagraphInTableCell( modelElement, conversionApi ) {
+	const { writer } = conversionApi;
+
+	if ( !modelElement.parent.is( 'element', 'tableCell' ) ) {
+		return;
+	}
+
+	const tableCell = modelElement.parent;
+	const isSingleParagraph = tableCell.childCount === 1;
+
+	if ( isSingleParagraph && !hasAnyAttribute( modelElement ) ) {
+		// Use display:inline-block to force Chrome/Safari to limit text mutations to this element.
+		// See #6062.
+		return writer.createContainerElement( 'span', { style: 'display:inline-block' } );
+	} else {
+		return writer.createContainerElement( 'p' );
+	}
+}
+
 // Converts a given {@link module:engine/view/element~Element} to a table widget:
 // * Adds a {@link module:engine/view/element~Element#_setCustomProperty custom property} allowing to recognize the table widget element.
 // * Calls the {@link module:widget/utils~toWidget} function with the proper element's label creator.
@@ -329,7 +361,8 @@ function createViewTableCellElement( tableSlot, tableAttributes, insertPosition,
 
 	conversionApi.mapper.bindElements( tableCell, cellElement );
 
-	if ( isSingleParagraph && !hasAnyAttribute( firstChild ) && !asWidget ) {
+	// Additional requirement for data pipeline to have backward compatible data tables.
+	if ( !asWidget && !hasAnyAttribute( firstChild ) && isSingleParagraph ) {
 		const innerParagraph = tableCell.getChild( 0 );
 
 		conversionApi.consumable.consume( innerParagraph, 'insert' );

+ 2 - 22
packages/ckeditor5-table/src/tableediting.js

@@ -11,6 +11,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 
 import upcastTable, { ensureParagraphInTableCell, skipEmptyTableRow } from './converters/upcasttable';
 import {
+	convertParagraphInTableCell,
 	downcastInsertCell,
 	downcastInsertRow,
 	downcastInsertTable,
@@ -114,31 +115,10 @@ export default class TableEditing extends Plugin {
 		// Duplicates code - needed to properly refresh paragraph inside table cell.
 		editor.conversion.for( 'editingDowncast' ).elementToElement( {
 			model: 'paragraph',
-			view: ( modelElement, conversionApi ) => {
-				const { writer } = conversionApi;
-
-				if ( !modelElement.parent.is( 'element', 'tableCell' ) ) {
-					return;
-				}
-
-				const tableCell = modelElement.parent;
-				const isSingleParagraph = tableCell.childCount === 1;
-
-				if ( isSingleParagraph && !hasAnyAttribute( modelElement ) ) {
-					// Use display:inline-block to force Chrome/Safari to limit text mutations to this element.
-					// See #6062.
-					return writer.createContainerElement( 'span', { style: 'display:inline-block' } );
-				} else {
-					return writer.createContainerElement( 'p' );
-				}
-			},
+			view: convertParagraphInTableCell,
 			converterPriority: 'high'
 		} );
 
-		function hasAnyAttribute( element ) {
-			return !![ ...element.getAttributeKeys() ].length;
-		}
-
 		// Table attributes conversion.
 		conversion.attributeToAttribute( { model: 'colspan', view: 'colspan' } );
 		conversion.attributeToAttribute( { model: 'rowspan', view: 'rowspan' } );