Skip to content

Commit c7b4ce3

Browse files
author
Amir Tocker
committed
Add Transformation component.
1 parent 8d6d761 commit c7b4ce3

File tree

6 files changed

+71
-10
lines changed

6 files changed

+71
-10
lines changed

src/components/CloudinaryComponent/CloudinaryComponent.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,28 @@ export default class CloudinaryComponent extends Component {
3939
return null;
4040
}
4141

42+
getChildTransformations(children) {
43+
return React.Children.map( children ,child =>
44+
CloudinaryComponent.getOptions(child.props, child.context)
45+
);
46+
}
47+
48+
getTransformation(){
49+
let options = CloudinaryComponent.getOptions(this.props, this.context);
50+
let childrenOptions = this.getChildTransformations(this.props.children);
51+
if(!Util.isEmpty(childrenOptions)){
52+
options.transformation = childrenOptions;
53+
}
54+
return options;
55+
}
4256
/**
4357
* Combine props and context to create an option Object that can be passed to Cloudinary methods.<br>
4458
* All names are converted to snake_case.
4559
* @param props
4660
* @param context
4761
* @returns {{}}
4862
*/
49-
getOptions(props, context) {
63+
static getOptions(props, context) {
5064
var options = {};
5165

5266
for(let key in context) {
@@ -65,8 +79,10 @@ export default class CloudinaryComponent extends Component {
6579
}
6680

6781
getUrl(props, context = {}) {
68-
var options = this.getOptions(props, context);
82+
var options = CloudinaryComponent.getOptions(props, context);
83+
options = this.getTransformation();
6984
let cl = Cloudinary.new(options);
85+
this.getChildTransformations(props.children);
7086
return cl.url(props.publicId, options);
7187
}
7288

src/components/Image/Image.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {Component, PropTypes} from 'react';
2-
import {Transformation} from 'cloudinary-core';
2+
import cloudinary from 'cloudinary-core';
33
import CloudinaryComponent from '../CloudinaryComponent';
44

55
export default class Image extends CloudinaryComponent {
@@ -36,8 +36,8 @@ export default class Image extends CloudinaryComponent {
3636
}
3737

3838
render() {
39-
var options = this.getOptions(this.props, this.context);
40-
var attributes = Transformation.new(options).toHtmlAttributes();
39+
var {children, ...options} = CloudinaryComponent.getOptions(this.props, this.context);
40+
var attributes = cloudinary.Transformation.new(options).toHtmlAttributes();
4141
return (
4242
<img {...attributes} src={this.state.url} />
4343
);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React, {Component} from 'react';
2+
import CloudinaryComponent from '../CloudinaryComponent';
3+
4+
export default class Transformation extends Component {
5+
constructor(props) {
6+
super(props);
7+
}
8+
9+
componentWillReceiveProps(nextProps) {
10+
}
11+
12+
componentWillMount() {
13+
}
14+
15+
componentDidMount() {
16+
}
17+
18+
componentWillUnmount() {
19+
}
20+
21+
componentWillUpdate(nextProps, nextState) {
22+
}
23+
24+
componentDidUpdate(prevProps, prevState) {
25+
}
26+
27+
shouldComponentUpdate(nextProps, nextState) {
28+
return true;
29+
}
30+
31+
render() {
32+
return null;
33+
}
34+
}
35+
Transformation.propTypes = CloudinaryComponent.propTypes;
36+
Transformation.defaultProps = {};
37+
Transformation.contextTypes = {};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "transformation",
3+
"version": "1.0.0",
4+
"main": "./Transformation.js",
5+
"private": true
6+
}

src/components/Video/Video.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default class Video extends CloudinaryComponent {
3939
let {publicId, poster, sourceTypes, fallback, sourceTransformation, ...options} = Object.assign({}, this.context, this.props);
4040
sourceTransformation = sourceTransformation || {};
4141
sourceTypes = sourceTypes || Cloudinary.DEFAULT_VIDEO_PARAMS.source_types;
42-
options = this.getOptions(options, {});
42+
options = CloudinaryComponent.getOptions(options, {});
4343
let cld = Cloudinary.new(options);
4444
let sources = [];
4545
let tagAttributes = Transformation.new(options).toHtmlAttributes();

stories/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { storiesOf, action, linkTo } from '@kadira/storybook';
33
import Image from '../src/components/Image';
44
import Video from '../src/components/Video';
5+
import Transformation from '../src/components/Transformation';
56
import CloudinaryContext from '../src/components/CloudinaryContext';
67
import cloudinary from 'cloudinary-core';
78

@@ -37,11 +38,12 @@ storiesOf('Image', module)
3738
}).addWithInfo('image with chained transformation', 'image with chained transformation', ()=> {
3839
let t = { width: 0.5, crop: "scale"};
3940
return(<div>
40-
<Image cloudName="demo" publicId="sample" width="100" crop="scale" angle="10">
41-
<Transformation width="100" crop="scale" angle="10"/>
42-
<Transformation width="100" crop="scale" angle="10"/>
41+
<Image cloudName="demo" publicId="sample" >
42+
<Transformation width="200" crop="scale" angle="10" />
43+
<Transformation width="100" crop="crop" />
44+
<Transformation width="100" crop="scale" angle="-10"/>
4345
</Image>
44-
<Image cloudName="demo" publicId="sample" width="100" crop="scale" angle="10" />
46+
<Image cloudName="demo" publicId="sample" width="100" crop="scale" />
4547
</div>
4648
)
4749
});

0 commit comments

Comments
 (0)