1
1
/* @flow strict */
2
2
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
+
3
12
function keydown ( fn : KeyboardEventHandler ) : KeyboardEventHandler {
4
13
return function ( event : KeyboardEvent ) {
5
14
if ( event . key === ' ' || event . key === 'Enter' ) {
@@ -31,7 +40,6 @@ class MarkdownButtonElement extends HTMLElement {
31
40
if ( ! this . hasAttribute ( 'role' ) ) {
32
41
this . setAttribute ( 'role' , 'button' )
33
42
}
34
- this . setAttribute ( 'data-md-button' , '' )
35
43
}
36
44
37
45
click ( ) {
@@ -60,6 +68,7 @@ class MarkdownHeaderButtonElement extends MarkdownButtonElement {
60
68
if ( ! window . customElements . get ( 'md-header' ) ) {
61
69
window . MarkdownHeaderButtonElement = MarkdownHeaderButtonElement
62
70
window . customElements . define ( 'md-header' , MarkdownHeaderButtonElement )
71
+ buttonSelectors . push ( 'md-header' )
63
72
}
64
73
65
74
class MarkdownBoldButtonElement extends MarkdownButtonElement {
@@ -77,6 +86,7 @@ class MarkdownBoldButtonElement extends MarkdownButtonElement {
77
86
if ( ! window . customElements . get ( 'md-bold' ) ) {
78
87
window . MarkdownBoldButtonElement = MarkdownBoldButtonElement
79
88
window . customElements . define ( 'md-bold' , MarkdownBoldButtonElement )
89
+ buttonSelectors . push ( 'md-bold' )
80
90
}
81
91
82
92
class MarkdownItalicButtonElement extends MarkdownButtonElement {
@@ -94,6 +104,7 @@ class MarkdownItalicButtonElement extends MarkdownButtonElement {
94
104
if ( ! window . customElements . get ( 'md-italic' ) ) {
95
105
window . MarkdownItalicButtonElement = MarkdownItalicButtonElement
96
106
window . customElements . define ( 'md-italic' , MarkdownItalicButtonElement )
107
+ buttonSelectors . push ( 'md-italic' )
97
108
}
98
109
99
110
class MarkdownQuoteButtonElement extends MarkdownButtonElement {
@@ -106,6 +117,7 @@ class MarkdownQuoteButtonElement extends MarkdownButtonElement {
106
117
if ( ! window . customElements . get ( 'md-quote' ) ) {
107
118
window . MarkdownQuoteButtonElement = MarkdownQuoteButtonElement
108
119
window . customElements . define ( 'md-quote' , MarkdownQuoteButtonElement )
120
+ buttonSelectors . push ( 'md-quote' )
109
121
}
110
122
111
123
class MarkdownCodeButtonElement extends MarkdownButtonElement {
@@ -118,6 +130,7 @@ class MarkdownCodeButtonElement extends MarkdownButtonElement {
118
130
if ( ! window . customElements . get ( 'md-code' ) ) {
119
131
window . MarkdownCodeButtonElement = MarkdownCodeButtonElement
120
132
window . customElements . define ( 'md-code' , MarkdownCodeButtonElement )
133
+ buttonSelectors . push ( 'md-code' )
121
134
}
122
135
123
136
class MarkdownLinkButtonElement extends MarkdownButtonElement {
@@ -135,6 +148,7 @@ class MarkdownLinkButtonElement extends MarkdownButtonElement {
135
148
if ( ! window . customElements . get ( 'md-link' ) ) {
136
149
window . MarkdownLinkButtonElement = MarkdownLinkButtonElement
137
150
window . customElements . define ( 'md-link' , MarkdownLinkButtonElement )
151
+ buttonSelectors . push ( 'md-link' )
138
152
}
139
153
140
154
class MarkdownImageButtonElement extends MarkdownButtonElement {
@@ -147,6 +161,7 @@ class MarkdownImageButtonElement extends MarkdownButtonElement {
147
161
if ( ! window . customElements . get ( 'md-image' ) ) {
148
162
window . MarkdownImageButtonElement = MarkdownImageButtonElement
149
163
window . customElements . define ( 'md-image' , MarkdownImageButtonElement )
164
+ buttonSelectors . push ( 'md-image' )
150
165
}
151
166
152
167
class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
@@ -159,6 +174,7 @@ class MarkdownUnorderedListButtonElement extends MarkdownButtonElement {
159
174
if ( ! window . customElements . get ( 'md-unordered-list' ) ) {
160
175
window . MarkdownUnorderedListButtonElement = MarkdownUnorderedListButtonElement
161
176
window . customElements . define ( 'md-unordered-list' , MarkdownUnorderedListButtonElement )
177
+ buttonSelectors . push ( 'md-unordered-list' )
162
178
}
163
179
164
180
class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
@@ -171,6 +187,7 @@ class MarkdownOrderedListButtonElement extends MarkdownButtonElement {
171
187
if ( ! window . customElements . get ( 'md-ordered-list' ) ) {
172
188
window . MarkdownOrderedListButtonElement = MarkdownOrderedListButtonElement
173
189
window . customElements . define ( 'md-ordered-list' , MarkdownOrderedListButtonElement )
190
+ buttonSelectors . push ( 'md-ordered-list' )
174
191
}
175
192
176
193
class MarkdownTaskListButtonElement extends MarkdownButtonElement {
@@ -188,6 +205,7 @@ class MarkdownTaskListButtonElement extends MarkdownButtonElement {
188
205
if ( ! window . customElements . get ( 'md-task-list' ) ) {
189
206
window . MarkdownTaskListButtonElement = MarkdownTaskListButtonElement
190
207
window . customElements . define ( 'md-task-list' , MarkdownTaskListButtonElement )
208
+ buttonSelectors . push ( 'md-task-list' )
191
209
}
192
210
193
211
class MarkdownMentionButtonElement extends MarkdownButtonElement {
@@ -200,6 +218,7 @@ class MarkdownMentionButtonElement extends MarkdownButtonElement {
200
218
if ( ! window . customElements . get ( 'md-mention' ) ) {
201
219
window . MarkdownMentionButtonElement = MarkdownMentionButtonElement
202
220
window . customElements . define ( 'md-mention' , MarkdownMentionButtonElement )
221
+ buttonSelectors . push ( 'md-mention' )
203
222
}
204
223
205
224
class MarkdownRefButtonElement extends MarkdownButtonElement {
@@ -212,6 +231,7 @@ class MarkdownRefButtonElement extends MarkdownButtonElement {
212
231
if ( ! window . customElements . get ( 'md-ref' ) ) {
213
232
window . MarkdownRefButtonElement = MarkdownRefButtonElement
214
233
window . customElements . define ( 'md-ref' , MarkdownRefButtonElement )
234
+ buttonSelectors . push ( 'md-ref' )
215
235
}
216
236
217
237
const modifierKey = navigator . userAgent . match ( / M a c i n t o s h / ) ? 'Meta' : 'Control'
@@ -231,7 +251,7 @@ class MarkdownToolbarElement extends HTMLElement {
231
251
this . field . addEventListener ( 'keydown' , fn )
232
252
shortcutListeners . set ( this , fn )
233
253
}
234
- const firstTabIndex = document . querySelector ( '[data-md-button]' )
254
+ const firstTabIndex = getButtons ( this ) [ 0 ]
235
255
if ( firstTabIndex ) firstTabIndex . setAttribute ( 'tabindex' , '0' )
236
256
}
237
257
@@ -257,25 +277,23 @@ function focusKeydown(event: KeyboardEvent) {
257
277
if ( key !== 'ArrowRight' && key !== 'ArrowLeft' && key !== 'Home' && key !== 'End' ) return
258
278
const target = event . target
259
279
const toolbar = event . currentTarget
260
- if ( ! ( target instanceof HTMLElement ) ) return
261
280
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
+ }
279
297
}
280
298
281
299
const shortcutListeners = new WeakMap ( )
0 commit comments