-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block List: Extract copy, scroll-into-view logic to separate non-visu…
…al components (#5021) * Block List: Extract copy logic to separate non-visual component * Block List: Restore multi-select scroll into view behavior * Editor: Abort scrollIntoView if no scrollable contaienr
- Loading branch information
Showing
8 changed files
with
183 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { serialize } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { documentHasSelection } from '../../utils/dom'; | ||
import { removeBlocks } from '../../store/actions'; | ||
import { | ||
getMultiSelectedBlocks, | ||
getMultiSelectedBlockUids, | ||
getSelectedBlock, | ||
} from '../../store/selectors'; | ||
|
||
class CopyHandler extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.onCopy = this.onCopy.bind( this ); | ||
this.onCut = this.onCut.bind( this ); | ||
} | ||
|
||
componentDidMount() { | ||
document.addEventListener( 'copy', this.onCopy ); | ||
document.addEventListener( 'cut', this.onCut ); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener( 'copy', this.onCopy ); | ||
document.removeEventListener( 'cut', this.onCut ); | ||
} | ||
|
||
onCopy( event ) { | ||
const { multiSelectedBlocks, selectedBlock } = this.props; | ||
|
||
if ( ! multiSelectedBlocks.length && ! selectedBlock ) { | ||
return; | ||
} | ||
|
||
// Let native copy behaviour take over in input fields. | ||
if ( selectedBlock && documentHasSelection() ) { | ||
return; | ||
} | ||
|
||
const serialized = serialize( selectedBlock || multiSelectedBlocks ); | ||
|
||
event.clipboardData.setData( 'text/plain', serialized ); | ||
event.clipboardData.setData( 'text/html', serialized ); | ||
|
||
event.preventDefault(); | ||
} | ||
|
||
onCut( event ) { | ||
const { multiSelectedBlockUids } = this.props; | ||
|
||
this.onCopy( event ); | ||
|
||
if ( multiSelectedBlockUids.length ) { | ||
this.props.onRemove( multiSelectedBlockUids ); | ||
} | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => { | ||
return { | ||
multiSelectedBlocks: getMultiSelectedBlocks( state ), | ||
multiSelectedBlockUids: getMultiSelectedBlockUids( state ), | ||
selectedBlock: getSelectedBlock( state ), | ||
}; | ||
}, | ||
{ onRemove: removeBlocks }, | ||
)( CopyHandler ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import scrollIntoView from 'dom-scroll-into-view'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { query } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getScrollContainer } from '../../utils/dom'; | ||
|
||
class MultiSelectScrollIntoView extends Component { | ||
componentDidUpdate() { | ||
// Relies on expectation that `componentDidUpdate` will only be called | ||
// if value of `extentUID` changes. | ||
this.scrollIntoView(); | ||
} | ||
|
||
/** | ||
* Ensures that if a multi-selection exists, the extent of the selection is | ||
* visible within the nearest scrollable container. | ||
* | ||
* @return {void} | ||
*/ | ||
scrollIntoView() { | ||
const { extentUID } = this.props; | ||
if ( ! extentUID ) { | ||
return; | ||
} | ||
|
||
const extentNode = document.querySelector( '[data-block="' + extentUID + '"]' ); | ||
if ( ! extentNode ) { | ||
return; | ||
} | ||
|
||
const scrollContainer = getScrollContainer( extentNode ); | ||
|
||
// If there's no scroll container, it follows that there's no scrollbar | ||
// and thus there's no need to try to scroll into view. | ||
if ( ! scrollContainer ) { | ||
return; | ||
} | ||
|
||
scrollIntoView( extentNode, scrollContainer, { | ||
onlyScrollIfNeeded: true, | ||
} ); | ||
} | ||
|
||
render() { | ||
return null; | ||
} | ||
} | ||
|
||
export default query( ( select ) => { | ||
return { | ||
extentUID: select( 'core/editor' ).getLastMultiSelectedBlockUid(), | ||
}; | ||
} )( MultiSelectScrollIntoView ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.