11/* @flow strict */
22
3+ const buttonSelectors = [ '[data-md-button]' ]
4+ function getButtons ( toolbar : Element ) {
5+ const els = [ ]
6+ for ( const button of toolbar . querySelectorAll ( buttonSelectors . join ( ', ' ) ) ) {
7+ if ( button . closest ( 'markdown-toolbar' ) === toolbar ) els . push ( button )
8+ }
9+ return els
10+ }
11+
312function keydown ( fn : KeyboardEventHandler ) : KeyboardEventHandler {
413 return function ( event : KeyboardEvent ) {
514 if ( event . key === ' ' || event . key === 'Enter' ) {
@@ -31,7 +40,6 @@ class MarkdownButtonElement extends HTMLElement {
3140 if ( ! this . hasAttribute ( 'role' ) ) {
3241 this . setAttribute ( 'role' , 'button' )
3342 }
34- this . setAttribute ( 'data-md-button' , '' )
3543 }
3644
3745 click ( ) {
@@ -60,6 +68,7 @@ class MarkdownHeaderButtonElement extends MarkdownButtonElement {
6068if ( ! window . customElements . get ( 'md-header' ) ) {
6169 window . MarkdownHeaderButtonElement = MarkdownHeaderButtonElement
6270 window . customElements . define ( 'md-header' , MarkdownHeaderButtonElement )
71+ buttonSelectors . push ( 'md-header' )
6372}
6473
6574class MarkdownBoldButtonElement extends MarkdownButtonElement {
@@ -77,6 +86,7 @@ class MarkdownBoldButtonElement extends MarkdownButtonElement {
7786if ( ! window . customElements . get ( 'md-bold' ) ) {
7887 window . MarkdownBoldButtonElement = MarkdownBoldButtonElement
7988 window . customElements . define ( 'md-bold' , MarkdownBoldButtonElement )
89+ buttonSelectors . push ( 'md-bold' )
8090}
8191
8292class MarkdownItalicButtonElement extends MarkdownButtonElement {
@@ -94,6 +104,7 @@ class MarkdownItalicButtonElement extends MarkdownButtonElement {
94104if ( ! window . customElements . get ( 'md-italic' ) ) {
95105 window . MarkdownItalicButtonElement = MarkdownItalicButtonElement
96106 window . customElements . define ( 'md-italic' , MarkdownItalicButtonElement )
107+ buttonSelectors . push ( 'md-italic' )
97108}
98109
99110class MarkdownQuoteButtonElement extends MarkdownButtonElement {
@@ -106,6 +117,7 @@ class MarkdownQuoteButtonElement extends MarkdownButtonElement {
106117if ( ! window . customElements . get ( 'md-quote' ) ) {
107118 window . MarkdownQuoteButtonElement = MarkdownQuoteButtonElement
108119 window . customElements . define ( 'md-quote' , MarkdownQuoteButtonElement )
120+ buttonSelectors . push ( 'md-quote' )
109121}
110122
111123class MarkdownCodeButtonElement extends MarkdownButtonElement {
@@ -118,6 +130,7 @@ class MarkdownCodeButtonElement extends MarkdownButtonElement {
118130if ( ! window . customElements . get ( 'md-code' ) ) {
119131 window . MarkdownCodeButtonElement = MarkdownCodeButtonElement
120132 window . customElements . define ( 'md-code' , MarkdownCodeButtonElement )
133+ buttonSelectors . push ( 'md-code' )
121134}
122135
123136class MarkdownLinkButtonElement extends MarkdownButtonElement {
@@ -135,6 +148,7 @@ class MarkdownLinkButtonElement extends MarkdownButtonElement {
135148if ( ! window . customElements . get ( 'md-link' ) ) {
136149 window . MarkdownLinkButtonElement = MarkdownLinkButtonElement
137150 window . customElements . define ( 'md-link' , MarkdownLinkButtonElement )
151+ buttonSelectors . push ( 'md-link' )
138152}
139153
140154class MarkdownImageButtonElement extends MarkdownButtonElement {
@@ -147,6 +161,7 @@ class MarkdownImageButtonElement extends MarkdownButtonElement {
147161if ( ! window . customElements . get ( 'md-image' ) ) {
148162 window . MarkdownImageButtonElement = MarkdownImageButtonElement
149163 window . customElements . define ( 'md-image' , MarkdownImageButtonElement )
164+ buttonSelectors . push ( 'md-image' )
150165}
151166
152167class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
@@ -159,6 +174,7 @@ class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
159174if ( ! window . customElements . get ( 'md-unordered-list' ) ) {
160175 window . MarkdownUnorderedListButtonElement = MarkdownUnorderedListButtonElement
161176 window . customElements . define ( 'md-unordered-list' , MarkdownUnorderedListButtonElement )
177+ buttonSelectors . push ( 'md-unordered-list' )
162178}
163179
164180class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
@@ -171,6 +187,7 @@ class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
171187if ( ! window . customElements . get ( 'md-ordered-list' ) ) {
172188 window . MarkdownOrderedListButtonElement = MarkdownOrderedListButtonElement
173189 window . customElements . define ( 'md-ordered-list' , MarkdownOrderedListButtonElement )
190+ buttonSelectors . push ( 'md-ordered-list' )
174191}
175192
176193class MarkdownTaskListButtonElement extends MarkdownButtonElement {
@@ -188,6 +205,7 @@ class MarkdownTaskListButtonElement extends MarkdownButtonElement {
188205if ( ! window . customElements . get ( 'md-task-list' ) ) {
189206 window . MarkdownTaskListButtonElement = MarkdownTaskListButtonElement
190207 window . customElements . define ( 'md-task-list' , MarkdownTaskListButtonElement )
208+ buttonSelectors . push ( 'md-task-list' )
191209}
192210
193211class MarkdownMentionButtonElement extends MarkdownButtonElement {
@@ -200,6 +218,7 @@ class MarkdownMentionButtonElement extends MarkdownButtonElement {
200218if ( ! window . customElements . get ( 'md-mention' ) ) {
201219 window . MarkdownMentionButtonElement = MarkdownMentionButtonElement
202220 window . customElements . define ( 'md-mention' , MarkdownMentionButtonElement )
221+ buttonSelectors . push ( 'md-mention' )
203222}
204223
205224class MarkdownRefButtonElement extends MarkdownButtonElement {
@@ -212,6 +231,7 @@ class MarkdownRefButtonElement extends MarkdownButtonElement {
212231if ( ! window . customElements . get ( 'md-ref' ) ) {
213232 window . MarkdownRefButtonElement = MarkdownRefButtonElement
214233 window . customElements . define ( 'md-ref' , MarkdownRefButtonElement )
234+ buttonSelectors . push ( 'md-ref' )
215235}
216236
217237const modifierKey = navigator . userAgent . match ( / M a c i n t o s h / ) ? 'Meta' : 'Control'
@@ -231,7 +251,7 @@ class MarkdownToolbarElement extends HTMLElement {
231251 this . field . addEventListener ( 'keydown' , fn )
232252 shortcutListeners . set ( this , fn )
233253 }
234- const firstTabIndex = document . querySelector ( '[data-md-button]' )
254+ const firstTabIndex = getButtons ( this ) [ 0 ]
235255 if ( firstTabIndex ) firstTabIndex . setAttribute ( 'tabindex' , '0' )
236256 }
237257
@@ -257,25 +277,23 @@ function focusKeydown(event: KeyboardEvent) {
257277 if ( key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End' ) return
258278 const target = event . target
259279 const toolbar = event . currentTarget
260- if ( ! ( target instanceof HTMLElement ) ) return
261280 if ( ! ( toolbar instanceof HTMLElement ) ) return
262- if ( ! target . hasAttribute ( 'data-md-button' ) ) return
263- if ( target . closest ( 'markdown-toolbar' ) !== toolbar ) return
264-
265- const buttons = [ ]
266- for ( const button of toolbar . querySelectorAll ( '[data-md-button]' ) ) {
267- button . setAttribute ( 'tabindex' , '-1' )
268- buttons . push ( button )
269- }
270- let i = 0
271- if ( key === 'ArrowLeft' ) i = buttons . indexOf ( target ) - 1
272- if ( key === 'ArrowRight' ) i = buttons . indexOf ( target ) + 1
273- if ( key === 'End' ) i = buttons . length - 1
274- if ( i < 0 ) i = buttons . length - 1
275- if ( i > buttons . length - 1 ) i = 0
276-
277- buttons [ i ] . setAttribute ( 'tabindex' , '0' )
278- buttons [ i ] . focus ( )
281+ const buttons = getButtons ( toolbar )
282+ if ( buttons . indexOf ( event . target ) === - 1 ) return
283+
284+ let n = 0
285+ if ( key === 'ArrowLeft' ) n = buttons . indexOf ( target ) - 1
286+ if ( key === 'ArrowRight' ) n = buttons . indexOf ( target ) + 1
287+ if ( key === 'End' ) n = buttons . length - 1
288+ if ( n < 0 ) n = buttons . length - 1
289+ if ( n > buttons . length - 1 ) n = 0
290+
291+ for ( let i = 0 ; i < buttons . length ; i += 1 ) {
292+ buttons [ i ] . setAttribute ( 'tabindex' , i === n ? '0' : '-1' )
293+ if ( i === n ) {
294+ buttons [ i ] . focus ( )
295+ }
296+ }
279297}
280298
281299const shortcutListeners = new WeakMap ( )
0 commit comments