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

Ensure that a selection exists before attempting to execCommand #2143

Merged
27 changes: 22 additions & 5 deletions blocks/library/table/table-block.js
Expand Up @@ -3,9 +3,29 @@ import BlockControls from '../../block-controls';
import { Toolbar, DropdownMenu } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function isTableSelected( editor ) {
return editor.dom.getParent(
editor.selection.getStart( true ),
'table',
editor.getBody().parentNode
);
}

function selectFirstCell( editor ) {
const cell = editor.getBody().querySelector( 'td,th' );
if ( cell ) {
cell.focus();
editor.selection.select( cell, true );
editor.selection.collapse( false );
}
}

function execCommand( command ) {
return ( editor ) => {
if ( editor ) {
if ( ! isTableSelected( editor ) ) {
selectFirstCell( editor );
}
editor.execCommand( command );
}
};
Expand Down Expand Up @@ -56,11 +76,8 @@ export default class TableBlock extends wp.element.Component {
handleSetup( editor, focus ) {
// select the end of the first table cell
editor.on( 'init', () => {
const cell = editor.getBody().querySelector( 'td,th' );
if ( cell && focus ) {
cell.focus();
editor.selection.select( cell, true );
editor.selection.collapse( false );
if ( focus ) {
selectFirstCell( editor );
}
} );
this.setState( { editor } );
Expand Down
7 changes: 3 additions & 4 deletions post-content.js
Expand Up @@ -160,10 +160,9 @@ return <Button>Click Me!</Button>;\n\
'<hr class="wp-block-separator" />',
'<!-- /wp:core/separator -->',

// Remove until #1795 is fixed.
// '<!-- wp:core/table -->',
// '<table class="widefat wp-block-table"><thead><tr><th>Version</th><th>Musician</th><th>Date</th></tr></thead><tbody><tr><th><a href="https://wordpress.org/news/2015/12/clifford/">4.4</a></th><td>Clifford Brown</td><td>December 8, 2015</td></tr><tr class="alt"><th><a href="https://wordpress.org/news/2016/04/coleman/">4.5</a></th><td>Coleman Hawkins</td><td>April 12, 2016</td></tr><tr><th><a href="https://wordpress.org/news/2016/08/pepper/">4.6</a></th><td>Pepper Adams</td><td>August 16, 2016</td></tr><tr class="alt"><th><a href="https://wordpress.org/news/2016/12/vaughan/">4.7</a></th><td>Sarah Vaughan</td><td>December 6, 2016</td></tr></tbody></table>',
// '<!-- /wp:core/table -->',
'<!-- wp:core/table -->',
'<table class="wp-block-table"><thead><tr><th>Version</th><th>Musician</th><th>Date</th></tr></thead><tbody><tr><th><a href="https://wordpress.org/news/2015/12/clifford/">4.4</a></th><td>Clifford Brown</td><td>December 8, 2015</td></tr><tr class="alt"><th><a href="https://wordpress.org/news/2016/04/coleman/">4.5</a></th><td>Coleman Hawkins</td><td>April 12, 2016</td></tr><tr><th><a href="https://wordpress.org/news/2016/08/pepper/">4.6</a></th><td>Pepper Adams</td><td>August 16, 2016</td></tr><tr class="alt"><th><a href="https://wordpress.org/news/2016/12/vaughan/">4.7</a></th><td>Sarah Vaughan</td><td>December 6, 2016</td></tr></tbody></table>',
'<!-- /wp:core/table -->',

'<!-- wp:core/heading -->',
'<h2>All that you can embed!</h2>',
Expand Down