forked from facebookarchive/draft-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.html
220 lines (199 loc) · 6.83 KB
/
color.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!--
Copyright (c) 2013-present, Facebook, Inc. All rights reserved.
This file provided by Facebook is for non-commercial testing and evaluation
purposes only. Facebook reserves all rights not expressly granted.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Draft • Color</title>
<link rel="stylesheet" href="../../../dist/Draft.css" />
</head>
<body>
<div style="font-family: Georgia, serif; font-size: 15px; padding: 20px; width: 600px; line-height: 24px;">
This example demonstrates how custom inline styles can be used to create
editors with an unlimited range of style combinations. Note also that
the state of the editor can be manipulated to enforce that only one
color may be active at any time.
</div>
<div id="target"></div>
<script src="../../../node_modules/react/dist/react.js"></script>
<script src="../../../node_modules/react-dom/dist/react-dom.js"></script>
<script src="../../../node_modules/immutable/dist/immutable.js"></script>
<script src="../../../node_modules/es6-shim/es6-shim.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.js"></script>
<script src="../../../dist/Draft.js"></script>
<script type="text/babel">
'use strict';
const {Editor, EditorState, Modifier, RichUtils} = Draft;
class ColorfulEditorExample extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.focus = () => this.refs.editor.focus();
this.onChange = (editorState) => this.setState({editorState});
this.toggleColor = (toggledColor) => this._toggleColor(toggledColor);
}
_toggleColor(toggledColor) {
const {editorState} = this.state;
const selection = editorState.getSelection();
// Let's just allow one color at a time. Turn off all active colors.
const nextContentState = Object.keys(colorStyleMap)
.reduce((contentState, color) => {
return Modifier.removeInlineStyle(contentState, selection, color)
}, editorState.getCurrentContent());
let nextEditorState = EditorState.push(
editorState,
nextContentState,
'change-inline-style'
);
const currentStyle = editorState.getCurrentInlineStyle();
// Unset style override for current color.
if (selection.isCollapsed()) {
nextEditorState = currentStyle.reduce((state, color) => {
return RichUtils.toggleInlineStyle(state, color);
}, nextEditorState);
}
// If the color is being toggled on, apply it.
if (!currentStyle.has(toggledColor)) {
nextEditorState = RichUtils.toggleInlineStyle(
nextEditorState,
toggledColor
);
}
this.onChange(nextEditorState);
}
render() {
const {editorState} = this.state;
return (
<div style={styles.root}>
<ColorControls
editorState={editorState}
onToggle={this.toggleColor}
/>
<div style={styles.editor} onClick={this.focus}>
<Editor
customStyleMap={colorStyleMap}
editorState={editorState}
onChange={this.onChange}
placeholder="Write something colorful..."
ref="editor"
/>
</div>
</div>
);
}
}
class StyleButton extends React.Component {
constructor(props) {
super(props);
this.onToggle = (e) => {
e.preventDefault();
this.props.onToggle(this.props.style);
};
}
render() {
let style;
if (this.props.active) {
style = {...styles.styleButton, ...colorStyleMap[this.props.style]};
} else {
style = styles.styleButton;
}
return (
<span style={style} onMouseDown={this.onToggle}>
{this.props.label}
</span>
);
}
}
var COLORS = [
{label: 'Red', style: 'red'},
{label: 'Orange', style: 'orange'},
{label: 'Yellow', style: 'yellow'},
{label: 'Green', style: 'green'},
{label: 'Blue', style: 'blue'},
{label: 'Indigo', style: 'indigo'},
{label: 'Violet', style: 'violet'},
];
const ColorControls = (props) => {
var currentStyle = props.editorState.getCurrentInlineStyle();
return (
<div style={styles.controls}>
{COLORS.map(type =>
<StyleButton
active={currentStyle.has(type.style)}
label={type.label}
onToggle={props.onToggle}
style={type.style}
/>
)}
</div>
);
};
// This object provides the styling information for our custom color
// styles.
const colorStyleMap = {
red: {
color: 'rgba(255, 0, 0, 1.0)',
},
orange: {
color: 'rgba(255, 127, 0, 1.0)',
},
yellow: {
color: 'rgba(180, 180, 0, 1.0)',
},
green: {
color: 'rgba(0, 180, 0, 1.0)',
},
blue: {
color: 'rgba(0, 0, 255, 1.0)',
},
indigo: {
color: 'rgba(75, 0, 130, 1.0)',
},
violet: {
color: 'rgba(127, 0, 255, 1.0)',
},
};
const styles = {
root: {
fontFamily: '\'Georgia\', serif',
fontSize: 14,
padding: 20,
width: 600,
},
editor: {
borderTop: '1px solid #ddd',
cursor: 'text',
fontSize: 16,
marginTop: 20,
minHeight: 400,
paddingTop: 20,
},
controls: {
fontFamily: '\'Helvetica\', sans-serif',
fontSize: 14,
marginBottom: 10,
userSelect: 'none',
},
styleButton: {
color: '#999',
cursor: 'pointer',
marginRight: 16,
padding: '2px 0',
},
};
ReactDOM.render(
<ColorfulEditorExample />,
document.getElementById('target')
);
</script>
</body>
</html>