@@ -84,6 +84,39 @@ type Style = {
84
84
}
85
85
86
86
const styles = new WeakMap < Element , Style > ( )
87
+ const manualStyles = {
88
+ 'header-1' : { prefix : '# ' } ,
89
+ 'header-2' : { prefix : '## ' } ,
90
+ 'header-3' : { prefix : '### ' } ,
91
+ 'header-4' : { prefix : '#### ' } ,
92
+ 'header-5' : { prefix : '##### ' } ,
93
+ 'header-6' : { prefix : '###### ' } ,
94
+ bold : { prefix : '**' , suffix : '**' , trimFirst : true } ,
95
+ italic : { prefix : '_' , suffix : '_' , trimFirst : true } ,
96
+ quote : { prefix : '> ' , multiline : true , surroundWithNewlines : true } ,
97
+ code : {
98
+ prefix : '`' ,
99
+ suffix : '`' ,
100
+ blockPrefix : '```' ,
101
+ blockSuffix : '```'
102
+ } ,
103
+ link : { prefix : '[' , suffix : '](url)' , replaceNext : 'url' , scanFor : 'https?://' } ,
104
+ image : { prefix : '' , replaceNext : 'url' , scanFor : 'https?://' } ,
105
+ 'unordered-list' : {
106
+ prefix : '- ' ,
107
+ multiline : true ,
108
+ unorderedList : true
109
+ } ,
110
+ 'ordered-list' : {
111
+ prefix : '1. ' ,
112
+ multiline : true ,
113
+ orderedList : true
114
+ } ,
115
+ 'task-list' : { prefix : '- [ ] ' , multiline : true , surroundWithNewlines : true } ,
116
+ mention : { prefix : '@' , prefixSpace : true } ,
117
+ ref : { prefix : '#' , prefixSpace : true } ,
118
+ strikethrough : { prefix : '~~' , suffix : '~~' , trimFirst : true }
119
+ } as const
87
120
88
121
class MarkdownButtonElement extends HTMLElement {
89
122
constructor ( ) {
@@ -275,6 +308,17 @@ if (!window.customElements.get('md-strikethrough')) {
275
308
window . customElements . define ( 'md-strikethrough' , MarkdownStrikethroughButtonElement )
276
309
}
277
310
311
+ function applyFromToolbar ( event : Event ) {
312
+ const { target, currentTarget} = event
313
+ if ( ! ( target instanceof HTMLElement ) ) return
314
+ const mdButton = target . closest ( '[data-md-button]' )
315
+ if ( ! mdButton || mdButton . closest ( 'markdown-toolbar' ) !== currentTarget ) return
316
+ const mdButtonStyle = target . getAttribute ( 'data-md-button' )
317
+ const style = manualStyles [ mdButtonStyle as keyof typeof manualStyles ]
318
+ if ( ! style ) return
319
+ applyStyle ( target , style )
320
+ }
321
+
278
322
class MarkdownToolbarElement extends HTMLElement {
279
323
connectedCallback ( ) : void {
280
324
if ( ! this . hasAttribute ( 'role' ) ) {
@@ -283,6 +327,8 @@ class MarkdownToolbarElement extends HTMLElement {
283
327
this . addEventListener ( 'keydown' , focusKeydown )
284
328
this . setAttribute ( 'tabindex' , '0' )
285
329
this . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
330
+ this . addEventListener ( 'keydown' , keydown ( applyFromToolbar ) )
331
+ this . addEventListener ( 'click' , applyFromToolbar )
286
332
}
287
333
288
334
disconnectedCallback ( ) : void {
0 commit comments