let why = `Because we always want to copy code
during our presentations, right?`
const http = require("http");
http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
}).listen(8081);
A 'Copy' button is automatically added to any <pre>
code block.
There are really only three steps:
Copy the copycode
folder to the plugins folder of the reveal.js folder, like this: plugin/copycode
. Now load the script and make a reference to it in the Reveal plugin options:
<script src="plugin/copycode/copycode.js"></script>
<script>
Reveal.initialize({
plugins: [ CopyCode ]
});
</script>
This plugin is also published to, and can be installed from, npm.
You can change the visibility, the icon use or button texts easily per element:
Turn it off
<pre data-cc="false">
<code>
Here is the code
</code>
</pre>
Or show only on hover over the code block
<pre data-cc="hover">
<code>
Here is the code
</code>
</pre>
This can also be set globally. See Global options. Hover-only codeblocks will always show the copy button on touch devices.
<pre data-cc-display="icons">
<code>
Here is the code
</code>
</pre>
<pre data-cc-display="both">
<code>
Here is the code
</code>
</pre>
This can also be set globally. See Global options.
<pre data-cc-copy="Copy HTML" data-cc-copied="Done">
<code>
Here is the code
</code>
</pre>
This can also be set globally. See Global options.
The button
option is to show the button, always or only on hover.
Reveal.initialize({
copycode: {
button: "always" // "always"|"hover"
},
plugins: [ CopyCode ]
})
This can also be set per element with data-attributes.
The display
option is to show text in the button, icons, or both.
Reveal.initialize({
copycode: {
display: "text" // "text"|"icons"|"both"
},
plugins: [ CopyCode ]
})
This can also be set per element with data-attributes.
The text
option is an object that contains the texts for the 'copy' and 'copied' state.
Reveal.initialize({
copycode: {
text: {
copy: "Copy",
copied: "Copied!"
}
},
plugins: [ CopyCode ]
This can also be set per element with data-attributes.
The plaintextonly
option can be set to false to allow copying of rich text and styles.
Reveal.initialize({
copycode: {
plaintextonly: true // true|false
},
plugins: [ CopyCode ]
})
The timeout
option is the time in milliseconds for the "Copied!"-state to revert back to "Copy".
Reveal.initialize({
copycode: {
timeout: 1000
},
plugins: [ CopyCode ]
})
The style
option is an object that contains settings for background colors, colors, sizing, spacing and border styles.
Reveal.initialize({
copycode: {
style: {
copybg: "orange", // Any CSS color, hex, named etc.
copiedbg: "green",
copycolor: "black",
copiedcolor: "white",
copyborder: "", // e.g. "1px solid blue"
copiedborder: "",
scale: 1,
offset: 0, // in em units
radius: 0 // in em units
}
},
plugins: [ CopyCode ]
})
The tooltip
option makes sure that the 'copied' text is shown in a tooltip, when an icon-only button display is used.
Reveal.initialize({
copycode: {
tooltip: true
},
plugins: [ CopyCode ]
})
The iconsvg
option is an object with placeholders for SVG icons for the 'copy' and 'copied' state. If left empty, it will use the default icons.
Reveal.initialize({
copycode: {
iconsvg: {
copy: '', // User can paste <svg>…</svg> code here
copied: '' // User can paste <svg>…</svg> code here
}
},
plugins: [ CopyCode ]
})
The csspath
option can be used when the you want to use your own stylesheet, instead of the provided one.
Reveal.initialize({
copycode: {
csspath: ''
},
plugins: [ CopyCode ]
})
Use this if you really want to change things that you can't override from the Reveal.js config, like the padding and so on.
The clipboardjspath
option is the path to ClipboardJS. When nothing is filled in here, it will automatically load from a CDN.
Reveal.initialize({
copycode: {
clipboardjspath: ''
},
plugins: [ CopyCode ]
})
The global options make CopyCode very customizable. For example, to make all buttons look more like the standard GitHub copy-buttons, you can tweak the configuration like this:
copycode: { timeout: 1200, button: "hover", display: "icons", style: { copybg: "rgba(255,255,255,128)", copiedbg: "white", copyborder: "2px solid gray", copiedborder: "2px solid green", copiedcolor: "green", offset: 0.5, radius: 0.2} }