-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove the API request from the embed block, use withSelect instead
- Loading branch information
1 parent
a528611
commit 9e1e74d
Showing
11 changed files
with
306 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Returns an action object used in signalling that the preview data for | ||
* a given URl has been received. | ||
* | ||
* @param {string} url URL to preview the embed for. | ||
* @param {Mixed} preview Preview data. | ||
* | ||
* @return {Object} Action object. | ||
*/ | ||
export function receivePreview( url, preview ) { | ||
return { | ||
type: 'RECEIVE_EMBED_PREVIEW', | ||
url, | ||
preview, | ||
}; | ||
} |
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,21 @@ | ||
/** | ||
* WordPress Dependencies | ||
*/ | ||
import { registerStore } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import reducer from './reducer'; | ||
import * as selectors from './selectors'; | ||
import * as actions from './actions'; | ||
import * as resolvers from './resolvers'; | ||
|
||
const store = registerStore( 'core/blocks', { | ||
reducer, | ||
actions, | ||
selectors, | ||
resolvers, | ||
} ); | ||
|
||
export default store; |
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,20 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { combineReducers } from '@wordpress/data'; | ||
|
||
export function embedPreviews( state = {}, action ) { | ||
switch ( action.type ) { | ||
case 'RECEIVE_EMBED_PREVIEW': | ||
const { url, preview } = action; | ||
return { | ||
...state, | ||
[ url ]: preview, | ||
}; | ||
} | ||
return state; | ||
} | ||
|
||
export default combineReducers( { | ||
embedPreviews, | ||
} ); |
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,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import apiRequest from '@wordpress/api-request'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { stringify } from 'querystring'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
receivePreview, | ||
} from './actions'; | ||
|
||
/** | ||
* Requests a preview from the from the Embed API. | ||
* | ||
* @param {Object} state State tree | ||
* @param {string} url URL to get the preview for. | ||
*/ | ||
export async function* getPreview( state, url ) { | ||
try { | ||
const embedProxyResponse = await apiRequest( { path: `/oembed/1.0/proxy?${ stringify( { url } ) }` } ); | ||
yield receivePreview( url, embedProxyResponse ); | ||
} catch ( error ) { | ||
// Embed API 404s if the URL cannot be embedded, so we have to catch the error from the apiRequest here. | ||
yield receivePreview( url, false ); | ||
} | ||
} |
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,26 @@ | ||
/** | ||
* Returns the embed for the given URL. | ||
* | ||
* @param {Object} state Data state. | ||
* @param {string} url Embedded URL. | ||
* | ||
* @return {Mixed} Undefined if the preview has not been fetched, false if the URL cannot be embedded, array of embed preview data if the preview has been fetched. | ||
*/ | ||
export function getPreview( state, url ) { | ||
const preview = state.embedPreviews[ url ]; | ||
|
||
if ( ! preview ) { | ||
return preview; | ||
} | ||
|
||
// this won't execute any scripts, so it's safe to check if the returned preview is just a single link | ||
const linkCheck = document.createElement( 'div' ); | ||
linkCheck.innerHTML = preview.html; | ||
|
||
if ( 1 === linkCheck.children.length && 'A' === linkCheck.children[ 0 ].nodeName && 0 === linkCheck.children[ 0 ].children.length ) { | ||
// single link, no children, it's oEmbed being helpful and creating a link for us, not actually embedding content | ||
return false; | ||
} | ||
|
||
return preview; | ||
} |
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,30 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import deepFreeze from 'deep-freeze'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { embedPreviews } from '../reducer'; | ||
|
||
describe( 'embedPreviews()', () => { | ||
it( 'returns an empty object by default', () => { | ||
const state = embedPreviews( undefined, {} ); | ||
|
||
expect( state ).toEqual( {} ); | ||
} ); | ||
|
||
it( 'returns with received preview', () => { | ||
const originalState = deepFreeze( {} ); | ||
const state = embedPreviews( originalState, { | ||
type: 'RECEIVE_EMBED_PREVIEW', | ||
url: 'http://twitter.com/notnownikki', | ||
preview: { data: 42 }, | ||
} ); | ||
|
||
expect( state ).toEqual( { | ||
'http://twitter.com/notnownikki': { data: 42 }, | ||
} ); | ||
} ); | ||
} ); |
Oops, something went wrong.