This repository was archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
feat: Copy code button #175
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/site-kit': minor | ||
| --- | ||
|
|
||
| Copy code button |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/site-kit/src/lib/actions/copy-code-descendants.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import DocsCopyCodeButton from '../docs/DocsCopyCodeButton.svelte'; | ||
| import { page } from '$app/stores'; | ||
|
|
||
| const map = new WeakMap(); | ||
|
|
||
| /** @type {import('svelte/action').Action} */ | ||
| export const copy_code_descendants = (node) => { | ||
| /** @type {NodeListOf<Element>} */ | ||
| let code_blocks; | ||
|
|
||
| function update() { | ||
| code_blocks = node.querySelectorAll('.shiki'); | ||
|
|
||
| // Add a button to each code block | ||
| for (const block of code_blocks) { | ||
| const parent_class = block.parentElement?.classList.toString() ?? ''; | ||
|
|
||
| // Exclude the ts-block properties and stuff | ||
| if (/ts-block/.test(parent_class)) continue; | ||
|
|
||
| const code = block.querySelector('code')?.innerText ?? ''; | ||
| if (!code) continue; | ||
|
|
||
| // This is to make sure that snippets with title get the button on their heading area | ||
| const target = /code-block/.test(parent_class) ? block.parentElement : block; | ||
| if (!target) continue; | ||
|
|
||
| map.set( | ||
| block, | ||
| new DocsCopyCodeButton({ | ||
| target: target, | ||
| props: { | ||
| code | ||
| } | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| function destroy() { | ||
| for (const block of code_blocks) { | ||
| map.get(block)?.$destroy(); | ||
| map.delete(block); | ||
| } | ||
| } | ||
|
|
||
| // Page changed. Update again | ||
| const unsubscribe = page.subscribe(update); | ||
|
|
||
| return { | ||
| update, | ||
| destroy() { | ||
| destroy(); | ||
| unsubscribe(); | ||
| } | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export { click_outside } from './click-outside.js'; | ||
| export { copy_code_descendants } from './copy-code-descendants.js'; | ||
| export { focus_outside } from './focus-outside.js'; | ||
| export { focusable_children, trap } from './focus.js'; | ||
| export { root_scroll } from './root-scroll.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <script> | ||
| import Icon from '$lib/components/Icon.svelte'; | ||
| import { cubicOut } from 'svelte/easing'; | ||
| import { fade } from 'svelte/transition'; | ||
|
|
||
| /** @type {string} */ | ||
| export let code; | ||
|
|
||
| let copying = false; | ||
|
|
||
| function copy() { | ||
| try { | ||
| navigator.clipboard.writeText(code); | ||
| } catch { | ||
| /** | ||
| * This is the fallback deprecated way of copying text to the clipboard. Only runs if it can't find the clipboard API. | ||
| * Taken from https://github.com/ghostdevv/svelte-copy/blob/main/src/lib/copy.ts | ||
| */ | ||
| const element = document.createElement('input'); | ||
|
|
||
| element.type = 'text'; | ||
| element.disabled = true; | ||
|
|
||
| element.style.cssText = `position: fixed;z-index: -100;pointer-events: none;opacity: 0;`; | ||
|
|
||
| element.value = code; | ||
|
|
||
| document.body.appendChild(element); | ||
|
|
||
| element.click(); | ||
| element.select(); | ||
| document.execCommand('copy'); | ||
|
|
||
| document.body.removeChild(element); | ||
| } finally { | ||
| copying = true; | ||
|
|
||
| setTimeout(() => { | ||
| copying = false; | ||
| }, 1000); | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <button id="copy-to-clipboard-button" on:click={copy}> | ||
| {#if copying} | ||
| <span transition:fade={{ easing: cubicOut, duration: 400 }}> | ||
| <Icon name="copy-to-clipboard-filled" /> | ||
| </span> | ||
| {:else} | ||
| <span transition:fade={{ easing: cubicOut, duration: 400 }}> | ||
| <Icon name="copy-to-clipboard-empty" /> | ||
| </span> | ||
| {/if} | ||
| </button> | ||
|
|
||
| <style> | ||
| :global(.code-block #copy-to-clipboard-button) { | ||
| top: 5px; | ||
| } | ||
|
|
||
| button { | ||
| position: absolute; | ||
| top: 1rem; | ||
| right: 1rem; | ||
|
|
||
| display: grid; | ||
| place-items: center; | ||
| grid-template-columns: 1fr; | ||
| grid-template-rows: 1fr; | ||
|
|
||
| opacity: 0.8; | ||
|
|
||
| height: 2.4rem; | ||
| width: 2.4rem; | ||
|
|
||
| overflow: hidden; | ||
|
|
||
| transition: opacity 0.1s ease-in-out; | ||
| } | ||
|
|
||
| button:hover { | ||
| opacity: 1; | ||
| } | ||
|
|
||
| span { | ||
| grid-column: 1 / span 1; | ||
| grid-row: 1 / span 1; | ||
| } | ||
|
|
||
| button :global(svg) { | ||
| stroke-width: 0 !important; | ||
| } | ||
| </style> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PuruVJ this returns
Promise, needs async catch I believe.ref: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText#return_value