-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtray.html
143 lines (128 loc) · 4.33 KB
/
tray.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
138
139
140
141
142
143
<!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 });
}
renderTray() {
return (
<div
style={{
color: 'firebrick',
fontSize: 14,
marginBottom: 8,
maxWidth: 500,
}}
>
Arbitrary information can be displayed in a tray, located between
the editor body and the plugin button controls. It's a good place
to display validation warnings or information related to plugin
data.
</div>
);
}
render() {
return (
<WithPlugin
editorState={this.state.editorState}
onChange={this.onChange}
keyCommandListeners={[Draft.RichUtils.handleKeyCommand]}
renderTray={this.renderTray}
/>
);
}
}
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>