Skip to content

Fix: Ctrl+C respects text selection instead of always copying full markdown#36

Merged
ThisIs-Developer merged 2 commits intomainfrom
copilot/fix-ctrl-c-shortcut-bug
Mar 5, 2026
Merged

Fix: Ctrl+C respects text selection instead of always copying full markdown#36
ThisIs-Developer merged 2 commits intomainfrom
copilot/fix-ctrl-c-shortcut-bug

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

The global keydown handler unconditionally intercepted Ctrl+C and copied the entire markdown content, breaking standard copy behavior whenever a user had text selected in the editor or preview pane.

Changes

  • script.js — keydown handler: Guard the full-markdown copy behind two conditions; only trigger when no text is selected and focus is not in a text control (<textarea>/<input>).
// Before
if ((e.ctrlKey || e.metaKey) && e.key === "c") {
  e.preventDefault();
  copyMarkdownButton.click();
}

// After
if ((e.ctrlKey || e.metaKey) && e.key === "c") {
  const activeEl = document.activeElement;
  const isTextControl = activeEl && (activeEl.tagName === "TEXTAREA" || activeEl.tagName === "INPUT");
  const hasSelection = window.getSelection && window.getSelection().toString().trim().length > 0;
  if (!isTextControl && !hasSelection) {
    e.preventDefault();
    copyMarkdownButton.click();
  }
}

Behavior matrix:

Context Ctrl+C result
Text selected in editor or preview Browser copies selection (native behavior restored)
Focused in <textarea> / <input> Browser copies selection (native behavior restored)
No selection, no text-control focus Copies full markdown (existing behavior preserved)
Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: Ctrl+C global shortcut copies full markdown instead of selected text</issue_title>
<issue_description>Description
The application currently intercepts the Ctrl+C (copy) keyboard shortcut at the document level and prevents the default browser behavior:

  • In script.js, a keydown handler uses e.preventDefault() and invokes the copy-markdown button when Ctrl+C is pressed. (code link)
  • The copy handler copies markdownEditor.value (the entire markdown content) via copyToClipboard, so pressing Ctrl+C always copies the full markdown instead of just the user's selection.

Expected behavior

  • When the user has a text selection (in the preview or editor), Ctrl+C should copy only the selected text (standard browser behavior).
  • The full markdown copy action should only run when there is no selection, or via a separate shortcut (e.g., Ctrl+Shift+C).

Suggested fix

  • Modify the keydown handler to check for a selection (window.getSelection().toString()) and/or whether the focused element is a text control before calling e.preventDefault() and triggering the copy action.
  • Alternatively, use a different shortcut for "copy full markdown" to avoid colliding with Ctrl+C.

This issue can negatively affect usability for users who frequently copy selected text from the editor or preview.

References

</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

@vercel
Copy link

vercel bot commented Mar 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
markdown-viwer Ready Ready Preview, Comment Mar 4, 2026 1:46pm

Co-authored-by: ThisIs-Developer <109382325+ThisIs-Developer@users.noreply.github.com>
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 4, 2026

Deploying markdown-viewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: ae80a33
Status: ✅  Deploy successful!
Preview URL: https://5e95d16e.markdown-viewer.pages.dev
Branch Preview URL: https://copilot-fix-ctrl-c-shortcut.markdown-viewer.pages.dev

View logs

Copilot AI changed the title [WIP] Fix Ctrl+C shortcut to copy selected text only Fix: Ctrl+C respects text selection instead of always copying full markdown Mar 4, 2026
@cloudflare-workers-and-pages
Copy link

Deploying markdownviewer with  Cloudflare Pages  Cloudflare Pages

Latest commit: ae80a33
Status: ✅  Deploy successful!
Preview URL: https://9c256769.markdownviewer.pages.dev
Branch Preview URL: https://copilot-fix-ctrl-c-shortcut.markdownviewer.pages.dev

View logs

@ThisIs-Developer ThisIs-Developer marked this pull request as ready for review March 5, 2026 03:09
@ThisIs-Developer ThisIs-Developer merged commit c6df362 into main Mar 5, 2026
6 checks passed
@ThisIs-Developer ThisIs-Developer deleted the copilot/fix-ctrl-c-shortcut-bug branch March 5, 2026 03:16
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.

Bug: Ctrl+C global shortcut copies full markdown instead of selected text

2 participants