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

Commit bce6766

Browse files
authored
Merge pull request #90 from ckeditor/t/34
Fix: Toggling headers should always include the column or row the selection is anchored to. Closes #34.
2 parents f03a755 + c5a5020 commit bce6766

File tree

4 files changed

+83
-80
lines changed

4 files changed

+83
-80
lines changed

src/commands/setheadercolumncommand.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,12 @@ export default class SetHeaderColumnCommand extends Command {
7474
const table = tableRow.parent;
7575

7676
const currentHeadingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 );
77+
const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
7778

78-
let { column } = tableUtils.getCellLocation( tableCell );
79-
80-
if ( column + 1 !== currentHeadingColumns ) {
81-
column++;
82-
}
79+
const headingColumnsToSet = currentHeadingColumns > selectionColumn ? selectionColumn : selectionColumn + 1;
8380

8481
model.change( writer => {
85-
updateNumericAttribute( 'headingColumns', column, table, writer, 0 );
82+
updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
8683
} );
8784
}
8885

src/commands/setheaderrowcommand.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,22 @@ export default class SetHeaderRowCommand extends Command {
7474
const table = tableRow.parent;
7575

7676
const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0;
77-
let rowIndex = tableRow.index;
77+
const selectionRow = tableRow.index;
7878

79-
if ( rowIndex + 1 !== currentHeadingRows ) {
80-
rowIndex++;
81-
}
79+
const headingRowsToSet = currentHeadingRows > selectionRow ? selectionRow : selectionRow + 1;
8280

8381
model.change( writer => {
84-
if ( rowIndex ) {
82+
if ( headingRowsToSet ) {
8583
// Changing heading rows requires to check if any of a heading cell is overlapping vertically the table head.
8684
// Any table cell that has a rowspan attribute > 1 will not exceed the table head so we need to fix it in rows below.
87-
const cellsToSplit = getOverlappingCells( table, rowIndex, currentHeadingRows );
85+
const cellsToSplit = getOverlappingCells( table, headingRowsToSet, currentHeadingRows );
8886

8987
for ( const cell of cellsToSplit ) {
90-
splitHorizontally( cell, rowIndex, writer );
88+
splitHorizontally( cell, headingRowsToSet, writer );
9189
}
9290
}
9391

94-
updateNumericAttribute( 'headingRows', rowIndex, table, writer, 0 );
92+
updateNumericAttribute( 'headingRows', headingRowsToSet, table, writer, 0 );
9593
} );
9694
}
9795

tests/commands/setheadercolumncommand.js

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,60 +133,43 @@ describe( 'HeaderColumnCommand', () => {
133133
describe( 'execute()', () => {
134134
it( 'should set heading columns attribute that cover column in which is selection', () => {
135135
setData( model, modelTable( [
136-
[ '00', '01' ],
137-
[ '[]10', '11' ],
138-
[ '20', '21' ]
136+
[ '00', '01[]', '02', '03' ]
139137
] ) );
140138

141139
command.execute();
142140

143141
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
144-
[ '00', '01' ],
145-
[ '[]10', '11' ],
146-
[ '20', '21' ]
147-
], { headingColumns: 1 } ) );
142+
[ '00', '01[]', '02', '03' ]
143+
], { headingColumns: 2 } ) );
148144
} );
149145

150-
it(
151-
'should set heading columns attribute if currently selected column is a heading so the heading section is before this column',
152-
() => {
153-
setData( model, modelTable( [
154-
[ '00', '01' ],
155-
[ '[]10', '11' ],
156-
[ '20', '21' ]
157-
], { headingColumns: 2 } ) );
158-
159-
command.execute();
160-
161-
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
162-
[ '00', '01' ],
163-
[ '[]10', '11' ],
164-
[ '20', '21' ]
165-
], { headingColumns: 1 } ) );
166-
}
167-
);
146+
it( 'should set heading columns attribute below current selection column', () => {
147+
setData( model, modelTable( [
148+
[ '00', '01[]', '02', '03' ]
149+
], { headingColumns: 3 } ) );
150+
151+
command.execute();
152+
153+
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
154+
[ '00', '01[]', '02', '03' ]
155+
], { headingColumns: 1 } ) );
156+
} );
168157

169158
it( 'should toggle of selected column', () => {
170159
setData( model, modelTable( [
171-
[ '00', '01' ],
172-
[ '10', '11[]' ],
173-
[ '20', '21' ]
160+
[ '00', '01[]', '02', '03' ]
174161
], { headingColumns: 2 } ) );
175162

176163
command.execute();
177164

178165
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
179-
[ '00', '01' ],
180-
[ '10', '11[]' ],
181-
[ '20', '21' ]
166+
[ '00', '01[]', '02', '03' ]
182167
], { headingColumns: 1 } ) );
183168

184169
command.execute();
185170

186171
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
187-
[ '00', '01' ],
188-
[ '10', '11[]' ],
189-
[ '20', '21' ]
172+
[ '00', '01[]', '02', '03' ]
190173
], { headingColumns: 2 } ) );
191174
} );
192175
} );

