-
Notifications
You must be signed in to change notification settings - Fork 890
/
slate-editor.tsx
218 lines (183 loc) · 4.07 KB
/
slate-editor.tsx
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
import React from 'react';
import SlateTypes from 'slate-prop-types';
import Types from 'prop-types';
import invariant from 'tiny-invariant';
import { Editor as Controller } from 'slate';
const EVENT_HANDLERS = [
'onBeforeInput',
'onBlur',
'onClick',
'onContextMenu',
'onCompositionEnd',
'onCompositionStart',
'onCopy',
'onCut',
'onDragEnd',
'onDragEnter',
'onDragExit',
'onDragLeave',
'onDragOver',
'onDragStart',
'onDrop',
'onInput',
'onFocus',
'onKeyDown',
'onKeyUp',
'onMouseDown',
'onMouseUp',
'onPaste',
'onSelect',
];
/**
* Editor.
*
* @type {Component}
*/
interface Props {
autoCorrect?: boolean;
className: string;
id: string;
onChange?: any;
readOnly?: boolean;
role: string;
spellCheck?: boolean;
style: object;
tabIndex: number;
controller: Controller;
onBlur?: (e: Event) => void;
onDrop?: (e: Event) => void;
onCut?: (e: Event) => void;
onCopy?: (e: Event) => void;
onPaste?: (e: Event) => void;
}
export class SlateEditor extends React.Component<Props> {
/**
* Property types.
*
* @type {Object}
*/
static propTypes = {
autoCorrect: Types.bool,
className: Types.string,
id: Types.string,
onChange: Types.func,
readOnly: Types.bool,
role: Types.string,
spellCheck: Types.bool,
style: Types.object,
tabIndex: Types.number,
value: SlateTypes.value,
...EVENT_HANDLERS.reduce((obj, handler) => {
obj[handler] = Types.func;
return obj;
}, {}),
};
/**
* Default properties.
*
* @type {Object}
*/
static defaultProps = {
autoCorrect: true,
onChange: () => {},
readOnly: false,
spellCheck: true,
};
/**
* Render the editor.
*
* @return {Element}
*/
render() {
// Set the current props on the controller.
const { controller, ...rest } = this.props;
// Render the editor's children with the controller.
return controller.run('renderEditor', {
...rest,
editor: this,
value: controller.value,
});
}
/**
* Mimic the API of the `Editor` controller, so that this component instance
* can be passed in its place to plugins.
*/
get controller(): Controller {
return this.props.controller;
}
get operations() {
return this.controller.operations;
}
get readOnly() {
return this.controller.readOnly;
}
get value() {
return this.controller.value;
}
applyOperation(...args) {
return this.controller.applyOperation(...args);
}
command(...args) {
return this.controller.command(...args);
}
hasCommand(...args) {
return this.controller.hasCommand(...args);
}
hasQuery(...args) {
return this.controller.hasQuery(...args);
}
normalize(...args) {
return this.controller.normalize(...args);
}
query(...args) {
return this.controller.query(...args);
}
registerCommand(...args) {
return this.controller.registerCommand(...args);
}
registerQuery(...args) {
return this.controller.registerQuery(...args);
}
run(...args) {
return this.controller.run(...args);
}
withoutNormalizing(...args) {
return this.controller.withoutNormalizing(...args);
}
/**
* Deprecated.
*/
get editor() {
return this.controller.editor;
}
get stack() {
invariant(
false,
'As of Slate 0.42, the `editor.stack` property no longer exists, and its functionality has been folded into the editor itself. Use the `editor` instead.'
);
}
call(...args) {
return this.controller.call(...args);
}
change(...args) {
return this.controller.change(...args);
}
onChange(...args) {
return this.controller.onChange(...args);
}
applyOperations(...args) {
return this.controller.applyOperations(...args);
}
setOperationFlag(...args) {
return this.controller.setOperationFlag(...args);
}
getFlag(...args) {
return this.controller.getFlag(...args);
}
unsetOperationFlag(...args) {
return this.controller.unsetOperationFlag(...args);
}
withoutNormalization(...args) {
return this.controller.withoutNormalization(...args);
}
}