Skip to content

Commit ce94934

Browse files
authored
Merge pull request #72 from github/allow-use-of-button-elements-with-data-md-button-style
allow use of <button> elements with `data-md-button="{style}"
2 parents e343a0b + 2c8a329 commit ce94934

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/index.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,39 @@ type Style = {
8484
}
8585

8686
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: '![', suffix: '](url)', 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

88121
class 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+
278322
class 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 {

test/test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,17 @@ describe('markdown-toolbar-element', function () {
877877

878878
assert.equal('## |title|', visualValue())
879879
})
880+
881+
it('can be triggered from a data-md-button', function () {
882+
const headerElement = document.createElement('button')
883+
headerElement.setAttribute('data-md-button', 'header-6')
884+
const toolbar = document.querySelector('markdown-toolbar')
885+
toolbar.appendChild(headerElement)
886+
setVisualValue('|title|')
887+
headerElement.click()
888+
889+
assert.equal('###### |title|', visualValue())
890+
})
880891
})
881892
})
882893
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy