diff --git a/editor/components/block-list/index.js b/editor/components/block-list/index.js index 19f7b99bffe713..7ceb4d2db9954f 100644 --- a/editor/components/block-list/index.js +++ b/editor/components/block-list/index.js @@ -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 ) { @@ -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; } diff --git a/editor/utils/dom.js b/editor/utils/dom.js index 4da3da41e4092d..04ed1ed96f7346 100644 --- a/editor/utils/dom.js +++ b/editor/utils/dom.js @@ -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; +}