Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ type Style = {
}

const styles = new WeakMap<Element, Style>()
const manualStyles = {
'header-1': {prefix: '# '},
'header-2': {prefix: '## '},
'header-3': {prefix: '### '},
'header-4': {prefix: '#### '},
'header-5': {prefix: '##### '},
'header-6': {prefix: '###### '},
bold: {prefix: '**', suffix: '**', trimFirst: true},
italic: {prefix: '_', suffix: '_', trimFirst: true},
quote: {prefix: '> ', multiline: true, surroundWithNewlines: true},
code: {
prefix: '`',
suffix: '`',
blockPrefix: '```',
blockSuffix: '```'
},
link: {prefix: '[', suffix: '](url)', replaceNext: 'url', scanFor: 'https?://'},
image: {prefix: '![', suffix: '](url)', replaceNext: 'url', scanFor: 'https?://'},
'unordered-list': {
prefix: '- ',
multiline: true,
unorderedList: true
},
'ordered-list': {
prefix: '1. ',
multiline: true,
orderedList: true
},
'task-list': {prefix: '- [ ] ', multiline: true, surroundWithNewlines: true},
mention: {prefix: '@', prefixSpace: true},
ref: {prefix: '#', prefixSpace: true},
strikethrough: {prefix: '~~', suffix: '~~', trimFirst: true}
} as const

class MarkdownButtonElement extends HTMLElement {
constructor() {
Expand Down Expand Up @@ -275,6 +308,17 @@ if (!window.customElements.get('md-strikethrough')) {
window.customElements.define('md-strikethrough', MarkdownStrikethroughButtonElement)
}

function applyFromToolbar(event: Event) {
const {target, currentTarget} = event
if (!(target instanceof HTMLElement)) return
const mdButton = target.closest('[data-md-button]')
if (!mdButton || mdButton.closest('markdown-toolbar') !== currentTarget) return
const mdButtonStyle = target.getAttribute('data-md-button')
const style = manualStyles[mdButtonStyle as keyof typeof manualStyles]
if (!style) return
applyStyle(target, style)
}

class MarkdownToolbarElement extends HTMLElement {
connectedCallback(): void {
if (!this.hasAttribute('role')) {
Expand All @@ -283,6 +327,8 @@ class MarkdownToolbarElement extends HTMLElement {
this.addEventListener('keydown', focusKeydown)
this.setAttribute('tabindex', '0')
this.addEventListener('focus', onToolbarFocus, {once: true})
this.addEventListener('keydown', keydown(applyFromToolbar))
this.addEventListener('click', applyFromToolbar)
}

disconnectedCallback(): void {
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,17 @@ describe('markdown-toolbar-element', function () {

assert.equal('## |title|', visualValue())
})

it('can be triggered from a data-md-button', function () {
const headerElement = document.createElement('button')
headerElement.setAttribute('data-md-button', 'header-6')
const toolbar = document.querySelector('markdown-toolbar')
toolbar.appendChild(headerElement)
setVisualValue('|title|')
headerElement.click()

assert.equal('###### |title|', visualValue())
})
})
})
})