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

Commit 07e8736

Browse files
authored
Merge pull request #202 from ckeditor/cf/tc-tables
Feature: `TableWalker` will now return `cell` value also for spanned cells when traversing a table with `includeSpanned` option set to `true`. Additionally, `isSpanned` property was introduced in returned values. Internal: Improvements in table plugin allowing for integration with track changes. BREAKING CHANGE: `TableWalker` will not return `undefined` as `cell` value for spanned cells anymore. Use `isSpanned` instead.
2 parents b47a94f + 773fa4c commit 07e8736

File tree

8 files changed

+216
-118
lines changed

8 files changed

+216
-118
lines changed

src/commands/setheadercolumncommand.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,27 @@ export default class SetHeaderColumnCommand extends Command {
6161
* When the selection is already in a header column, it will set `headingColumns` so the heading section will end before that column.
6262
*
6363
* @fires execute
64+
* @params {Boolean} [forceValue] If set, the command will set (`true`) or unset (`false`) header columns according to `forceValue`
65+
* parameter instead of the current model state.
6466
*/
65-
execute() {
67+
execute( forceValue = null ) {
6668
const model = this.editor.model;
6769
const doc = model.document;
6870
const selection = doc.selection;
6971
const tableUtils = this.editor.plugins.get( 'TableUtils' );
7072

7173
const position = selection.getFirstPosition();
72-
const tableCell = findAncestor( 'tableCell', position.parent );
74+
const tableCell = findAncestor( 'tableCell', position );
7375
const tableRow = tableCell.parent;
7476
const table = tableRow.parent;
7577

76-
const currentHeadingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
7778
const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
7879

79-
const headingColumnsToSet = currentHeadingColumns > selectionColumn ? selectionColumn : selectionColumn + 1;
80+
if ( forceValue === this.value ) {
81+
return;
82+
}
83+
84+
const headingColumnsToSet = this.value ? selectionColumn : selectionColumn + 1;
8085

8186
model.change( writer => {
8287
updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );

src/commands/setheaderrowcommand.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export default class SetHeaderRowCommand extends Command {
6060
* When the selection is already in a header row, it will set `headingRows` so the heading section will end before that row.
6161
*
6262
* @fires execute
63+
* @params {Boolean} [forceValue] If set, the command will set (`true`) or unset (`false`) header rows according to `forceValue`
64+
* parameter instead of the current model state.
6365
*/
64-
execute() {
66+
execute( forceValue = null ) {
6567
const model = this.editor.model;
6668
const doc = model.document;
6769
const selection = doc.selection;
@@ -74,7 +76,11 @@ export default class SetHeaderRowCommand extends Command {
7476
const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0;
7577
const selectionRow = tableRow.index;
7678

77-
const headingRowsToSet = currentHeadingRows > selectionRow ? selectionRow : selectionRow + 1;
79+
if ( forceValue === this.value ) {
80+
return;
81+
}
82+
83+
const headingRowsToSet = this.value ? selectionRow : selectionRow + 1;
7884

7985
model.change( writer => {
8086
if ( headingRowsToSet ) {
@@ -151,19 +157,21 @@ function splitHorizontally( tableCell, headingRows, writer ) {
151157
attributes.rowspan = spanToSet;
152158
}
153159

160+
const colspan = parseInt( tableCell.getAttribute( 'colspan' ) || 1 );
161+
162+
if ( colspan > 1 ) {
163+
attributes.colspan = colspan;
164+
}
165+
154166
const startRow = table.getChildIndex( tableRow );
155167
const endRow = startRow + newRowspan;
156168
const tableMap = [ ...new TableWalker( table, { startRow, endRow, includeSpanned: true } ) ];
157169

158170
let columnIndex;
159171

160-
for ( const { row, column, cell, colspan, cellIndex } of tableMap ) {
161-
if ( cell === tableCell ) {
172+
for ( const { row, column, cell, cellIndex } of tableMap ) {
173+
if ( cell === tableCell && columnIndex === undefined ) {
162174
columnIndex = column;
163-
164-
if ( colspan > 1 ) {
165-
attributes.colspan = colspan;
166-
}
167175
}
168176

169177
if ( columnIndex !== undefined && columnIndex === column && row === endRow ) {

src/converters/downcast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ function renameViewTableCell( tableCell, desiredCellElementName, conversionApi,
345345
renamedCell = viewWriter.rename( desiredCellElementName, viewCell );
346346
}
347347

348+
conversionApi.mapper.unbindViewElement( viewCell );
348349
conversionApi.mapper.bindElements( tableCell, renamedCell );
349350
}
350351

src/tableutils.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export default class TableUtils extends Plugin {
180180
* | | c | | | c |
181181
* +---+---+---+ will give: +---+---+---+---+---+
182182
* | d | e | f | | d | | | e | f |
183-
* +---+ +---+ +---+---+---+ +---+
183+
* +---+ +---+ +---+---+---+ +---+
184184
* | g | | h | | g | | | | h |
185185
* +---+---+---+ +---+---+---+---+---+
186186
* | i | | i |
@@ -219,13 +219,16 @@ export default class TableUtils extends Plugin {
219219

220220
const tableWalker = new TableWalker( table, { column: insertAt, includeSpanned: true } );
221221

222-
for ( const { row, column, cell, colspan, rowspan, cellIndex } of tableWalker ) {
222+
for ( const { row, cell, cellIndex } of tableWalker ) {
223223
// When iterating over column the table walker outputs either:
224224
// - cells at given column index (cell "e" from method docs),
225225
// - spanned columns (spanned cell from row between cells "g" and "h" - spanned by "e", only if `includeSpanned: true`),
226226
// - or a cell from the same row which spans over this column (cell "a").
227227

228-
if ( column !== insertAt ) {
228+
const rowspan = parseInt( cell.getAttribute( 'rowspan' ) || 1 );
229+
const colspan = parseInt( cell.getAttribute( 'colspan' ) || 1 );
230+
231+
if ( cell.index !== insertAt && colspan > 1 ) {
229232
// If column is different than `insertAt`, it is a cell that spans over an inserted column (cell "a" & "i").
230233
// For such cells expand them by a number of columns inserted.
231234
writer.setAttribute( 'colspan', colspan + columnsToInsert, cell );

0 commit comments

Comments
 (0)