Create dynamic inline styles for draft-js
npm install @numberworks/draft-js-custom-styles
This package allows you to use custom inline styles and also export them so they can be
rendered to using draft-js-export-html package
It will export:
- default inline styles (BOLD, ITALIC, UNDERLINE, etc)
- customStyleMap (Your customStyleMap styles that you will pass to the editor)
- customStyles (Your custom styles)
- Installation
- Usage
- API
- Example
- Support
- Contributing
``sh`
npm i --save draft-js-custom-styles
Pass an array of css properties to the createStyles function.
prefix and the customStyleMaps are optional
`javascript`
import createStyles from 'draft-js-custom-styles';
const { styles, customStyleFn, exporter } = createStyles(['font-size', 'color'], 'PREFIX', customStyleMap);
`
You will have access to new functions to add, remove, toggle the new styles.
javascript
// color
const toggleColor = styles.color.toggle;
const addColor = styles.color.add;
const removeColor = styles.color.remove;
const currentColor = styles.color.current;
// fontSize
const toggleFontSize = styles.fontSize.toggle;
const addFontSize = styles.fontSize.add;
const removeFontSize = styles.fontSize.remove;
const currentFontSize = styles.fontSize.current
`
`javascript`
const { styles, customStyleFn, exporter } = createStyles(['font-size', 'color', 'text-transform'], 'PREFIX_', customStyleMap);
styles
- .add(editorState, cssPropertyVal)
Adds a new customStyle
- .remove(editorState)
Removes a customStyle
- .toggle(editorState, cssPropVal)
Toggles a customStyle
- .current(editorState)
Returns the current value of the custom style
How to use the exporter?
If you are using draft-js-export-html. you can export the inline styles with by passing
the exporter your editorState.
`javascript`
const inlineStyles = exporter(this.state.editorState);
const html = stateToHTML(this.state.editorState.getCurrentContent(), { inlineStyles });
`
Example
javascript``
import React from 'react';
import { Editor, convertToRaw } from 'draft-js';
import { stateToHTML } from 'draft-js-export-html';
import Raw from 'draft-js-raw-content-state';
import createStyles from 'draft-js-custom-styles';
const customStyleMap = {
MARK: {
backgroundColor: 'Yellow',
fontStyle: 'italic',
},
};
// Passing the customStyleMap is optional
const { styles, customStyleFn, exporter } = createStyles(['font-size', 'color', 'text-transform'], 'CUSTOM_', customStyleMap);
class RichEditor extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: new Raw().addBlock('Hello World', 'header-two').toEditorState(),
readOnly: false,
};
this.updateEditorState = editorState => this.setState({ editorState });
}
toggleFontSize = fontSize => {
const newEditorState = styles.fontSize.toggle(this.state.editorState, fontSize);
return this.updateEditorState(newEditorState);
};
removeFontSize = () => {
const newEditorState = styles.fontSize.remove(this.state.editorState);
return this.updateEditorState(newEditorState);
};
addFontSize = val => () => {
const newEditorState = styles.fontSize.add(this.state.editorState, val);
return this.updateEditorState(newEditorState);
};
toggleColor = color => {
const newEditorState = styles.color.toggle(this.state.editorState, color);
return this.updateEditorState(newEditorState);
};
toggleTextTransform = color => {
const newEditorState = styles.textTransform.toggle(this.state.editorState, color);
return this.updateEditorState(newEditorState);
};
render() {
const { editorState } = this.state;
const inlineStyles = exporter(this.state.editorState);
const html = stateToHTML(this.state.editorState.getCurrentContent(), { inlineStyles });
const options = x => x.map(fontSize => {
return ;
});
return (
onClick={this.removeFontSize}
>
Remove FontSize
onClick={this.addFontSize('24px')}
>
Add FontSize
r
Draft-JS Editor
customStyleMap={customStyleMap}
editorState={editorState}
onChange={this.updateEditorState}
onTab={this.onTab}
placeholder="Tell a story..."
readOnly={this.state.readOnly}
spellCheck
/>
Exported To HTML
ContentState
{JSON.stringify(convertToRaw(this.state.editorState.getCurrentContent()), null, 2)}
);
}
}
export default RichEditor;
Please open an issue for support.
Please contribute using Github Flow. Create a branch, add commits, and open a pull request.