forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multi-select on iOS Safari/touch devices (WordPress#63671)
- Loading branch information
Showing
15 changed files
with
237 additions
and
36 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
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
72 changes: 72 additions & 0 deletions
72
packages/block-editor/src/components/writing-flow/use-event-redirect.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,72 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRefEffect } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getSelectionRoot } from './utils'; | ||
|
||
/** | ||
* Whenever content editable is enabled on writing flow, it will have focus, so | ||
* we need to dispatch some events to the root of the selection to ensure | ||
* compatibility with rich text. In the future, perhaps the rich text event | ||
* handlers should be attached to the window instead. | ||
* | ||
* Alternatively, we could try to find a way to always maintain rich text focus. | ||
*/ | ||
export default function useEventRedirect() { | ||
return useRefEffect( ( node ) => { | ||
function onInput( event ) { | ||
if ( event.target !== node ) { | ||
return; | ||
} | ||
|
||
const { ownerDocument } = node; | ||
const { defaultView } = ownerDocument; | ||
const prototype = Object.getPrototypeOf( event ); | ||
const constructorName = prototype.constructor.name; | ||
const Constructor = defaultView[ constructorName ]; | ||
const root = getSelectionRoot( ownerDocument ); | ||
|
||
if ( ! root || root === node ) { | ||
return; | ||
} | ||
|
||
const init = {}; | ||
|
||
for ( const key in event ) { | ||
init[ key ] = event[ key ]; | ||
} | ||
|
||
init.bubbles = false; | ||
|
||
const newEvent = new Constructor( event.type, init ); | ||
const cancelled = ! root.dispatchEvent( newEvent ); | ||
|
||
if ( cancelled ) { | ||
event.preventDefault(); | ||
} | ||
} | ||
|
||
const events = [ | ||
'beforeinput', | ||
'input', | ||
'compositionstart', | ||
'compositionend', | ||
'compositionupdate', | ||
'keydown', | ||
]; | ||
|
||
events.forEach( ( eventType ) => { | ||
node.addEventListener( eventType, onInput ); | ||
} ); | ||
|
||
return () => { | ||
events.forEach( ( eventType ) => { | ||
node.removeEventListener( eventType, onInput ); | ||
} ); | ||
}; | ||
}, [] ); | ||
} |
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
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.