-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathblockStyles.html
137 lines (121 loc) · 4.27 KB
/
blockStyles.html
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
127
128
129
130
131
132
133
134
135
136
137
<!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,
Toolbar,
KeyCommandController,
createPlugin,
} = window.DraftExtend;
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' },
];
const createBlockStyleButton = ({ label, style }) => {
return ({ editorState, onChange }) => {
const toggleStyle = () => {
onChange(RichUtils.toggleBlockType(editorState, style));
};
const currentBlockStyle = editorState
.getCurrentContent()
.getBlockForKey(editorState.getSelection().getStartKey())
.getType();
const isActive = currentBlockStyle === style;
return (
<ToolbarButton
active={isActive}
label={label}
onClick={toggleStyle}
/>
);
};
};
const BlockPlugin = createPlugin({
buttons: BLOCK_TYPES.map(createBlockStyleButton),
});
const WithPlugin = BlockPlugin(Editor);
const WithPluginToolbar = BlockPlugin(Toolbar);
const toHTML = BlockPlugin(convertToHTML);
const fromHTML = BlockPlugin(convertFromHTML);
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>'
)
),
};
this.onChange = this.onChange.bind(this);
}
onChange(editorState) {
console.log(toHTML(editorState.getCurrentContent()));
this.setState({ editorState });
}
render() {
return (
<div>
<WithPlugin
{...this.props}
editorState={this.state.editorState}
onChange={this.onChange}
/>
<WithPluginToolbar
{...this.props}
editorState={this.state.editorState}
onChange={this.onChange}
/>
</div>
);
}
}
const WrappedComponent = KeyCommandController(BlockStylesExample);
ReactDOM.render(<WrappedComponent />, document.getElementById('target'));
</script>
<script>
eval(
Babel.transform(
document.querySelector('script[type="text/babel"]').innerText,
{ presets: ['es2015', 'react'] }
).code
);
</script>
</body>
</html>