From e052923f2c3742a1bee1106fe6224c0b07448710 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Tue, 2 Jun 2020 12:04:01 +0200 Subject: [PATCH 1/3] [WIP] Marker conversion for table cells. --- .../src/converters/downcast.js | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-table/src/converters/downcast.js b/packages/ckeditor5-table/src/converters/downcast.js index d65187bba32..6c557885fe3 100644 --- a/packages/ckeditor5-table/src/converters/downcast.js +++ b/packages/ckeditor5-table/src/converters/downcast.js @@ -8,7 +8,7 @@ */ import TableWalker from './../tablewalker'; -import { toWidgetEditable } from '@ckeditor/ckeditor5-widget/src/utils'; +import { toWidgetEditable, setHighlightHandling } from '@ckeditor/ckeditor5-widget/src/utils'; import { toTableWidget } from '../utils'; /** @@ -330,6 +330,13 @@ function renameViewTableCell( tableCell, desiredCellElementName, conversionApi, const editable = viewWriter.createEditableElement( desiredCellElementName, viewCell.getAttributes() ); renamedCell = toWidgetEditable( editable, viewWriter ); + setHighlightHandling( + renamedCell, + viewWriter, + ( element, descriptor, writer ) => writer.addClass( normalizeToArray( descriptor.classes ), element ), + ( element, descriptor, writer ) => writer.removeClass( normalizeToArray( descriptor.classes ), element ) + ); + viewWriter.insert( viewWriter.createPositionAfter( viewCell ), renamedCell ); viewWriter.move( viewWriter.createRangeIn( viewCell ), viewWriter.createPositionAt( renamedCell, 0 ) ); viewWriter.remove( viewWriter.createRangeOn( viewCell ) ); @@ -341,6 +348,10 @@ function renameViewTableCell( tableCell, desiredCellElementName, conversionApi, conversionApi.mapper.bindElements( tableCell, renamedCell ); } +function normalizeToArray( classes ) { + return Array.isArray( classes ) ? classes : [ classes ]; +} + // Renames a table cell element in the view according to its location in the table. // // @param {module:table/tablewalker~TableSlot} tableSlot @@ -375,6 +386,15 @@ function createViewTableCellElement( tableSlot, tableAttributes, insertPosition, toWidgetEditable( conversionApi.writer.createEditableElement( cellElementName ), conversionApi.writer ) : conversionApi.writer.createContainerElement( cellElementName ); + if ( asWidget ) { + setHighlightHandling( + cellElement, + conversionApi.writer, + ( element, descriptor, writer ) => writer.addClass( normalizeToArray( descriptor.classes ), element ), + ( element, descriptor, writer ) => writer.removeClass( normalizeToArray( descriptor.classes ), element ) + ); + } + const tableCell = tableSlot.cell; const firstChild = tableCell.getChild( 0 ); From 21d51f829c6fceddca5018be99d127ed9f14ed8f Mon Sep 17 00:00:00 2001 From: Kuba Niegowski Date: Fri, 5 Jun 2020 17:43:45 +0200 Subject: [PATCH 2/3] Added tests for conversion of markers on table cells. --- .../tests/converters/downcast.js | 267 +++++++++++++++++- 1 file changed, 266 insertions(+), 1 deletion(-) diff --git a/packages/ckeditor5-table/tests/converters/downcast.js b/packages/ckeditor5-table/tests/converters/downcast.js index dbc79a9be9a..7df7d2d5261 100644 --- a/packages/ckeditor5-table/tests/converters/downcast.js +++ b/packages/ckeditor5-table/tests/converters/downcast.js @@ -329,6 +329,7 @@ describe( 'downcast converters', () => { defaultSchema( model.schema ); defaultConversion( editor.conversion, true ); + markerConversion( editor.conversion ); } ); } ); @@ -350,6 +351,55 @@ describe( 'downcast converters', () => { '' ); } ); + + it( 'should apply marker class on tableCell', () => { + setModelData( model, modelTable( [ [ '00' ] ] ) ); + + model.change( writer => { + const cell = root.getNodeByPath( [ 0, 0, 0 ] ); + + writer.addMarker( 'marker:yellow', { + range: writer.createRangeOn( cell ), + usingOperation: false + } ); + + checkCustomPropertyForHighlight( editor.editing.mapper.toViewElement( cell ) ); + } ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + + model.change( writer => { + writer.removeMarker( 'marker:yellow' ); + } ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + } ); } ); } ); @@ -569,6 +619,7 @@ describe( 'downcast converters', () => { defaultSchema( model.schema ); defaultConversion( editor.conversion, true ); + markerConversion( editor.conversion ); } ); } ); @@ -602,6 +653,69 @@ describe( 'downcast converters', () => { '' ); } ); + + it( 'should apply marker class on tableCell', () => { + setModelData( model, modelTable( [ [ '00' ] ] ) ); + + const table = root.getChild( 0 ); + + model.change( writer => { + const firstRow = writer.createElement( 'tableRow' ); + const cell = writer.createElement( 'tableCell' ); + + writer.insert( firstRow, table, 1 ); + writer.insert( cell, firstRow, 'end' ); + + writer.addMarker( 'marker:yellow', { + range: writer.createRangeOn( cell ), + usingOperation: false + } ); + } ); + + const cell = root.getNodeByPath( [ 0, 1, 0 ] ); + checkCustomPropertyForHighlight( editor.editing.mapper.toViewElement( cell ) ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + + model.change( writer => { + writer.removeMarker( 'marker:yellow' ); + } ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + } ); } ); } ); @@ -731,6 +845,7 @@ describe( 'downcast converters', () => { defaultSchema( model.schema ); defaultConversion( editor.conversion, true ); + markerConversion( editor.conversion ); } ); } ); @@ -747,7 +862,64 @@ describe( 'downcast converters', () => { assertEqualMarkup( getViewData( view, { withoutSelection: true } ), '
' + - '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + } ); + + it( 'should apply marker class on tableCell', () => { + setModelData( model, modelTable( [ [ '00' ] ] ) ); + + const table = root.getChild( 0 ); + + model.change( writer => { + const row = table.getChild( 0 ); + const cell = writer.createElement( 'tableCell' ); + + writer.insert( cell, row, 'end' ); + writer.addMarker( 'marker:yellow', { + range: writer.createRangeOn( cell ), + usingOperation: false + } ); + } ); + + const cell = root.getNodeByPath( [ 0, 0, 1 ] ); + checkCustomPropertyForHighlight( editor.editing.mapper.toViewElement( cell ) ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '
' + + '00' + + '
' + + '
' + ); + + model.change( writer => { + writer.removeMarker( 'marker:yellow' ); + } ); + + assertEqualMarkup( getViewData( view, { withoutSelection: true } ), + '
' + + '
' + '' + '' + '' + @@ -925,6 +1097,7 @@ describe( 'downcast converters', () => { defaultSchema( model.schema ); defaultConversion( editor.conversion, true ); + markerConversion( editor.conversion ); } ); } ); @@ -952,6 +1125,40 @@ describe( 'downcast converters', () => { '' ); } ); + + it( 'should preserve marker class on tableCell', () => { + setModelData( model, modelTable( [ + [ '00', '01', '02', '03' ], + [ '10', '11', '12', '13' ] + ], { headingColumns: 1 } ) ); + + const table = root.getChild( 0 ); + + model.change( writer => { + const cell = root.getNodeByPath( [ 0, 0, 1 ] ); + + writer.addMarker( 'marker:yellow', { + range: writer.createRangeOn( cell ), + usingOperation: false + } ); + } ); + + model.change( writer => { + writer.setAttribute( 'headingColumns', 3, table ); + } ); + + const cell = root.getNodeByPath( [ 0, 0, 1 ] ); + const viewElement = editor.editing.mapper.toViewElement( cell ); + + checkCustomPropertyForHighlight( viewElement ); + expect( viewElement.hasClass( 'highlight-yellow' ) ).to.be.true; + + model.change( writer => { + writer.removeMarker( 'marker:yellow' ); + } ); + + expect( viewElement.hasClass( 'highlight-yellow' ) ).to.be.false; + } ); } ); } ); @@ -965,6 +1172,8 @@ describe( 'downcast converters', () => { doc = model.document; root = doc.getRoot( 'main' ); view = editor.editing.view; + + markerConversion( editor.conversion, 'marker' ); } ); } ); @@ -1174,6 +1383,41 @@ describe( 'downcast converters', () => { '' ); } ); + + it( 'should preserve marker class on tableCell', () => { + setModelData( model, modelTable( [ + [ '00', '01' ], + [ '10', '11' ], + [ '20', '21' ] + ], { headingRows: 1 } ) ); + + const table = root.getChild( 0 ); + + model.change( writer => { + const cell = root.getNodeByPath( [ 0, 1, 0 ] ); + + writer.addMarker( 'marker:yellow', { + range: writer.createRangeOn( cell ), + usingOperation: false + } ); + } ); + + model.change( writer => { + writer.setAttribute( 'headingRows', 2, table ); + } ); + + const cell = root.getNodeByPath( [ 0, 1, 0 ] ); + const viewElement = editor.editing.mapper.toViewElement( cell ); + + checkCustomPropertyForHighlight( viewElement ); + expect( viewElement.hasClass( 'highlight-yellow' ) ).to.be.true; + + model.change( writer => { + writer.removeMarker( 'marker:yellow' ); + } ); + + expect( viewElement.hasClass( 'highlight-yellow' ) ).to.be.false; + } ); } ); } ); @@ -1485,4 +1729,25 @@ describe( 'downcast converters', () => { ], { asWidget: true } ) ); } ); } ); + + function markerConversion( conversion, extraClasses = null ) { + conversion.for( 'editingDowncast' ).markerToHighlight( { + model: 'marker', + view: data => { + const className = 'highlight-' + data.markerName.split( ':' )[ 1 ]; + + return { + classes: extraClasses ? [ ...extraClasses, className ] : className + }; + } + } ); + } + + function checkCustomPropertyForHighlight( viewElement ) { + const set = viewElement.getCustomProperty( 'addHighlight' ); + const remove = viewElement.getCustomProperty( 'removeHighlight' ); + + expect( typeof set ).to.equal( 'function' ); + expect( typeof remove ).to.equal( 'function' ); + } } ); From 597795167916b867d8be4755542d69f791b084a6 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Tue, 9 Jun 2020 14:46:18 +0200 Subject: [PATCH 3/3] Tests: Added a Table feature to the Highlight manual test. --- .../tests/manual/highlight.html | 56 +++++++++++++++---- .../tests/manual/highlight.js | 5 +- .../tests/manual/highlight.md | 7 ++- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/packages/ckeditor5-engine/tests/manual/highlight.html b/packages/ckeditor5-engine/tests/manual/highlight.html index df65f02e5cb..26b7d5fea97 100644 --- a/packages/ckeditor5-engine/tests/manual/highlight.html +++ b/packages/ckeditor5-engine/tests/manual/highlight.html @@ -1,37 +1,61 @@