JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<!--
Source of the code: https://github.com/facebook/draft-js/blob/master/examples/draft-0-10-0/color/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Draft • Color</title>
</head>
<body>
<div style="font-family: Georgia, serif; font-size: 15px; padding: 20px; width: 600px; line-height: 24px;">
This example demonstrates how custom inline styles can be used to create
editors with an unlimited range of style combinations. Note also that
the state of the editor can be manipulated to enforce that only one
color may be active at any time.
</div>
<div id="target"></div>
</body>
</html>
const {Editor, EditorState, Modifier, RichUtils} = Draft;
class ColorfulEditorExample extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => this.setState({editorState});
this.toggleColor = (toggledColor) => this._toggleColor(toggledColor);
}
_toggleColor(toggledColor) {
const {editorState} = this.state;
const selection = editorState.getSelection();
// Let's just allow one color at a time. Turn off all active colors.
const nextContentState = Object.keys(colorStyleMap)
.reduce((contentState, color) => {
return Modifier.removeInlineStyle(contentState, selection, color)
}, editorState.getCurrentContent());
let nextEditorState = EditorState.push(
editorState,
nextContentState,
'change-inline-style'
);
const currentStyle = editorState.getCurrentInlineStyle();
// Unset style override for current color.
if (selection.isCollapsed()) {
nextEditorState = currentStyle.reduce((state, color) => {
return RichUtils.toggleInlineStyle(state, color);
}, nextEditorState);
}
// If the color is being toggled on, apply it.
if (!currentStyle.has(toggledColor)) {
nextEditorState = RichUtils.toggleInlineStyle(
nextEditorState,
toggledColor
);
}
this.onChange(nextEditorState);
}
render() {
const {editorState} = this.state;
return (
<div style={styles.root}>
<ColorControls
editorState={editorState}
onToggle={this.toggleColor}
/>
<div style={styles.editor} onClick={this.focus}>
<Editor
customStyleMap={colorStyleMap}
editorState={editorState}
onChange={this.onChange}
placeholder="Write something colorful..."
ref="editor"
/>
</div>
</div>
);
}
}
class StyleButton extends React.Component {
constructor(props) {
super(props);
this.onToggle = (e) => {
e.preventDefault();
this.props.onToggle(this.props.style);
};
}
render() {
let style;
if (this.props.active) {
style = {...styles.styleButton, ...colorStyleMap[this.props.style]};
} else {
style = styles.styleButton;
}
return (
<span style={style} onMouseDown={this.onToggle}>
{this.props.label}
</span>
);
}
}
var COLORS = [
{label: 'Red', style: 'red'},
{label: 'Orange', style: 'orange'},
{label: 'Yellow', style: 'yellow'},
{label: 'Green', style: 'green'},
{label: 'Blue', style: 'blue'},
{label: 'Indigo', style: 'indigo'},
{label: 'Violet', style: 'violet'},
];
const ColorControls = (props) => {
var currentStyle = props.editorState.getCurrentInlineStyle();
return (
<div style={styles.controls}>
{COLORS.map(type =>
<StyleButton
active={currentStyle.has(type.style)}
label={type.label}
onToggle={props.onToggle}
style={type.style}
/>
)}
</div>
);
};
// This object provides the styling information for our custom color
// styles.
const colorStyleMap = {
red: {
color: 'rgba(255, 0, 0, 1.0)',
},
orange: {
color: 'rgba(255, 127, 0, 1.0)',
},
yellow: {
color: 'rgba(180, 180, 0, 1.0)',
},
green: {
color: 'rgba(0, 180, 0, 1.0)',
},
blue: {
color: 'rgba(0, 0, 255, 1.0)',
},
indigo: {
color: 'rgba(75, 0, 130, 1.0)',
},
violet: {
color: 'rgba(127, 0, 255, 1.0)',
},
};
const styles = {
root: {
fontFamily: '\'Georgia\', serif',
fontSize: 14,
padding: 20,
width: 600,
},
editor: {
borderTop: '1px solid #ddd',
cursor: 'text',
fontSize: 16,
marginTop: 20,
minHeight: 400,
paddingTop: 20,
},
controls: {
fontFamily: '\'Helvetica\', sans-serif',
fontSize: 14,
marginBottom: 10,
userSelect: 'none',
},
styleButton: {
color: '#999',
cursor: 'pointer',
marginRight: 16,
padding: '2px 0',
},
};
ReactDOM.render(
<ColorfulEditorExample />,
document.getElementById('target')
);
Also see: Tab Triggers