Skip to content

Commit

Permalink
More async parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Jul 26, 2023
1 parent 410cf61 commit f010718
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export function useClipboardHandler() {
}, [] )
.flat();
} else {
blocks = pasteHandler( {
blocks = await pasteHandler( {
HTML: html,
plainText,
mode: 'BLOCKS',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export function MediaPlaceholder( {
}

async function onHTMLDrop( HTML ) {
const blocks = pasteHandler( { HTML } );
const blocks = await pasteHandler( { HTML } );
return await handleBlocksDrop( blocks );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function RichTextWrapper(
);

const onPaste = useCallback(
( {
async ( {
value,
onChange,
html,
Expand Down Expand Up @@ -412,7 +412,7 @@ function RichTextWrapper(
// Only process file if no HTML is present.
// Note: a pasted file may have the URL as plain text.
if ( files && files.length && ! html ) {
const content = pasteHandler( {
const content = await pasteHandler( {
HTML: filePasteHandler( files ),
mode: 'BLOCKS',
tagName,
Expand Down Expand Up @@ -463,7 +463,7 @@ function RichTextWrapper(
mode = 'BLOCKS';
}

const content = pasteHandler( {
const content = await pasteHandler( {
HTML: html,
plainText,
mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export function useInputRules( props ) {
).filter( ( { type } ) => type === 'prefix' );
const transformation = findTransform(
prefixTransforms,
( { prefix } ) => {
return trimmedTextBefore === prefix;
}
( { prefix } ) => trimmedTextBefore === prefix
);

if ( ! transformation ) {
Expand All @@ -95,7 +93,7 @@ export function useInputRules( props ) {
return true;
}

function onInput( event ) {
async function onInput( event ) {
const { inputType, type } = event;
const {
getValue,
Expand All @@ -109,8 +107,8 @@ export function useInputRules( props ) {
return;
}

if ( __unstableAllowPrefixTransformations && inputRule ) {
if ( inputRule() ) return;
if ( __unstableAllowPrefixTransformations ) {
if ( await inputRule() ) return;
}

const value = getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export function usePasteHandler( props ) {
mode = 'BLOCKS';
}

const content = pasteHandler( {
const content = await pasteHandler( {
HTML: html,
plainText,
mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ export function onHTMLDrop(
targetBlockIndex,
insertOrReplaceBlocks
) {
return ( HTML ) => {
const blocks = pasteHandler( { HTML, mode: 'BLOCKS' } );
return async ( HTML ) => {
const blocks = await pasteHandler( { HTML, mode: 'BLOCKS' } );

if ( blocks.length ) {
insertOrReplaceBlocks( blocks );
Expand Down
2 changes: 1 addition & 1 deletion packages/blocks/src/api/raw-handling/get-raw-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { getBlockTransforms } from '../factory';

export async function getRawTransforms() {
const transforms = getBlockTransforms( 'from' );
const transforms = await getBlockTransforms( 'from' );
return transforms
.filter( ( { type } ) => type === 'raw' )
.map( ( transform ) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/raw-handling/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ export function deprecatedGetPhrasingContentSchema( context ) {
*
* @return {Array} A list of blocks.
*/
export function rawHandler( { HTML = '' } ) {
export async function rawHandler( { HTML = '' } ) {
// If we detect block delimiters, parse entirely as blocks.
if ( HTML.indexOf( '<!-- wp:' ) !== -1 ) {
return parse( HTML );
}

// An array of HTML strings and block objects. The blocks replace matched
// shortcodes.
const pieces = shortcodeConverter( HTML );
const pieces = await shortcodeConverter( HTML );
const blockContentSchema = getBlockContentSchema();

return pieces
Expand Down
6 changes: 3 additions & 3 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function filterInlineHTML( HTML, preserveWhiteSpace ) {
*
* @return {Array|string} A list of blocks or a string, depending on `handlerMode`.
*/
export function pasteHandler( {
export async function pasteHandler( {
HTML = '',
plainText = '',
mode = 'AUTO',
Expand All @@ -106,7 +106,7 @@ export function pasteHandler( {
const content = HTML ? HTML : plainText;

if ( content.indexOf( '<!-- wp:' ) !== -1 ) {
return parse( content );
return await parse( content );
}
}

Expand Down Expand Up @@ -156,7 +156,7 @@ export function pasteHandler( {

// An array of HTML strings and block objects. The blocks replace matched
// shortcodes.
const pieces = shortcodeConverter( HTML );
const pieces = await shortcodeConverter( HTML );

// The call to shortcodeConverter will always return more than one element
// if shortcodes are matched. The reason is when shortcodes are matched
Expand Down
14 changes: 7 additions & 7 deletions packages/blocks/src/api/raw-handling/shortcode-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import { applyBuiltInValidationFixes } from '../parser/apply-built-in-validation
const castArray = ( maybeArray ) =>
Array.isArray( maybeArray ) ? maybeArray : [ maybeArray ];

function segmentHTMLToShortcodeBlock(
async function segmentHTMLToShortcodeBlock(
HTML,
lastIndex = 0,
excludedBlockNames = []
) {
// Get all matches.
const transformsFrom = getBlockTransforms( 'from' );
const transformsFrom = await getBlockTransforms( 'from' );

const transformation = findTransform(
transformsFrom,
( transform ) =>
excludedBlockNames.indexOf( transform.blockName ) === -1 &&
transform.type === 'shortcode' &&
! excludedBlockNames.includes( transform.blockName ) &&
castArray( transform.tag ).some( ( tag ) =>
regexp( tag ).test( HTML )
)
Expand Down Expand Up @@ -60,7 +60,7 @@ function segmentHTMLToShortcodeBlock(
/^\s*(\n|<\/p>)/.test( afterHTML )
)
) {
return segmentHTMLToShortcodeBlock( HTML, lastIndex );
return await segmentHTMLToShortcodeBlock( HTML, lastIndex );
}

// If a transformation's `isMatch` predicate fails for the inbound
Expand All @@ -75,7 +75,7 @@ function segmentHTMLToShortcodeBlock(
transformation.isMatch &&
! transformation.isMatch( match.shortcode.attrs )
) {
return segmentHTMLToShortcodeBlock( HTML, previousIndex, [
return await segmentHTMLToShortcodeBlock( HTML, previousIndex, [
...excludedBlockNames,
transformation.blockName,
] );
Expand Down Expand Up @@ -143,9 +143,9 @@ function segmentHTMLToShortcodeBlock(
}

return [
...segmentHTMLToShortcodeBlock( beforeHTML ),
...( await segmentHTMLToShortcodeBlock( beforeHTML ) ),
...blocks,
...segmentHTMLToShortcodeBlock( afterHTML ),
...( await segmentHTMLToShortcodeBlock( afterHTML ) ),
];
}

Expand Down

0 comments on commit f010718

Please sign in to comment.