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
@siddharthkp
siddharthkp requested a balanced review from Copilot October 7, 2026 13:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused lifecycle fix is consistent with existing button behavior and includes appropriate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Moves toolbar event listener registration to construction time, preventing duplicate keyboard handling after DOM moves.

Changes:

  • Registers keyboard and click listeners once in the constructor.
  • Adds regression coverage for moved toolbars.
File Description
src/​index.ts Prevents duplicate event listener registration.
test/​test.js Tests keyboard activation after moving the toolbar.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

2 participants