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

Commit

Permalink
Merge pull request #270 from ckeditor/i/6403
Browse files Browse the repository at this point in the history
Internal: Used table selection helpers across the package. Closes ckeditor/ckeditor5#6403.
  • Loading branch information
Reinmar authored Mar 10, 2020
2 parents 149399e + 6d1d492 commit b231e4d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 36 deletions.
7 changes: 4 additions & 3 deletions src/commands/mergecellcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

import Command from '@ckeditor/ckeditor5-core/src/command';
import TableWalker from '../tablewalker';
import { findAncestor, updateNumericAttribute } from './utils';
import { updateNumericAttribute } from './utils';
import { getTableCellsContainingSelection } from '../utils';

/**
* The merge cell command.
Expand Down Expand Up @@ -78,7 +79,7 @@ export default class MergeCellCommand extends Command {
execute() {
const model = this.editor.model;
const doc = model.document;
const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];
const cellToMerge = this.value;
const direction = this.direction;

Expand Down Expand Up @@ -118,7 +119,7 @@ export default class MergeCellCommand extends Command {
_getMergeableCell() {
const model = this.editor.model;
const doc = model.document;
const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];

if ( !tableCell ) {
return;
Expand Down
10 changes: 4 additions & 6 deletions src/commands/removecolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

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

/**
* The remove column command.
Expand All @@ -31,8 +32,7 @@ export default class RemoveColumnCommand extends Command {
const editor = this.editor;
const selection = editor.model.document.selection;
const tableUtils = editor.plugins.get( 'TableUtils' );

const tableCell = findAncestor( 'tableCell', selection.getFirstPosition() );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];

this.isEnabled = !!tableCell && tableUtils.getColumns( tableCell.parent.parent ) > 1;
}
Expand All @@ -44,9 +44,7 @@ export default class RemoveColumnCommand extends Command {
const model = this.editor.model;
const selection = model.document.selection;

const firstPosition = selection.getFirstPosition();

const tableCell = findAncestor( 'tableCell', firstPosition );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const table = tableRow.parent;

Expand Down
10 changes: 4 additions & 6 deletions src/commands/removerowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import Command from '@ckeditor/ckeditor5-core/src/command';

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

/**
* The remove row command.
Expand All @@ -30,8 +31,7 @@ export default class RemoveRowCommand extends Command {
refresh() {
const model = this.editor.model;
const doc = model.document;

const tableCell = findAncestor( 'tableCell', doc.selection.getFirstPosition() );
const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];

this.isEnabled = !!tableCell && tableCell.parent.parent.childCount > 1;
}
Expand All @@ -42,9 +42,7 @@ export default class RemoveRowCommand extends Command {
execute() {
const model = this.editor.model;
const selection = model.document.selection;

const firstPosition = selection.getFirstPosition();
const tableCell = findAncestor( 'tableCell', firstPosition );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const table = tableRow.parent;

Expand Down
11 changes: 4 additions & 7 deletions src/commands/setheadercolumncommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

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

import { findAncestor, updateNumericAttribute } from './utils';
import { updateNumericAttribute } from './utils';
import { getTableCellsContainingSelection } from '../utils';

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

const position = selection.getFirstPosition();
const tableCell = findAncestor( 'tableCell', position );
const tableCell = getTableCellsContainingSelection( doc.selection )[ 0 ];

const isInTable = !!tableCell;

Expand Down Expand Up @@ -71,8 +69,7 @@ export default class SetHeaderColumnCommand extends Command {
const selection = doc.selection;
const tableUtils = this.editor.plugins.get( 'TableUtils' );

const position = selection.getFirstPosition();
const tableCell = findAncestor( 'tableCell', position );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const table = tableRow.parent;

Expand Down
12 changes: 7 additions & 5 deletions src/commands/setheaderrowcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@

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

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

/**
Expand All @@ -35,8 +39,7 @@ export default class SetHeaderRowCommand extends Command {
const doc = model.document;
const selection = doc.selection;

const position = selection.getFirstPosition();
const tableCell = findAncestor( 'tableCell', position );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const isInTable = !!tableCell;

this.isEnabled = isInTable;
Expand Down Expand Up @@ -69,8 +72,7 @@ export default class SetHeaderRowCommand extends Command {
const doc = model.document;
const selection = doc.selection;

const position = selection.getFirstPosition();
const tableCell = findAncestor( 'tableCell', position );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];
const tableRow = tableCell.parent;
const table = tableRow.parent;

Expand Down
7 changes: 2 additions & 5 deletions src/tableediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import RemoveRowCommand from './commands/removerowcommand';
import RemoveColumnCommand from './commands/removecolumncommand';
import SetHeaderRowCommand from './commands/setheaderrowcommand';
import SetHeaderColumnCommand from './commands/setheadercolumncommand';
import { findAncestor } from './commands/utils';
import { getTableCellsContainingSelection } from './utils';
import TableUtils from '../src/tableutils';

import injectTableLayoutPostFixer from './converters/table-layout-post-fixer';
Expand Down Expand Up @@ -195,10 +195,7 @@ export default class TableEditing extends Plugin {

return ( domEventData, cancel ) => {
const selection = editor.model.document.selection;

const firstPosition = selection.getFirstPosition();

const tableCell = findAncestor( 'tableCell', firstPosition );
const tableCell = getTableCellsContainingSelection( selection )[ 0 ];

if ( !tableCell ) {
return;
Expand Down
7 changes: 5 additions & 2 deletions src/tableselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import TableWalker from './tablewalker';
import TableUtils from './tableutils';
import MouseEventsObserver from './tableselection/mouseeventsobserver';
import { getSelectedTableCells } from './utils';
import {
getSelectedTableCells,
getTableCellsContainingSelection
} from './utils';
import { findAncestor } from './commands/utils';
import cropTable from './tableselection/croptable';

Expand Down Expand Up @@ -161,7 +164,7 @@ export default class TableSelection extends Plugin {
return;
}

const anchorCell = findAncestor( 'tableCell', editor.model.document.selection.anchor.parent );
const anchorCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];

if ( !anchorCell ) {
return;
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
import Table from '../../src/table';
import TableCellProperties from '../../src/tablecellproperties';
import { findAncestor } from '../../src/commands/utils';
import { getTableCellsContainingSelection } from '../../src/utils';
import global from '@ckeditor/ckeditor5-utils/src/dom/global';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import View from '@ckeditor/ckeditor5-ui/src/view';
Expand Down Expand Up @@ -75,7 +76,7 @@ describe( 'UI Utils', () => {
'</tableRow></table>' );
repositionContextualBalloon( editor, 'cell' );

const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
const viewCell = editor.editing.mapper.toViewElement( modelCell );

sinon.assert.calledWithExactly( spy, {
Expand Down Expand Up @@ -160,7 +161,7 @@ describe( 'UI Utils', () => {
'</tableRow></table>' );

const data = getBalloonCellPositionData( editor );
const modelCell = findAncestor( 'tableCell', editor.model.document.selection.getFirstPosition() );
const modelCell = getTableCellsContainingSelection( editor.model.document.selection )[ 0 ];
const viewCell = editor.editing.mapper.toViewElement( modelCell );

expect( data ).to.deep.equal( {
Expand Down

0 comments on commit b231e4d

Please sign in to comment.