Skip to content

Commit 2c58a92

Browse files
committed
updated readme
added and exposed createStyleRenderer added react and flat render styles tests with renderToStaticMarkup
1 parent fe56e6c commit 2c58a92

File tree

7 files changed

+2774
-27
lines changed

7 files changed

+2774
-27
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"presets": ["latest"],
2+
"presets": ["latest", "react"],
33
"plugins": ["array-includes"]
44
}

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,38 @@ export default class Renderer extends Component {
133133
redraft(Object:raw, Object:renderers, Object:options)
134134
```
135135
Returns an array of rendered blocks.
136-
- raw - result of the Draft.js convertToRaw
137-
- renderers - object with 3 groups of renders inline, blocks and entities refer to example for more info
138-
- options - optional settings
136+
- **raw** - result of the Draft.js convertToRaw
137+
- **renderers** - object with 3 groups of renders inline (or style), blocks and entities refer to example for more info
138+
- **options** - optional settings
139139
140+
#### Using style renderer instead of inline
141+
If provided with a style renderer in the renders, redraft will use it instead of the inline one. This allows a flatter render more like draft.js does in the editor. Redraft also exposes a helper to create the style renderer.
140142
```js
141-
RawParser.parse(block)
142-
```
143-
Parses the provided block and returns an ContentNode object
143+
import React from 'react';
144+
import redraft, { createStylesRenderer } from 'redraft';
144145

145-
```js
146-
renderNode(Object:node, Object:inlineRendrers, Object:entityRenderers, Object:entityMap, Object:options)
146+
const styleMap = {
147+
BOLD: {
148+
fontWeight: 'bold',
149+
},
150+
ITALIC: {
151+
fontStyle: 'italic',
152+
},
153+
UNDERLINE: {
154+
textDecoration: 'underline',
155+
},
156+
};
157+
158+
// This is a wrapper callback for the inline styles
159+
// the style object contains all the relevant styles from the styleMap
160+
// it needs a key as redraft returns arrays not Components
161+
const InlineWrapper = ({ children, style, key }) => <span key={key} style={style}>{children}</span>
162+
163+
const renderers = {
164+
style: createStylesRenderer(InlineWrapper, styleMap),
165+
...
166+
};
147167
```
148-
Returns an rendered single block.
149-
- node - ContentNode from `RawParser.parse(block)` method
150-
- inlineRendrers, entityRenderers - callbacks
151-
- entityMap - the entityMap from raw state `raw.entityMap`
152168
153169
### Options
154170
- `cleanup` - cleans up blocks with no text or data (metadata or entities), by default cleanup only removes empty `unstyled` blocks inserted directly after `atomic`. Accepts false or an object containing cleanup settings:

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redraft",
3-
"version": "0.7.0",
3+
"version": "0.8.0-beta.1",
44
"description": "Renders the result of Draft.js convertToRaw using provided callbacks, works well with React",
55
"main": "./lib/index.js",
66
"scripts": {
@@ -37,6 +37,7 @@
3737
"linkify-it": "^2.0.3",
3838
"mocha": "^3.0.2",
3939
"rimraf": "^2.5.2",
40-
"tlds": "^1.182.0"
40+
"babel-preset-react": "^6.23.0",
41+
"react-dom": "^15.4.2"
4142
}
4243
}

src/createStyleRenderer.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Returns a single style object provided styleArray and stylesMap
3+
*/
4+
const reduceStyles = (styleArray, stylesMap) => styleArray
5+
.map(style => stylesMap[style])
6+
.reduce((prev, next) => Object.assign({}, prev, next), {});
7+
8+
/**
9+
* Returns a styleRenderer from a customStyleMap and a wrapper callback (Component)
10+
*/
11+
const createStyleRenderer = (wrapper, stylesMap) => (children, styleArray, params) => {
12+
const style = reduceStyles(styleArray, stylesMap);
13+
return wrapper(Object.assign({}, { children }, params, { style }));
14+
};
15+
16+
export default createStyleRenderer;

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import RawParser from './RawParser';
2+
import createStylesRenderer from './createStyleRenderer';
23
import { renderNode, render } from './render';
34

45
export {
6+
createStylesRenderer,
57
RawParser,
68
renderNode,
79
};

test/flat.js

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import chai from 'chai';
2-
import redraft from '../src';
2+
import redraft, { createStylesRenderer } from '../src';
33
import * as raws from './raws';
44
import { joinRecursively, makeList } from './helpers';
5+
import ReactDOMServer from 'react-dom/server'
6+
import React from 'react';
57

68
const should = chai.should();
79

810
const customStyleMap = {
11+
BOLD: {
12+
'font-weight': 'bold',
13+
},
14+
ITALIC: {
15+
'font-style': 'italic',
16+
},
17+
UNDERLINE: {
18+
'text-decoration': 'underline',
19+
},
20+
};
21+
22+
const customStyleMapReact = {
923
BOLD: {
1024
fontWeight: 'bold',
1125
},
@@ -17,14 +31,22 @@ const customStyleMap = {
1731
},
1832
};
1933

34+
const stringifyStyles = reduced => Object.keys(reduced)
35+
.map(key => `${key}:${reduced[key]};`).join('');
2036

21-
const mapStyles = styleArray => {
22-
const stylesMap = styleArray.map(style => customStyleMap[style])
23-
const reduced = stylesMap.reduce((prev, next) => Object.assign({}, prev, next), {})
24-
return Object.keys(reduced).map(key => `${key}: ${reduced[key]}`).join(';')
25-
};
2637
// render to HTML
27-
const styles = (children, styleArray) => `<span style="${mapStyles(styleArray)}">${joinRecursively(children)}</span>`;
38+
const styles = createStylesRenderer(
39+
({ children, style }) => `<span style="${stringifyStyles(style)}">${joinRecursively(children)}</span>`,
40+
customStyleMap
41+
);
42+
43+
// needs a key
44+
const Inline = ({ children, style, key }) => <span key={key} style={style}>{children}</span>
45+
46+
const stylesReact = createStylesRenderer(
47+
Inline,
48+
customStyleMapReact
49+
);
2850

2951
const blocks = {
3052
unstyled: (children) => `<p>${joinRecursively(children)}</p>`,
@@ -33,27 +55,51 @@ const blocks = {
3355
'unordered-list-item': (children) => `<ul>${makeList(children)}</ul>`,
3456
};
3557

58+
59+
const blocksReact = {
60+
unstyled: (children, { keys }) =>
61+
children.map((child, index) => <p key={keys[index]}>{child}</p>),
62+
blockquote: (children, { keys }) => <blockquote key={keys[0]} >{children}</blockquote>,
63+
};
64+
3665
const entities = {
37-
LINK: (children, entity) => `<a href="${entity.url}" >${joinRecursively(children)}</a>`,
66+
LINK: (children, entity) => `<a href="${entity.url}">${joinRecursively(children)}</a>`,
3867
ENTITY: (children, entity) => `<div style="color: ${entity.data.color}" >${joinRecursively(children)}</div>`,
3968
};
4069

70+
const entitiesReact = {
71+
LINK: (children, entity, { key }) => <a key={key} href={entity.url} >{children}</a>,
72+
};
73+
4174
const renderers = {
4275
styles,
4376
blocks,
4477
entities,
4578
};
4679

80+
const renderersReact = {
81+
styles: stylesReact,
82+
blocks: blocksReact,
83+
entities: entitiesReact,
84+
};
85+
4786
// Helpers for te
48-
const bold = 'fontWeight: bold';
49-
const italic = 'fontStyle: italic';
87+
const bold = 'font-weight:bold;';
88+
const italic = 'font-style:italic;';
89+
90+
const corretRender = `<p><span style="${bold}">Lorem </span><a href="http://zombo.com/"><span style="${bold}${italic}">ipsum</span></a><span style="${bold}${italic}"> dolor</span><span style="${italic}"> sit amet,</span> pro nisl sonet ad. </p><blockquote>Eos affert numquam id, in est meis nobis. Legimus singulis suscipiantur eum in, <span style="${italic}">ceteros invenire </span>tractatos his id. </blockquote><p><span style="${bold}">Facer facilis definiebas ea pro, mei malis libris latine an. Senserit moderatius vituperata vis in.</span></p>` // eslint-disable-line max-len
91+
5092

5193
describe('redraft with flat styles', () => {
5294
it('should render flat styles correctly', () => {
5395
const rendered = redraft(raws.raw, renderers);
5496
const joined = joinRecursively(rendered);
55-
joined.should.equal(
56-
`<p><span style="${bold}">Lorem </span><a href="http://zombo.com/" ><span style="${bold};${italic}">ipsum</span></a><span style="${bold};${italic}"> dolor</span><span style="${italic}"> sit amet,</span> pro nisl sonet ad. </p><blockquote>Eos affert numquam id, in est meis nobis. Legimus singulis suscipiantur eum in, <span style="${italic}">ceteros invenire </span>tractatos his id. </blockquote><p><span style="${bold}">Facer facilis definiebas ea pro, mei malis libris latine an. Senserit moderatius vituperata vis in.</span></p>` // eslint-disable-line max-len
97+
joined.should.equal(corretRender);
98+
});
99+
it('should render flat styles correctly with ReactDOMServer.renderToStaticMarkup', () => {
100+
const rendered = <div>{redraft(raws.raw, renderersReact)}</div>;
101+
const stringified = ReactDOMServer.renderToStaticMarkup(rendered);
102+
stringified.should.equal(`<div>${corretRender}</div>`
57103
);
58104
});
59105
});

0 commit comments

Comments
 (0)