Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 27 additions & 29 deletions example/ToolbarButton.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
window.ToolbarButton = createReactClass({
getDefaultProps: function() {
return {
active: false,
label: '',
onClick: function() {}
};
},
window.ToolbarButton = function({ active, label, onClick }) {
var toolbarButtonStyle = {
display: 'inline-block',
minWidth: '24px',
padding: '4px',
borderRadius: '3px',
textAlign: 'center',
cursor: 'pointer',
};

render: function() {
var toolbarButtonStyle = {
display: 'inline-block',
minWidth: '24px',
padding: '4px',
borderRadius: '3px',
textAlign: 'center',
cursor: 'pointer'
};
if (active) {
toolbarButtonStyle.background = '#000';
toolbarButtonStyle.color = '#fff';
}

if (this.props.active) {
toolbarButtonStyle.background = '#000';
toolbarButtonStyle.color = '#fff';
}
return React.createElement(
'div',
{
style: toolbarButtonStyle,
onClick: onClick,
},
label
);
};

return (
React.createElement('div', {
style: toolbarButtonStyle,
onClick: this.props.onClick
}, this.props.label)
);
}
});
ToolbarButton.defaultProps = {
active: false,
label: '',
onClick: function() {},
};
85 changes: 41 additions & 44 deletions example/blockStyles.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<body>
<div id="target"></div>
<script src="../node_modules/react/umd/react.development.js"></script>
<script src="../node_modules/create-react-class/create-react-class.js"></script>
<script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
<script src="../node_modules/react-dom/umd/react-dom-server.browser.development.js"></script>
<script src="../node_modules/immutable/dist/immutable.min.js"></script>
Expand All @@ -27,46 +26,35 @@
<script src="../node_modules/draft-convert/dist/draft-convert.js"></script>
<script src="./ToolbarButton.js"></script>
<script type="text/babel">
const {
EditorState,
RichUtils
} = Draft;
const { EditorState, RichUtils } = Draft;

const {
Editor,
Toolbar,
KeyCommandController,
createPlugin
createPlugin,
} = window.DraftExtend;

const {
convertToHTML,
convertFromHTML
} = window.DraftConvert;
const { convertToHTML, convertFromHTML } = window.DraftConvert;

const BLOCK_TYPES = [
{label: 'Plain', style: 'unstyled'},
{label: 'H1', style: 'header-one'},
{label: 'H2', style: 'header-two'},
{label: 'H3', style: 'header-three'},
{label: 'H4', style: 'header-four'},
{label: 'H5', style: 'header-five'},
{label: 'H6', style: 'header-six'},
{label: 'Blockquote', style: 'blockquote'},
{label: 'UL', style: 'unordered-list-item'},
{label: 'OL', style: 'ordered-list-item'},
{label: 'Code Block', style: 'code-block'}
{ label: 'Plain', style: 'unstyled' },
{ label: 'H1', style: 'header-one' },
{ label: 'H2', style: 'header-two' },
{ label: 'H3', style: 'header-three' },
{ label: 'H4', style: 'header-four' },
{ label: 'H5', style: 'header-five' },
{ label: 'H6', style: 'header-six' },
{ label: 'Blockquote', style: 'blockquote' },
{ label: 'UL', style: 'unordered-list-item' },
{ label: 'OL', style: 'ordered-list-item' },
{ label: 'Code Block', style: 'code-block' },
];

