@@ -22,6 +22,8 @@ import type {
22
22
import type { Extension } from '@codemirror/state' ;
23
23
import type { EditorView , ViewUpdate } from '@codemirror/view' ;
24
24
25
+ type HdsCodeEditorBlurHandler = ( editor : EditorView , event : FocusEvent ) => void ;
26
+
25
27
export interface HdsCodeEditorSignature {
26
28
Args : {
27
29
Named : {
@@ -30,7 +32,7 @@ export interface HdsCodeEditorSignature {
30
32
language ?: HdsCodeEditorLanguages ;
31
33
value ?: string ;
32
34
onInput ?: ( newVal : string ) => void ;
33
- onBlur ?: ( editor : EditorView , event : FocusEvent ) => void ;
35
+ onBlur ?: HdsCodeEditorBlurHandler ;
34
36
onSetup ?: ( editor : EditorView ) => unknown ;
35
37
} ;
36
38
} ;
@@ -81,8 +83,10 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
81
83
editor ! : EditorView ;
82
84
element ! : HTMLElement ;
83
85
86
+ onBlur : HdsCodeEditorSignature [ 'Args' ] [ 'Named' ] [ 'onBlur' ] ;
84
87
onInput : HdsCodeEditorSignature [ 'Args' ] [ 'Named' ] [ 'onInput' ] ;
85
88
89
+ blurHandler ! : ( event : FocusEvent ) => void ;
86
90
observer ! : IntersectionObserver ;
87
91
88
92
constructor (
@@ -91,7 +95,13 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
91
95
) {
92
96
super ( owner , args ) ;
93
97
94
- registerDestructor ( this , ( ) => this . observer ?. disconnect ( ) ) ;
98
+ registerDestructor ( this , ( ) => {
99
+ this . observer ?. disconnect ( ) ;
100
+
101
+ if ( this . onBlur !== undefined ) {
102
+ this . element . removeEventListener ( 'blur' , this . blurHandler ) ;
103
+ }
104
+ } ) ;
95
105
}
96
106
97
107
modify (
@@ -122,6 +132,21 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
122
132
}
123
133
}
124
134
135
+ private _setupEditorBlurHandler (
136
+ element : HTMLElement ,
137
+ onBlur : HdsCodeEditorBlurHandler
138
+ ) {
139
+ const inputElement = element . querySelector ( '.cm-content' ) ;
140
+
141
+ if ( inputElement === null ) {
142
+ return ;
143
+ }
144
+
145
+ this . blurHandler = ( event : FocusEvent ) => onBlur ( this . editor , event ) ;
146
+
147
+ ( inputElement as HTMLElement ) . addEventListener ( 'blur' , this . blurHandler ) ;
148
+ }
149
+
125
150
private _setupEditorAriaAttributes (
126
151
editor : EditorView ,
127
152
{
@@ -282,11 +307,20 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
282
307
_positional : PositionalArgs < HdsCodeEditorSignature > ,
283
308
named : NamedArgs < HdsCodeEditorSignature >
284
309
) => {
285
- const { onInput, onSetup, ariaLabel, ariaLabelledBy, language, value } =
286
- named ;
310
+ const {
311
+ onBlur,
312
+ onInput,
313
+ onSetup,
314
+ ariaLabel,
315
+ ariaLabelledBy,
316
+ language,
317
+ value,
318
+ } = named ;
287
319
288
- this . element = element ;
289
320
this . onInput = onInput ;
321
+ this . onBlur = onBlur ;
322
+
323
+ this . element = element ;
290
324
291
325
const editor = await this . _createEditorTask . perform ( element , {
292
326
language,
@@ -299,6 +333,10 @@ export default class HdsCodeEditorModifier extends Modifier<HdsCodeEditorSignatu
299
333
300
334
this . editor = editor ;
301
335
336
+ if ( onBlur !== undefined ) {
337
+ this . _setupEditorBlurHandler ( element , onBlur ) ;
338
+ }
339
+
302
340
this . _setupEditorAriaAttributes ( editor , { ariaLabel, ariaLabelledBy } ) ;
303
341
304
342
onSetup ?.( this . editor ) ;
0 commit comments