Skip to content

Commit 7d80f0f

Browse files
committed
fix: avoid applying data-md-button styles twice after the toolbar is moved
1 parent 7b0de3a commit 7d80f0f

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/index.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,15 +348,19 @@ function unsetFocusManagement(toolbar: MarkdownToolbarElement) {
348348
class MarkdownToolbarElement extends HTMLElement {
349349
static observedAttributes = ['data-no-focus']
350350

351+
constructor() {
352+
super()
353+
this.addEventListener('keydown', keydown(applyFromToolbar))
354+
this.addEventListener('click', applyFromToolbar)
355+
}
356+
351357
connectedCallback(): void {
352358
if (!this.hasAttribute('role')) {
353359
this.setAttribute('role', 'toolbar')
354360
}
355361
if (!this.hasAttribute('data-no-focus')) {
356362
setFocusManagement(this)
357363
}
358-
this.addEventListener('keydown', keydown(applyFromToolbar))
359-
this.addEventListener('click', applyFromToolbar)
360364
}
361365

362366
attributeChangedCallback(name: string, oldValue: string, newValue: string): void {

‎test/test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,18 @@ describe('markdown-toolbar-element', function () {
921921

922922
assert.equal('###### |title|', visualValue())
923923
})
924+
925+
it('applies a data-md-button style once from the keyboard after the toolbar is moved', function () {
926+
const headerElement = document.createElement('button')
927+
headerElement.setAttribute('data-md-button', 'header-6')
928+
const toolbar = document.querySelector('markdown-toolbar')
929+
toolbar.appendChild(headerElement)
930+
toolbar.parentElement.append(toolbar)
931+
setVisualValue('|title|')
932+
headerElement.dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter', bubbles: true, cancelable: true}))
933+
934+
assert.equal('###### |title|', visualValue())
935+
})
924936
})
925937
})
926938
})

0 commit comments

Comments
 (0)