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

Commit

Permalink
Merge e7f2806 into 9f64452
Browse files Browse the repository at this point in the history
  • Loading branch information
jodator committed Aug 22, 2019
2 parents 9f64452 + e7f2806 commit f99d94d
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/converters/table-cell-refresh-post-fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function tableCellRefreshPostFixer( model ) {
for ( const change of differ.getChanges() ) {
const parent = change.type == 'insert' || change.type == 'remove' ? change.position.parent : change.range.start.parent;

if ( parent.is( 'tableCell' ) ) {
if ( parent.is( 'tableCell' ) && checkRefresh( parent, change.type ) ) {
differ.refreshItem( parent );

fixed = true;
Expand All @@ -39,3 +39,46 @@ function tableCellRefreshPostFixer( model ) {

return fixed;
}

// Check if a table cell in the view requires refreshing.
//
// This methods detects changes that will require:
// - <span> to <p> rename in the view,
// - adding a missing <paragraph>,
// - or wrapping a text node in <paragraph>
//
// thus requiring refreshing the table cell view.
//
// @param {module:engine/model/element~Element} tableCell Table cell to check.
// @param {String} type Type of change.
function checkRefresh( tableCell, type ) {
// If children of a table cell were removed - refresh it.
if ( !tableCell.childCount ) {
return true;
}

const children = Array.from( tableCell.getChildren() );
const hasInnerText = children.some( child => child.is( 'text' ) );

// If a bare text node (not wrapped in a paragraph) was added - refresh it.
if ( hasInnerText ) {
return true;
}

const hasInnerParagraph = children.some( child => child.is( 'paragraph' ) );

// If there is no paragraph in table cell then the view doesn't require refreshing.
if ( !hasInnerParagraph ) {
return false;
}

// For attribute change we only refresh single paragraphs as they might trigger <span> to <p> change in the view.
if ( type == 'attribute' ) {
return tableCell.childCount === 1;
}

// For other changes (insert/remove) the <span> to <p> change can occur when:
// - sibling is added to a single paragraph (childCount == 2)
// - sibling is removed and single paragraph is left (childCount == 1)
return tableCell.childCount < 3;
}

0 comments on commit f99d94d

Please sign in to comment.