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

Integrated multi cell selection with set column /row header commands #268

Merged
merged 12 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
24 changes: 12 additions & 12 deletions src/commands/setheadercolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

import { updateNumericAttribute } from './utils';
import { getTableCellsContainingSelection } from '../utils';
import { getSelectionAffectedTableCells } from '../utils';

/**
* The header column command.
Expand All @@ -33,10 +33,8 @@ export default class SetHeaderColumnCommand extends Command {
*/
refresh() {
const model = this.editor.model;
const doc = model.document;
const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];

const isInTable = !!tableCell;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const isInTable = selectedCells.length > 0;

this.isEnabled = isInTable;

Expand All @@ -48,7 +46,7 @@ export default class SetHeaderColumnCommand extends Command {
* @readonly
* @member {Boolean} #value
*/
this.value = isInTable && this._isInHeading( tableCell, tableCell.parent.parent );
this.value = isInTable && selectedCells.every( cell => this._isInHeading( cell, cell.parent.parent ) );
}

/**
Expand All @@ -65,21 +63,23 @@ export default class SetHeaderColumnCommand extends Command {
*/
execute( options = {} ) {
const model = this.editor.model;
const doc = model.document;
const selection = doc.selection;
const tableUtils = this.editor.plugins.get( 'TableUtils' );

const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const firstCell = selectedCells[ 0 ];
const lastCell = selectedCells[ selectedCells.length - 1 ];
const tableRow = firstCell.parent;
const table = tableRow.parent;

const { column: selectionColumn } = tableUtils.getCellLocation( tableCell );
const [ selectedColumnMin, selectedColumnMax ] =
// Returned cells might not necessary be in order, so make sure to sort it.
[ tableUtils.getCellLocation( firstCell ).column, tableUtils.getCellLocation( lastCell ).column ].sort();

if ( options.forceValue === this.value ) {
return;
}

const headingColumnsToSet = this.value ? selectionColumn : selectionColumn + 1;
const headingColumnsToSet = this.value ? selectedColumnMin : selectedColumnMax + 1;

model.change( writer => {
updateNumericAttribute( 'headingColumns', headingColumnsToSet, table, writer, 0 );
Expand Down
32 changes: 14 additions & 18 deletions src/commands/setheaderrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

import Command from '@ckeditor/ckeditor5-core/src/command';

import {
createEmptyTableCell,
updateNumericAttribute
} from './utils';
import { getTableCellsContainingSelection } from '../utils';
import { createEmptyTableCell, updateNumericAttribute } from './utils';
import { getSelectionAffectedTableCells } from '../utils';
import TableWalker from '../tablewalker';

/**
Expand All @@ -36,11 +33,8 @@ export default class SetHeaderRowCommand extends Command {
*/
refresh() {
const model = this.editor.model;
const doc = model.document;
const selection = doc.selection;

const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const isInTable = !!tableCell;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const isInTable = selectedCells.length > 0;

this.isEnabled = isInTable;

Expand All @@ -52,7 +46,7 @@ export default class SetHeaderRowCommand extends Command {
* @readonly
* @member {Boolean} #value
*/
this.value = isInTable && this._isInHeading( tableCell, tableCell.parent.parent );
this.value = isInTable && selectedCells.every( cell => this._isInHeading( cell, cell.parent.parent ) );
}

/**
Expand All @@ -69,21 +63,23 @@ export default class SetHeaderRowCommand extends Command {
*/
execute( options = {} ) {
const model = this.editor.model;
const doc = model.document;
const selection = doc.selection;

const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const table = tableRow.parent;
const selectedCells = getSelectionAffectedTableCells( model.document.selection );
const firstCell = selectedCells[ 0 ];
const lastCell = selectedCells[ selectedCells.length - 1 ];
const table = firstCell.parent.parent;

const currentHeadingRows = table.getAttribute( 'headingRows' ) || 0;
const selectionRow = tableRow.index;

const [ selectedRowMin, selectedRowMax ] =
// Returned cells might not necessary be in order, so make sure to sort it.
[ firstCell.parent.index, lastCell.parent.index ].sort();

if ( options.forceValue === this.value ) {
return;
}

const headingRowsToSet = this.value ? selectionRow : selectionRow + 1;
const headingRowsToSet = this.value ? selectedRowMin : selectedRowMax + 1;

model.change( writer => {
if ( headingRowsToSet ) {
Expand Down
Loading