-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
table-cell-refresh-post-fixer.js
87 lines (73 loc) · 3.82 KB
/
table-cell-refresh-post-fixer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module table/converters/table-cell-refresh-post-fixer
*/
/**
* Injects a table cell post-fixer into the model which marks the table cell in the differ to have it re-rendered.
*
* Model `paragraph` inside a table cell can be rendered as `<span>` or `<p>`. It is rendered as `<span>` if this is the only block
* element in that table cell and it does not have any attributes. It is rendered as `<p>` otherwise.
*
* When table cell content changes, for example a second `paragraph` element is added, we need to ensure that the first `paragraph` is
* re-rendered so it changes from `<span>` to `<p>`. The easiest way to do it is to re-render the entire table cell.
*
* @param {module:engine/model/model~Model} model
*/
export default function injectTableCellRefreshPostFixer( model ) {
model.document.registerPostFixer( () => tableCellRefreshPostFixer( model ) );
}
function tableCellRefreshPostFixer( model ) {
const differ = model.document.differ;
// Stores cells to be refreshed so the table cell will be refreshed once for multiple changes.
const cellsToRefresh = new Set();
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' ) && checkRefresh( parent, change.type ) ) {
cellsToRefresh.add( parent );
}
}
if ( cellsToRefresh.size ) {
for ( const tableCell of cellsToRefresh.values() ) {
differ.refreshItem( tableCell );
}
return true;
}
return false;
}
// Checks if the model table cell requires refreshing to be re-rendered to a proper state in the view.
//
// This method detects changes that will require renaming `<span>` to `<p>` (or vice versa) in the view.
//
// This method is a simple heuristic that checks only a single change and will sometimes give a false positive result when multiple changes
// will result in a state that does not require renaming in the view (but will be seen as requiring a refresh).
//
// For instance: A `<span>` should be renamed to `<p>` when adding an attribute to a `<paragraph>`.
// But adding one attribute and removing another one will result in a false positive: the check for an added attribute will see one
// attribute on a paragraph and will falsely qualify such change as adding an attribute to a paragraph without any attribute.
//
// @param {module:engine/model/element~Element} tableCell The table cell to check.
// @param {String} type Type of change.
function checkRefresh( tableCell, type ) {
const hasInnerParagraph = Array.from( tableCell.getChildren() ).some( child => child.is( 'paragraph' ) );
// If there is no paragraph in table cell then the view doesn't require refreshing.
//
// Why? What we really want to achieve is to make all the old paragraphs (which weren't added in this batch) to be
// converted once again, so that the paragraph-in-table-cell converter can correctly create a `<p>` or a `<span>` element.
// If there are no paragraphs in the table cell, we don't care.
if ( !hasInnerParagraph ) {
return false;
}
// For attribute change we only refresh if there is a single paragraph as in this case we may want to change existing `<span>` to `<p>`.
if ( type == 'attribute' ) {
const attributesCount = Array.from( tableCell.getChild( 0 ).getAttributeKeys() ).length;
return tableCell.childCount === 1 && attributesCount < 2;
}
// For other changes (insert, remove) the `<span>` to `<p>` change is needed when:
//
// - another element is added to a single paragraph (childCount becomes >= 2)
// - another element is removed and a single paragraph is left (childCount == 1)
return tableCell.childCount <= ( type == 'insert' ? 2 : 1 );
}