-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathinlineStyles.html
More file actions
126 lines (110 loc) · 3.76 KB
/
inlineStyles.html
File metadata and controls
126 lines (110 loc) · 3.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>draft-extend</title>
<link rel="stylesheet" href="../node_modules/draft-js/dist/Draft.css" />
<link rel="stylesheet" href="../dist/draft-extend.css" />
<style>
#target {
font-family: sans-serif;
margin: 20px;
border: solid 1px #ccc;
}
</style>
</head>
<body>
<div id="target"></div>
<script src="../node_modules/react/umd/react.development.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>
<script src="../node_modules/es6-shim/es6-shim.js"></script>
<script src="../node_modules/@babel/standalone/babel.min.js"></script>
<script src="../node_modules/draft-js/dist/Draft.js"></script>
<script src="../dist/draft-extend.js"></script>
<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 { Editor, createPlugin } = window.DraftExtend;
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' },
];
const styleToHTML = style => {
if (style === 'STRIKETHROUGH') {
return <s />;
}
if (style === 'CODE') {
return <div style={{ fontFamily: 'monospace' }} />;
}
};
const createInlineStyle = ({ label, style }) => {
return ({ editorState, onChange }) => {
const toggleStyle = () => {
onChange(RichUtils.toggleInlineStyle(editorState, style));
};
const currentInlineStyle = editorState.getCurrentInlineStyle();
const isActive = currentInlineStyle.has(style);
return (
<ToolbarButton
active={isActive}
label={label}
onClick={toggleStyle}
/>
);
};
};
const InlinePlugin = createPlugin({
buttons: INLINE_STYLES.map(createInlineStyle),
styleToHTML,
});
const WithPlugin = InlinePlugin(Editor);
const toHTML = InlinePlugin(convertToHTML);
const fromHTML = InlinePlugin(convertFromHTML);
class InlineStylesExample extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createWithContent(
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 });
}
render() {
return (
<WithPlugin
editorState={this.state.editorState}
onChange={this.onChange}
keyCommandListeners={[Draft.RichUtils.handleKeyCommand]}
/>
);
}
}
ReactDOM.render(
<InlineStylesExample />,
document.getElementById('target')
);
</script>
<script>
eval(
Babel.transform(
document.querySelector('script[type="text/babel"]').innerText,
{ presets: ['es2015', 'react'] }
).code
);
</script>
</body>
</html>