9
9
getDefaultKeyBinding ,
10
10
KeyBindingUtil ,
11
11
} from 'draft-js' ;
12
+
12
13
import createCompositeDecorator from '../utils/createCompositeDecorator' ;
13
14
import moveSelectionToEnd from '../utils/moveSelectionToEnd' ;
14
15
import moveToEndOfSelectedBlock from '../modifiers/moveToEndOfSelectedBlock' ;
@@ -60,54 +61,6 @@ export default class PluginEditor extends Component {
60
61
}
61
62
} ;
62
63
63
- onDownArrow = ( keyboardEvent ) => {
64
- // TODO allow to provide a custom onDownArrow
65
-
66
- this . plugins . map ( ( plugin ) => {
67
- if ( plugin . onDownArrow ) {
68
- plugin . onDownArrow ( keyboardEvent ) ;
69
- }
70
-
71
- return undefined ;
72
- } ) ;
73
- } ;
74
-
75
- onUpArrow = ( keyboardEvent ) => {
76
- // TODO allow to provide a custom onUpArrow
77
-
78
- this . plugins . map ( ( plugin ) => {
79
- if ( plugin . onUpArrow ) {
80
- plugin . onUpArrow ( keyboardEvent ) ;
81
- }
82
-
83
- return undefined ;
84
- } ) ;
85
- } ;
86
-
87
- onEscape = ( keyboardEvent ) => {
88
- // TODO allow to provide a custom onEscape
89
-
90
- this . plugins . map ( ( plugin ) => {
91
- if ( plugin . onEscape ) {
92
- plugin . onEscape ( keyboardEvent ) ;
93
- }
94
-
95
- return undefined ;
96
- } ) ;
97
- } ;
98
-
99
- onTab = ( keyboardEvent ) => {
100
- // TODO allow to provide a custom onTab
101
-
102
- this . plugins . map ( ( plugin ) => {
103
- if ( plugin . onTab ) {
104
- plugin . onTab ( keyboardEvent ) ;
105
- }
106
-
107
- return undefined ;
108
- } ) ;
109
- } ;
110
-
111
64
getEditorState = ( ) => this . editorState ;
112
65
113
66
handleKeyCommand = ( command ) => {
@@ -144,25 +97,6 @@ export default class PluginEditor extends Component {
144
97
return preventDefaultBehaviour === true ;
145
98
} ;
146
99
147
- handleReturn = ( keyboardEvent ) => {
148
- // TODO optimize to break after the first one
149
- const preventDefaultBehaviour = this . plugins
150
- . map ( ( plugin ) => {
151
- if ( plugin . handleReturn ) {
152
- const handled = plugin . handleReturn ( keyboardEvent ) ;
153
- if ( handled === true ) {
154
- return handled ;
155
- }
156
- }
157
-
158
- return undefined ;
159
- } )
160
- . find ( ( result ) => result === true ) ;
161
-
162
- // TODO allow to provide a custom handleReturn
163
- return preventDefaultBehaviour === true ;
164
- } ;
165
-
166
100
keyBindingFn = ( keyboardEvent ) => {
167
101
// TODO optimize to break after the first one
168
102
let command = this . plugins
@@ -289,8 +223,51 @@ export default class PluginEditor extends Component {
289
223
this . refs . editor . focus ( ) ;
290
224
} ;
291
225
226
+ createHandleListener = ( name ) => ( event ) => (
227
+ this . plugins
228
+ . filter ( ( plug ) => plug [ name ] )
229
+ . map ( ( plugin ) => plugin [ name ] ( event ) )
230
+ . find ( ( result ) => result === true ) === true
231
+ ) ;
232
+
233
+ createOnListener = ( name ) => ( event ) => (
234
+ this . plugins
235
+ . filter ( plug => typeof plug [ name ] === 'function' )
236
+ . forEach ( plug => plug [ name ] ( event ) )
237
+ ) ;
238
+
239
+ createEventListeners = ( ) => {
240
+ const listeners = {
241
+ onChange : this . onChange ,
242
+ handleKeyCommand : this . handleKeyCommand ,
243
+ keyBindingFn : this . keyBindingFn ,
244
+ handleReturn : this . handleReturn ,
245
+ } ;
246
+
247
+ const keepHandlers = [ 'onChange' , 'handleKeyCommand' ] ;
248
+
249
+ // bind random onListeners and handleListeners
250
+ this . plugins . forEach ( ( plug ) => {
251
+ Object . keys ( plug ) . forEach ( ( attrName ) => {
252
+ if ( attrName . indexOf ( 'on' ) === 0 && ! keepHandlers . includes ( attrName ) ) {
253
+ listeners [ attrName ] = this . createOnListener ( attrName ) ;
254
+ }
255
+
256
+ if ( attrName . indexOf ( 'handle' ) === 0 && ! keepHandlers . includes ( attrName ) ) {
257
+ listeners [ attrName ] = this . createHandleListener ( attrName ) ;
258
+ }
259
+ } ) ;
260
+ } ) ;
261
+
262
+ return listeners ;
263
+ } ;
264
+
292
265
render ( ) {
293
266
let pluginProps = { } ;
267
+
268
+ // This puts pluginProps and the object inside getEditorProps
269
+ // on the Editor component (main use case is for aria props right now)
270
+ // Last plugin wins right now (not ideal)
294
271
this . plugins . forEach ( ( plugin ) => {
295
272
if ( plugin . getEditorProps ) {
296
273
pluginProps = {
@@ -300,22 +277,17 @@ export default class PluginEditor extends Component {
300
277
}
301
278
} ) ;
302
279
280
+ const listeners = this . createEventListeners ( ) ;
281
+
303
282
return (
304
283
< Editor
305
284
{ ...pluginProps }
306
285
{ ...this . props }
307
- onChange = { this . onChange }
286
+ { ... listeners }
308
287
handleDroppedFiles = { this . handleDroppedFiles }
309
288
handleDrop = { this . handleDrop }
310
- editorState = { this . editorState }
311
- blockRendererFn = { this . blockRendererFn }
312
- handleKeyCommand = { this . handleKeyCommand }
313
- keyBindingFn = { this . keyBindingFn }
314
- onDownArrow = { this . onDownArrow }
315
- onTab = { this . onTab }
316
- onUpArrow = { this . onUpArrow }
317
- onEscape = { this . onEscape }
318
- handleReturn = { this . handleReturn }
289
+ editorState = { this . editorState }
290
+ blockRendererFn = { this . blockRendererFn }
319
291
ref = "editor"
320
292
/>
321
293
) ;
0 commit comments