Merge with use-publicFolder
This commit is contained in:
commit
2af586867f
@ -3,6 +3,7 @@ import ImmutablePropTypes from 'react-immutable-proptypes';
|
|||||||
import Waypoint from 'react-waypoint';
|
import Waypoint from 'react-waypoint';
|
||||||
import { Map } from 'immutable';
|
import { Map } from 'immutable';
|
||||||
import history from '../../routing/history';
|
import history from '../../routing/history';
|
||||||
|
import { resolvePath } from '../../lib/pathHelper';
|
||||||
import { selectFields, selectInferedField } from '../../reducers/collections';
|
import { selectFields, selectInferedField } from '../../reducers/collections';
|
||||||
import { Card } from '../UI';
|
import { Card } from '../UI';
|
||||||
import styles from './EntryListing.css';
|
import styles from './EntryListing.css';
|
||||||
@ -39,9 +40,7 @@ export default class EntryListing extends React.Component {
|
|||||||
const label = entry.get('label');
|
const label = entry.get('label');
|
||||||
const title = label || entry.getIn(['data', inferedFields.titleField]);
|
const title = label || entry.getIn(['data', inferedFields.titleField]);
|
||||||
let image = entry.getIn(['data', inferedFields.imageField]);
|
let image = entry.getIn(['data', inferedFields.imageField]);
|
||||||
if (image && image.indexOf('/') === -1) {
|
image = resolvePath(image, publicFolder);
|
||||||
image = `/${ publicFolder }/${ image }`;
|
|
||||||
}
|
|
||||||
return (
|
return (
|
||||||
<Card
|
<Card
|
||||||
key={entry.get('slug')}
|
key={entry.get('slug')}
|
||||||
|
18
src/lib/pathHelper.js
Normal file
18
src/lib/pathHelper.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const absolutePath = new RegExp('^(?:[a-z]+:)?//', 'i');
|
||||||
|
const normalizePath = path => path.replace(/[\\\/]+/g, '/');
|
||||||
|
|
||||||
|
export function resolvePath(path, basePath) { // eslint-disable-line
|
||||||
|
// No path provided, skip
|
||||||
|
if (!path) return null;
|
||||||
|
|
||||||
|
// It's an absolute path.
|
||||||
|
if (absolutePath.test(path)) return normalizePath(path);
|
||||||
|
|
||||||
|
if (path.indexOf('/') === -1) {
|
||||||
|
// It's a single file name, no directories. Prepend public folder
|
||||||
|
return normalizePath(`/${ basePath }/${ path }`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// It's a relative path. Prepend a forward slash.
|
||||||
|
return normalizePath(`/${ path }`);
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import { Map } from 'immutable';
|
import { Map } from 'immutable';
|
||||||
|
import { resolvePath } from '../lib/pathHelper';
|
||||||
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
|
import { ADD_MEDIA, REMOVE_MEDIA } from '../actions/media';
|
||||||
import MediaProxy from '../valueObjects/MediaProxy';
|
import MediaProxy from '../valueObjects/MediaProxy';
|
||||||
|
|
||||||
@ -17,13 +18,14 @@ const medias = (state = Map(), action) => {
|
|||||||
export default medias;
|
export default medias;
|
||||||
|
|
||||||
export const getMedia = (publicFolder, state, path) => {
|
export const getMedia = (publicFolder, state, path) => {
|
||||||
|
// No path provided, skip
|
||||||
|
if (!path) return null;
|
||||||
|
|
||||||
if (state.has(path)) {
|
if (state.has(path)) {
|
||||||
|
// There is already a MediaProxy in memmory for this path. Use it.
|
||||||
return state.get(path);
|
return state.get(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
let localPath = path;
|
// Create a new MediaProxy (for consistency) and return it.
|
||||||
if (path && path.indexOf('/') === -1) {
|
return new MediaProxy(resolvePath(path, publicFolder), null, true);
|
||||||
localPath = `/${ publicFolder }/${ localPath }`;
|
|
||||||
}
|
|
||||||
return new MediaProxy(localPath, null, true);
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user