Skip to content

Commit

Permalink
Allow copying text in block (non input)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 16, 2018
1 parent 3e08d83 commit 186c6c5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions editor/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
isSelectionEnabled,
} from '../../store/selectors';
import { startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../store/actions';
import { isInputField } from '../../utils/dom';
import { documentHasSelection } from '../../utils/dom';

class BlockList extends Component {
constructor( props ) {
Expand Down Expand Up @@ -116,7 +116,7 @@ class BlockList extends Component {
}

// Let native copy behaviour take over in input fields.
if ( selectedBlock && isInputField( document.activeElement ) ) {
if ( selectedBlock && documentHasSelection() ) {
return;
}

Expand Down
17 changes: 17 additions & 0 deletions editor/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,20 @@ export function isInputField( { nodeName, contentEditable } ) {
contentEditable === 'true'
);
}

/**
* Check wether the current document has a selection.
* This checks both for focus in an input field and general text selection.
*
* @returns {Boolean} True if there is selection, false if not.
*/
export function documentHasSelection() {
if ( isInputField( document.activeElement ) ) {
return true;
}

const selection = window.getSelection();
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;

return range && ! range.collapsed;
}

0 comments on commit 186c6c5

Please sign in to comment.