@@ -49,6 +49,7 @@ export default class SimpleMDEEditor extends React.PureComponent<
49
49
private elementWrapperRef : HTMLDivElement | null ;
50
50
private setElementWrapperRef : ( element : HTMLDivElement ) => void ;
51
51
private keyChange = false ;
52
+ private customEventCallbacks : SimpleMdeToCodemirror = { }
52
53
53
54
static defaultProps = {
54
55
events : { } ,
@@ -92,6 +93,8 @@ export default class SimpleMDEEditor extends React.PureComponent<
92
93
this . simpleMde ! . value ( this . props . value || "" ) ;
93
94
}
94
95
this . keyChange = false ;
96
+
97
+ this . updateCustomEvents ( this . props . events , prevProps . events ) ;
95
98
}
96
99
97
100
componentWillUnmount ( ) {
@@ -156,22 +159,37 @@ export default class SimpleMDEEditor extends React.PureComponent<
156
159
this . simpleMde . codemirror . on ( "change" , this . eventWrapper ) ;
157
160
this . simpleMde . codemirror . on ( "cursorActivity" , this . getCursor ) ;
158
161
159
- const { events } = this . props ;
160
-
161
- // Handle custom events
162
- events &&
163
- Object . entries ( events ) . forEach ( ( [ eventName , callback ] ) => {
164
- if ( eventName && callback ) {
165
- this . simpleMde &&
166
- this . simpleMde . codemirror . on (
167
- eventName as DOMEvent ,
168
- callback as any
169
- ) ;
170
- }
171
- } ) ;
162
+ this . updateCustomEvents ( this . props . events )
172
163
}
173
164
} ;
174
165
166
+ updateCustomEvents = ( events : SimpleMdeToCodemirror | undefined , prevEvents ?: SimpleMdeToCodemirror | undefined ) => {
167
+ prevEvents &&
168
+ Object . entries ( prevEvents ) . forEach ( ( [ eventName , callback ] ) => {
169
+ if ( eventName && callback && ! events ?. [ eventName as CodemirrorEvents | DOMEvent ] ) {
170
+ this . simpleMde &&
171
+ this . simpleMde . codemirror . off (
172
+ eventName as DOMEvent ,
173
+ this . customEventCallbacks [ eventName as CodemirrorEvents | DOMEvent ] as any
174
+ ) ;
175
+ }
176
+ } ) ;
177
+ events &&
178
+ Object . entries ( events ) . forEach ( ( [ eventName , callback ] ) => {
179
+ if ( eventName && callback && ! prevEvents ?. [ eventName as CodemirrorEvents | DOMEvent ] ) {
180
+ if ( this . simpleMde ) {
181
+ this . customEventCallbacks [ eventName as CodemirrorEvents | DOMEvent ] = ( instance : any ) => {
182
+ ( this . props . events ?. [ eventName as CodemirrorEvents | DOMEvent ] as any ) ?.( instance ) ;
183
+ }
184
+ this . simpleMde . codemirror . on (
185
+ eventName as DOMEvent ,
186
+ this . customEventCallbacks [ eventName as CodemirrorEvents | DOMEvent ] as any
187
+ ) ;
188
+ }
189
+ }
190
+ } ) ;
191
+ } ;
192
+
175
193
getCursor = ( ) => {
176
194
// https://codemirror.net/doc/manual.html#api_selection
177
195
if ( this . props . getLineAndCursor ) {
0 commit comments