From 186c6c54ebb1c416c6969963d1101e3a9f050673 Mon Sep 17 00:00:00 2001 From: iseulde Date: Tue, 16 Jan 2018 23:13:03 +0100 Subject: [PATCH] Allow copying text in block (non input) --- editor/components/block-list/index.js | 4 ++-- editor/utils/dom.js | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) 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; +}