Skip to content

Commit 0940478

Browse files
authored
Allow copying text in block (non input) (#4531)
1 parent 79e9552 commit 0940478

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

editor/components/block-list/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
isSelectionEnabled,
3737
} from '../../store/selectors';
3838
import { startMultiSelect, stopMultiSelect, multiSelect, selectBlock } from '../../store/actions';
39-
import { isInputField } from '../../utils/dom';
39+
import { documentHasSelection } from '../../utils/dom';
4040

4141
class BlockList extends Component {
4242
constructor( props ) {
@@ -116,7 +116,7 @@ class BlockList extends Component {
116116
}
117117

118118
// Let native copy behaviour take over in input fields.
119-
if ( selectedBlock && isInputField( document.activeElement ) ) {
119+
if ( selectedBlock && documentHasSelection() ) {
120120
return;
121121
}
122122

editor/utils/dom.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,20 @@ export function isInputField( { nodeName, contentEditable } ) {
324324
contentEditable === 'true'
325325
);
326326
}
327+
328+
/**
329+
* Check wether the current document has a selection.
330+
* This checks both for focus in an input field and general text selection.
331+
*
332+
* @returns {Boolean} True if there is selection, false if not.
333+
*/
334+
export function documentHasSelection() {
335+
if ( isInputField( document.activeElement ) ) {
336+
return true;
337+
}
338+
339+
const selection = window.getSelection();
340+
const range = selection.rangeCount ? selection.getRangeAt( 0 ) : null;
341+
342+
return range && ! range.collapsed;
343+
}

0 commit comments

Comments
 (0)