CopyCode

for Reveal.js


let why = `Because we always want to copy code during our presentations, right?`

Works out of the box

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.

Setup

Basics

There are really only three steps:

  1. Install and set up CopyCode
  2. Copy your code from a code block
  3. Paste your code somewhere

Install CopyCode

Copy the copycode folder to the plugins folder of the reveal.js folder, like this: plugin/copycode. Load the script and reference it from 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:
npm install --save reveal.js-copycode

Now change it

You can change window mode, visibility, the icon use or button texts easily per element:

Make it in a window style

<pre data-cc-window="My Code Example">
    <code>
        Here is the code
    </code>
</pre>

Data attributes for the window style are: data-cc-window-title (an other way of setting the title, where values like true can be used), data-cc-window-controls, data-cc-window-controlopacity, data-cc-window-padding.

This can also be set globally. See Global options.

Change the visibility per element

Turn the button off

<pre data-cc-button="false"> // Or turn it all off, including window, with data-cc="false"
    <code>
        Here is the code
    </code>
</pre>

Or show only on hover over the code block

<pre data-cc-button="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.

Change the display of icons per element

<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.

Change the display of text per element

<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.

Global options

  1. button
  2. display
  3. text
  4. plaintextonly
  5. timeout
  6. style
  7. window
  8. tooltip
  9. iconsvg
  10. cssautoload
  11. csspath

Option 1: button

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.

Option 2: display

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.

Option 3: text

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.

Option 4: plaintextonly

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 ]
})

Option 5: timeout

The timeout option is the time in milliseconds for the "Copied!"-state to revert back to "Copy".

Reveal.initialize({
    copycode: {
        timeout: 1000
    },
    plugins: [ CopyCode ]
})

Option 6: style

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 ]
})

Option 7: window

The window option (if not false) shows styled window controls, an optional title, and the copybutton vertically aligned at the top of the codeblock. The button will then never cover any code.

Reveal.initialize({
    copycode: {
        window: {
            title: "",          // Default window title
            controls: "color",  // "color" | "light" | "dark"
            controlsOpacity: 1  // 0.0 - 1.0
            padding: "0.5rem"   // CSS size for padding around whole window
        }
    },
    plugins: [ CopyCode ]
})

All of the window options can also be set per element with data-attributes.

Option 8: tooltip

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 ]
})

Option 9: iconsvg

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 ]
})

Option 10: cssautoload

CopyCode will automatically load the CSS if this is set to true.

Reveal.initialize({
    copycode: {
        cssautoload: true
    },
    plugins: [ CopyCode ]
})

If CopyCode runs in a bundler or module environment, where you should use import for your styling, it will automatically turn off autoloading. You can still turn on autoloading, but you will need to manually add this setting like shown above.

Option 11: csspath

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.

Using the global options

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} }