Skip to content

Commit

Permalink
add styles
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 27, 2016
1 parent c4a8406 commit d8fbc83
Show file tree
Hide file tree
Showing 5 changed files with 433 additions and 9 deletions.
9 changes: 9 additions & 0 deletions components/EditorSection/EditorSection.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.controlBar {
position: fixed;
bottom: 10px;
background-color: #fff;
border: 2px solid #eee;
border-radius: 3px;
width: 100%;
z-index: 999;
}
141 changes: 132 additions & 9 deletions components/EditorSection/EditorSection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
import * as React from "react";
import {Editor, EditorState, ContentState, convertFromHTML, RichUtils} from 'draft-js';
import StyleButton from '../StyleButton';
import s from './EditorSection.css';

// Custom overrides for "code" style.
const styleMap = {
CODE: {
backgroundColor: 'rgba(0, 0, 0, 0.05)',
fontFamily: '"Inconsolata", "Menlo", "Consolas", monospace',
fontSize: 16,
padding: 2,
},
};

function getBlockStyle(block) {
switch (block.getType()) {
case 'blockquote':
return 'RichEditor-blockquote';
default:
return null;
}
}


const BLOCK_TYPES = [
{label: 'H1', style: 'header-one'},
{label: 'H2', style: 'header-two'},
{label: 'Blockquote', style: 'blockquote'},
{label: 'UL', style: 'unordered-list-item'},
{label: 'OL', style: 'ordered-list-item'},
{label: 'Code Block', style: 'code-block'},
];

const BlockStyleControls = (props) => {
const {editorState} = props;
const selection = editorState.getSelection();
const blockType = editorState
.getCurrentContent()
.getBlockForKey(selection.getStartKey())
.getType();

return (
<div className="RichEditor-controls">
{BLOCK_TYPES.map((type, i) =>
<StyleButton
key={i}
active={type.style === blockType}
label={type.label}
onToggle={props.onToggle}
style={type.style}
/>
)}
</div>
);
};

var INLINE_STYLES = [
{label: 'Bold', style: 'BOLD'},
{label: 'Italic', style: 'ITALIC'},
{label: 'Underline', style: 'UNDERLINE'},
{label: 'Monospace', style: 'CODE'},
];

const InlineStyleControls = (props) => {
var currentStyle = props.editorState.getCurrentInlineStyle();
return (
<div className="RichEditor-controls">
{INLINE_STYLES.map((type, i) =>
<StyleButton
key={i}
active={currentStyle.has(type.style)}
label={type.label}
onToggle={props.onToggle}
style={type.style}
/>
)}
</div>
);
};

class EditorSection extends React.Component {
constructor(props) {
Expand All @@ -11,27 +89,72 @@ class EditorSection extends React.Component {
editorState: editorState
};

this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => this.setState({editorState});
this.handleKeyCommand = this.handleKeyCommand.bind(this);
}

this.handleKeyCommand = (command) => this._handleKeyCommand(command);
this.toggleBlockType = (type) => this._toggleBlockType(type);
this.toggleInlineStyle = (style) => this._toggleInlineStyle(style);
}

handleKeyCommand(command) {
const newState = RichUtils.handleKeyCommand(this.state.editorState, command);
_handleKeyCommand(command) {
const {editorState} = this.state;
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
this.onChange(newState);
return true;
}
return false;
}

_toggleBlockType(blockType) {
this.onChange(
RichUtils.toggleBlockType(
this.state.editorState,
blockType
)
);
}

_toggleInlineStyle(inlineStyle) {
this.onChange(
RichUtils.toggleInlineStyle(
this.state.editorState,
inlineStyle
)
);
}

render() {
const {editorState} = this.state;

return (
<Editor
editorState={this.state.editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.onChange}
/>
<div>
<div className="RichEditor-root">
<div className={s.editorStyle} onClick={this.focus}>
<Editor
blockStyleFn={getBlockStyle}
customStyleMap={styleMap}
editorState={editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.onChange}
placeholder="Tell a story..."
ref="editor"
spellCheck={true}
/>
</div>
</div>
<div className={s.controlBar}>
<BlockStyleControls
editorState={editorState}
onToggle={this.toggleBlockType}
/>
<InlineStyleControls
editorState={editorState}
onToggle={this.toggleInlineStyle}
/>
</div>
</div>
);
}
}
Expand Down
6 changes: 6 additions & 0 deletions components/StyleButton/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "StyleButton",
"version": "0.0.0",
"private": true,
"main": "./StyleButton.js"
}
Loading

0 comments on commit d8fbc83

Please sign in to comment.