Skip to content

Commit

Permalink
add copy buttons to all code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh committed May 28, 2023
1 parent 6f675dd commit 7f3fb5c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pre code {
display: inline-block;
}
pre {
position: relative;
border-radius: 4pt;
font-size: 8pt;
background-color: rgba(255, 255, 255, 0.05);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const jmol = {
Bh: [224, 0, 56],
Hs: [230, 0, 46],
Mt: [235, 0, 38],
} as const // Record<ElementSymbol, [number, number, number]>
} as const

export const vesta = {
Ac: [112, 171, 250],
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(demos)/structure/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
let width
let height
$: href = `https://materialsproject.org/materials/${mp_id[0]}`
$: structure = structures.find((struct) => struct.id === mp_id[0])
$: structure = structures.find((struct) => struct.id === mp_id[0]) || {}
</script>
<form>
Expand Down
20 changes: 18 additions & 2 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { afterNavigate, goto } from '$app/navigation'
import { page } from '$app/stores'
import { element_data } from '$lib'
import { repository } from '$root/package.json'
import { Footer } from '$site'
import { demos } from '$site/stores'
import { CmdPalette } from 'svelte-multiselect'
import { GitHubCorner } from 'svelte-zoo'
import { CopyButton, GitHubCorner } from 'svelte-zoo'
import '../app.css'
afterNavigate(() => {
for (const node of document.querySelectorAll('pre > code')) {
// skip if <pre> already contains a button (presumably for copy)
const pre = node.parentElement
if (!pre || pre.querySelector(`button`)) continue
new CopyButton({
target: pre,
props: {
content: node.textContent ?? '',
style: 'position: absolute; top: 1ex; right: 1ex;',
},
})
}
})
const routes = Object.keys(import.meta.glob(`./**/+page.{svx,svelte,md}`)).map(
(filename) => {
const parts = filename.split(`/`).filter((part) => !part.startsWith(`(`)) // remove hidden route segments
Expand Down
2 changes: 0 additions & 2 deletions static/prism-vsc-dark-plus.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ pre[class*='language-'] {
padding: 1em;
margin: 0.5em 0;
overflow: auto;
background: #1e1e1e;
}

:not(pre) > code[class*='language-'] {
padding: 0.1em 0.3em;
border-radius: 0.3em;
color: #db4c69;
background: #f9f2f4;
}

/* Tokens */
Expand Down

0 comments on commit 7f3fb5c

Please sign in to comment.