-
Notifications
You must be signed in to change notification settings - Fork 18
Changes from 23 commits
6fd5103
c43ecb3
1026056
b21451f
e45ba7f
9dde19b
b2919df
a8d9a5c
3d0ede9
9f8a1be
1404f22
72c9709
e797859
67244ca
a5a3140
5e6744b
710b8a6
969347b
27e9222
91da813
dbb0a53
df93871
8d95244
0608377
4d7e2c7
a50de97
7e03fe5
c887db8
cf7f363
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved. | ||
* For licensing, see LICENSE.md. | ||
*/ | ||
|
||
/** | ||
* @module table/commands/setheadercolumncommand | ||
*/ | ||
|
||
import Command from '@ckeditor/ckeditor5-core/src/command'; | ||
|
||
import { getParentTable, updateNumericAttribute } from './utils'; | ||
|
||
/** | ||
* The header coloumn command. | ||
* | ||
* @extends module:core/command~Command | ||
*/ | ||
export default class SetHeaderColumnCommand extends Command { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
refresh() { | ||
const model = this.editor.model; | ||
const doc = model.document; | ||
const selection = doc.selection; | ||
|
||
const position = selection.getFirstPosition(); | ||
const tableParent = getParentTable( position ); | ||
|
||
this.isEnabled = !!tableParent; | ||
|
||
this.value = this.isEnabled && this._isInHeading( position.parent, tableParent ); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure it does more than that. It must be explained (see: |
||
*/ | ||
execute() { | ||
const model = this.editor.model; | ||
const doc = model.document; | ||
const selection = doc.selection; | ||
const tableUtils = this.editor.plugins.get( 'TableUtils' ); | ||
|
||
const position = selection.getFirstPosition(); | ||
const tableCell = position.parent; | ||
const tableRow = tableCell.parent; | ||
const table = tableRow.parent; | ||
|
||
const currentHeadingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 ); | ||
|
||
const { column } = tableUtils.getCellLocation( tableCell ); | ||
|
||
const columnsToSet = column + 1 !== currentHeadingColumns ? column + 1 : column; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ify it let { column } = tableUtils.getCellLocation( tableCell );
if ( column !== currentHeadingColumns - 1 ) {
column ++;
} |
||
|
||
model.change( writer => { | ||
updateNumericAttribute( 'headingColumns', columnsToSet, table, writer, 0 ); | ||
} ); | ||
} | ||
|
||
/** | ||
* Checks if table cell is in heading section. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @param {module:engine/model/element~Element} tableCell | ||
* @param {module:engine/model/element~Element} table | ||
* @returns {Boolean} | ||
* @private | ||
*/ | ||
_isInHeading( tableCell, table ) { | ||
const headingColumns = parseInt( table.getAttribute( 'headingColumns' ) || 0 ); | ||
|
||
const tableUtils = this.editor.plugins.get( 'TableUtils' ); | ||
|
||
const { column } = tableUtils.getCellLocation( tableCell ); | ||
|
||
return !!headingColumns && column < headingColumns; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
*/ | ||
|
||
/** | ||
* @module table/commands/settableheaderscommand | ||
* @module table/commands/setheaderrowcommand | ||
*/ | ||
|
||
import Command from '@ckeditor/ckeditor5-core/src/command'; | ||
|
@@ -14,11 +14,11 @@ import { getParentTable, updateNumericAttribute } from './utils'; | |
import TableWalker from '../tablewalker'; | ||
|
||
/** | ||
* The set table headers command. | ||
* The header row command. | ||
* | ||
* @extends module:core/command~Command | ||
*/ | ||
export default class SetTableHeadersCommand extends Command { | ||
export default class SetHeaderRowCommand extends Command { | ||
/** | ||
* @inheritDoc | ||
*/ | ||
|
@@ -27,27 +27,34 @@ export default class SetTableHeadersCommand extends Command { | |
const doc = model.document; | ||
const selection = doc.selection; | ||
|
||
const tableParent = getParentTable( selection.getFirstPosition() ); | ||
const position = selection.getFirstPosition(); | ||
const tableParent = getParentTable( position ); | ||
|
||
this.isEnabled = !!tableParent; | ||
|
||
this.value = this.isEnabled && this._isInHeading( position.parent, tableParent ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in |
||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
execute( options = {} ) { | ||
execute() { | ||
const model = this.editor.model; | ||
const doc = model.document; | ||
const selection = doc.selection; | ||
|
||
const rowsToSet = parseInt( options.rows ) || 0; | ||
const position = selection.getFirstPosition(); | ||
const tableCell = position.parent; | ||
const tableRow = tableCell.parent; | ||
const table = tableRow.parent; | ||
|
||
const table = getParentTable( selection.getFirstPosition() ); | ||
const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0; | ||
const rowIndex = tableRow.index; | ||
|
||
model.change( writer => { | ||
const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0; | ||
const rowsToSet = rowIndex + 1 !== currentHeadingRows ? rowIndex + 1 : rowIndex; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my comment in |
||
|
||
if ( currentHeadingRows !== rowsToSet && rowsToSet > 0 ) { | ||
model.change( writer => { | ||
if ( rowsToSet ) { | ||
// Changing heading rows requires to check if any of a heading cell is overlaping vertically the table head. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Any table cell that has a rowspan attribute > 1 will not exceed the table head so we need to fix it in rows below. | ||
const cellsToSplit = getOverlappingCells( table, rowsToSet, currentHeadingRows ); | ||
|
@@ -57,11 +64,23 @@ export default class SetTableHeadersCommand extends Command { | |
} | ||
} | ||
|
||
const columnsToSet = parseInt( options.columns ) || 0; | ||
updateTableAttribute( table, 'headingColumns', columnsToSet, writer ); | ||
updateTableAttribute( table, 'headingRows', rowsToSet, writer ); | ||
updateNumericAttribute( 'headingRows', rowsToSet, table, writer, 0 ); | ||
} ); | ||
} | ||
|
||
/** | ||
* Checks if table cell is in heading section. | ||
* | ||
* @param {module:engine/model/element~Element} tableCell | ||
* @param {module:engine/model/element~Element} table | ||
* @returns {Boolean} | ||
* @private | ||
*/ | ||
_isInHeading( tableCell, table ) { | ||
const headingRows = parseInt( table.getAttribute( 'headingRows' ) || 0 ); | ||
|
||
return !!headingRows && tableCell.parent.index < headingRows; | ||
} | ||
} | ||
|
||
// Returns cells that span beyond new heading section. | ||
|
@@ -86,15 +105,6 @@ function getOverlappingCells( table, headingRowsToSet, currentHeadingRows ) { | |
return cellsToSplit; | ||
} | ||
|
||
// @private | ||
function updateTableAttribute( table, attributeName, newValue, writer ) { | ||
const currentValue = table.getAttribute( attributeName ) || 0; | ||
|
||
if ( newValue !== currentValue ) { | ||
updateNumericAttribute( attributeName, newValue, table, writer, 0 ); | ||
} | ||
} | ||
|
||
// Splits table cell horizontally. | ||
// | ||
// @param {module:engine/model/element~Element} tableCell | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the value dependent on
isEnabled
? https://docs.ckeditor.com/ckeditor5/latest/api/module_core_command-Command.html#member-value does not mention that and I couldn't find another command which acts like that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update this piece of code as it does not make sense to check if selection is in heading when there is no table.