-
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.
POC: DON'T MERGE THIS. Allow tab keypress formatting in rich text fie…
…lds in editor.
- Loading branch information
Showing
3 changed files
with
44 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
import { insert } from '@wordpress/rich-text'; | ||
import { useRefEffect } from '@wordpress/compose'; | ||
import { TAB } from '@wordpress/keycodes'; | ||
|
||
export function useTab( props ) { | ||
const propsRef = useRef( props ); | ||
propsRef.current = props; | ||
return useRefEffect( ( element ) => { | ||
function onKeyDown( event ) { | ||
const { keyCode } = event; | ||
|
||
if ( event.defaultPrevented ) { | ||
return; | ||
} | ||
|
||
const { value, onChange } = propsRef.current; | ||
const _value = { ...value }; | ||
|
||
if ( keyCode === TAB ) { | ||
event.preventDefault(); | ||
|
||
const { start, end } = value; | ||
|
||
onChange( insert( _value, '\t', start, end ) ); | ||
} | ||
} | ||
|
||
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