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.

docs: Add copy code button to portico docs.

UBC Student1cbff7094b9a
diff --git a/web/src/portico/help.js b/web/src/portico/help.js
index a109b73b57b..4e584ebba04 100644
--- a/web/src/portico/help.js
+++ b/web/src/portico/help.js
@@ -1,6 +1,9 @@
+import ClipboardJS from "clipboard";
import $ from "jquery";
import SimpleBar from "simplebar";
+import tippy from "tippy.js";
+import copy_to_clipboard_svg from "../../templates/copy_to_clipboard_svg.hbs";
import * as common from "../common";
import * as google_analytics from "./google-analytics";
@@ -27,6 +30,37 @@ function registerCodeSection($codeSection) {
});
}
+// Display the copy-to-clipboard button inside the markdown.pre element
+// within the API and Help Center docs using clipboard.js
+function add_copy_to_clipboard_element($pre) {
+ const $copy_button = $("<button>").addClass("copy-codeblock");
+ $copy_button.html(copy_to_clipboard_svg());
+
+ $($pre).append($copy_button);
+
+ const clipboard = new ClipboardJS($copy_button[0], {
+ text(copy_element) {
+ return $(copy_element).siblings("code").text();
+ },
+ });
+
+ // Show a tippy tooltip when the code is copied
+ clipboard.on("success", () => {
+ const tooltip = tippy($copy_button[0], {
+ content: "Copied!",
+ trigger: "manual",
+ placement: "top",
+ });
+
+ tooltip.show();
+
+ // Show the tooltip for 1s
+ setTimeout(() => {
+ tooltip.hide();
+ }, 1000);
+ });
+}
+
function highlight_current_article() {
$(".help .sidebar a").removeClass("highlighted");
$(".help .sidebar a").attr("tabindex", "0");
@@ -56,6 +90,11 @@ function render_code_sections() {
registerCodeSection($(this));
});
+ // Add a copy-to-clipboard button for each pre element
+ $(".markdown pre").each(function () {
+ add_copy_to_clipboard_element($(this));
+ });
+
highlight_current_article();
common.adjust_mac_kbd_tags(".markdown kbd");
diff --git a/web/styles/portico/markdown.css b/web/styles/portico/markdown.css
index a2b77f9099a..0577300f98c 100644
--- a/web/styles/portico/markdown.css
+++ b/web/styles/portico/markdown.css
@@ -382,6 +382,10 @@
}
& pre {
+ /* Relative positioning is used to place the copy-to-clipboard button
+ in the top-right corner. */
+ position: relative;
+
& code {
font-size: 14px;
white-space: pre-wrap;
@@ -392,6 +396,23 @@
}
}
+ .copy-codeblock {
+ /* Invisible default button */
+ background: none;
+ border: none;
+ padding: 0;
+ position: absolute;
+ /* Position the button in the top-right corner */
+ top: 10px;
+ right: 10px;
+ line-height: 3px;
+
+ &:focus {
+ outline-offset: -1px;
+ outline-color: hsl(176deg 46% 41%);
+ }
+ }
+
.code-section {
& ol {
margin-left: 15px;