-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
table-heading-rows-refresh-post-fixer.js
54 lines (43 loc) · 1.81 KB
/
table-heading-rows-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
/**
* @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-heading-rows-refresh-post-fixer
*/
/**
* Injects a table post-fixer into the model which marks the table in the differ to have it re-rendered.
*
* Table heading rows are represented in the model by a `headingRows` attribute. However, in the view, it's represented as separate
* sections of the table (`<thead>` or `<tbody>`) and changing `headingRows` attribute requires moving table rows between two sections.
* This causes problems with structural changes in a table (like adding and removing rows) thus atomic converters cannot be used.
*
* When table `headingRows` attribute changes, the entire table is re-rendered.
*
* @param {module:engine/model/model~Model} model
*/
export default function injectTableHeadingRowsRefreshPostFixer( model ) {
model.document.registerPostFixer( () => tableHeadingRowsRefreshPostFixer( model ) );
}
function tableHeadingRowsRefreshPostFixer( model ) {
const differ = model.document.differ;
// Stores tables to be refreshed so the table will be refreshed once for multiple changes.
const tablesToRefresh = new Set();
for ( const change of differ.getChanges() ) {
if ( change.type != 'attribute' ) {
continue;
}
const element = change.range.start.nodeAfter;
if ( element && element.is( 'element', 'table' ) && change.attributeKey == 'headingRows' ) {
tablesToRefresh.add( element );
}
}
if ( tablesToRefresh.size ) {
// @if CK_DEBUG_TABLE // console.log( `Post-fixing table: refreshing heading rows (${ tablesToRefresh.size }).` );
for ( const table of tablesToRefresh.values() ) {
differ.refreshItem( table );
}
return true;
}
return false;
}