-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rich text: replace global event handlers with local ones (#34492)
- Loading branch information
Showing
5 changed files
with
112 additions
and
40 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
41 changes: 21 additions & 20 deletions
41
packages/block-editor/src/components/rich-text/input-event.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 |
---|---|---|
@@ -1,30 +1,31 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { useEffect, useContext, useRef } from '@wordpress/element'; | ||
|
||
export class __unstableRichTextInputEvent extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { inputEventContext } from './'; | ||
|
||
this.onInput = this.onInput.bind( this ); | ||
} | ||
export function __unstableRichTextInputEvent( { inputType, onInput } ) { | ||
const callbacks = useContext( inputEventContext ); | ||
const onInputRef = useRef(); | ||
onInputRef.current = onInput; | ||
|
||
onInput( event ) { | ||
if ( event.inputType === this.props.inputType ) { | ||
this.props.onInput(); | ||
useEffect( () => { | ||
function callback( event ) { | ||
if ( event.inputType === inputType ) { | ||
onInputRef.current(); | ||
event.preventDefault(); | ||
} | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
document.addEventListener( 'input', this.onInput, true ); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener( 'input', this.onInput, true ); | ||
} | ||
callbacks.current.add( callback ); | ||
return () => { | ||
callbacks.current.delete( callback ); | ||
}; | ||
}, [ inputType ] ); | ||
|
||
render() { | ||
return null; | ||
} | ||
return null; | ||
} |
33 changes: 24 additions & 9 deletions
33
packages/block-editor/src/components/rich-text/shortcut.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 |
---|---|---|
@@ -1,17 +1,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useKeyboardShortcut } from '@wordpress/compose'; | ||
import { rawShortcut } from '@wordpress/keycodes'; | ||
import { isKeyboardEvent } from '@wordpress/keycodes'; | ||
import { useEffect, useContext, useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { keyboardShortcutContext } from './'; | ||
|
||
export function RichTextShortcut( { character, type, onUse } ) { | ||
const callback = () => { | ||
onUse(); | ||
return false; | ||
}; | ||
useKeyboardShortcut( rawShortcut[ type ]( character ), callback, { | ||
bindGlobal: true, | ||
} ); | ||
const keyboardShortcuts = useContext( keyboardShortcutContext ); | ||
const onUseRef = useRef(); | ||
onUseRef.current = onUse; | ||
|
||
useEffect( () => { | ||
function callback( event ) { | ||
if ( isKeyboardEvent[ type ]( event, character ) ) { | ||
onUseRef.current(); | ||
event.preventDefault(); | ||
} | ||
} | ||
|
||
keyboardShortcuts.current.add( callback ); | ||
return () => { | ||
keyboardShortcuts.current.delete( callback ); | ||
}; | ||
}, [ character, type ] ); | ||
|
||
return null; | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/block-editor/src/components/rich-text/use-input-events.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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
|
||
export function useInputEvents( inputEvents ) { | ||
return useRefEffect( ( element ) => { | ||
function onInput( event ) { | ||
for ( const keyboardShortcut of inputEvents.current ) { | ||
keyboardShortcut( event ); | ||
} | ||
} | ||
|
||
element.addEventListener( 'input', onInput ); | ||
return () => { | ||
element.removeEventListener( 'input', onInput ); | ||
}; | ||
}, [] ); | ||
} |
19 changes: 19 additions & 0 deletions
19
packages/block-editor/src/components/rich-text/use-shortcuts.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,19 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
|
||
export function useShortcuts( keyboardShortcuts ) { | ||
return useRefEffect( ( element ) => { | ||
function onKeyDown( event ) { | ||
for ( const keyboardShortcut of keyboardShortcuts.current ) { | ||
keyboardShortcut( event ); | ||
} | ||
} | ||
|
||
element.addEventListener( 'keydown', onKeyDown ); | ||
return () => { | ||
element.removeEventListener( 'keydown', onKeyDown ); | ||
}; | ||
}, [] ); | ||
} |