Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Copy to clipboard" button. #37

Merged
merged 7 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ assets/assets
/resources
.python-version
.vscode
.idea

.vercel
.env
Expand Down
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@rollup/plugin-eslint": "^8.0.1",
"@rollup/plugin-node-resolve": "^13.0.0",
"@rollup/plugin-replace": "^4.0.0",
"rollup-plugin-svg-import": "^1.6.0",
"algoliasearch": "^4.9.1",
"atomic-algolia": "^0.3.19",
"autoprefixer": "^10.2.5",
Expand Down
7 changes: 7 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import eslint from "@rollup/plugin-eslint"
import { terser } from "rollup-plugin-terser"
import postcss from "rollup-plugin-postcss"
import replace from "@rollup/plugin-replace"
import svg from "rollup-plugin-svg-import"

import path from "path"

Expand Down Expand Up @@ -38,6 +39,9 @@ const dev = {
configFile: path.resolve(__dirname, "babel.config.json"),
babelHelpers: "bundled",
}),
svg({
stringify: true,
}),
],
}

Expand Down Expand Up @@ -72,6 +76,9 @@ const prod = {
babelHelpers: "bundled",
}),
terser(),
svg({
stringify: true,
}),
],
}

Expand Down
2 changes: 2 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import SelectController from "./js/controllers/select_controller"
import ModeSwitchController from "./js/controllers/mode_switch_controller"
import FlyoverController from "./js/controllers/flyover_controller"
import TabsController from "./js/controllers/tabs_controller"
import ClipboardController from "./js/controllers/clipboard_controller"

const application = Application.start()
application.register("clipboard", ClipboardController)
application.register("transition", TransitionController)
application.register("click-outside", ClickOutsideController)
application.register("menu", MenuController)
Expand Down
37 changes: 37 additions & 0 deletions src/css/_clipboard.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.clipboard-button {
position: absolute;
right: 0;
padding: 2px 7px 5px 7px;
margin: 5px;
color: rgb(14 165 233 / var(--tw-border-opacity));
--tw-border-opacity: 0.4;
background-color: rgb(186 230 253 / var(--tw-bg-opacity));
--tw-bg-opacity: 0.1;
border: 1px solid rgb(14 165 233 / var(--tw-border-opacity));
border-radius: 6px;
font-size: 0.8em;
z-index: 10;
opacity: 0.5;
transition: 0.1s;
}
.clipboard-button > svg {
fill: rgb(208 212 252 / var(--tw-text-opacity));
}
.clipboard-button:hover {
cursor: pointer;
border-color: #696969;
background-color: #e0e0e0;
}
.clipboard-button:hover > svg {
fill: #696969;
}
.clipboard-button:focus {
outline: 0;
}
.highlight {
position: relative;
}
.highlight:hover > .clipboard-button {
opacity: 1;
transition: 0.2s;
}
11 changes: 6 additions & 5 deletions src/css/app.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
@import "tailwindcss/base";
@import "tailwindcss/components";
@import "tailwindcss/utilities";
@import "./_fonts";
@import "./_base";
@import "./_components";
@import "./_content";
@import "./_fonts.css";
@import "./_base.css";
@import "./_components.css";
@import "./_content.css";
@import "./_docs.css";
@import "./_windows.css";
@import "./_highlight";
@import "./_highlight.css";
@import "./_search.css";
@import "./_clipboard.css";
28 changes: 28 additions & 0 deletions src/js/controllers/clipboard_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import checkmark from "../../../static/images/checkmark.svg"
import copyIcon from "../../../static/images/content_copy.svg"
import { Controller } from "stimulus"

export default class extends Controller {
initialize() {
document
.querySelectorAll("div.clipboard > div.highlight > pre")
.forEach((codeBlock) => {
const button = document.createElement("button")
button.className = "clipboard-button"
button.type = "button"
button.title = "Copy to clipboard"
button.innerHTML = copyIcon
button.addEventListener("click", () => {
navigator.clipboard.writeText(codeBlock.innerText).then(
() => {
button.blur()
button.innerHTML = checkmark
setTimeout(() => (button.innerHTML = copyIcon), 2000)
},
() => (button.innerHTML = "Error")
)
})
codeBlock.parentNode.insertBefore(button, codeBlock)
})
}
}
18 changes: 18 additions & 0 deletions static/images/checkmark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions static/images/content_copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions themes/poetry/layouts/docs/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@
class="bg-secondary-400 flex-shrink-0 inline-block h-2 w-2 mr-2 rounded-full dark:bg-indigo-300"
></span>
{{ end }}


<span class="flex-grow font-medium inline-block truncate">
{{ index (split .Name "|") 0 -}}
</span>
Expand All @@ -134,6 +132,7 @@
<main
id="docs"
class="min-w-0 w-full lg:px-12 lg:flex-auto lg:static lg:max-h-full lg:overflow-visible"
data-controller="clipboard"
>
<h1 class="doc-title">{{ index (split .Name "|") 0 -}}</h1>
{{ .TableOfContents }}
Expand Down
3 changes: 3 additions & 0 deletions themes/poetry/layouts/shortcodes/clipboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="clipboard">
{{ printf "%s" .Inner | markdownify }}
</div>