@@ -320,20 +320,43 @@ function applyFromToolbar(event: Event) {
320
320
applyStyle ( target , style )
321
321
}
322
322
323
+ function setFocusManagement ( toolbar : MarkdownToolbarElement ) {
324
+ toolbar . addEventListener ( 'keydown' , focusKeydown )
325
+ toolbar . setAttribute ( 'tabindex' , '0' )
326
+ toolbar . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
327
+ }
328
+
329
+ function unsetFocusManagement ( toolbar : MarkdownToolbarElement ) {
330
+ toolbar . removeEventListener ( 'keydown' , focusKeydown )
331
+ toolbar . removeAttribute ( 'tabindex' )
332
+ toolbar . removeEventListener ( 'focus' , onToolbarFocus )
333
+ }
334
+
323
335
class MarkdownToolbarElement extends HTMLElement {
336
+ static observedAttributes = [ 'data-no-focus' ]
337
+
324
338
connectedCallback ( ) : void {
325
339
if ( ! this . hasAttribute ( 'role' ) ) {
326
340
this . setAttribute ( 'role' , 'toolbar' )
327
341
}
328
- this . addEventListener ( 'keydown' , focusKeydown )
329
- this . setAttribute ( 'tabindex' , '0' )
330
- this . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
342
+ if ( ! this . hasAttribute ( 'data-no-focus' ) ) {
343
+ setFocusManagement ( this )
344
+ }
331
345
this . addEventListener ( 'keydown' , keydown ( applyFromToolbar ) )
332
346
this . addEventListener ( 'click' , applyFromToolbar )
333
347
}
334
348
349
+ attributeChangedCallback ( name : string , oldValue : string , newValue : string ) : void {
350
+ if ( name !== 'data-no-focus' ) return
351
+ if ( newValue === null ) {
352
+ setFocusManagement ( this )
353
+ } else {
354
+ unsetFocusManagement ( this )
355
+ }
356
+ }
357
+
335
358
disconnectedCallback ( ) : void {
336
- this . removeEventListener ( 'keydown' , focusKeydown )
359
+ unsetFocusManagement ( this )
337
360
}
338
361
339
362
get field ( ) : HTMLTextAreaElement | null {
@@ -350,7 +373,6 @@ class MarkdownToolbarElement extends HTMLElement {
350
373
351
374
function onToolbarFocus ( { target} : FocusEvent ) {
352
375
if ( ! ( target instanceof Element ) ) return
353
- if ( target . hasAttribute ( 'data-no-focus' ) ) return
354
376
target . removeAttribute ( 'tabindex' )
355
377
let tabindex = '0'
356
378
for ( const button of getButtons ( target ) ) {
@@ -367,7 +389,6 @@ function focusKeydown(event: KeyboardEvent) {
367
389
if ( key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End' ) return
368
390
const toolbar = event . currentTarget
369
391
if ( ! ( toolbar instanceof HTMLElement ) ) return
370
- if ( toolbar . hasAttribute ( 'data-no-focus' ) ) return
371
392
const buttons = getButtons ( toolbar )
372
393
const index = buttons . indexOf ( event . target as HTMLElement )
373
394
const length = buttons . length
0 commit comments