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.

rendered_markdown: Refine templates and logic for REPL, copy buttons.

Karl Stolley2cf726c53b3c
diff --git a/web/src/rendered_markdown.ts b/web/src/rendered_markdown.ts
index b22977d0af2..ec916dc13ae 100644
--- a/web/src/rendered_markdown.ts
+++ b/web/src/rendered_markdown.ts
@@ -3,9 +3,8 @@ import {isValid, parseISO} from "date-fns";
import $ from "jquery";
import assert from "minimalistic-assert";
-import copy_code_button from "../templates/copy_code_button.hbs";
+import code_buttons_container from "../templates/code_buttons_container.hbs";
import render_markdown_timestamp from "../templates/markdown_timestamp.hbs";
-import view_code_in_playground from "../templates/view_code_in_playground.hbs";
import * as blueslip from "./blueslip";
import {show_copied_confirmation} from "./copied_tooltip";
@@ -280,37 +279,42 @@ export const update_elements = ($content: JQuery): void => {
const $codehilite = $(this);
const $pre = $codehilite.find("pre");
const fenced_code_lang = $codehilite.data("code-language");
+ let playground_info;
if (fenced_code_lang !== undefined) {
- const playground_info =
- realm_playground.get_playground_info_for_languages(fenced_code_lang);
- if (playground_info !== undefined) {
- // If a playground is configured for this language,
- // offer to view the code in that playground. When
- // there are multiple playgrounds, we display a
- // popover listing the options.
- let title = $t({defaultMessage: "View in playground"});
- const $view_in_playground_button = $(view_code_in_playground());
- $pre.prepend($view_in_playground_button);
- if (playground_info.length === 1) {
- title = $t(
- {defaultMessage: "View in {playground_name}"},
- {playground_name: playground_info[0].name},
- );
- } else {
- $view_in_playground_button.attr("aria-haspopup", "true");
- }
- $view_in_playground_button.attr("data-tippy-content", title);
- $view_in_playground_button.attr("aria-label", title);
+ playground_info = realm_playground.get_playground_info_for_languages(fenced_code_lang);
+ }
+ const show_playground_button =
+ fenced_code_lang !== undefined && playground_info !== undefined;
+
+ const $buttonContainer = $(code_buttons_container({show_playground_button}));
+ $pre.prepend($buttonContainer);
+
+ if (show_playground_button) {
+ // If a playground is configured for this language,
+ // offer to view the code in that playground. When
+ // there are multiple playgrounds, we display a
+ // popover listing the options.
+ let title = $t({defaultMessage: "View in playground"});
+ const $view_in_playground_button = $buttonContainer.find(".code_external_link");
+ if (playground_info && playground_info.length === 1) {
+ title = $t(
+ {defaultMessage: "View in {playground_name}"},
+ {playground_name: playground_info[0].name},
+ );
+ } else {
+ $view_in_playground_button.attr("aria-haspopup", "true");
}
+ $view_in_playground_button.attr("data-tippy-content", title);
+ $view_in_playground_button.attr("aria-label", title);
}
- const $copy_button = $(copy_code_button());
- $pre.prepend($copy_button);
- const clipboard = new ClipboardJS($copy_button[0], {
+
+ const clipboard = new ClipboardJS($buttonContainer[0], {
text(copy_element) {
return $(copy_element).siblings("code").text();
},
});
clipboard.on("success", () => {
+ const $copy_button = $buttonContainer.find(".copy_codeblock");
show_copied_confirmation($copy_button[0]);
});
$codehilite.addClass("zulip-code-block");
diff --git a/web/templates/code_buttons_container.hbs b/web/templates/code_buttons_container.hbs
new file mode 100644
index 00000000000..09286dffe38
--- /dev/null
+++ b/web/templates/code_buttons_container.hbs
@@ -0,0 +1,6 @@
+<div class="code-buttons-container">
+ {{~> copy_code_button~}}
+ {{~#if show_playground_button~}}
+ {{~> view_code_in_playground~}}
+ {{~/if~}}
+</div>
diff --git a/web/tests/rendered_markdown.test.js b/web/tests/rendered_markdown.test.js
index a4fdef6e2ad..2003e82ed08 100644
--- a/web/tests/rendered_markdown.test.js
+++ b/web/tests/rendered_markdown.test.js
@@ -536,10 +536,16 @@ function test_code_playground(mock_template, viewing_code) {
$hilite.data("code-language", "javascript");
+ const $code_buttons_container = $.create("code_buttons_container", {
+ children: ["copy-code-stub", "view-code-stub"],
+ });
const $copy_code_button = $.create("copy_code_button", {children: ["copy-code-stub"]});
const $view_code_in_playground = $.create("view_code_in_playground");
- // The code playground code prepends a few buttons
+ $code_buttons_container.set_find_results(".copy_codeblock", $copy_code_button);
+ $code_buttons_container.set_find_results(".code_external_link", $view_code_in_playground);
+
+ // The code playground code prepends a button container
// to the <pre> section of a highlighted piece of code.
// The args to prepend should be jQuery objects (or in
// our case "fake" zjquery objects).
@@ -549,15 +555,15 @@ function test_code_playground(mock_template, viewing_code) {
prepends.push(arg);
};
- mock_template("copy_code_button.hbs", false, (data) => {
- assert.equal(data, undefined);
- return {to_$: () => $copy_code_button};
- });
-
if (viewing_code) {
- mock_template("view_code_in_playground.hbs", false, (data) => {
- assert.equal(data, undefined);
- return {to_$: () => $view_code_in_playground};
+ mock_template("code_buttons_container.hbs", true, (data) => {
+ assert.equal(data.show_playground_button, true);
+ return {to_$: () => $code_buttons_container};
+ });
+ } else {
+ mock_template("code_buttons_container.hbs", true, (data) => {
+ assert.equal(data.show_playground_button, false);
+ return {to_$: () => $code_buttons_container};
});
}
@@ -565,6 +571,7 @@ function test_code_playground(mock_template, viewing_code) {
return {
prepends,
+ $button_container: $code_buttons_container,
$copy_code: $copy_code_button,
$view_code: $view_code_in_playground,
};
@@ -578,8 +585,8 @@ run_test("code playground none", ({override, mock_template}) => {
override(copied_tooltip, "show_copied_confirmation", noop);
- const {prepends, $copy_code, $view_code} = test_code_playground(mock_template, false);
- assert.deepEqual(prepends, [$copy_code]);
+ const {prepends, $button_container, $view_code} = test_code_playground(mock_template, false);
+ assert.deepEqual(prepends, [$button_container]);
assert_clipboard_setup();
assert.equal($view_code.attr("data-tippy-content"), undefined);
@@ -594,8 +601,8 @@ run_test("code playground single", ({override, mock_template}) => {
override(copied_tooltip, "show_copied_confirmation", noop);
- const {prepends, $copy_code, $view_code} = test_code_playground(mock_template, true);
- assert.deepEqual(prepends, [$view_code, $copy_code]);
+ const {prepends, $button_container, $view_code} = test_code_playground(mock_template, true);
+ assert.deepEqual(prepends, [$button_container]);
assert_clipboard_setup();
assert.equal(
@@ -614,8 +621,8 @@ run_test("code playground multiple", ({override, mock_template}) => {
override(copied_tooltip, "show_copied_confirmation", noop);
- const {prepends, $copy_code, $view_code} = test_code_playground(mock_template, true);
- assert.deepEqual(prepends, [$view_code, $copy_code]);
+ const {prepends, $button_container, $view_code} = test_code_playground(mock_template, true);
+ assert.deepEqual(prepends, [$button_container]);
assert_clipboard_setup();
assert.equal($view_code.attr("data-tippy-content"), "translated: View in playground");