const createBlockStyleButton = ({label, style}) => {
return ({editorState, onChange}) => {
const createBlockStyleButton = ({ label, style }) => {
return ({ editorState, onChange }) => {
const toggleStyle = () => {
onChange(
RichUtils.toggleBlockType(
editorState,
style
)
);
onChange(RichUtils.toggleBlockType(editorState, style));
};

const currentBlockStyle = editorState
Expand All @@ -87,27 +75,33 @@
};

const BlockPlugin = createPlugin({
buttons: BLOCK_TYPES.map(createBlockStyleButton)
buttons: BLOCK_TYPES.map(createBlockStyleButton),
});

const WithPlugin = BlockPlugin(Editor);
const WithPluginToolbar = BlockPlugin(Toolbar);
const toHTML = BlockPlugin(convertToHTML);
const fromHTML = BlockPlugin(convertFromHTML);

const BlockStylesExample = createReactClass({
getInitialState() {
return {
class BlockStylesExample extends React.Component {
constructor(props) {
super(props);

this.state = {
editorState: EditorState.createWithContent(
fromHTML('<h1>Block style plugin</h1><ul><li>list item 1</li><li>list item 2</li></ul>')
)
fromHTML(
'<h1>Block style plugin</h1><ul><li>list item 1</li><li>list item 2</li></ul>'
)
),
};
},

this.onChange = this.onChange.bind(this);
}

onChange(editorState) {
console.log(toHTML(editorState.getCurrentContent()));
this.setState({editorState});
},
this.setState({ editorState });
}

render() {
return (
Expand All @@ -125,16 +119,19 @@
</div>
);
}
});
}

const WrappedComponent = KeyCommandController(BlockStylesExample);

ReactDOM.render(
<WrappedComponent />,
document.getElementById('target')
);
ReactDOM.render(<WrappedComponent />, document.getElementById('target'));
</script>
<script>
eval(Babel.transform(document.querySelector('script[type="text/babel"]').innerText, { presets: ['es2015', 'react']}).code)
eval(
Babel.transform(
document.querySelector('script[type="text/babel"]').innerText,
{ presets: ['es2015', 'react'] }
).code
);
</script>
</body>
</html>
75 changes: 36 additions & 39 deletions example/inlineStyles.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
<body>
<div id="target"></div>
<script src="../node_modules/react/umd/react.development.js"></script>
<script src="../node_modules/create-react-class/create-react-class.js"></script>
<script src="../node_modules/react-dom/umd/react-dom.development.js"></script>
<script src="../node_modules/react-dom/umd/react-dom-server.browser.development.js"></script>
<script src="../node_modules/immutable/dist/immutable.min.js"></script>
Expand All @@ -27,47 +26,33 @@
<script src="../node_modules/draft-convert/dist/draft-convert.js"></script>
<script src="./ToolbarButton.js"></script>
<script type="text/babel">
const {
EditorState,
RichUtils
} = Draft;
const { EditorState, RichUtils } = Draft;

const {
Editor,
createPlugin
} = window.DraftExtend;
const { Editor, createPlugin } = window.DraftExtend;

const {
convertToHTML,
convertFromHTML
} = window.DraftConvert;
const { convertToHTML, convertFromHTML } = window.DraftConvert;

const INLINE_STYLES = [
{label: 'Bold', style: 'BOLD'},
{label: 'Italic', style: 'ITALIC'},
{label: 'Underline', style: 'UNDERLINE'},
{label: 'Code', style: 'CODE'},
{label: 'Strikethrough', style: 'STRIKETHROUGH'}
{ label: 'Bold', style: 'BOLD' },
{ label: 'Italic', style: 'ITALIC' },
{ label: 'Underline', style: 'UNDERLINE' },
{ label: 'Code', style: 'CODE' },
{ label: 'Strikethrough', style: 'STRIKETHROUGH' },
];

const styleToHTML = (style) => {
const styleToHTML = style => {
if (style === 'STRIKETHROUGH') {
return <s />;
}
if (style === 'CODE') {
return <div style={{fontFamily: 'monospace'}} />;
return <div style={{ fontFamily: 'monospace' }} />;
}
};

const createInlineStyle = ({label, style}) => {
return ({editorState, onChange}) => {
const createInlineStyle = ({ label, style }) => {
return ({ editorState, onChange }) => {
const toggleStyle = () => {
onChange(
RichUtils.toggleInlineStyle(
editorState,
style
)
);
onChange(RichUtils.toggleInlineStyle(editorState, style));
};

const currentInlineStyle = editorState.getCurrentInlineStyle();
Expand All @@ -86,26 +71,32 @@

const InlinePlugin = createPlugin({
buttons: INLINE_STYLES.map(createInlineStyle),
styleToHTML
styleToHTML,
});

const WithPlugin = InlinePlugin(Editor);
const toHTML = InlinePlugin(convertToHTML);
const fromHTML = InlinePlugin(convertFromHTML);

const InlineStylesExample = createReactClass({
getInitialState() {
return {
class InlineStylesExample extends React.Component {
constructor(props) {
super(props);

this.state = {
editorState: EditorState.createWithContent(
fromHTML('<div><strong>Inline</strong> styles <em>example</em></div>')
)
fromHTML(
'<div><strong>Inline</strong> styles <em>example</em></div>'
)
),
};
},

this.onChange = this.onChange.bind(this);
}

onChange(editorState) {
console.log(toHTML(editorState.getCurrentContent()));
this.setState({editorState});
},
this.setState({ editorState });
}

render() {
return (
Expand All @@ -116,14 +107,20 @@
/>
);
}
});
}

ReactDOM.render(
<InlineStylesExample />,
document.getElementById('target')
);
</script>
<script>
eval(Babel.transform(document.querySelector('script[type="text/babel"]').innerText, { presets: ['es2015', 'react']}).code)
eval(
Babel.transform(
document.querySelector('script[type="text/babel"]').innerText,
{ presets: ['es2015', 'react'] }
).code
);
</script>
</body>
</html>
Loading