Skip to content

Commit 57cd281

Browse files
andymac-2Dylan-DPC
authored andcommitted
Option to display copy buttons. (rust-lang#1050)
* Option to display copy buttons. - Added field to playpen data structure - Communicate through window.playpen_copyable - Javascript updated to check before displaying copy buttons. * html -> html_config Also: - update description of copyable in source code. - update description of line_numbers (my last PR to this repository)
1 parent c639977 commit 57cd281

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

book-example/src/format/config.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ The following configuration options are available:
181181
Available configuration options for the `[output.html.playpen]` table:
182182

183183
- **editable:** Allow editing the source code. Defaults to `false`.
184+
- **copyable:** Display the copy button on code snippets. Defaults to `true`.
184185
- **copy-js:** Copy JavaScript files for the editor to the output directory.
185186
Defaults to `true`.
186187
- **line-numbers** Display line numbers on editable sections of code. Requires both `editable` and `copy-js` to be `true`. Defaults to `false`.

src/config.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,17 +492,20 @@ impl HtmlConfig {
492492
pub struct Playpen {
493493
/// Should playpen snippets be editable? Default: `false`.
494494
pub editable: bool,
495+
/// Display the copy button. Default: `true`.
496+
pub copyable: bool,
495497
/// Copy JavaScript files for the editor to the output directory?
496498
/// Default: `true`.
497499
pub copy_js: bool,
498-
/// Display line numbers on playpen snippets
500+
/// Display line numbers on playpen snippets. Default: `false`.
499501
pub line_numbers: bool,
500502
}
501503

502504
impl Default for Playpen {
503505
fn default() -> Playpen {
504506
Playpen {
505507
editable: false,
508+
copyable: true,
506509
copy_js: true,
507510
line_numbers: false,
508511
}
@@ -638,6 +641,7 @@ mod tests {
638641
};
639642
let playpen_should_be = Playpen {
640643
editable: true,
644+
copyable: true,
641645
copy_js: true,
642646
line_numbers: false,
643647
};

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,9 @@ fn make_data(
456456
data.insert("playpen_line_numbers".to_owned(), json!(true));
457457
}
458458
}
459+
if html_config.playpen.copyable {
460+
data.insert("playpen_copyable".to_owned(), json!(true));
461+
}
459462

460463
let search = html_config.search.clone();
461464
if cfg!(feature = "search") {

src/theme/book.js

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,27 @@ function playpen_text(playpen) {
202202
});
203203
});
204204

205-
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
206-
var pre_block = block.parentNode;
207-
if (!pre_block.classList.contains('playpen')) {
208-
var buttons = pre_block.querySelector(".buttons");
209-
if (!buttons) {
210-
buttons = document.createElement('div');
211-
buttons.className = 'buttons';
212-
pre_block.insertBefore(buttons, pre_block.firstChild);
213-
}
205+
if (window.playpen_copyable) {
206+
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
207+
var pre_block = block.parentNode;
208+
if (!pre_block.classList.contains('playpen')) {
209+
var buttons = pre_block.querySelector(".buttons");
210+
if (!buttons) {
211+
buttons = document.createElement('div');
212+
buttons.className = 'buttons';
213+
pre_block.insertBefore(buttons, pre_block.firstChild);
214+
}
214215

215-
var clipButton = document.createElement('button');
216-
clipButton.className = 'fa fa-copy clip-button';
217-
clipButton.title = 'Copy to clipboard';
218-
clipButton.setAttribute('aria-label', clipButton.title);
219-
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
216+
var clipButton = document.createElement('button');
217+
clipButton.className = 'fa fa-copy clip-button';
218+
clipButton.title = 'Copy to clipboard';
219+
clipButton.setAttribute('aria-label', clipButton.title);
220+
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
220221

221-
buttons.insertBefore(clipButton, buttons.firstChild);
222-
}
223-
});
222+
buttons.insertBefore(clipButton, buttons.firstChild);
223+
}
224+
});
225+
}
224226

225227
// Process playpen code blocks
226228
Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) {
@@ -238,19 +240,21 @@ function playpen_text(playpen) {
238240
runCodeButton.title = 'Run this code';
239241
runCodeButton.setAttribute('aria-label', runCodeButton.title);
240242

241-
var copyCodeClipboardButton = document.createElement('button');
242-
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
243-
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
244-
copyCodeClipboardButton.title = 'Copy to clipboard';
245-
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
246-
247243
buttons.insertBefore(runCodeButton, buttons.firstChild);
248-
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
249-
250244
runCodeButton.addEventListener('click', function (e) {
251245
run_rust_code(pre_block);
252246
});
253247

248+
if (window.playpen_copyable) {
249+
var copyCodeClipboardButton = document.createElement('button');
250+
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
251+
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
252+
copyCodeClipboardButton.title = 'Copy to clipboard';
253+
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
254+
255+
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
256+
}
257+
254258
let code_block = pre_block.querySelector("code");
255259
if (window.ace && code_block.classList.contains("editable")) {
256260
var undoChangesButton = document.createElement('button');

src/theme/index.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@
235235
window.playpen_line_numbers = true;
236236
</script>
237237
{{/if}}
238+
239+
{{#if playpen_copyable}}
240+
<script type="text/javascript">
241+
window.playpen_copyable = true;
242+
</script>
243+
{{/if}}
238244

239245
{{#if playpen_js}}
240246
<script src="{{ path_to_root }}ace.js" type="text/javascript" charset="utf-8"></script>

0 commit comments

Comments
 (0)