From 4d5e423965bc22ed251529ec0fb7ac12edd1af49 Mon Sep 17 00:00:00 2001 From: Andrew Pritchard Date: Fri, 4 Oct 2019 23:14:23 +1000 Subject: [PATCH 1/2] Option to display copy buttons. - Added field to playpen data structure - Communicate through window.playpen_copyable - Javascript updated to check before displaying copy buttons. --- book-example/src/format/config.md | 1 + src/config.rs | 4 ++ src/renderer/html_handlebars/hbs_renderer.rs | 3 ++ src/theme/book.js | 54 +++++++++++--------- src/theme/index.hbs | 12 +++++ 5 files changed, 49 insertions(+), 25 deletions(-) diff --git a/book-example/src/format/config.md b/book-example/src/format/config.md index d443911f88..5cf08347b3 100644 --- a/book-example/src/format/config.md +++ b/book-example/src/format/config.md @@ -177,6 +177,7 @@ The following configuration options are available: Available configuration options for the `[output.html.playpen]` table: - **editable:** Allow editing the source code. Defaults to `false`. +- **copyable:** Display the copy button on code snippets. Defaults to `true`. - **copy-js:** Copy JavaScript files for the editor to the output directory. Defaults to `true`. diff --git a/src/config.rs b/src/config.rs index 17c941d255..00ff7d91ba 100644 --- a/src/config.rs +++ b/src/config.rs @@ -469,6 +469,8 @@ impl HtmlConfig { pub struct Playpen { /// Should playpen snippets be editable? Default: `false`. pub editable: bool, + /// display the copy button + pub copyable: bool, /// Copy JavaScript files for the editor to the output directory? /// Default: `true`. pub copy_js: bool, @@ -478,6 +480,7 @@ impl Default for Playpen { fn default() -> Playpen { Playpen { editable: false, + copyable: true, copy_js: true, } } @@ -612,6 +615,7 @@ mod tests { }; let playpen_should_be = Playpen { editable: true, + copyable: true, copy_js: true, }; let html_should_be = HtmlConfig { diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 7a7f0ef6d7..b2cab2aa58 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -443,6 +443,9 @@ fn make_data( if html.playpen.editable && html.playpen.copy_js { data.insert("playpen_js".to_owned(), json!(true)); } + if html.playpen.copyable { + data.insert("playpen_copyable".to_owned(), json!(true)); + } let search = html_config.search.clone(); if cfg!(feature = "search") { diff --git a/src/theme/book.js b/src/theme/book.js index ca73ee14d0..077a06a8c5 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -231,25 +231,27 @@ function playpen_text(playpen) { }); }); - Array.from(document.querySelectorAll('pre code')).forEach(function (block) { - var pre_block = block.parentNode; - if (!pre_block.classList.contains('playpen')) { - var buttons = pre_block.querySelector(".buttons"); - if (!buttons) { - buttons = document.createElement('div'); - buttons.className = 'buttons'; - pre_block.insertBefore(buttons, pre_block.firstChild); - } + if (window.playpen_copyable) { + Array.from(document.querySelectorAll('pre code')).forEach(function (block) { + var pre_block = block.parentNode; + if (!pre_block.classList.contains('playpen')) { + var buttons = pre_block.querySelector(".buttons"); + if (!buttons) { + buttons = document.createElement('div'); + buttons.className = 'buttons'; + pre_block.insertBefore(buttons, pre_block.firstChild); + } - var clipButton = document.createElement('button'); - clipButton.className = 'fa fa-copy clip-button'; - clipButton.title = 'Copy to clipboard'; - clipButton.setAttribute('aria-label', clipButton.title); - clipButton.innerHTML = ''; + var clipButton = document.createElement('button'); + clipButton.className = 'fa fa-copy clip-button'; + clipButton.title = 'Copy to clipboard'; + clipButton.setAttribute('aria-label', clipButton.title); + clipButton.innerHTML = ''; - buttons.insertBefore(clipButton, buttons.firstChild); - } - }); + buttons.insertBefore(clipButton, buttons.firstChild); + } + }); + } // Process playpen code blocks Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) { @@ -267,19 +269,21 @@ function playpen_text(playpen) { runCodeButton.title = 'Run this code'; runCodeButton.setAttribute('aria-label', runCodeButton.title); - var copyCodeClipboardButton = document.createElement('button'); - copyCodeClipboardButton.className = 'fa fa-copy clip-button'; - copyCodeClipboardButton.innerHTML = ''; - copyCodeClipboardButton.title = 'Copy to clipboard'; - copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); - buttons.insertBefore(runCodeButton, buttons.firstChild); - buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); - runCodeButton.addEventListener('click', function (e) { run_rust_code(pre_block); }); + if (window.playpen_copyable) { + var copyCodeClipboardButton = document.createElement('button'); + copyCodeClipboardButton.className = 'fa fa-copy clip-button'; + copyCodeClipboardButton.innerHTML = ''; + copyCodeClipboardButton.title = 'Copy to clipboard'; + copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title); + + buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild); + } + let code_block = pre_block.querySelector("code"); if (window.ace && code_block.classList.contains("editable")) { var undoChangesButton = document.createElement('button'); diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 4e29807c6f..e9c5409495 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -230,6 +230,18 @@ {{/if}} + {{#if playpen_line_numbers}} + + {{/if}} + + {{#if playpen_copyable}} + + {{/if}} + {{#if playpen_js}} From 1c5d56a8c6899a7597f0d6801c1a0c9c4bdaf5e3 Mon Sep 17 00:00:00 2001 From: Andrew Pritchard Date: Wed, 16 Oct 2019 21:53:53 +0800 Subject: [PATCH 2/2] html -> html_config Also: - update description of copyable in source code. - update description of line_numbers (my last PR to this repository) --- src/config.rs | 4 ++-- src/renderer/html_handlebars/hbs_renderer.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0f70fac872..822a90a4a9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -476,12 +476,12 @@ impl HtmlConfig { pub struct Playpen { /// Should playpen snippets be editable? Default: `false`. pub editable: bool, - /// display the copy button + /// Display the copy button. Default: `true`. pub copyable: bool, /// Copy JavaScript files for the editor to the output directory? /// Default: `true`. pub copy_js: bool, - /// Display line numbers on playpen snippets + /// Display line numbers on playpen snippets. Default: `false`. pub line_numbers: bool, } diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 51893f2e28..f118a79a88 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -445,7 +445,7 @@ fn make_data( data.insert("playpen_line_numbers".to_owned(), json!(true)); } } - if html.playpen.copyable { + if html_config.playpen.copyable { data.insert("playpen_copyable".to_owned(), json!(true)); }