tests/commands/setheaderrowcommand.js

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,60 +135,85 @@ describe( 'HeaderRowCommand', () => {
135135
describe( 'execute()', () => {
136136
it( 'should set heading rows attribute that cover row in which is selection', () => {
137137
setData( model, modelTable( [
138-
[ '00', '01' ],
139-
[ '[]10', '11' ],
140-
[ '20', '21' ]
138+
[ '00' ],
139+
[ '[]10' ],
140+
[ '20' ],
141+
[ '30' ]
141142
] ) );
142143

143144
command.execute();
144145

145146
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
146-
[ '00', '01' ],
147-
[ '[]10', '11' ],
148-
[ '20', '21' ]
147+
[ '00' ],
148+
[ '[]10' ],
149+
[ '20' ],
150+
[ '30' ]
149151
], { headingRows: 2 } ) );
150152
} );
151153

152154
it( 'should toggle heading rows attribute', () => {
153155
setData( model, modelTable( [
154-
[ '[]00', '01' ],
155-
[ '10', '11' ],
156-
[ '20', '21' ]
157-
] ) );
156+
[ '00' ],
157+
[ '[]10' ],
158+
[ '20' ],
159+
[ '30' ]
160+
], { headingRows: 2 } ) );
158161

159162
command.execute();
160163

161164
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
162-
[ '[]00', '01' ],
163-
[ '10', '11' ],
164-
[ '20', '21' ]
165+
[ '00' ],
166+
[ '[]10' ],
167+
[ '20' ],
168+
[ '30' ]
165169
], { headingRows: 1 } ) );
166170

167171
command.execute();
168172

169173
setData( model, modelTable( [
170-
[ '[]00', '01' ],
171-
[ '10', '11' ],
172-
[ '20', '21' ]
173-
] ) );
174+
[ '00' ],
175+
[ '[]10' ],
176+
[ '20' ],
177+
[ '30' ]
178+
], { headingRows: 2 } ) );
174179
} );
175180

176181
it( 'should set heading rows attribute if currently selected row is a heading so the heading section is below this row', () => {
177182
setData( model, modelTable( [
178-
[ '00', '01' ],
179-
[ '[]10', '11' ],
180-
[ '20', '21' ]
181-
], { headingRows: 2 } ) );
183+
[ '00' ],
184+
[ '[]10' ],
185+
[ '20' ],
186+
[ '30' ]
187+
], { headingRows: 3 } ) );
182188

183189
command.execute();
184190

185191
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
186-
[ '00', '01' ],
187-
[ '[]10', '11' ],
188-
[ '20', '21' ]
192+
[ '00' ],
193+
[ '[]10' ],
194+
[ '20' ],
195+
[ '30' ]
189196
], { headingRows: 1 } ) );
190197
} );
191198

199+
it( 'should unsetset heading rows attribute', () => {
200+
setData( model, modelTable( [
201+
[ '[]00' ],
202+
[ '10' ],
203+
[ '20' ],
204+
[ '30' ]
205+
], { headingRows: 3 } ) );
206+
207+
command.execute();
208+
209+
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
210+
[ '[]00' ],
211+
[ '10' ],
212+
[ '20' ],
213+
[ '30' ]
214+
] ) );
215+
} );
216+
192217
it( 'should fix rowspaned cells on the edge of an table head section', () => {
193218
setData( model, modelTable( [
194219
[ '00', '01', '02' ],
@@ -229,29 +254,29 @@ describe( 'HeaderRowCommand', () => {
229254

230255
it( 'should fix rowspaned cells on the edge of an table head section when creating section', () => {
231256
setData( model, modelTable( [
232-
[ { rowspan: 2, contents: '[]00' }, '01' ],
233-
[ '11' ]
257+
[ { rowspan: 2, contents: '00' }, '01' ],
258+
[ '[]11' ]
234259
], { headingRows: 2 } ) );
235260

236261
command.execute();
237262

238263
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
239-
[ '[]00', '01' ],
240-
[ '', '11' ]
264+
[ '00', '01' ],
265+
[ '', '[]11' ]
241266
], { headingRows: 1 } ) );
242267
} );
243268

244269
it( 'should fix rowspaned cells inside a row', () => {
245270
setData( model, modelTable( [
246-
[ '00', { rowspan: 2, contents: '[]01' } ],
247-
[ '10' ]
271+
[ '00', { rowspan: 2, contents: '01' } ],
272+
[ '[]10' ]
248273
], { headingRows: 2 } ) );
249274

250275
command.execute();
251276

252277
expect( formatTable( getData( model ) ) ).to.equal( formattedModelTable( [
253-
[ '00', '[]01' ],
254-
[ '10', '' ]
278+
[ '00', '01' ],
279+
[ '[]10', '' ]
255280
], { headingRows: 1 } ) );
256281
} );
257282

0 commit comments

Comments
 (0)