Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

markdown: Add copy-to-clipboard button in codeblocks.

Sumanth V Rao42d30bc14b0d
diff --git a/frontend_tests/node_tests/rendered_markdown.js b/frontend_tests/node_tests/rendered_markdown.js
index c770d5f997f..a9c3a3999b6 100644
--- a/frontend_tests/node_tests/rendered_markdown.js
+++ b/frontend_tests/node_tests/rendered_markdown.js
@@ -73,6 +73,7 @@ const get_content_element = () => {
$content.set_find_results("span.timestamp-error", $array([]));
$content.set_find_results(".emoji", $array([]));
$content.set_find_results("div.spoiler-header", $array([]));
+ $content.set_find_results("div.codehilite", $array([]));
return $content;
};
diff --git a/static/js/rendered_markdown.js b/static/js/rendered_markdown.js
index 9f08755a271..2d8458643cc 100644
--- a/static/js/rendered_markdown.js
+++ b/static/js/rendered_markdown.js
@@ -1,7 +1,10 @@
"use strict";
+const ClipboardJS = require("clipboard");
const moment = require("moment");
+const copy_code_button = require("../templates/copy_code_button.hbs");
+
/*
rendered_markdown
@@ -185,6 +188,17 @@ exports.update_elements = (content) => {
$(this).prepend(toggle_button_html);
});
+ // Display the copy-to-clipboard button inside the div.codehilite element.
+ content.find("div.codehilite").each(function () {
+ const copy_button = $(copy_code_button());
+ $(this).find("pre").prepend(copy_button);
+ new ClipboardJS(copy_button[0], {
+ text(copy_element) {
+ return $(copy_element).siblings("code").text();
+ },
+ });
+ });
+
// Display emoji (including realm emoji) as text if
// page_params.emojiset is 'text'.
if (page_params.emojiset === "text") {
diff --git a/static/templates/copy_code_button.hbs b/static/templates/copy_code_button.hbs
new file mode 100644
index 00000000000..c3571dcc4b2
--- /dev/null
+++ b/static/templates/copy_code_button.hbs
@@ -0,0 +1,5 @@
+<button class="btn pull-left copy_codeblock" data-toggle="tooltip" title="{{t "Copy code" }}" aria-label="{{t "Copy code" }}">
+ <svg height="17" width="14" viewBox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg" id="clipboard_image">
+ <path fill="#777" d="M128 768h256v64H128v-64z m320-384H128v64h320v-64z m128 192V448L384 640l192 192V704h320V576H576z m-288-64H128v64h160v-64zM128 704h160v-64H128v64z m576 64h64v128c-1 18-7 33-19 45s-27 18-45 19H64c-35 0-64-29-64-64V192c0-35 29-64 64-64h192C256 57 313 0 384 0s128 57 128 128h192c35 0 64 29 64 64v320h-64V320H64v576h640V768zM128 256h512c0-35-29-64-64-64h-64c-35 0-64-29-64-64s-29-64-64-64-64 29-64 64-29 64-64 64h-64c-35 0-64 29-64 64z" />
+ </svg>
+</button>