-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rich text: move Autocomplete and Enter key handler to ref callback (#…
- Loading branch information
Showing
5 changed files
with
161 additions
and
88 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
105 changes: 105 additions & 0 deletions
105
packages/block-editor/src/components/rich-text/use-enter.js
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,105 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
import { useRefEffect } from '@wordpress/compose'; | ||
import { ENTER } from '@wordpress/keycodes'; | ||
import { | ||
insert, | ||
__unstableIsEmptyLine as isEmptyLine, | ||
__unstableInsertLineSeparator as insertLineSeparator, | ||
} from '@wordpress/rich-text'; | ||
import { getBlockTransforms, findTransform } from '@wordpress/blocks'; | ||
import { useDispatch } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
export function useEnter( props ) { | ||
const { __unstableMarkAutomaticChange } = useDispatch( blockEditorStore ); | ||
const propsRef = useRef( props ); | ||
propsRef.current = props; | ||
return useRefEffect( ( element ) => { | ||
function onKeyDown( event ) { | ||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
|
||
const { | ||
removeEditorOnlyFormats, | ||
value, | ||
onReplace, | ||
onSplit, | ||
multiline, | ||
onChange, | ||
disableLineBreaks, | ||
splitValue, | ||
onSplitAtEnd, | ||
} = propsRef.current; | ||
|
||
if ( event.keyCode !== ENTER ) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
|
||
const _value = { ...value }; | ||
_value.formats = removeEditorOnlyFormats( value ); | ||
const canSplit = onReplace && onSplit; | ||
|
||
if ( onReplace ) { | ||
const transforms = getBlockTransforms( 'from' ).filter( | ||
( { type } ) => type === 'enter' | ||
); | ||
const transformation = findTransform( transforms, ( item ) => { | ||
return item.regExp.test( _value.text ); | ||
} ); | ||
|
||
if ( transformation ) { | ||
onReplace( [ | ||
transformation.transform( { | ||
content: _value.text, | ||
} ), | ||
] ); | ||
__unstableMarkAutomaticChange(); | ||
} | ||
} | ||
|
||
if ( multiline ) { | ||
if ( event.shiftKey ) { | ||
if ( ! disableLineBreaks ) { | ||
onChange( insert( _value, '\n' ) ); | ||
} | ||
} else if ( canSplit && isEmptyLine( _value ) ) { | ||
splitValue( _value ); | ||
} else { | ||
onChange( insertLineSeparator( _value ) ); | ||
} | ||
} else { | ||
const { text, start, end } = _value; | ||
const canSplitAtEnd = | ||
onSplitAtEnd && start === end && end === text.length; | ||
|
||
if ( event.shiftKey || ( ! canSplit && ! canSplitAtEnd ) ) { | ||
if ( ! disableLineBreaks ) { | ||
onChange( insert( _value, '\n' ) ); | ||
} | ||
} else if ( ! canSplit && canSplitAtEnd ) { | ||
onSplitAtEnd(); | ||
} else if ( canSplit ) { | ||
splitValue( _value ); | ||
} | ||
} | ||
} | ||
|
||
element.addEventListener( 'keydown', onKeyDown ); | ||
return () => { | ||
element.removeEventListener( 'keydown', onKeyDown ); | ||
}; | ||
}, [] ); | ||
} |
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