@@ -84,6 +84,39 @@ type Style = {
8484}
8585
8686const 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
87120
88121class MarkdownButtonElement extends HTMLElement {
89122 constructor ( ) {
@@ -275,6 +308,17 @@ if (!window.customElements.get('md-strikethrough')) {
275308 window . customElements . define ( 'md-strikethrough' , MarkdownStrikethroughButtonElement )
276309}
277310
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+
278322class MarkdownToolbarElement extends HTMLElement {
279323 connectedCallback ( ) : void {
280324 if ( ! this . hasAttribute ( 'role' ) ) {
@@ -283,6 +327,8 @@ class MarkdownToolbarElement extends HTMLElement {
283327 this . addEventListener ( 'keydown' , focusKeydown )
284328 this . setAttribute ( 'tabindex' , '0' )
285329 this . addEventListener ( 'focus' , onToolbarFocus , { once : true } )
330+ this . addEventListener ( 'keydown' , keydown ( applyFromToolbar ) )
331+ this . addEventListener ( 'click' , applyFromToolbar )
286332 }
287333
288334 disconnectedCallback ( ) : void {
0 commit comments