Skip to content

Extract the clipboard write shared by the copy features - #3306

Closed
enf0rc3 wants to merge 1 commit into
mainfrom
wl/clipboard-module
Closed

Extract the clipboard write shared by the copy features#3306
enf0rc3 wants to merge 1 commit into
mainfrom
wl/clipboard-module

Conversation

@enf0rc3

@enf0rc3 enf0rc3 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Pure extraction, split out of #3303 so the heading work reviews on its own.

copy-markdown.js carries a clipboard write with two code paths: navigator.clipboard.writeText where a secure context allows it, falling back to a hidden textarea plus document.execCommand('copy') for HTTP and older browsers. The heading copy button in #3303 needs exactly the same logic, so it moves to src/scripts/modules/clipboard.js rather than becoming a second copy.

What changed

  • writeToClipboard and execCommandCopyFallback move out of the CopyMarkdown class into a module, unchanged apart from the warning prefix ([copy-md][clipboard]) and JSDoc types.
  • copy-markdown.js imports it and calls writeToClipboard(text) in place of this.writeToClipboard(text).

No behavior change: the same two paths are tried in the same order, and the return contract (true on success) is the same.

code-blocks.js has its own simpler inline version. It is bound up in a larger icon-swapping flow, so it is left alone — worth a follow-up if this module proves out.

Verification

  • pnpm build exits 0, 2697 pages, on this branch alone.
  • The extracted code is bundled into the page script ([clipboard] appears in dist/docs/_astro/Default.astro_astro_type_script_index_0_lang.*.js), so the import resolves rather than being tree-shaken away.
  • prettier --check and cspell on both files: clean.

Stacked on #3301.

🤖 Generated with Claude Code

copy-markdown.js carries a secure-context check plus an execCommand fallback
for browsers where navigator.clipboard is unavailable. The heading copy button
needs the same logic, so it moves to its own module rather than becoming a
second copy.

Behavior is unchanged: the same two code paths in the same order, with the
warning prefix generalized from [copy-md] to [clipboard].

code-blocks.js still has its own simpler inline version. It is bound up in a
larger icon-swapping flow, so it is left alone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@team-marketing-branch-protections

Copy link
Copy Markdown

Pull request environment is available at https://stoctodocspr3306.z22.web.core.windows.net.

You can view the ephemeral environment status in Octopus Deploy.

This environment will be automatically deprovisioned when the pull request is closed, or after 7 days of inactivity.

@enf0rc3

enf0rc3 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #3303. navigator.clipboard.writeText has been Baseline since 2020, so the shared module and its execCommand fallback are unnecessary - the four-line write is inlined at the call site instead.

@enf0rc3 enf0rc3 closed this Aug 5, 2026
@enf0rc3
enf0rc3 deleted the wl/clipboard-module branch August 5, 2026 22: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.

2 participants