Skip to content

Commit

Permalink
Move embed previews into core-data
Browse files Browse the repository at this point in the history
  • Loading branch information
notnownikki committed May 30, 2018
1 parent 9939995 commit 067f54d
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 228 deletions.
2 changes: 1 addition & 1 deletion core-blocks/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function getEmbedBlockSettings( { title, description, icon, category = 'embed',
const { url } = ownProps.attributes;
// Preview is undefined if we don't know the status of it, false if it failed,
// otherwise it will be an object containing the embed response.
const preview = url ? select( 'core/block-data' ).getPreview( url ) : undefined;
const preview = url ? select( 'core' ).getPreview( url ) : undefined;
return {
preview,
};
Expand Down
1 change: 0 additions & 1 deletion core-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import './style.scss';
import './store';
import * as paragraph from './paragraph';
import * as image from './image';
import * as heading from './heading';
Expand Down
16 changes: 0 additions & 16 deletions core-blocks/store/actions.js

This file was deleted.

21 changes: 0 additions & 21 deletions core-blocks/store/index.js

This file was deleted.

20 changes: 0 additions & 20 deletions core-blocks/store/reducer.js

This file was deleted.

32 changes: 0 additions & 32 deletions core-blocks/store/resolvers.js

This file was deleted.

24 changes: 0 additions & 24 deletions core-blocks/store/selectors.js

This file was deleted.

30 changes: 0 additions & 30 deletions core-blocks/store/test/reducer.js

This file was deleted.

45 changes: 0 additions & 45 deletions core-blocks/store/test/resolvers.js

This file was deleted.

34 changes: 0 additions & 34 deletions core-blocks/store/test/selectors.js

This file was deleted.

17 changes: 17 additions & 0 deletions core-data/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,20 @@ export function receiveThemeSupportsFromIndex( index ) {
themeSupports: index.theme_supports,
};
}

/**
* 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,
};
}
21 changes: 21 additions & 0 deletions core-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ function entity( entityConfig ) {
};
}

/**
* Reducer managing embed preview data.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function embedPreviews( state = {}, action ) {
switch ( action.type ) {
case 'RECEIVE_EMBED_PREVIEW':
const { url, preview } = action;
return {
...state,
[ url ]: preview,
};
}
return state;
}

const entitiesByKind = groupBy( entitiesConfig, 'kind' );
export const entities = combineReducers( Object.entries( entitiesByKind ).reduce( ( memo, [ kind, subEntities ] ) => {
const kindReducer = combineReducers( subEntities.reduce(
Expand All @@ -155,4 +175,5 @@ export default combineReducers( {
taxonomies,
themeSupports,
entities,
embedPreviews,
} );
22 changes: 22 additions & 0 deletions core-data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import apiRequest from '@wordpress/api-request';

/**
* External dependencies
*/
import { stringify } from 'querystring';

/**
* Internal dependencies
*/
Expand All @@ -11,6 +16,7 @@ import {
receiveUserQuery,
receiveEntityRecords,
receiveThemeSupportsFromIndex,
receivePreview,
} from './actions';
import { getEntity } from './entities';

Expand Down Expand Up @@ -65,3 +71,19 @@ export async function* getThemeSupports() {
const index = await apiRequest( { path: '/' } );
yield receiveThemeSupportsFromIndex( index );
}

/**
* 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 );
}
}
25 changes: 25 additions & 0 deletions core-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,28 @@ export const getEntityRecords = createSelector(
export function getThemeSupports( state ) {
return state.themeSupports;
}

/**
* Returns the embed preview 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;
}

const oEmbedLinkCheck = '<a href="' + url + '">' + url + '</a>';

if ( oEmbedLinkCheck === preview.html ) {
// just a link to the url, it's oEmbed being helpful and creating a link for us, not actually embedding content
return false;
}

return preview;
}
File renamed without changes.
Loading

0 comments on commit 067f54d

Please sign in to comment.