Skip to content

fix: avoid applying data-md-button styles twice after the toolbar is moved - #110

Open
kwy404 wants to merge 1 commit into
github:mainfrom
kwy404:fix-toolbar-listeners-on-reconnect
Open

kwy404 wants to merge 1 commit into
github:mainfrom
kwy404:fix-toolbar-listeners-on-reconnect

Conversation

@kwy404

@kwy404 kwy404 commented Oct 1, 2026

Copy link
Copy Markdown

Root cause

MarkdownToolbarElement.connectedCallback() adds its keydown and click listeners every time the element is connected. The click listener is the same function each time, so the browser ignores the duplicate, but the keydown listener is a new keydown(applyFromToolbar) wrapper on every call. Each time the toolbar is moved in the DOM (disconnected and connected again) it gains one more keydown listener. Pressing Enter or Space on a data-md-button element then applies the style more than once, and for toggling styles like headers or bold the second run undoes the first, so the key press seems to do nothing.

Fix

Add the two listeners once in the constructor, the same way MarkdownButtonElement already does. They live on the element itself, so they do not need to be removed or added again on disconnect and reconnect.

Test

Added a test that adds a <button data-md-button="header-6">, moves the toolbar, and presses Enter on the button. Without the fix the textarea ends up as |title| (the header is added and then removed) and the test fails; with the fix it is ###### |title| and the test passes. npm test (lint, build and the full Karma suite) passes: 144 tests.

@kwy404
kwy404 requested a review from a team as a code owner October 1, 2026 16:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant