Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table caption feature #9242

Merged
merged 76 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
1666340
Add downcast.
maxbarnas Feb 23, 2021
05801af
Simplified conversion.
maxbarnas Mar 3, 2021
10666d2
Data and editing downcasting of table captions.
maxbarnas Mar 8, 2021
eabc2f7
Update postfixers. Clean up a bit.
maxbarnas Mar 8, 2021
034f257
Upcast fixes.
maxbarnas Mar 9, 2021
8785049
Typo.
maxbarnas Mar 9, 2021
81f7437
Docs improvements.
maxbarnas Mar 9, 2021
7b09f1c
Add test.
maxbarnas Mar 9, 2021
24d5fa5
Remove export.
maxbarnas Mar 10, 2021
3d7af2a
Review fixes.
niegowski Mar 10, 2021
12bd828
Update tests.
maxbarnas Mar 10, 2021
4a23f33
Clean up tests.
maxbarnas Mar 11, 2021
b3c1c3a
Editing view test.
maxbarnas Mar 11, 2021
7719969
Merge pull request #9187 from ckeditor/i/9084
niegowski Mar 15, 2021
2814a79
Merge remote-tracking branch 'origin/master' into i/3204-table-caption
maxbarnas Mar 17, 2021
24dd854
Add postfixer.
maxbarnas Mar 10, 2021
6f719fe
Add postfixer for repeated captions.
maxbarnas Mar 10, 2021
4a2d6d2
Update postfixer. Merge multiple captions into one and move it to the…
maxbarnas Mar 11, 2021
f10e232
Update TableUtils.
maxbarnas Mar 11, 2021
4f17391
More incorrect row counting fixed.
maxbarnas Mar 12, 2021
1a50e8f
Merge scattered captions into one. Update tests. Keep postfixer in se…
maxbarnas Mar 15, 2021
922408d
Update tests.
maxbarnas Mar 15, 2021
7596af2
Review fixes.
maxbarnas Mar 16, 2021
f81e2e4
Merge remote-tracking branch 'origin/master' into i/3204-table-caption
maxbarnas Mar 23, 2021
cf25917
Merge branch 'i/3204-table-caption' into i/9105-table-algorithms
maxbarnas Mar 23, 2021
0541ef5
Add table caption UI.
maxbarnas Mar 17, 2021
2303a3c
Update placement of the toolbar.
maxbarnas Mar 19, 2021
d0a868a
Allow toggling the caption from both the table and content toolbar.
maxbarnas Mar 22, 2021
cd4e637
Clean up the code. Add tests.
maxbarnas Mar 23, 2021
4924bdf
Add styles.
maxbarnas Mar 24, 2021
aa5aee1
Fix QA review issue: table cell buttons enabled when in caption.
maxbarnas Mar 26, 2021
98d7aa3
Fix QA issues: default focus on caption hiding, placeholder styling.
maxbarnas Mar 26, 2021
9cb8fd0
Update tests.
maxbarnas Mar 29, 2021
c782eec
Initial docs update.
maxbarnas Mar 30, 2021
f690859
Merge branch 'master' into i/3204-table-caption
niegowski Mar 30, 2021
88bb8db
Merge branch 'i/3204-table-caption' into i/9105-table-algorithms
niegowski Mar 30, 2021
fe67c4b
Fixed TableWalker ignoring non tableRow elements.
niegowski Mar 30, 2021
79bcea1
Apply suggestions from code review
maxbarnas Mar 30, 2021
f629562
Doc update.
maxbarnas Mar 30, 2021
681e790
Import entire TableCaption plugin.
maxbarnas Mar 30, 2021
f11d849
Review fixes.
maxbarnas Mar 31, 2021
552fa33
WIP: tableutils.
maxbarnas Apr 8, 2021
ca5b0e9
Review fixes for tableutils.
maxbarnas Apr 12, 2021
2ad76b9
Update comment.
maxbarnas Apr 13, 2021
032d356
Review fixes.
maxbarnas Apr 14, 2021
4fd63b7
Merge remote-tracking branch 'origin/master' into i/3204-table-caption
maxbarnas Apr 16, 2021
7be53fd
Merge branch 'i/3204-table-caption' into i/9105-table-algorithms
maxbarnas Apr 16, 2021
2d7fe6d
Merge branch 'i/9105-table-algorithms' into i/9104-caption-toolbar
maxbarnas Apr 16, 2021
1e24af8
Add tests.
maxbarnas Apr 19, 2021
2598cfd
Docs and CSS.
maxbarnas Apr 19, 2021
aa8d950
Proper imports in tests. Moved the doc section up.
maxbarnas Apr 20, 2021
7cbf15d
Update packages/ckeditor5-table/tests/tableutils.js
maxbarnas Apr 22, 2021
7fc744d
Removed unnecessary checks in split*Cell methods, updated tests.
maxbarnas Apr 22, 2021
5a972db
Rename `getClosestParentTable` to `getSelectionAffectedTable`.
maxbarnas Apr 22, 2021
26ae590
Remove `tableToolbar` config from manual tests.
maxbarnas Apr 22, 2021
c97ceac
Misc review fixes.
maxbarnas Apr 22, 2021
ef70403
Wrong test desc fixed.
maxbarnas Apr 22, 2021
1846a39
Merge pull request #9209 from ckeditor/i/9105-table-algorithms
niegowski Apr 22, 2021
e1be5bf
Cleanup.
maxbarnas Apr 22, 2021
89f7743
New section in docs for installation of the table caption plugin.
maxbarnas Apr 22, 2021
2d8fa7f
Merge pull request #9263 from ckeditor/i/9104-caption-toolbar
niegowski Apr 22, 2021
75d586c
Merge remote-tracking branch 'origin/i/3204-table-caption' into i/908…
maxbarnas Apr 22, 2021
0756be7
Docs for table caption got a demo snippet.
maxbarnas Apr 22, 2021
eb6b39c
Revert table tour ballong removal.
maxbarnas Apr 26, 2021
fb58c54
Change the demo of table caption feature.
maxbarnas Apr 26, 2021
3bf7aa7
Merge remote-tracking branch 'origin/master' into i/3204-table-caption
maxbarnas Apr 26, 2021
9387759
Merge branch 'i/3204-table-caption' into i/9085-table-caption-docs
maxbarnas Apr 26, 2021
522c293
Update demo of table caption.
maxbarnas Apr 26, 2021
fb8c775
Merge pull request #9384 from ckeditor/i/9085-table-caption-docs
niegowski Apr 26, 2021
6c797b9
Store a caption in TableCaptionEditing caption registry.
maxbarnas Apr 29, 2021
5b5ec29
Merge remote-tracking branch 'origin/master' into i/3204-table-caption
maxbarnas Apr 29, 2021
d01f4ad
Merge branch 'i/3204-table-caption' into i/9543-table-caption-storage
maxbarnas Apr 29, 2021
32f157f
Fixed the test.
niegowski May 5, 2021
424190d
Apply suggestions from code review
niegowski May 5, 2021
8424e05
Merge pull request #9590 from ckeditor/i/9543-table-caption-storage
niegowski May 5, 2021
43f07af
Merge branch 'master' into i/3204-table-caption
niegowski May 7, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function tableCaptionPostFixer( writer, model ) {
const firstCaption = captionsToMerge.shift();

if ( !firstCaption ) {
return;
continue;
}

// Move all the contents of the captions to the first one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ function getChildrenLengths( table ) {
// TableWalker will not provide items for the empty rows, we need to pre-fill this array.
const lengths = new Array( table.childCount ).fill( 0 );

for ( const { row } of new TableWalker( table, { includeAllSlots: true } ) ) {
lengths[ row ]++;
for ( const { rowIndex } of new TableWalker( table, { includeAllSlots: true } ) ) {
lengths[ rowIndex ]++;
}

return lengths;
Expand Down
7 changes: 0 additions & 7 deletions packages/ckeditor5-table/src/tableediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ export default class TableEditing extends Plugin {
// Allow all $block content inside a table cell.
schema.extend( '$block', { allowIn: 'tableCell' } );

// Disallow a table in a table.
schema.addChildCheck( ( context, childDefinition ) => {
if ( childDefinition.name == 'table' && Array.from( context.getNames() ).includes( 'table' ) ) {
return false;
}
} );

// Figure conversion.
conversion.for( 'upcast' ).add( upcastTableFigure() );

Expand Down
30 changes: 28 additions & 2 deletions packages/ckeditor5-table/src/tableutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @module table/tableutils
*/

import { CKEditorError } from 'ckeditor5/src/utils';
import { Plugin } from 'ckeditor5/src/core';

import TableWalker from './tablewalker';
Expand Down Expand Up @@ -153,6 +154,19 @@ export default class TableUtils extends Plugin {
const rows = this.getRows( table );
const columns = this.getColumns( table );

if ( insertAt > rows ) {
/**
* The `options.at` points at a row position that does not exist.
*
* @error tableutils-insertrows-insert-out-of-range
*/
throw new CKEditorError(
'tableutils-insertrows-insert-out-of-range',
this,
{ options }
);
}

model.change( writer => {
const headingRows = table.getAttribute( 'headingRows' ) || 0;

Expand Down Expand Up @@ -334,8 +348,11 @@ export default class TableUtils extends Plugin {
const model = this.editor.model;

const rowsToRemove = options.rows || 1;
const rowCount = this.getRows( table );
const first = options.at;
const last = first + rowsToRemove - 1;

// Make sure we are not removing too much, e.g. non-row elements at the end of the table.
const last = Math.min( first + rowsToRemove - 1, rowCount - 1 );

model.change( writer => {
// Removing rows from the table require that most calculations to be done prior to changing table structure.
Expand Down Expand Up @@ -479,6 +496,10 @@ export default class TableUtils extends Plugin {
* @param {Number} numberOfCells
*/
splitCellVertically( tableCell, numberOfCells = 2 ) {
if ( !tableCell || !tableCell.is( 'element', 'tableCell' ) ) {
return;
}

const model = this.editor.model;
const tableRow = tableCell.parent;
const table = tableRow.parent;
Expand Down Expand Up @@ -615,6 +636,10 @@ export default class TableUtils extends Plugin {
* @param {Number} numberOfCells
*/
splitCellHorizontally( tableCell, numberOfCells = 2 ) {
if ( !tableCell || !tableCell.is( 'element', 'tableCell' ) ) {
return;
}

const model = this.editor.model;

const tableRow = tableCell.parent;
Expand Down Expand Up @@ -723,7 +748,8 @@ export default class TableUtils extends Plugin {
*/
getColumns( table ) {
// Analyze first row only as all the rows should have the same width.
// We are taking first row without checking because we expect that table will have only tableRow models at the beginning.
// Using the first row without checking if it's a tableRow because we expect
// that table will have only tableRow model elements at the beginning.
const row = table.getChild( 0 );

return [ ...row.getChildren() ].reduce( ( columns, row ) => {
Expand Down
40 changes: 37 additions & 3 deletions packages/ckeditor5-table/src/tablewalker.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ export default class TableWalker {
*/
this._row = 0;

/**
* The index of the current row element in the table.
*
* @type {Number}
* @protected
*/
this._rowIndex = 0;

/**
* The current column index.
*
Expand Down Expand Up @@ -209,15 +217,21 @@ export default class TableWalker {
* @returns {module:table/tablewalker~TableSlot} The next table walker's value.
*/
next() {
const row = this._table.getChild( this._row );
const row = this._table.getChild( this._rowIndex );

// Iterator is done when there's no row (table ended) or the row is after `endRow` limit.
if ( !row || this._isOverEndRow() ) {
return { done: true };
}

// We step over current row when it is not a tableRow instance or we reached the end of it.
if ( !row.is( 'element', 'tableRow' ) || this._isOverEndColumn() ) {
// We step over current element when it is not a tableRow instance.
if ( !row.is( 'element', 'tableRow' ) ) {
this._rowIndex++;

return this.next();
}

if ( this._isOverEndColumn() ) {
return this._advanceToNextRow();
}

Expand Down Expand Up @@ -281,6 +295,7 @@ export default class TableWalker {
*/
_advanceToNextRow() {
this._row++;
this._rowIndex++;
this._column = 0;
this._cellIndex = 0;
this._nextCellAtColumn = -1;
Expand Down Expand Up @@ -466,6 +481,15 @@ class TableSlot {
*/
this._cellIndex = tableWalker._cellIndex;

/**
* The index of the current row element in the table.
*
* @readonly
* @member {Number}
* @private
*/
this._rowIndex = tableWalker._rowIndex;

/**
* The table element.
*
Expand Down Expand Up @@ -506,6 +530,16 @@ class TableSlot {
return parseInt( this.cell.getAttribute( 'rowspan' ) || 1 );
}

/**
* The index of the current row element in the table.
*
* @readonly
* @returns {Number}
*/
get rowIndex() {
return this._rowIndex;
}

/**
* Returns the {@link module:engine/model/position~Position} before the table slot.
*
Expand Down
6 changes: 3 additions & 3 deletions packages/ckeditor5-table/src/utils/structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { createEmptyTableCell, updateNumericAttribute } from './common';
*
* const croppedTable = cropTableToDimensions( table, {
* startRow: 1,
* endRow: 1,
* startColumn: 3,
* endColumn: 3
* endRow: 3,
* startColumn: 1,
* endColumn: 3done
* }, writer );
*
* Calling the code above for the table below:
Expand Down
24 changes: 24 additions & 0 deletions packages/ckeditor5-table/tests/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,30 @@ describe( 'MergeCellCommand', () => {
expect( command.value ).to.be.undefined;
} );

it( 'should be undefined if in last row - ignore non-row elements', () => {
model.schema.register( 'foo', {
allowIn: 'table',
allowContentOf: '$block',
isLimit: true
} );

setData( model,
'<table>' +
'<tableRow>' +
'<tableCell><paragraph>00</paragraph></tableCell>' +
'<tableCell><paragraph>10</paragraph></tableCell>' +
'</tableRow>' +
'<tableRow>' +
'<tableCell><paragraph>10[]</paragraph></tableCell>' +
'<tableCell><paragraph>11</paragraph></tableCell>' +
'</tableRow>' +
'<foo>An extra element</foo>' +
'</table>'
);

expect( command.value ).to.be.undefined;
} );

it( 'should be set to mergeable cell with the same rowspan', () => {
setData( model, modelTable( [
[ { colspan: 2, contents: '00[]' }, '02' ],
Expand Down
39 changes: 39 additions & 0 deletions packages/ckeditor5-table/tests/commands/removerowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,45 @@ describe( 'RemoveRowCommand', () => {
] ) );
} );

it( 'should remove last row - ignore non-row elements', () => {
model.schema.register( 'foo', {
allowIn: 'table',
allowContentOf: '$block',
isLimit: true
} );

editor.conversion.elementToElement( {
view: 'foo',
model: 'foo'
} );

setData( model,
'<table>' +
'<tableRow>' +
'<tableCell><paragraph>00</paragraph></tableCell>' +
'<tableCell><paragraph>01</paragraph></tableCell>' +
'</tableRow>' +
'<tableRow>' +
'<tableCell><paragraph>[]10</paragraph></tableCell>' +
'<tableCell><paragraph>11</paragraph></tableCell>' +
'</tableRow>' +
'<foo>An extra element</foo>' +
'</table>'
);

command.execute();

assertEqualMarkup( getData( model ),
'<table>' +
'<tableRow>' +
'<tableCell><paragraph>[]00</paragraph></tableCell>' +
'<tableCell><paragraph>01</paragraph></tableCell>' +
'</tableRow>' +
'<foo>An extra element</foo>' +
'</table>'
);
} );

it( 'should change heading rows if removing a heading row', () => {
setData( model, modelTable( [
[ '00', '01' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
import { getData as getModelData, parse, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';

import TableEditing from '../../src/tableediting';
import TableCaptionEditing from '../../src/tablecaption/tablecaptionediting';
import { assertEqualMarkup } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
import TableWalker from '../../src/tablewalker';

describe( 'Table caption post-fixer', () => {
let editor, model, root;
Expand Down Expand Up @@ -120,6 +121,71 @@ describe( 'Table caption post-fixer', () => {
);
} );

it( 'should merge all captions in between the rows (and TableWalker should still provide valid rows)', () => {
const modelTable =
'<table>' +
'<caption>caption 0</caption>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>00</paragraph>' +
'</tableCell>' +
'<tableCell>' +
'<paragraph>01</paragraph>' +
'</tableCell>' +
'</tableRow>' +
'<caption>caption 1</caption>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>10</paragraph>' +
'</tableCell>' +
'<tableCell>' +
'<paragraph>11</paragraph>' +
'</tableCell>' +
'</tableRow>' +
'<caption>caption 2</caption>' +
'</table>';
const parsed = parse( modelTable, model.schema );

model.change( writer => {
writer.remove( writer.createRangeIn( root ) );
writer.insert( parsed, root );

const slots = Array.from( new TableWalker( root.getChild( 0 ) ) );

expect( slots.length ).to.equal( 4 );
expect( slots[ 0 ].row ).to.equal( 0 );
expect( slots[ 0 ].column ).to.equal( 0 );
expect( slots[ 1 ].row ).to.equal( 0 );
expect( slots[ 1 ].column ).to.equal( 1 );
expect( slots[ 2 ].row ).to.equal( 1 );
expect( slots[ 2 ].column ).to.equal( 0 );
expect( slots[ 3 ].row ).to.equal( 1 );
expect( slots[ 3 ].column ).to.equal( 1 );
} );

assertEqualMarkup( getModelData( model, { withoutSelection: true } ),
'<table>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>00</paragraph>' +
'</tableCell>' +
'<tableCell>' +
'<paragraph>01</paragraph>' +
'</tableCell>' +
'</tableRow>' +
'<tableRow>' +
'<tableCell>' +
'<paragraph>10</paragraph>' +
'</tableCell>' +
'<tableCell>' +
'<paragraph>11</paragraph>' +
'</tableCell>' +
'</tableRow>' +
'<caption>caption 0caption 1caption 2</caption>' +
'</table>'
);
} );

it( 'should move final caption at the end of the table', () => {
const modelTable =
'<table>' +
Expand Down
Loading