Skip to content

Commit

Permalink
use async/await instead of promises
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jul 16, 2018
1 parent ffa5b67 commit a03aaf5
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 213 deletions.
15 changes: 7 additions & 8 deletions blocks/api/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,13 @@ export function createBlockWithFallback( blockNode ) {
* @return {Function} An implementation which parses the post content.
*/
export const createParse = ( parseImplementation ) =>
( content ) => parseImplementation( content )
.then( ( parsed ) => parsed.reduce( ( memo, blockNode ) => {
const block = createBlockWithFallback( blockNode );
if ( block ) {
memo.push( block );
}
return memo;
}, [] ) );
async ( content ) => ( await parseImplementation( content ) ).reduce( ( memo, blockNode ) => {
const block = createBlockWithFallback( blockNode );
if ( block ) {
memo.push( block );
}
return memo;
}, [] );

/**
* Parses the post content with a PegJS grammar and returns a list of blocks.
Expand Down
2 changes: 1 addition & 1 deletion blocks/api/post-parser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { parse as syncParse } from './post.pegjs';

export const parse = ( document ) => Promise.resolve( syncParse( document ) );
export const parse = ( postContent ) => Promise.resolve( syncParse( postContent ) );
12 changes: 6 additions & 6 deletions editor/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class BlockDropZone extends Component {
}
}

onHTMLDrop( HTML, position ) {
rawHandler( { HTML, mode: 'BLOCKS' } ).then( ( blocks ) => {
if ( blocks.length ) {
this.props.insertBlocks( blocks, this.getInsertIndex( position ) );
}
} );
async onHTMLDrop( HTML, position ) {
const blocks = await rawHandler( { HTML, mode: 'BLOCKS' } );

if ( blocks.length ) {
this.props.insertBlocks( blocks, this.getInsertIndex( position ) );
}
}

onDrop( event, position ) {
Expand Down
13 changes: 4 additions & 9 deletions editor/components/block-list/invalid-block-warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
*/
import { __ } from '@wordpress/i18n';
import { Button } from '@wordpress/components';
import {
getBlockType,
createBlock,
rawHandler,
} from '@wordpress/blocks';
import { createBlock, getBlockType, rawHandler } from '@wordpress/blocks';
import { withDispatch } from '@wordpress/data';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -44,11 +39,11 @@ export default withDispatch( ( dispatch, { block } ) => {
content: block.originalContent,
} ) );
},
convertToBlocks() {
rawHandler( {
async convertToBlocks() {
replaceBlock( block.uid, await rawHandler( {
HTML: block.originalContent,
mode: 'BLOCKS',
} ).then( ( content ) => replaceBlock( block.uid, content ) );
} ) );
},
};
} )( InvalidBlockWarning );
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ export default compose(
};
} ),
withDispatch( ( dispatch, { block, canUserUseUnfilteredHTML } ) => ( {
onClick: () => rawHandler( {
HTML: getBlockContent( block ),
mode: 'BLOCKS',
canUserUseUnfilteredHTML,
} ).then( ( content ) => dispatch( 'core/editor' ).replaceBlocks( block.uid, content ) ),
onClick: async () => dispatch( 'core/editor' ).replaceBlocks(
block.uid,
await rawHandler( {
HTML: getBlockContent( block ),
mode: 'BLOCKS',
canUserUseUnfilteredHTML,
} )
),
} ) ),
)( BlockConvertButton );
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ export default compose(
};
} ),
withDispatch( ( dispatch, { block, canUserUseUnfilteredHTML } ) => ( {
onClick: () => dispatch( 'core/editor' ).replaceBlocks(
onClick: async () => dispatch( 'core/editor' ).replaceBlocks(
block.uid,
rawHandler( {
( await rawHandler( {
HTML: serialize( block ),
mode: 'BLOCKS',
canUserUseUnfilteredHTML,
} )
} ) )
),
} ) ),
)( BlockConvertButton );
8 changes: 3 additions & 5 deletions editor/components/post-text-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ export default compose( [
onChange( content ) {
editPost( { content } );
},
onPersist( content ) {
parse( content ).then( ( blocks ) => {
resetBlocks( blocks );
checkTemplateValidity();
} );
async onPersist( content ) {
resetBlocks( await parse( content ) );
checkTemplateValidity();
},
};
} ),
Expand Down
64 changes: 32 additions & 32 deletions editor/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class RichText extends Component {
*
* @param {PasteEvent} event The paste event as triggered by TinyMCE.
*/
onPaste( event ) {
async onPaste( event ) {
const dataTransfer =
event.clipboardData ||
event.dataTransfer ||
Expand All @@ -275,27 +275,27 @@ export class RichText extends Component {
// Note: a pasted file may have the URL as plain text.
if ( item && ! HTML ) {
const file = item.getAsFile ? item.getAsFile() : item;
rawHandler( {
const content = await rawHandler( {
HTML: `<img src="${ createBlobURL( file ) }">`,
mode: 'BLOCKS',
tagName: this.props.tagName,
} ).then( ( content ) => {
const shouldReplace = this.props.onReplace && this.isEmpty();
} );

// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', file );

if ( shouldReplace ) {
// Necessary to allow the paste bin to be removed without errors.
this.props.setTimeout( () => this.props.onReplace( content ) );
} else if ( this.props.onSplit ) {
// Necessary to get the right range.
// Also done in the TinyMCE paste plugin.
this.props.setTimeout( () => this.splitContent( content ) );
}
const shouldReplace = this.props.onReplace && this.isEmpty();

event.preventDefault();
} );
// Allows us to ask for this information when we get a report.
window.console.log( 'Received item:\n\n', file );

if ( shouldReplace ) {
// Necessary to allow the paste bin to be removed without errors.
this.props.setTimeout( () => this.props.onReplace( content ) );
} else if ( this.props.onSplit ) {
// Necessary to get the right range.
// Also done in the TinyMCE paste plugin.
this.props.setTimeout( () => this.splitContent( content ) );
}

event.preventDefault();
}

this.pastedPlainText = plainText;
Expand All @@ -312,7 +312,7 @@ export class RichText extends Component {
* @param {PrePasteProcessEvent} event The PrePasteProcess event as triggered
* by TinyMCE.
*/
onPastePreProcess( event ) {
async onPastePreProcess( event ) {
const HTML = this.isPlainTextPaste ? '' : event.content;

event.preventDefault();
Expand Down Expand Up @@ -350,27 +350,27 @@ export class RichText extends Component {
mode = 'AUTO';
}

rawHandler( {
const content = await rawHandler( {
HTML,
plainText: this.pastedPlainText,
mode,
tagName: this.props.tagName,
canUserUseUnfilteredHTML: this.props.canUserUseUnfilteredHTML,
} ).then( ( content ) => {
if ( typeof content === 'string' ) {
this.editor.insertContent( content );
} else if ( this.props.onSplit ) {
if ( ! content.length ) {
return;
}
} );

if ( shouldReplace ) {
this.props.onReplace( content );
} else {
this.splitContent( content, { paste: true } );
}
if ( typeof content === 'string' ) {
this.editor.insertContent( content );
} else if ( this.props.onSplit ) {
if ( ! content.length ) {
return;
}
} );

if ( shouldReplace ) {
this.props.onReplace( content );
} else {
this.splitContent( content, { paste: true } );
}
}
}

/**
Expand Down
Loading

0 comments on commit a03aaf5

Please sign in to comment.