Skip to content

Commit

Permalink
Framework: Extract isEditableElement utility
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Jun 22, 2017
1 parent de8de1b commit 5c24018
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 2 additions & 3 deletions editor/modes/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import VisualEditorBlockList from './block-list';
import PostTitle from '../../post-title';
import { getBlockUids } from '../../selectors';
import { clearSelectedBlock, multiSelect } from '../../actions';
import { isEditableElement } from '../../utils/dom';

class VisualEditor extends Component {
constructor() {
Expand Down Expand Up @@ -54,10 +55,8 @@ class VisualEditor extends Component {

onKeyDown( event ) {
const { uids } = this.props;
const isEditable = [ 'textarea', 'input', 'select' ].indexOf( document.activeElement.tagName.toLowerCase() ) !== -1
|| document.activeElement.isContentEditable;
if (
! isEditable &&
! isEditableElement( document.activeElement ) &&
( event.ctrlKey || event.metaKey ) &&
event.keyCode === CHAR_A
) {
Expand Down
13 changes: 13 additions & 0 deletions editor/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/**
* Utility function to check whether the domElement provided is editable or not
* An editable element means we can type in it to edit its content
* This includes inputs and contenteditables
*
* @param {DomElement} domElement DOM Element
* @return {Boolean} Whether the DOM Element is editable or not
*/
export function isEditableElement( domElement ) {
return [ 'textarea', 'input', 'select' ].indexOf( domElement.tagName.toLowerCase() ) !== -1
|| !! domElement.isContentEditable;
}
35 changes: 35 additions & 0 deletions editor/utils/test/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import { isEditableElement } from '../dom';

describe.only( 'isEditableElement', () => {
it( 'should return false for non editable nodes', () => {
const div = document.createElement( 'div' );

expect( isEditableElement( div ) ).to.be.false();
} );

it( 'should return true for inputs', () => {
const input = document.createElement( 'input' );

expect( isEditableElement( input ) ).to.be.true();
} );

it( 'should return true for textareas', () => {
const textarea = document.createElement( 'textarea' );

expect( isEditableElement( textarea ) ).to.be.true();
} );

it( 'should return true for selects', () => {
const select = document.createElement( 'select' );

expect( isEditableElement( select ) ).to.be.true();
} );
} );

0 comments on commit 5c24018

Please sign in to comment.